Exemplo n.º 1
0
    private void ConfigureShaders()
    {
        this.ConfigureTheForestShaders();
        if (this.ShadowsActive)
        {
            SunshineKeywords.SetFilterStyle(Sunshine.Instance.ShadowFilter);
        }
        else
        {
            SunshineKeywords.DisableShadows();
        }
        Matrix4x4 lhs = Matrix4x4.identity;

        lhs = SunshineMath.ToRectSpaceProjection(Sunshine.Instance.CascadeRect(0)) * Sunshine.Instance.SunLightCamera.projectionMatrix;
        SunshineMath.SetLinearDepthProjection(ref lhs, Sunshine.Instance.SunLightCamera.farClipPlane);
        Matrix4x4 matrix4x = lhs * Sunshine.Instance.SunLightCamera.worldToCameraMatrix;
        Matrix4x4 mat      = matrix4x * this.AttachedCamera.cameraToWorldMatrix;

        Shader.SetGlobalMatrix("sunshine_CameraVToSunVP", mat);
        Shader.SetGlobalMatrix("sunshine_WorldToSunVP", matrix4x);
        float num = (float)Sunshine.Instance.Lightmap.width;

        Shader.SetGlobalVector("sunshine_ShadowParamsAndHalfTexel", new Vector4(Sunshine.Instance.SunLight.shadowStrength, Sunshine.Instance.CascadeFade, 0.5f / num, 0.5f / num));
        this.ConfigureCascadeClips(this.AttachedCamera.farClipPlane);
        Vector3   position  = (!this.AttachedCamera.orthographic) ? base.transform.position : this.AttachedCamera.ViewportToWorldPoint(new Vector3(0f, 0f, 0f));
        Vector3   position2 = this.AttachedCamera.ViewportToWorldPoint(new Vector3(0f, 0f, this.AttachedCamera.farClipPlane));
        Vector3   position3 = this.AttachedCamera.ViewportToWorldPoint(new Vector3(1f, 0f, this.AttachedCamera.farClipPlane));
        Vector3   position4 = this.AttachedCamera.ViewportToWorldPoint(new Vector3(0f, 1f, this.AttachedCamera.farClipPlane));
        Transform transform = Sunshine.Instance.SunLightCamera.transform;
        Vector3   vector    = transform.InverseTransformPoint(position);
        Vector3   vector2   = transform.InverseTransformPoint(position2);
        Vector3   vector3   = transform.InverseTransformPoint(position3);
        Vector3   vector4   = transform.InverseTransformPoint(position4);
        Vector2   vector5   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position));
        Vector2   vector6   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position2));
        Vector2   vector7   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position3));
        Vector2   vector8   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position4));
        Vector4   vector9   = new Vector4(vector6.x, vector6.y, vector2.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector.y);
        Vector4   vector10  = new Vector4(vector5.x, vector5.y, vector.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector.y);
        Vector4   vec       = vector9 - vector10;
        Vector4   vec2      = new Vector4(vector7.x, vector7.y, vector3.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector3.y) - vector9;
        Vector4   vec3      = new Vector4(vector8.x, vector8.y, vector4.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector4.y) - vector9;
        Rect      rect      = Sunshine.Instance.CascadeRect(0);

        SunshineMath.ShadowCoordDataInRect(ref vector10, ref rect);
        SunshineMath.ShadowCoordDataRayInRect(ref vec, ref rect);
        SunshineMath.ShadowCoordDataRayInRect(ref vec2, ref rect);
        SunshineMath.ShadowCoordDataRayInRect(ref vec3, ref rect);
        Shader.SetGlobalFloat("sunshine_IsOrthographic", (!this.AttachedCamera.orthographic) ? 0f : 1f);
        Shader.SetGlobalVector("sunshine_ShadowCoordDepthStart", vector10);
        Shader.SetGlobalVector("sunshine_ShadowCoordDepthRayZ", vec);
        Shader.SetGlobalVector("sunshine_ShadowCoordDepthRayU", vec2);
        Shader.SetGlobalVector("sunshine_ShadowCoordDepthRayV", vec3);
        float   num2     = Sunshine.Instance.SunLightCamera.orthographicSize * 2f;
        Vector2 vector11 = new Vector2(num2, num2);

        vector11.x /= rect.width;
        vector11.y /= rect.height;
        Vector3 v = new Vector3(vector11.x, vector11.y, Sunshine.Instance.SunLightCamera.farClipPlane) / this.AttachedCamera.farClipPlane;

        Shader.SetGlobalVector("sunshine_ShadowToWorldScale", v);
        Matrix4x4 zero      = Matrix4x4.zero;
        Vector3   position5 = Sunshine.Instance.SunLightCamera.ViewportToWorldPoint(new Vector3(0f, 0f, 0f));
        Vector3   position6 = Sunshine.Instance.SunLightCamera.ViewportToWorldPoint(new Vector3(1f, 1f, 0f));

        for (int i = 0; i < Sunshine.Instance.CascadeCount; i++)
        {
            Vector4 v2 = new Vector4(0f, 0f, 1f, 1f);
            if (i > 0)
            {
                Camera  camera   = Sunshine.Instance.SunLightCameras[i];
                Vector3 vector12 = camera.WorldToViewportPoint(position5);
                Vector3 vector13 = camera.WorldToViewportPoint(position6);
                v2 = new Vector4(vector12.x, vector12.y, vector13.x, vector13.y);
            }
            Rect rect2 = Sunshine.Instance.CascadeRect(i);
            v2.x  = rect2.xMin + rect2.width * v2.x;
            v2.y  = rect2.yMin + rect2.height * v2.y;
            v2.z  = rect2.xMin + rect2.width * v2.z;
            v2.w  = rect2.yMin + rect2.height * v2.w;
            v2.z -= v2.x;
            v2.w -= v2.y;
            zero.SetRow(i, v2);
        }
        Vector4 row = zero.GetRow(0);

        for (int j = 0; j < Sunshine.Instance.CascadeCount; j++)
        {
            Vector4 row2 = zero.GetRow(j);
            row2.z /= row.z;
            row2.w /= row.w;
            zero.SetRow(j, row2);
        }
        bool flag = Sunshine.Instance.OvercastTexture;

        this.ConfigureOvercast(flag, (!flag) ? Sunshine.Instance.BlankOvercastTexture : Sunshine.Instance.OvercastTexture, Sunshine.Instance.OvercastScale, Sunshine.Instance.OvercastMovement, Sunshine.Instance.OvercastPlaneHeight);
        Shader.SetGlobalMatrix("sunshine_CascadeRanges", zero);
    }
