示例#1
0
        private Mesh GetMesh(int index)
        {
            SgtHelper.ClearCapacity(positions, 1024);
            SgtHelper.ClearCapacity(coords0, 1024);
            SgtHelper.ClearCapacity(indices, 1024);
            SgtHelper.ClearCapacity(colors, 1024);
            SgtHelper.ClearCapacity(normals, 1024);

            if (index >= meshes.Count)
            {
                var newMesh = SgtHelper.CreateTempMesh("Aurora Mesh (Generated)");

                meshes.Add(newMesh);

                return(newMesh);
            }

            var mesh = meshes[index];

            if (mesh == null)
            {
                mesh = meshes[index] = SgtHelper.CreateTempMesh("Aurora Mesh (Generated)");
            }

            return(mesh);
        }
示例#2
0
        private void UpdateMesh()
        {
            if (mesh == null)
            {
                mesh = SgtHelper.CreateTempMesh("Aurora Mesh (Generated)");
            }

            SgtHelper.ClearCapacity(tempPositions, 1024);
            SgtHelper.ClearCapacity(tempCoords0, 1024);
            SgtHelper.ClearCapacity(tempIndices, 1024);
            SgtHelper.ClearCapacity(tempColors, 1024);
            SgtHelper.ClearCapacity(tempNormals, 1024);

            if (pathDetail > 0 && pathLengthMin > 0.0f && pathLengthMax > 0.0f)
            {
                var vertexCount = 0;

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

            BakeMesh(mesh);
        }
        public void UpdateMesh()
        {
            if (mesh == null)
            {
                mesh = SgtHelper.CreateTempMesh("Lightning");
            }
            else
            {
                mesh.Clear(false);
            }

            var detailAddOne = Detail + 1;
            var positions    = new Vector3[detailAddOne * detailAddOne];
            var coords       = new Vector2[detailAddOne * detailAddOne];
            var indices      = new int[Detail * Detail * 6];
            var invDetail    = SgtHelper.Reciprocal(Detail);

            for (var y = 0; y < detailAddOne; y++)
            {
                for (var x = 0; x < detailAddOne; x++)
                {
                    var vertex = x + y * detailAddOne;
                    var fracX  = x * invDetail;
                    var fracY  = y * invDetail;
                    var angX   = (fracX - 0.5f) * Size;
                    var angY   = (fracY - 0.5f) * Size;

                    // TODO: Manually do this rotation
                    positions[vertex] = Quaternion.Euler(angX, angY, 0.0f) * new Vector3(0.0f, 0.0f, Radius);

                    coords[vertex] = new Vector2(fracX, fracY);
                }
            }

            for (var y = 0; y < Detail; y++)
            {
                for (var x = 0; x < Detail; x++)
                {
                    var index  = (x + y * Detail) * 6;
                    var vertex = x + y * detailAddOne;

                    indices[index + 0] = vertex;
                    indices[index + 1] = vertex + 1;
                    indices[index + 2] = vertex + detailAddOne;
                    indices[index + 3] = vertex + detailAddOne + 1;
                    indices[index + 4] = vertex + detailAddOne;
                    indices[index + 5] = vertex + 1;
                }
            }

            mesh.vertices  = positions;
            mesh.uv        = coords;
            mesh.triangles = indices;
        }
示例#4
0
        private Mesh GetOrNewMesh(SgtQuadsModel model)
        {
            var mesh = model.Mesh;

            if (mesh == null)
            {
                mesh = SgtHelper.CreateTempMesh("Quads Mesh (Generated)");

                model.SetMesh(mesh);
            }
            else
            {
                mesh.Clear(false);
            }

            return(mesh);
        }
示例#5
0
        private void UpdateMesh()
        {
            dirtyMesh = false;

            var count = BeginQuads();

            BuildRects();
            ConvertRectsToCoords();

            if (mesh == null)
            {
                mesh = SgtHelper.CreateTempMesh("Quads Mesh (Generated)");
            }
            else
            {
                mesh.Clear();
            }

            BuildMesh(mesh, count);

            EndQuads();
        }
        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();
            }
        }
