예제 #1
0
        public override void CalculateShadow(SgtLight light)
        {
            var direction = default(Vector3);
            var position  = default(Vector3);
            var color     = default(Color);

            SgtLight.Calculate(light, transform.position, null, null, ref position, ref direction, ref color);

            var dot      = Vector3.Dot(direction, transform.up);
            var radiusXZ = (transform.lossyScale.x + transform.lossyScale.z) * 0.5f * RadiusMax;
            var radiusY  = transform.lossyScale.y * RadiusMax;
            var radius   = GetRadius(radiusY, radiusXZ, dot * Mathf.PI * 0.5f);
            var rotation = Quaternion.FromToRotation(direction, Vector3.back);
            var vector   = rotation * transform.up;
            var spin     = Quaternion.LookRotation(Vector3.forward, new Vector2(-vector.x, vector.y));             // Orient the shadow ellipse
            var scale    = SgtHelper.Reciprocal3(new Vector3(radiusXZ, radius, 1.0f));
            var shadowT  = Matrix4x4.Translate(-transform.position);
            var shadowR  = Matrix4x4.Rotate(spin * rotation);
            var shadowS  = Matrix4x4.Scale(scale);

            cachedActive  = true;
            cachedMatrix  = shadowS * shadowR * shadowT;
            cachedRatio   = SgtHelper.Divide(RadiusMax, RadiusMax - RadiusMin);
            cachedRadius  = SgtHelper.UniformScale(transform.lossyScale) * RadiusMax;
            cachedTexture = generatedTexture;
        }
예제 #2
0
        private float GetSky(Camera camera)
        {
            if (Lit == true && Night == true)
            {
                var mask   = 1 << gameObject.layer;
                var lights = SgtLight.Find(Lit, mask);

                var lighting        = 0.0f;
                var cameraDirection = (camera.transform.position - transform.position).normalized;

                for (var i = 0; i < lights.Count && i < 2; i++)
                {
                    var light     = lights[i];
                    var position  = default(Vector3);
                    var direction = default(Vector3);
                    var color     = default(Color);

                    SgtLight.Calculate(light, transform.position, null, null, ref position, ref direction, ref color);

                    var dot     = Vector3.Dot(direction, cameraDirection) * 0.5f + 0.5f;
                    var night01 = Mathf.InverseLerp(NightEnd, NightStart, dot);
                    var night   = SgtEase.Evaluate(NightEase, 1.0f - Mathf.Pow(night01, NightPower));

                    if (night > lighting)
                    {
                        lighting = night;
                    }
                }

                return(Mathf.Lerp(NightSky, Sky, lighting));
            }

            return(Sky);
        }
예제 #3
0
        protected virtual void LateUpdate()
        {
            if (generatedMesh == null || dirtyMesh == true)
            {
                Rebuild();
            }

            if (generatedMesh != null && material != null)
            {
                Properties.SetFloat(SgtShader._WaterLevel, waterLevel);

                // Write direction of nearest light?
                if (material.GetFloat("_HasNight") == 1.0f)
                {
                    var mask   = 1 << gameObject.layer;
                    var lights = SgtLight.Find(true, mask, transform.position);

                    SgtLight.FilterOut(transform.position);

                    if (lights.Count > 0)
                    {
                        var position  = Vector3.zero;
                        var direction = Vector3.forward;
                        var color     = Color.white;

                        SgtLight.Calculate(lights[0], transform.position, default(Transform), default(Transform), ref position, ref direction, ref color);

                        properties.SetVector(Shader.PropertyToID("_NightDirection"), -direction);
                    }
                }
            }
        }
        public override void CalculateShadow(SgtLight light)
        {
            if (Texture != null)
            {
                var direction = default(Vector3);
                var position  = default(Vector3);
                var color     = default(Color);

                SgtLight.Calculate(light, transform.position, null, null, ref position, ref direction, ref color);

                var rotation = Quaternion.FromToRotation(direction, Vector3.back);
                var squash   = Vector3.Dot(direction, transform.up);                 // Find how squashed the ellipse is based on light direction
                var width    = transform.lossyScale.x * RadiusMax;
                var length   = transform.lossyScale.z * RadiusMax;
                var axis     = rotation * transform.up;                                                // Find the transformed up axis
                var spin     = Quaternion.LookRotation(Vector3.forward, new Vector2(-axis.x, axis.y)); // Orient the shadow ellipse
                var scale    = SgtHelper.Reciprocal3(new Vector3(width, length * Mathf.Abs(squash), 1.0f));
                var skew     = Mathf.Tan(SgtHelper.Acos(-squash));

                var shadowT = Matrix4x4.Translate(-transform.position);
                var shadowR = Matrix4x4.Rotate(spin * rotation);            // Spin the shadow so lines up with its tilt
                var shadowS = Matrix4x4.Scale(scale);                       // Scale the ring into an oval
                var shadowK = SgtHelper.ShearingZ(new Vector2(0.0f, skew)); // Skew the shadow so it aligns with the ring plane

                cachedActive  = true;
                cachedMatrix  = shadowS * shadowK * shadowR * shadowT;
                cachedRatio   = SgtHelper.Divide(RadiusMax, RadiusMax - RadiusMin);
                cachedRadius  = SgtHelper.UniformScale(transform.lossyScale) * RadiusMax;
                cachedTexture = Texture;
            }
            else
            {
                cachedActive = false;
            }
        }