Exemplo n.º 2
0
    private void ConfigureShaders()
    {
        this.ConfigureTheForestShaders();
        if (this.ShadowsActive)
        {
            SunshineKeywords.SetFilterStyle(Sunshine.Instance.ShadowFilter);
        }
        else
        {
            SunshineKeywords.DisableShadows();
        }
        Matrix4x4 lhs = Matrix4x4.identity;

        lhs = SunshineMath.ToRectSpaceProjection(Sunshine.Instance.CascadeRect(0)) * Sunshine.Instance.SunLightCamera.projectionMatrix;
        SunshineMath.SetLinearDepthProjection(ref lhs, Sunshine.Instance.SunLightCamera.farClipPlane);
        Matrix4x4 matrix4x = lhs * Sunshine.Instance.SunLightCamera.worldToCameraMatrix;
        Matrix4x4 value    = matrix4x * this.AttachedCamera.cameraToWorldMatrix;

        Shader.SetGlobalMatrix("sunshine_CameraVToSunVP", value);
        Shader.SetGlobalMatrix("sunshine_WorldToSunVP", matrix4x);
        float num = (float)Sunshine.Instance.Lightmap.width;

        Shader.SetGlobalVector("sunshine_ShadowParamsAndHalfTexel", new Vector4(Sunshine.Instance.SunLight.shadowStrength, Sunshine.Instance.CascadeFade, 0.5f / num, 0.5f / num));
        this.ConfigureCascadeClips(this.AttachedCamera.farClipPlane);
        Vector3   position  = this.AttachedCamera.ViewportToWorldPoint(new Vector3(0f, 0f, this.AttachedCamera.farClipPlane));
        Vector3   position2 = this.AttachedCamera.ViewportToWorldPoint(new Vector3(1f, 0f, this.AttachedCamera.farClipPlane));
        Vector3   position3 = this.AttachedCamera.ViewportToWorldPoint(new Vector3(0f, 1f, this.AttachedCamera.farClipPlane));
        Transform transform = Sunshine.Instance.SunLightCamera.transform;
        Vector3   position4 = (!this.AttachedCamera.orthographic) ? base.transform.position : this.AttachedCamera.ViewportToWorldPoint(new Vector3(0f, 0f, 0f));
        Vector3   vector    = transform.InverseTransformPoint(position4);
        Vector3   vector2   = transform.InverseTransformPoint(position);
        Vector3   vector3   = transform.InverseTransformPoint(position2);
        Vector3   vector4   = transform.InverseTransformPoint(position3);
        Vector2   vector5   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position4));
        Vector2   vector6   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position));
        Vector2   vector7   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position2));
        Vector2   vector8   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position3));
        Vector4   vector9   = new Vector4(vector6.x, vector6.y, vector2.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector.y);
        Vector4   vector10  = new Vector4(vector5.x, vector5.y, vector.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector.y);
        Vector4   value2    = vector9 - vector10;
        Vector4   value3    = new Vector4(vector7.x, vector7.y, vector3.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector3.y) - vector9;
        Vector4   value4    = new Vector4(vector8.x, vector8.y, vector4.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector4.y) - vector9;
        Rect      rect      = Sunshine.Instance.CascadeRect(0);

        SunshineMath.ShadowCoordDataInRect(ref vector10, ref rect);
        SunshineMath.ShadowCoordDataRayInRect(ref value2, ref rect);
        SunshineMath.ShadowCoordDataRayInRect(ref value3, ref rect);
        SunshineMath.ShadowCoordDataRayInRect(ref value4, ref rect);
        Shader.SetGlobalFloat("sunshine_IsOrthographic", (!this.AttachedCamera.orthographic) ? 0f : 1f);
        Shader.SetGlobalVector("sunshine_ShadowCoordDepthStart", vector10);
        Shader.SetGlobalVector("sunshine_ShadowCoordDepthRayZ", value2);
        Shader.SetGlobalVector("sunshine_ShadowCoordDepthRayU", value3);
        Shader.SetGlobalVector("sunshine_ShadowCoordDepthRayV", value4);
        if (ForestVR.Enabled)
        {
            Vector3   a                   = Quaternion.Inverse(InputTracking.GetLocalRotation(VRNode.LeftEye)) * InputTracking.GetLocalPosition(VRNode.LeftEye);
            Vector3   b                   = Quaternion.Inverse(InputTracking.GetLocalRotation(VRNode.RightEye)) * InputTracking.GetLocalPosition(VRNode.RightEye);
            Vector3   vector11            = (a - b) * 0.5f;
            Matrix4x4 cameraToWorldMatrix = this.AttachedCamera.cameraToWorldMatrix;
            Vector3   vector12            = cameraToWorldMatrix.MultiplyPoint(-vector11);
            Vector3   vector13            = cameraToWorldMatrix.MultiplyPoint(vector11);
            position4 = vector12;
            Vector3[] array  = new Vector3[4];
            Vector3[] array2 = new Vector3[4];
            this.AttachedCamera.CalculateFrustumCorners(new Rect(0f, 0f, 1f, 1f), this.AttachedCamera.farClipPlane, Camera.MonoOrStereoscopicEye.Left, array);
            position  = this.AttachedCamera.transform.TransformVector(array[0]);
            position2 = this.AttachedCamera.transform.TransformVector(array[3]);
            position3 = this.AttachedCamera.transform.TransformVector(array[1]);
            vector    = transform.InverseTransformPoint(position4);
            vector2   = transform.InverseTransformPoint(position);
            vector3   = transform.InverseTransformPoint(position2);
            vector4   = transform.InverseTransformPoint(position3);
            vector5   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position4));
            vector6   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position));
            vector7   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position2));
            vector8   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position3));
            vector9   = new Vector4(vector6.x, vector6.y, vector2.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector.y);
            Vector4 value5 = new Vector4(vector5.x, vector5.y, vector.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector.y);
            Vector4 value6 = vector9 - vector10;
            Vector4 value7 = new Vector4(vector7.x, vector7.y, vector3.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector3.y) - vector9;
            Vector4 value8 = new Vector4(vector8.x, vector8.y, vector4.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector4.y) - vector9;
            position4 = vector13;
            this.AttachedCamera.CalculateFrustumCorners(new Rect(0f, 0f, 1f, 1f), this.AttachedCamera.farClipPlane, Camera.MonoOrStereoscopicEye.Right, array2);
            position  = this.AttachedCamera.transform.TransformVector(array2[0]);
            position2 = this.AttachedCamera.transform.TransformVector(array2[3]);
            position3 = this.AttachedCamera.transform.TransformVector(array2[1]);
            vector    = transform.InverseTransformPoint(position4);
            vector2   = transform.InverseTransformPoint(position);
            vector3   = transform.InverseTransformPoint(position2);
            vector4   = transform.InverseTransformPoint(position3);
            vector5   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position4));
            vector6   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position));
            vector7   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position2));
            vector8   = SunshineMath.xy(Sunshine.Instance.SunLightCamera.WorldToViewportPoint(position3));
            vector9   = new Vector4(vector6.x, vector6.y, vector2.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector.y);
            Vector4 value9  = new Vector4(vector5.x, vector5.y, vector.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector.y);
            Vector4 value10 = vector9 - vector10;
            Vector4 value11 = new Vector4(vector7.x, vector7.y, vector3.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector3.y) - vector9;
            Vector4 value12 = new Vector4(vector8.x, vector8.y, vector4.z / Sunshine.Instance.SunLightCamera.farClipPlane, vector4.y) - vector9;
            Shader.SetGlobalVector("L_sunshine_ShadowCoordDepthStart", value5);
            Shader.SetGlobalVector("L_sunshine_ShadowCoordDepthRayZ", value6);
            Shader.SetGlobalVector("L_sunshine_ShadowCoordDepthRayU", value7);
            Shader.SetGlobalVector("L_sunshine_ShadowCoordDepthRayV", value8);
            Shader.SetGlobalVector("R_sunshine_ShadowCoordDepthStart", value9);
            Shader.SetGlobalVector("R_sunshine_ShadowCoordDepthRayZ", value10);
            Shader.SetGlobalVector("R_sunshine_ShadowCoordDepthRayU", value11);
            Shader.SetGlobalVector("R_sunshine_ShadowCoordDepthRayV", value12);
        }
        float   num2     = Sunshine.Instance.SunLightCamera.orthographicSize * 2f;
        Vector2 vector14 = new Vector2(num2, num2);

        vector14.x /= rect.width;
        vector14.y /= rect.height;
        Vector3 v = new Vector3(vector14.x, vector14.y, Sunshine.Instance.SunLightCamera.farClipPlane) / this.AttachedCamera.farClipPlane;

        Shader.SetGlobalVector("sunshine_ShadowToWorldScale", v);
        Matrix4x4 zero      = Matrix4x4.zero;
        Vector3   position5 = Sunshine.Instance.SunLightCamera.ViewportToWorldPoint(new Vector3(0f, 0f, 0f));
        Vector3   position6 = Sunshine.Instance.SunLightCamera.ViewportToWorldPoint(new Vector3(1f, 1f, 0f));

        for (int i = 0; i < Sunshine.Instance.CascadeCount; i++)
        {
            Vector4 v2 = new Vector4(0f, 0f, 1f, 1f);
            if (i > 0)
            {
                Camera  camera   = Sunshine.Instance.SunLightCameras[i];
                Vector3 vector15 = camera.WorldToViewportPoint(position5);
                Vector3 vector16 = camera.WorldToViewportPoint(position6);
                v2 = new Vector4(vector15.x, vector15.y, vector16.x, vector16.y);
            }
            Rect rect2 = Sunshine.Instance.CascadeRect(i);
            v2.x  = rect2.xMin + rect2.width * v2.x;
            v2.y  = rect2.yMin + rect2.height * v2.y;
            v2.z  = rect2.xMin + rect2.width * v2.z;
            v2.w  = rect2.yMin + rect2.height * v2.w;
            v2.z -= v2.x;
            v2.w -= v2.y;
            zero.SetRow(i, v2);
        }
        Vector4 row = zero.GetRow(0);

        for (int j = 0; j < Sunshine.Instance.CascadeCount; j++)
        {
            Vector4 row2 = zero.GetRow(j);
            row2.z /= row.z;
            row2.w /= row.w;
            zero.SetRow(j, row2);
        }
        bool flag = Sunshine.Instance.OvercastTexture;

        this.ConfigureOvercast(flag, (!flag) ? Sunshine.Instance.BlankOvercastTexture : Sunshine.Instance.OvercastTexture, Sunshine.Instance.OvercastScale, Sunshine.Instance.OvercastMovement, Sunshine.Instance.OvercastPlaneHeight);
        Shader.SetGlobalMatrix("sunshine_CascadeRanges", zero);
    }