2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.examples.galaxy_demo;
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
8 import eu.svjatoslav.sixth.e3d.math.Transform;
9 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.GlowingPoint;
11 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Random;
17 import static java.lang.Math.*;
19 public class Galaxy extends AbstractCompositeShape {
22 * The number of unique colors used in the galaxy.
24 public static final int UNIQUE_COLORS_COUNT = 30;
27 * A list of all colors used in the galaxy.
28 * Used to reuse textures of glowing points of the same color.
30 private static List<Color> colors;
32 public Galaxy(final int galaxySize, final int tailCount, final int starsCount,
33 Transform transform) {
37 ensureColorsAreInitialized();
39 final double angle1 = random() * 10;
40 final double angle2 = random() * 10;
42 final double angleSin1 = sin(angle1);
43 final double angleCos1 = cos(angle1);
44 final double angleSin2 = sin(angle2);
45 final double angleCos2 = cos(angle2);
47 Random random = new Random();
49 double starSize = galaxySize / 70d;
51 for (int i = 1; i < starsCount; i++) {
52 final double b = random() * 10;
54 final double s = (b * b) / 30;
56 final double v1 = (random() * (11.5 - b)) / 3;
57 final double v1p = v1 / 2;
59 final double ane = ((random() * (s / 2)) / tailCount) * 2;
60 final double sba = ((2 * PI) / tailCount)
61 * random.nextInt(tailCount);
63 final double x = (((sin((b - sba) + ane) * s) + (random() * v1)) - v1p) * galaxySize;
64 final double z = (((cos((b - sba) + ane) * s) + (random() * v1)) - v1p) * galaxySize;
65 final double y = ((random() * v1) - v1p) * galaxySize;
67 final double x1 = (x * angleCos1) + (z * angleSin1);
68 final double z1 = (z * angleCos1) - (x * angleSin1);
70 final double y1 = (y * angleCos2) + (z1 * angleSin2);
71 final double z2 = (z1 * angleCos2) - (y * angleSin2);
73 addStar(new Point3D(x1, y1, z2), starSize);
77 private void addStar(final Point3D starLocation, double size) {
78 addShape(new GlowingPoint(starLocation, size, colors.get((int) (random() * colors.size()))));
82 * Initializes the list of colors used in the galaxy.
83 * Used to reuse textures of glowing points of the same color.
85 private synchronized void ensureColorsAreInitialized() {
86 if (colors != null) return;
88 colors = new ArrayList<>();
90 for (int i = 0; i < UNIQUE_COLORS_COUNT; i++)