예제 #1
0
    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        if (TargetMaterial != null)
        {
            Matrix4x4 frustumCorners = Matrix4x4.identity;

            float fov    = TargetCamera.fieldOfView;
            float near   = TargetCamera.nearClipPlane;
            float aspect = TargetCamera.aspect;

            float   halfHeight = near * Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);
            Vector3 toRight    = CameraTransform.right * halfHeight * aspect;
            Vector3 toTop      = CameraTransform.up * halfHeight;

            Vector3 topLeft = CameraTransform.forward * near + toTop - toRight;
            float   scale   = topLeft.magnitude / near;

            topLeft.Normalize();
            topLeft *= scale;

            Vector3 topRight = CameraTransform.forward * near + toRight + toTop;
            topRight.Normalize();
            topRight *= scale;

            Vector3 bottomLeft = CameraTransform.forward * near - toTop - toRight;
            bottomLeft.Normalize();
            bottomLeft *= scale;

            Vector3 bottomRight = CameraTransform.forward * near + toRight - toTop;
            bottomRight.Normalize();
            bottomRight *= scale;

            frustumCorners.SetRow(0, bottomLeft);
            frustumCorners.SetRow(1, bottomRight);
            frustumCorners.SetRow(2, topRight);
            frustumCorners.SetRow(3, topLeft);

            TargetMaterial.SetMatrix("_FrustumCornersRay", frustumCorners);

            TargetMaterial.SetFloat("_FogDensity", fogDensity);
            TargetMaterial.SetColor("_FogColor", fogColor);
            TargetMaterial.SetFloat("_FogStart", fogStart);
            TargetMaterial.SetFloat("_FogEnd", fogEnd);

            TargetMaterial.SetTexture("_NoiseTex", noiseTexture);
            TargetMaterial.SetFloat("_FogXSpeed", fogXSpeed);
            TargetMaterial.SetFloat("_FogYSpeed", fogYSpeed);
            TargetMaterial.SetFloat("_NoiseAmount", noiseAmount);
            Graphics.Blit(src, dest, TargetMaterial);
        }
        else
        {
            Graphics.Blit(src, dest);
        }
    }
예제 #2
0
    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        if (TargetMaterial != null)
        {
            TargetMaterial.SetFloat("_BlurSize", blurSize);

            // 上一帧的矩阵
            TargetMaterial.SetMatrix("_PreviousViewProjectionMatrix", previousViewProjectionMatrix);

            // 投影矩阵 * 视角矩阵 ,用于给下一帧计算该帧时的位置
            Matrix4x4 currentViewProjectionMatrix = TargetCamera.projectionMatrix * TargetCamera.worldToCameraMatrix;
            // 矩阵取逆,用于计算该帧的位置
            Matrix4x4 currentViewProjectionInverseMatrix = currentViewProjectionMatrix.inverse;
            TargetMaterial.SetMatrix("_CurrentViewProjectionInverseMatrix", currentViewProjectionInverseMatrix);
            previousViewProjectionMatrix = currentViewProjectionMatrix;
            Graphics.Blit(src, dest, TargetMaterial);
        }
        else
        {
            Graphics.Blit(src, dest);
        }
    }
        void Update()
        {
            var pInfos = shaderValues.GetType().GetFields();

            foreach (var p in pInfos)
            {
                if (Attribute.IsDefined(p, typeof(ShaderValue)))
                {
                    if (p.FieldType == typeof(Color))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetColor(p.Name, (Color)p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(Color[]))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetColorArray(p.Name, (Color[])p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(List <Color>))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetColorArray(p.Name, (List <Color>)p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(float))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetFloat(p.Name, (float)p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(float[]))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetFloatArray(p.Name, (float[])p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(List <float>))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetFloatArray(p.Name, (List <float>)p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(int))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetInt(p.Name, (int)p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(bool))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetInt(p.Name, (bool)p.GetValue(shaderValues) ? 1 : 0);
                        }
                    }
                    else if (p.FieldType == typeof(Matrix4x4))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetMatrix(p.Name, (Matrix4x4)p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(Matrix4x4[]))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetMatrixArray(p.Name, (Matrix4x4[])p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(List <Matrix4x4>))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetMatrixArray(p.Name, (List <Matrix4x4>)p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(string))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetOverrideTag(p.Name, (string)p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(Texture2D))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetTexture(p.Name, (Texture2D)p.GetValue(shaderValues));
                        }

                        //a vector2 can be a vector, or a material offset or scale
                        //TODO: test this!
                    }
                    else if (p.FieldType == typeof(Vector2))
                    {
                        if (Attribute.IsDefined(p, typeof(TextureOffset)))
                        {
                            foreach (Material TargetMaterial in TargetMaterials)
                            {
                                TargetMaterial.SetTextureOffset(((TextureOffset)Attribute.GetCustomAttribute(p, typeof(TextureOffset))).Name, (Vector2)p.GetValue(shaderValues));
                            }
                        }
                        else if (Attribute.IsDefined(p, typeof(TextureScale)))
                        {
                            foreach (Material TargetMaterial in TargetMaterials)
                            {
                                TargetMaterial.SetTextureOffset(((TextureScale)Attribute.GetCustomAttribute(p, typeof(TextureScale))).Name, (Vector2)p.GetValue(shaderValues));
                            }
                        }
                        else
                        {
                            foreach (Material TargetMaterial in TargetMaterials)
                            {
                                TargetMaterial.SetVector(p.Name, (Vector2)p.GetValue(shaderValues));
                            }
                        }
                    }
                    else if (p.FieldType == typeof(Vector3))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetVector(p.Name, (Vector3)p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(Vector4))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetVector(p.Name, (Vector4)p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(Vector4[]))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetVectorArray(p.Name, (Vector4[])p.GetValue(shaderValues));
                        }
                    }
                    else if (p.FieldType == typeof(List <Vector4>))
                    {
                        foreach (Material TargetMaterial in TargetMaterials)
                        {
                            TargetMaterial.SetVectorArray(p.Name, (List <Vector4>)p.GetValue(shaderValues));
                        }
                    }
                }
            }
        }