示例#7
0
        public void UpdateMesh()
        {
            if (mesh == null)
            {
                mesh = SgtHelper.CreateTempMesh("Plane");

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

                        if (model != null)
                        {
                            model.SetMesh(mesh);
                        }
                    }
                }
            }
            else
            {
                mesh.Clear(false);
            }

            if (PlaneDetail >= 2)
            {
                var detail    = Mathf.Min(PlaneDetail, SgtHelper.QuadsPerMesh / 2);                 // Limit the amount of vertices that get made
                var positions = new Vector3[detail * 2 + 2];
                var normals   = new Vector3[detail * 2 + 2];
                var coords1   = new Vector2[detail * 2 + 2];
                var coords2   = new Vector2[detail * 2 + 2];
                var indices   = new int[detail * 6];
                var angleStep = SgtHelper.Divide(Mathf.PI * 2.0f, detail);
                var coordStep = SgtHelper.Reciprocal(detail);

                for (var i = 0; i <= detail; i++)
                {
                    var coord = coordStep * i;
                    var angle = angleStep * i;
                    var sin   = Mathf.Sin(angle);
                    var cos   = Mathf.Cos(angle);
                    var offV  = i * 2;

                    positions[offV + 0] = new Vector3(sin * RadiusMin, 0.0f, cos * RadiusMin);
                    positions[offV + 1] = new Vector3(sin * RadiusMax, 0.0f, cos * RadiusMax);

                    normals[offV + 0] = Vector3.up;
                    normals[offV + 1] = Vector3.up;

                    coords1[offV + 0] = new Vector2(0.0f, coord * RadiusMin);
                    coords1[offV + 1] = new Vector2(1.0f, coord * RadiusMax);

                    coords2[offV + 0] = new Vector2(RadiusMin, 0.0f);
                    coords2[offV + 1] = new Vector2(RadiusMax, 0.0f);
                }

                for (var i = 0; i < detail; i++)
                {
                    var offV = i * 2;
                    var offI = i * 6;

                    indices[offI + 0] = offV + 0;
                    indices[offI + 1] = offV + 1;
                    indices[offI + 2] = offV + 2;
                    indices[offI + 3] = offV + 3;
                    indices[offI + 4] = offV + 2;
                    indices[offI + 5] = offV + 1;
                }

                mesh.vertices  = positions;
                mesh.normals   = normals;
                mesh.uv        = coords1;
                mesh.uv2       = coords2;
                mesh.triangles = indices;
            }
        }
示例#8
0
        private void UpdateMesh()
        {
            if (quadsX > 0 && quadsZ > 0)
            {
                if (generatedMesh == null)
                {
                    generatedMesh = SgtHelper.CreateTempMesh("Spacetime Mesh (Generated)");
                }

                var vertsX    = quadsX + 1;
                var vertsZ    = quadsZ + 1;
                var total     = vertsX * vertsZ;
                var positions = new Vector3[total];
                var coords    = new Vector2[total];
                var indices   = new int[quadsX * quadsZ * 6];
                var stepX     = 1.0f / quadsX;
                var stepZ     = 1.0f / quadsZ;

                for (var z = 0; z < vertsZ; z++)
                {
                    for (var x = 0; x < vertsX; x++)
                    {
                        var u = x * stepX;
                        var v = z * stepZ;
                        var i = x + z * vertsX;

                        positions[i] = new Vector3(u - 0.5f, 0.0f, v - 0.5f);
                        coords[i]    = new Vector2(u, v);
                    }
                }

                for (var z = 0; z < quadsZ; z++)
                {
                    for (var x = 0; x < quadsX; x++)
                    {
                        var i = (x + z * quadsX) * 6;
                        var a = x + z * vertsX;
                        var b = a + 1;
                        var c = a + vertsX;
                        var d = b + vertsX;

                        indices[i + 0] = a;
                        indices[i + 1] = b;
                        indices[i + 2] = c;
                        indices[i + 3] = d;
                        indices[i + 4] = c;
                        indices[i + 5] = b;
                    }
                }

                generatedMesh.Clear(false);
                generatedMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
                generatedMesh.vertices    = positions;
                generatedMesh.uv          = coords;
                generatedMesh.triangles   = indices;
                generatedMesh.RecalculateNormals();
                //generatedMesh.RecalculateBounds();

                generatedMesh.bounds = new Bounds(Vector3.zero, Vector3.one);
            }

            ApplyMesh();
        }
