示例#1
0
        public void UpdateMeshesAndModels()
        {
            if (meshes == null)
            {
                meshes = new List <Mesh>();
            }

            if (PathDetail > 0 && PathLengthMin > 0.0f && PathLengthMax > 0.0f)
            {
                var meshCount   = 1;
                var mesh        = GetMesh(0);
                var vertexCount = 0;

                SgtHelper.BeginRandomSeed(Seed);
                {
                    for (var i = 0; i < PathCount; i++)
                    {
                        AddPath(ref mesh, ref meshCount, ref vertexCount);
                    }
                }
                SgtHelper.EndRandomSeed();

                BakeMesh(mesh);

                for (var i = meshes.Count - 1; i >= meshCount; i--)
                {
                    var extraMesh = meshes[i];

                    if (extraMesh != null)
                    {
                        extraMesh.Clear(false);

                        SgtObjectPool <Mesh> .Add(extraMesh);
                    }

                    meshes.RemoveAt(i);
                }
            }

            for (var i = 0; i < meshes.Count; i++)
            {
                var model = SgtHelper.GetIndex(ref models, i);

                if (model == null)
                {
                    model = models[i] = SgtAuroraModel.Create(this);
                }

                model.SetMesh(meshes[i]);
                model.SetMaterial(material);
            }

            // Remove any excess
            if (models != null)
            {
                var min = Mathf.Max(0, meshes.Count);

                for (var i = models.Count - 1; i >= min; i--)
                {
                    SgtAuroraModel.Pool(models[i]);

                    models.RemoveAt(i);
                }
            }
        }
 protected override void EndQuads()
 {
     SgtHelper.EndRandomSeed();
 }
示例#3
0
        public void UpdateDebris()
        {
            var size = (long)System.Math.Ceiling(SgtHelper.Divide(HideDistance, (float)CellSize));

            if (Target != null && CellSize > 0.0f && Prefabs != null && DebrisCountTarget > 0 && size > 0)
            {
                var worldPoint = Target.position - transform.position;
                var centerX    = (long)System.Math.Round(worldPoint.x / CellSize);
                var centerY    = (long)System.Math.Round(worldPoint.y / CellSize);
                var centerZ    = (long)System.Math.Round(worldPoint.z / CellSize);

                var newBounds = new SgtBoundsL(centerX, centerY, centerZ, size);

                if (newBounds != bounds)
                {
                    var probability = DebrisCountTarget / (size * size * size);
                    var cellMin     = (float)CellSize * (0.5f - CellNoise);
                    var cellMax     = (float)CellSize * (0.5f + CellNoise);

                    for (var z = newBounds.minZ; z <= newBounds.maxZ; z++)
                    {
                        for (var y = newBounds.minY; y <= newBounds.maxY; y++)
                        {
                            for (var x = newBounds.minX; x <= newBounds.maxX; x++)
                            {
                                if (bounds.Contains(x, y, z) == false)
                                {
                                    SgtHelper.BeginRandomSeed(Seed, x, y, z);
                                    {
                                        // Can debris potentially spawn in this cell?
                                        if (Random.value < probability)
                                        {
                                            var debrisX     = x * CellSize + Random.Range(cellMin, cellMax);
                                            var debrisY     = y * CellSize + Random.Range(cellMin, cellMax);
                                            var debrisZ     = z * CellSize + Random.Range(cellMin, cellMax);
                                            var debrisPoint = new Vector3((float)debrisX, (float)debrisY, (float)debrisZ);

                                            // Spawn everywhere, or only inside specified shapes?
                                            if (SpawnInside == null || Random.value < SpawnInside.GetDensity(debrisPoint))
                                            {
                                                Spawn(x, y, z, debrisPoint);
                                            }
                                        }
                                    }
                                    SgtHelper.EndRandomSeed();
                                }
                            }
                        }
                    }

                    bounds = newBounds;

                    if (debris != null)
                    {
                        for (var i = debris.Count - 1; i >= 0; i--)
                        {
                            var debris = this.debris[i];

                            if (debris == null)
                            {
                                this.debris.RemoveAt(i);
                            }
                            else if (bounds.Contains(debris.Cell) == false)
                            {
                                Despawn(debris, i);
                            }
                        }
                    }
                }

                UpdateDebrisScale(worldPoint);
            }
            else
            {
                ClearDebris();
            }
        }
