Exemple #1
0
        public BoidController(MeshFilter meshFilter)
        {
            template = MeshDraft.Tetrahedron(0.3f);

            // Avoid vertex count overflow
            swarmCount = Mathf.Min(65000 / template.vertices.Count, swarmCount);
            // Optimization trick: in each frame we simulate only small percent of all boids
            simulationUpdate = Mathf.RoundToInt(swarmCount * simulationPercent);
            int vertexCount = swarmCount * template.vertices.Count;

            draft = new MeshDraft
            {
                name      = "Boids",
                vertices  = new List <Vector3>(vertexCount),
                triangles = new List <int>(vertexCount),
                normals   = new List <Vector3>(vertexCount),
                uv        = new List <Vector2>(vertexCount),
                colors    = new List <Color>(vertexCount)
            };
            for (var i = 0; i < swarmCount; i++)
            {
                boids.Add(new Boid());
                draft.Add(template);
            }

            mesh = draft.ToMesh();
            mesh.MarkDynamic();
            meshFilter.mesh = mesh;
        }
        /// <summary>
        /// Generate new colors and positions for boids
        /// </summary>
        public Mesh Generate(Color colorA, Color colorB)
        {
            template = MeshDraft.Tetrahedron(0.3f);

            // Avoid vertex count overflow
            swarmCount = Mathf.Min(65000 / template.vertices.Count, swarmCount);
            // Optimization trick: in each frame we simulate only small percent of all boids
            maxSimulationSteps = Mathf.RoundToInt(swarmCount * simulationPercent);
            // int vertexCount = swarmCount*template.vertices.Count;

            // Paint template in random color
            template.colors.Clear();
            // Assuming that we are dealing with tetrahedron, first vertex should be boid's "nose"
            template.colors.Add(colorA);
            for (int i = 1; i < template.vertices.Count; i++)
            {
                template.colors.Add(Tile.Lighten(colorB, i * 0.1f));
            }

            draft = new MeshDraft
            {
                name      = "Boids",
                vertices  = new List <Vector3>(),
                triangles = new List <int>(),
                normals   = new List <Vector3>(),
                uv        = new List <Vector2>(),
                colors    = new List <Color>()
            };

            for (var i = 0; i < swarmCount; i++)
            {
                // Assign random starting values for each boid
                var boid = new Boid
                {
                    position = Random.onUnitSphere * worldSphere,
                    rotation = Random.rotation,
                    velocity = Random.onUnitSphere * maxSpeed
                };
                boid.position.y = Mathf.Abs(boid.position.y);
                boid.velocity.y = Mathf.Abs(boid.velocity.y);
                boids.Add(boid);

                draft.Add(template);
            }

            mesh = draft.ToMesh();
            mesh.MarkDynamic();

            return(mesh);
        }
        private void Generate()
        {
            controller = new BoidController();

            GeneratePalette();
            Color colorA = GetMainColorHSV().ToColor();
            Color colorB = GetSecondaryColorHSV().ToColor();

            config.template = MeshDraft.Tetrahedron(0.3f);
            // Assuming that we are dealing with tetrahedron, first vertex should be boid's "nose"
            config.template.colors.Add(colorA);
            for (int i = 1; i < config.template.vertexCount; i++)
            {
                config.template.colors.Add(colorB);
            }

            meshFilter.mesh = controller.Generate(config);
        }
Exemple #4
0
 private void Start()
 {
     GetComponent <MeshFilter>().mesh = MeshDraft.Tetrahedron(radius).ToMesh();
 }
Exemple #5
0
 public static void Tetrahedron()
 {
     PrimitiveTemplate(tetrahedron, () => MeshDraft.Tetrahedron(1).ToMesh());
 }