示例#9
0
        private void HandleCameraDraw(Camera camera)
        {
            if (SgtHelper.CanDraw(gameObject, camera) == false)
            {
                return;
            }

            if (orbit != null)
            {
                var floatingCamera = default(SgtFloatingCamera);

                if (SgtFloatingCamera.TryGetInstance(ref floatingCamera) == true)
                {
                    if (visualMesh == null)
                    {
                        visualMesh = SgtHelper.CreateTempMesh("Orbit Visual");
                    }

                    meshPositions.Clear();
                    meshCoords.Clear();
                    meshColors.Clear();
                    meshIndices.Clear();

                    var position = floatingCamera.CalculatePosition(orbit.ParentPoint.Position);
                    var rotation = orbit.ParentPoint.transform.rotation * Quaternion.Euler(orbit.Tilt);
                    var r1       = orbit.Radius;
                    var r2       = orbit.Radius * (1.0f - orbit.Oblateness);
                    var i1       = r1 - thickness * 0.5;
                    var i2       = r2 - thickness * 0.5;
                    var o1       = i1 + thickness;
                    var o2       = i2 + thickness;
                    var step     = 360.0 / points;
                    var stepI    = 1.0f / (points - 1);

                    for (var i = 0; i < points; i++)
                    {
                        var angle = (orbit.Angle - i * step) * Mathf.Deg2Rad;
                        var sin   = System.Math.Sin(angle);
                        var cos   = System.Math.Cos(angle);

                        // Inner
                        {
                            var point   = position;
                            var offsetX = orbit.Offset.x + sin * i1;
                            var offsetY = orbit.Offset.y + 0.0;
                            var offsetZ = orbit.Offset.z + cos * i2;

                            SgtFloatingOrbit.Rotate(rotation, ref offsetX, ref offsetY, ref offsetZ);

                            point.x += (float)offsetX;
                            point.y += (float)offsetY;
                            point.z += (float)offsetZ;

                            point = transform.InverseTransformPoint(point);

                            meshPositions.Add(point);
                        }

                        // Outer
                        {
                            var point   = position;
                            var offsetX = orbit.Offset.x + sin * o1;
                            var offsetY = orbit.Offset.y + 0.0;
                            var offsetZ = orbit.Offset.z + cos * o2;

                            SgtFloatingOrbit.Rotate(rotation, ref offsetX, ref offsetY, ref offsetZ);

                            point.x += (float)offsetX;
                            point.y += (float)offsetY;
                            point.z += (float)offsetZ;

                            point = transform.InverseTransformPoint(point);

                            meshPositions.Add(point);
                        }

                        var u     = stepI * i;
                        var color = colors.Evaluate(u);

                        meshCoords.Add(new Vector2(u, 0.0f));
                        meshCoords.Add(new Vector2(u, 1.0f));

                        meshColors.Add(color);
                        meshColors.Add(color);
                    }

                    for (var i = 0; i < points; i++)
                    {
                        var indexA = i * 2 + 0;
                        var indexB = i * 2 + 1;
                        var indexC = i * 2 + 2; indexC %= points * 2;
                        var indexD = i * 2 + 3; indexD %= points * 2;

                        meshIndices.Add(indexA);
                        meshIndices.Add(indexB);
                        meshIndices.Add(indexC);
                        meshIndices.Add(indexD);
                        meshIndices.Add(indexC);
                        meshIndices.Add(indexB);
                    }

                    visualMesh.SetVertices(meshPositions);
                    visualMesh.SetTriangles(meshIndices, 0);
                    visualMesh.SetUVs(0, meshCoords);
                    visualMesh.SetColors(meshColors);
                    visualMesh.RecalculateBounds();
                }

                if (visualMesh != null)
                {
                    Graphics.DrawMesh(visualMesh, transform.localToWorldMatrix, material, gameObject.layer, camera);
                }
            }
        }