示例#4
0
        public void UpdateMesh()
        {
            if (Detail > 2)
            {
                if (generatedMesh == null)
                {
                    generatedMesh = SgtHelper.CreateTempMesh("Mesh (Generated)");

                    ApplyMesh();
                }

                var total     = Detail + 1;
                var positions = new Vector3[total];
                var coords1   = new Vector2[total];
                var indices   = new int[Detail * 3];
                var angleStep = (Mathf.PI * 2.0f) / Detail;
                var noiseStep = 0.0f;

                if (Noise == true && NoisePoints > 0)
                {
                    SgtHelper.BeginRandomSeed(NoiseSeed);
                    {
                        points.Clear();

                        for (var i = 0; i < NoisePoints; i++)
                        {
                            points.Add(Random.value);
                        }

                        noiseStep = NoisePoints / (float)Detail;
                    }
                    SgtHelper.EndRandomSeed();
                }

                // Write center vertices
                positions[0] = Vector3.zero;
                coords1[0]   = Vector2.zero;

                // Write outer vertices
                for (var point = 0; point < Detail; point++)
                {
                    var angle = angleStep * point;
                    var x     = Mathf.Sin(angle);
                    var y     = Mathf.Cos(angle);
                    var r     = Radius;

                    if (Wave == true)
                    {
                        var waveAngle = (angle + WavePhase * Mathf.Deg2Rad) * WavePoints;

                        r += Mathf.Pow(Mathf.Cos(waveAngle) * 0.5f + 0.5f, WavePower * WavePower) * WaveStrength;
                    }

                    if (Noise == true && NoisePoints > 0)
                    {
                        var noise = Mathf.Repeat(noiseStep * point + NoisePhase, NoisePoints);
                        //var noise = point * noiseStep + NoisePhase;
                        var index  = (int)noise;
                        var frac   = noise % 1.0f;
                        var pointA = points[(index + 0) % NoisePoints];
                        var pointB = points[(index + 1) % NoisePoints];
                        var pointC = points[(index + 2) % NoisePoints];
                        var pointD = points[(index + 3) % NoisePoints];

                        r += SgtHelper.CubicInterpolate(pointA, pointB, pointC, pointD, frac) * NoiseStrength;
                    }

                    // Write outer vertices
                    var v = point + 1;

                    positions[v] = new Vector3(x * r, y * r, 0.0f);
                    coords1[v]   = new Vector2(1.0f, 0.0f);
                }

                for (var tri = 0; tri < Detail; tri++)
                {
                    var i  = tri * 3;
                    var v0 = tri + 1;
                    var v1 = tri + 2;

                    if (v1 >= total)
                    {
                        v1 = 1;
                    }

                    indices[i + 0] = 0;
                    indices[i + 1] = v0;
                    indices[i + 2] = v1;
                }

                generatedMesh.Clear(false);
                generatedMesh.vertices  = positions;
                generatedMesh.uv        = coords1;
                generatedMesh.triangles = indices;
                generatedMesh.RecalculateNormals();
                generatedMesh.RecalculateBounds();
            }
        }
        public void UpdateDebris()
        {
            if (target != null && cellCount > 0.0f && prefabs != null && debrisCountTarget > 0)
            {
                var cellSize   = (long)System.Math.Ceiling(hideDistance / cellCount);
                var worldPoint = target.position - transform.position;
                var centerX    = (long)System.Math.Round(worldPoint.x / cellSize);
                var centerY    = (long)System.Math.Round(worldPoint.y / cellSize);
                var centerZ    = (long)System.Math.Round(worldPoint.z / cellSize);
                var newBounds  = new SgtLongBounds(centerX, centerY, centerZ, cellCount);

                if (newBounds != bounds)
                {
                    var probability = debrisCountTarget / (cellSize * cellSize * cellSize);
                    var cellMin     = cellSize * (0.5f - cellNoise);
                    var cellMax     = cellSize * (0.5f + cellNoise);

                    for (var z = newBounds.minZ; z <= newBounds.maxZ; z++)
                    {
                        for (var y = newBounds.minY; y <= newBounds.maxY; y++)
                        {
                            for (var x = newBounds.minX; x <= newBounds.maxX; x++)
                            {
                                if (bounds.Contains(x, y, z) == false)
                                {
                                    SgtHelper.BeginRandomSeed(seed, x, y, z);
                                    {
                                        // Can debris potentially spawn in this cell?
                                        if (Random.value < probability)
                                        {
                                            var debrisPoint = default(Vector3);

                                            debrisPoint.x = x * cellSize + Random.Range(cellMin, cellMax);
                                            debrisPoint.y = y * cellSize + Random.Range(cellMin, cellMax);
                                            debrisPoint.z = z * cellSize + Random.Range(cellMin, cellMax);

                                            // Spawn everywhere, or only inside specified shapes?
                                            if (spawnInside == null || Random.value < spawnInside.GetDensity(debrisPoint))
                                            {
                                                Spawn(x, y, z, debrisPoint);
                                            }
                                        }
                                    }
                                    SgtHelper.EndRandomSeed();
                                }
                            }
                        }
                    }

                    bounds = newBounds;

                    if (spawnedDebris != null)
                    {
                        for (var i = spawnedDebris.Count - 1; i >= 0; i--)
                        {
                            var debris = spawnedDebris[i];

                            if (debris == null)
                            {
                                spawnedDebris.RemoveAt(i);
                            }
                            else if (bounds.Contains(debris.Cell) == false)
                            {
                                Despawn(debris, i);
                            }
                        }
                    }
                }

                UpdateDebrisScale(target.position);
            }
            else
            {
                ClearDebris();
            }
        }