Ejemplo n.º 1
0
        private void HandleCameraDraw(Camera camera)
        {
            if (SgtHelper.CanDraw(gameObject, camera) == false)
            {
                return;
            }

            var matrix = transform.localToWorldMatrix;

            if (cameraOffset != 0.0f)
            {
                var direction = Vector3.Normalize(camera.transform.position - transform.position);

                matrix = Matrix4x4.Translate(direction * cameraOffset) * matrix;
            }

            SgtHelper.BeginRandomSeed(seed);
            for (var i = 0; i < planeCount; i++)
            {
                var rotation = Matrix4x4.Rotate(Random.rotation);

                Graphics.DrawMesh(mesh, matrix * rotation, material, gameObject.layer, camera);
            }
            SgtHelper.EndRandomSeed();
        }
Ejemplo n.º 2
0
        private void HandleCameraDraw(Camera camera)
        {
            if (SgtHelper.CanDraw(gameObject, camera) == false)
            {
                return;
            }

            if (depthTex != null)
            {
#if UNITY_EDITOR
                SgtHelper.MakeTextureReadable(depthTex);
#endif
                material.SetFloat(SgtShader._Sky, GetSky(camera.transform.position));
            }

            var scale  = SgtHelper.Divide(radius, meshRadius);
            var matrix = transform.localToWorldMatrix * Matrix4x4.Scale(Vector3.one * scale);

            if (cameraOffset != 0.0f)
            {
                var direction = Vector3.Normalize(camera.transform.position - transform.position);

                matrix = Matrix4x4.Translate(direction * cameraOffset) * matrix;
            }

            Graphics.DrawMesh(mesh, matrix, material, gameObject.layer, camera);
        }
Ejemplo n.º 3
0
        private void HandleCameraDraw(Camera camera)
        {
            if (SgtHelper.CanDraw(gameObject, camera) == false)
            {
                return;
            }

            UpdateWells();

            if (mesh != null)
            {
                var modifier = GetComponent <SgtSpacetimeModifier>();

                if (modifier != null)
                {
                    foreach (var matrix in modifier.GetMatrices())
                    {
                        Graphics.DrawMesh(mesh, matrix, material, 0, camera);
                    }
                }
                else
                {
                    var matrix = transform.localToWorldMatrix;

                    matrix *= Matrix4x4.Translate(new Vector3(center.x, 0.0f, center.y));
                    matrix *= Matrix4x4.Scale(new Vector3(size.x, 1.0f, size.y));

                    Graphics.DrawMesh(mesh, matrix, material, 0, camera);
                }
            }
        }
Ejemplo n.º 4
0
        protected override void HandleCameraDraw(Camera camera)
        {
            if (SgtHelper.CanDraw(gameObject, camera) == false)
            {
                return;
            }

            var properties = shaderProperties.GetProperties(material, camera);
            var velocity   = stretchVector;
            var sgtCamera  = default(SgtCamera);

            if (SgtCamera.TryFind(camera, ref sgtCamera) == true)
            {
                properties.SetFloat(SgtShader._CameraRollAngle, sgtCamera.RollAngle * Mathf.Deg2Rad);

                var cameraVelocity = sgtCamera.Velocity;
                var cameraSpeed    = cameraVelocity.magnitude;

                if (cameraSpeed > stretchLimit)
                {
                    cameraVelocity = cameraVelocity.normalized * stretchLimit;
                }

                velocity += cameraVelocity * stretchScale;
            }
            else
            {
                properties.SetFloat(SgtShader._CameraRollAngle, 0.0f);
            }

            if (stretch == true)
            {
                properties.SetVector(SgtShader._StretchVector, velocity);
                properties.SetVector(SgtShader._StretchDirection, velocity.normalized);
                properties.SetFloat(SgtShader._StretchLength, velocity.magnitude);
            }
            else
            {
                properties.SetVector(SgtShader._StretchVector, Vector3.zero);
                properties.SetVector(SgtShader._StretchDirection, Vector3.zero);
                properties.SetFloat(SgtShader._StretchLength, 0.0f);
            }

            if (camera.orthographic == true)
            {
                properties.SetFloat(SgtShader._ClampSizeScale, camera.orthographicSize * 0.0025f);
            }
            else
            {
                properties.SetFloat(SgtShader._ClampSizeScale, Mathf.Tan(camera.fieldOfView * Mathf.Deg2Rad * 0.5f) * 2.0f);
            }

            HandleDrawMesh(camera, properties);
        }
Ejemplo n.º 5
0
        private void HandleCameraDraw(Camera camera)
        {
            if (SgtHelper.CanDraw(gameObject, camera) == false)
            {
                return;
            }

            // Write camera-dependent shader values
            if (innerMaterial != null && outerMaterial != null)
            {
                var localPosition  = CachedTransform.InverseTransformPoint(camera.transform.position);
                var localDistance  = localPosition.magnitude;
                var innerThickness = default(float);
                var outerThickness = default(float);
                var innerRatio     = SgtHelper.Divide(innerMeshRadius, OuterRadius);
                var middleRatio    = Mathf.Lerp(innerRatio, 1.0f, middle);
                var distance       = SgtHelper.Divide(localDistance, OuterRadius);
                var innerDensity   = 1.0f - innerFog;
                var outerDensity   = 1.0f - outerFog;

                SgtHelper.CalculateHorizonThickness(innerRatio, middleRatio, distance, out innerThickness, out outerThickness);

                innerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(innerThickness * innerDensity));
                outerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(outerThickness * outerDensity));

                if (outerDepthTex != null)
                {
#if UNITY_EDITOR
                    SgtHelper.MakeTextureReadable(outerDepthTex);
#endif
                    outerMaterial.SetFloat(SgtShader._Sky, GetSky(localDistance));
                }

                UpdateMaterialNonSerialized();
            }

            var scale  = SgtHelper.Divide(OuterRadius, outerMeshRadius);
            var matrix = CachedTransform.localToWorldMatrix * Matrix4x4.Scale(Vector3.one * scale);

            if (cameraOffset != 0.0f)
            {
                var direction = Vector3.Normalize(camera.transform.position - cachedTransform.position);

                matrix = Matrix4x4.Translate(direction * cameraOffset) * matrix;
            }

            Graphics.DrawMesh(outerMesh, matrix, outerMaterial, gameObject.layer, camera);
        }