示例#10
0
        private void UpdateMesh()
        {
            if (Segments > 0 && SegmentDetail > 0 && RadiusDetail > 0)
            {
                if (generatedMesh == null)
                {
                    generatedMesh = SgtHelper.CreateTempMesh("Ring Mesh (Generated)");

                    ApplyMesh();
                }

                var slices    = SegmentDetail + 1;
                var rings     = RadiusDetail + 1;
                var total     = slices * rings * 2;
                var positions = new Vector3[total];
                var coords1   = new Vector2[total];
                var coords2   = new Vector2[total];
                var colors    = new Color[total];
                var indices   = new int[SegmentDetail * RadiusDetail * 6];
                var yawStep   = (Mathf.PI * 2.0f) / Segments / SegmentDetail;
                var sliceStep = 1.0f / SegmentDetail;
                var ringStep  = 1.0f / RadiusDetail;

                for (var slice = 0; slice < slices; slice++)
                {
                    var a = yawStep * slice;
                    var x = Mathf.Sin(a);
                    var z = Mathf.Cos(a);

                    for (var ring = 0; ring < rings; ring++)
                    {
                        var v       = rings * slice + ring;
                        var slice01 = sliceStep * slice;
                        var ring01  = ringStep * ring;
                        var radius  = Mathf.Lerp(RadiusMin, RadiusMax, ring01);

                        positions[v] = new Vector3(x * radius, 0.0f, z * radius);
                        colors[v]    = new Color(1.0f, 1.0f, 1.0f, 0.0f);
                        coords1[v]   = new Vector2(ring01, slice01);
                        coords2[v]   = new Vector2(radius, slice01 * radius);
                    }
                }

                for (var slice = 0; slice < SegmentDetail; slice++)
                {
                    for (var ring = 0; ring < RadiusDetail; ring++)
                    {
                        var i  = (slice * RadiusDetail + ring) * 6;
                        var v0 = slice * rings + ring;
                        var v1 = v0 + rings;

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

                generatedMesh.Clear(false);
                generatedMesh.vertices  = positions;
                generatedMesh.colors    = colors;
                generatedMesh.uv        = coords1;
                generatedMesh.uv2       = coords2;
                generatedMesh.triangles = indices;
                generatedMesh.RecalculateNormals();
                generatedMesh.RecalculateBounds();

                var bounds = generatedMesh.bounds;

                generatedMesh.bounds = SgtHelper.NewBoundsCenter(bounds, bounds.center + bounds.center.normalized * BoundsShift);
            }

            if (Shadow != null)
            {
                Shadow.RadiusMin = RadiusMin;
                Shadow.RadiusMax = RadiusMax;
            }

            ApplyMesh();
        }
示例#11
0
        public void Draw(SgtFloatingOrbit orbit)
        {
            if (cachedMeshFilterSet == false)
            {
                cachedMeshFilter    = GetComponent <MeshFilter>();
                cachedMeshFilterSet = true;
            }

            if (visualMesh == null)
            {
                visualMesh = cachedMeshFilter.sharedMesh = SgtHelper.CreateTempMesh("Orbit Visual");
            }

            positions.Clear();
            coords.Clear();
            colors.Clear();
            indices.Clear();

            foreach (var floatingCamera in SgtFloatingCamera.Instances)
            {
                var position = floatingCamera.CalculatePosition(ref orbit.ParentPoint.Position);
                var rotation = orbit.ParentPoint.transform.rotation * Quaternion.Euler(orbit.Tilt);
                var r1       = orbit.Radius;
                var r2       = orbit.Radius * (1.0f - orbit.Oblateness);
                var i1       = r1 - Thickness * 0.5;
                var i2       = r2 - Thickness * 0.5;
                var o1       = i1 + Thickness;
                var o2       = i2 + Thickness;
                var step     = 360.0 / Points;
                var stepI    = 1.0f / (Points - 1);

                for (var i = 0; i < Points; i++)
                {
                    var angle = (orbit.Angle - i * step) * Mathf.Deg2Rad;
                    var sin   = System.Math.Sin(angle);
                    var cos   = System.Math.Cos(angle);

                    // Inner
                    {
                        var point   = position;
                        var offsetX = sin * i1;
                        var offsetY = 0.0;
                        var offsetZ = cos * i2;

                        SgtFloatingOrbit.Rotate(rotation, ref offsetX, ref offsetY, ref offsetZ);

                        point.x += (float)offsetX;
                        point.y += (float)offsetY;
                        point.z += (float)offsetZ;

                        point = transform.InverseTransformPoint(point);

                        positions.Add(point);
                    }

                    // Outer
                    {
                        var point   = position;
                        var offsetX = sin * o1;
                        var offsetY = 0.0;
                        var offsetZ = cos * o2;

                        SgtFloatingOrbit.Rotate(rotation, ref offsetX, ref offsetY, ref offsetZ);

                        point.x += (float)offsetX;
                        point.y += (float)offsetY;
                        point.z += (float)offsetZ;

                        point = transform.InverseTransformPoint(point);

                        positions.Add(point);
                    }

                    var u     = stepI * i;
                    var color = Colors.Evaluate(u);

                    coords.Add(new Vector2(u, 0.0f));
                    coords.Add(new Vector2(u, 1.0f));

                    colors.Add(color);
                    colors.Add(color);
                }

                for (var i = 0; i < Points; i++)
                {
                    var indexA = i * 2 + 0;
                    var indexB = i * 2 + 1;
                    var indexC = i * 2 + 2; indexC %= Points * 2;
                    var indexD = i * 2 + 3; indexD %= Points * 2;

                    indices.Add(indexA);
                    indices.Add(indexB);
                    indices.Add(indexC);
                    indices.Add(indexD);
                    indices.Add(indexC);
                    indices.Add(indexB);
                }

                visualMesh.SetVertices(positions);
                visualMesh.SetTriangles(indices, 0);
                visualMesh.SetUVs(0, coords);
                visualMesh.SetColors(colors);
                visualMesh.RecalculateBounds();

                return;
            }
        }
        public void UpdateMesh()
        {
            if (generatedMesh == null)
            {
                generatedMesh = SgtHelper.CreateTempMesh("Atmosphere OuterMesh (Generated)");

                ApplyMesh();
            }

            var positions  = new Vector3[Rings * Detail + 2];
            var indices    = new int[Rings * Detail * 6];
            var detailStep = (Mathf.PI * 2.0f) / Detail;
            var ringStep   = 1.0f / Rings;
            var ringOffset = 0.5f / Rings;

            positions[1] = new Vector3(0.0f, 0.0f, -1.0f);
            positions[0] = new Vector3(0.0f, 0.0f, 1.0f);

            for (var d = 0; d < Detail; d++)
            {
                var angle = d * detailStep;
                var x     = Mathf.Sin(angle);
                var y     = Mathf.Cos(angle);
                var o     = d * Rings + 2;

                for (var r = 0; r < Rings; r++)
                {
                    var z = r * ringStep + ringOffset;                     // 0..1

                    z = z * z;
                    z = z * 2.0f - 1.0f;                     // -1..1

                    var s = Mathf.Sin(Mathf.Acos(z));

                    positions[o + r] = new Vector3(x * s, y * s, -z);
                }
            }

            var i = 0;

            for (var d = 0; d < Detail; d++)
            {
                var a = d * Rings + 2;
                var b = ((d + 1) % Detail) * Rings + 2;

                // Caps
                indices[i++] = 0;
                indices[i++] = b;
                indices[i++] = a;

                indices[i++] = 1;
                indices[i++] = a + Rings - 1;
                indices[i++] = b + Rings - 1;

                // Body
                for (var r = 0; r < Rings - 1; r++)
                {
                    indices[i++] = b + r;
                    indices[i++] = b + r + 1;
                    indices[i++] = a + r;
                    indices[i++] = b + r + 1;
                    indices[i++] = a + r + 1;
                    indices[i++] = a + r;
                }
            }

            generatedMesh.Clear(false);
            generatedMesh.vertices  = positions;
            generatedMesh.triangles = indices;
            generatedMesh.RecalculateBounds();
        }