Ejemplo n.º 1
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);
                }
            }
        }
Ejemplo n.º 2
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;
            }
        }