Ejemplo n.º 6
0
        private void HandleCameraDraw(Camera camera)
        {
            if (SgtHelper.CanDraw(gameObject, camera) == false)
            {
                return;
            }

            var matrix = cachedTransform.localToWorldMatrix;

            if (cameraOffset != 0.0f)
            {
                var direction = Vector3.Normalize(camera.transform.position - cachedTransform.position);

                matrix = Matrix4x4.Translate(direction * cameraOffset) * matrix;
            }

            Graphics.DrawMesh(mesh, matrix, material, gameObject.layer, camera);
        }
Ejemplo n.º 7
0
        private void HandleCameraDraw(Camera camera)
        {
            if (SgtHelper.CanDraw(gameObject, camera) == false)
            {
                return;
            }

            if (OnDrawQuad != null)
            {
                foreach (var cube in cubes)
                {
                    foreach (var quad in cube.Quads)
                    {
                        OnDrawQuad.Invoke(camera, quad, transform.localToWorldMatrix, gameObject.layer);
                    }
                }
            }
        }
Ejemplo n.º 8
0
        protected override void HandleCameraDraw(Camera camera)
        {
            if (SgtHelper.CanDraw(gameObject, camera) == false)
            {
                return;
            }

            var properties = shaderProperties.GetProperties(material, camera);

            // Change brightness based on viewing angle?
            var dir    = (transform.position - camera.transform.position).normalized;
            var theta  = Mathf.Abs(Vector3.Dot(transform.up, dir));
            var bright = Mathf.Lerp(horizontalBrightness, Brightness, Mathf.Pow(theta, horizontalPower));
            var color  = SgtHelper.Brighten(Color, Color.a * bright);

            properties.SetColor(SgtShader._Color, color);

            Graphics.DrawMesh(mesh, transform.localToWorldMatrix, material, gameObject.layer, camera, 0, properties);
        }
Ejemplo n.º 9
0
        private void HandleCameraDraw(Camera camera)
        {
            if (SgtHelper.CanDraw(gameObject, camera) == false)
            {
                return;
            }

            if (segments > 0)
            {
                var matrix   = transform.localToWorldMatrix;
                var rotation = Matrix4x4.Rotate(Quaternion.Euler(0.0f, 360.0f / segments, 0.0f));

                for (var i = 0; i < segments; i++)
                {
                    Graphics.DrawMesh(mesh, matrix, material, gameObject.layer, camera);

                    matrix *= rotation;
                }
            }
        }
Ejemplo n.º 10
0
        protected override void HandleCameraDraw(Camera camera)
        {
            if (SgtHelper.CanDraw(gameObject, camera) == false)
            {
                return;
            }

            var properties = shaderProperties.GetProperties(material, camera);
            var sgtCamera  = default(SgtCamera);

            if (SgtCamera.TryFind(camera, ref sgtCamera) == true)
            {
                properties.SetFloat(SgtShader._CameraRollAngle, sgtCamera.RollAngle * Mathf.Deg2Rad);
            }
            else
            {
                properties.SetFloat(SgtShader._CameraRollAngle, 0.0f);
            }

            Graphics.DrawMesh(mesh, transform.localToWorldMatrix, material, gameObject.layer, camera, 0, properties);
        }
Ejemplo n.º 11
0
        protected override void HandleCameraDraw(Camera camera)
        {
            if (SgtHelper.CanDraw(gameObject, camera) == false)
            {
                return;
            }

            var properties = shaderProperties.GetProperties(material, camera);

            if (camera.orthographic == true)
            {
                properties.SetFloat(SgtShader._ClampSizeScale, camera.orthographicSize * 0.0025f);
            }
            else
            {
                properties.SetFloat(SgtShader._ClampSizeScale, Mathf.Tan(camera.fieldOfView * Mathf.Deg2Rad * 0.5f) * 2.0f);
            }

            var matrix = Matrix4x4.Translate(camera.transform.position - transform.position) * transform.localToWorldMatrix;

            Graphics.DrawMesh(mesh, matrix, material, gameObject.layer, camera, 0, properties);
        }
Ejemplo n.º 12
0
        private void HandleCameraDraw(Camera camera)
        {
            if (SgtHelper.CanDraw(gameObject, camera) == false)
            {
                return;
            }

            //var layer = SgtHelper.GetRenderingLayers(gameObject, renderingLayer);
            var layer = gameObject.layer;

            Graphics.DrawMesh(generatedMesh, transform.localToWorldMatrix, material, layer, camera, 0, properties);

            var finalSharedMaterial = sharedMaterial;

            if (OnOverrideSharedMaterial != null)
            {
                OnOverrideSharedMaterial.Invoke(ref finalSharedMaterial, camera);
            }

            if (SgtHelper.Enabled(finalSharedMaterial) == true && finalSharedMaterial.Material != null)
            {
                Graphics.DrawMesh(generatedMesh, transform.localToWorldMatrix, finalSharedMaterial.Material, layer, camera, 0, properties);
            }
        }
Ejemplo n.º 13
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);
                }
            }
        }