예제 #5
0
        public override bool CalculateShadow(ref Matrix4x4 matrix, ref float ratio)
        {
            var light = default(SgtLight);

            if (SgtLight.Find(ref light) == true)
            {
                var direction = default(Vector3);
                var position  = default(Vector3);
                var color     = default(Color);

                SgtLight.Calculate(light.CachedLight, transform.position, null, null, ref position, ref direction, ref color);

                var dot      = Vector3.Dot(direction, transform.up);
                var radiusXZ = (transform.lossyScale.x + transform.lossyScale.z) * 0.5f * RadiusMax;
                var radiusY  = transform.lossyScale.y * RadiusMax;
                var radius   = GetRadius(radiusY, radiusXZ, dot * Mathf.PI * 0.5f);
                var rotation = Quaternion.FromToRotation(direction, Vector3.back);
                var vector   = rotation * transform.up;
                var spin     = Quaternion.LookRotation(Vector3.forward, new Vector2(-vector.x, vector.y));                 // Orient the shadow ellipse
                var scale    = SgtHelper.Reciprocal3(new Vector3(radiusXZ, radius, 1.0f));
                var shadowT  = SgtHelper.Translation(-transform.position);
                var shadowR  = SgtHelper.Rotation(spin * rotation);
                var shadowS  = SgtHelper.Scaling(scale);

                matrix = shadowS * shadowR * shadowT;
                ratio  = SgtHelper.Divide(RadiusMax, RadiusMax - RadiusMin);

                return(true);
            }

            return(false);
        }
예제 #6
0
        public override bool CalculateShadow(ref Matrix4x4 matrix, ref float ratio)
        {
            var light = default(SgtLight);

            if (Texture != null && SgtLight.Find(ref light) == true)
            {
                var direction = default(Vector3);
                var position  = default(Vector3);
                var color     = default(Color);

                SgtLight.Calculate(light.CachedLight, transform.position, null, null, ref position, ref direction, ref color);

                var rotation = Quaternion.FromToRotation(direction, Vector3.back);
                var squash   = Vector3.Dot(direction, transform.up);                 // Find how squashed the ellipse is based on light direction
                var width    = transform.lossyScale.x * RadiusMax;
                var length   = transform.lossyScale.z * RadiusMax;
                var axis     = rotation * transform.up;                                                // Find the transformed up axis
                var spin     = Quaternion.LookRotation(Vector3.forward, new Vector2(-axis.x, axis.y)); // Orient the shadow ellipse
                var scale    = SgtHelper.Reciprocal3(new Vector3(width, length * Mathf.Abs(squash), 1.0f));
                var skew     = Mathf.Tan(SgtHelper.Acos(-squash));

                var shadowT = SgtHelper.Translation(-transform.position);
                var shadowR = SgtHelper.Rotation(spin * rotation);          // Spin the shadow so lines up with its tilt
                var shadowS = SgtHelper.Scaling(scale);                     // Scale the ring into an oval
                var shadowK = SgtHelper.ShearingZ(new Vector2(0.0f, skew)); // Skew the shadow so it aligns with the ring plane

                matrix = shadowS * shadowK * shadowR * shadowT;
                ratio  = SgtHelper.Divide(RadiusMax, RadiusMax - RadiusMin);

                return(true);
            }

            return(false);
        }
예제 #7
0
        private float GetSky(Vector3 eye, float localDistance)
        {
            var height01 = Mathf.InverseLerp(OuterRadius, innerMeshRadius, localDistance);
            var mul      = outerDepthTex.GetPixelBilinear(height01 / (1.0f - outerFog), 0.0f).a;

            if (lit == true && night == true)
            {
                var mask   = 1 << gameObject.layer;
                var lights = SgtLight.Find(lit, mask, cachedTransform.position);

                var lighting        = 0.0f;
                var cameraDirection = (eye - cachedTransform.position).normalized;

                for (var i = 0; i < lights.Count && i < 2; i++)
                {
                    var light     = lights[i];
                    var position  = default(Vector3);
                    var direction = default(Vector3);
                    var color     = default(Color);

                    SgtLight.Calculate(light, cachedTransform.position, null, null, ref position, ref direction, ref color);

                    var dot     = Vector3.Dot(direction, cameraDirection) * 0.5f + 0.5f;
                    var night01 = Mathf.InverseLerp(nightEnd, nightStart, dot);
                    var night   = SgtEase.Evaluate(nightEase, 1.0f - Mathf.Pow(night01, nightPower));

                    if (night > lighting)
                    {
                        lighting = night;
                    }
                }

                return(Mathf.Lerp(nightSky, sky, lighting) * mul);
            }

            return(sky * mul);
        }