예제 #1
0
    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        if (TargetMaterial != null)
        {
            // 创建积累图像
            if (accumulationTexture == null || accumulationTexture.width != src.width || accumulationTexture.height != src.height)
            {
                DestroyImmediate(accumulationTexture);
                accumulationTexture           = new RenderTexture(src.width, src.height, 0);
                accumulationTexture.hideFlags = HideFlags.HideAndDontSave; // 变量不显示在Hierarchy中,也不会保存到场景
                Graphics.Blit(src, accumulationTexture);                   // 原始图像存入积累纹理
            }

            // 表明需要进行一个恢复操作。渲染恢复操作:发生在渲染到纹理,而该纹理有没有被提前情况或销毁情况下。
            accumulationTexture.MarkRestoreExpected();          // accumulationTexture就不需要提前清空了

            TargetMaterial.SetFloat("_BlurAmount", 1.0f - blurAmount);

            // 混合当前屏幕和之前存的混合图像
            Graphics.Blit(src, accumulationTexture, TargetMaterial);
            // 最后输出混合图像
            Graphics.Blit(accumulationTexture, dest);
        }
        else
        {
            Graphics.Blit(src, dest);
        }
    }
예제 #2
0
    public void RenderToTexture(GameObject target, RenderTexture texture)
    {
        // 第一步:把渲染的物体渲染进纹理先
        additionalCamera.cullingMask   = 1;
        additionalCamera.targetTexture = texture;
        additionalCamera.targetTexture.Release();
        additionalCamera.Render();

        // 第二步:把这个纹理备份到_SceneTex
        temRT1 = RenderTexture.GetTemporary(texture.width, texture.height);
        Graphics.Blit(additionalCamera.targetTexture, temRT1);
        TargetMaterial.SetTexture("_SceneTex", temRT1);

        // 第三步:渲染物体的面积
        additionalCamera.cullingMask = 1 << LayerMask.NameToLayer("PostEffect");
        meshFilters = target.GetComponentsInChildren <MeshFilter>();
        for (int j = 0; j < meshFilters.Length; j++)
        {
            Graphics.DrawMesh(meshFilters[j].sharedMesh, meshFilters[j].transform.localToWorldMatrix, OccupiedMaterial, LayerMask.NameToLayer("PostEffect"), additionalCamera); // 描绘选中物体的所占面积
        }
        additionalCamera.Render();

        // 第四步:实现描边效果
        TargetMaterial.SetColor("_Color", outlineColor);
        TargetMaterial.SetInt("_Width", outlineWidth);
        TargetMaterial.SetInt("_Iterations", iterations);
        TargetMaterial.SetFloat("_Gradient", gradient);
        temRT2 = RenderTexture.GetTemporary(texture.width, texture.height);
        Graphics.Blit(additionalCamera.targetTexture, temRT2, TargetMaterial);
        Graphics.Blit(temRT2, additionalCamera.targetTexture);
    }
        void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            if (TargetMaterial != null && drawOccupied != null && additionalCamera != null && targetsDic.Count != 0)
            {
                SetupAddtionalCamera();
                tempRT = RenderTexture.GetTemporary(source.width, source.height, 0);
                additionalCamera.targetTexture = tempRT;
                //---这种方法对于蒙皮动画无效,但是效率较高
                //foreach (var item in targetsDic.Values)
                //{
                //    foreach (var meshInfo in item)
                //    {
                //        for (int i = 0; i < meshInfo.Value.Count; i++)
                //        {
                //            for (int k = 0; k < meshInfo.Value[i].subMeshCount; k++)
                //            {
                //                Graphics.DrawMesh(meshInfo.Value[i], meshInfo.Key.localToWorldMatrix,
                //           OccupiedMaterial, GetLayer(layerMask), additionalCamera, k); // 描绘选中物体的所占面积
                //            }

                //        }
                //    }
                //}

                //设置层级
                foreach (var item in targetsDic.Values)
                {
                    foreach (var transInfo in item)
                    {
                        transInfo.Key.gameObject.layer = GetLayer(layerMask);
                    }
                }
                additionalCamera.SetReplacementShader(drawOccupied, "");
                additionalCamera.Render();  // 需要调用渲染函数,才能及时把描绘物体渲染到纹理中
                foreach (var item in targetsDic.Values)
                {
                    foreach (var transInfo in item)
                    {
                        transInfo.Key.gameObject.layer = transInfo.Value;
                    }
                }


                TargetMaterial.SetTexture("_SceneTex", source);
                TargetMaterial.SetColor("_Color", outlineColor);
                TargetMaterial.SetInt("_Width", outlineWidth);
                TargetMaterial.SetInt("_Iterations", iterations);
                TargetMaterial.SetFloat("_Gradient", gradient);
                Graphics.Blit(tempRT, TestRender);
                // 使用描边混合材质实现描边效果
                Graphics.Blit(tempRT, destination, TargetMaterial);
                additionalCamera.targetTexture = null;
                RenderTexture.ReleaseTemporary(tempRT);
            }
            else
            {
                Graphics.Blit(source, destination);
            }
        }
예제 #4
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);
        }
    }
예제 #5
0
 /// <summary>
 /// 抓取屏幕图像
 /// </summary>
 /// <param name="src">源</param>
 /// <param name="dest">目标</param>
 void OnRenderImage(RenderTexture src, RenderTexture dest)
 {
     if (TargetMaterial != null)
     {
         TargetMaterial.SetFloat("_Brightness", brightness);
         TargetMaterial.SetFloat("_Saturation", saturation);
         TargetMaterial.SetFloat("_Contrast", contrast);
         Graphics.Blit(src, dest, TargetMaterial);
     }
     else
     {
         Graphics.Blit(src, dest);
     }
 }
예제 #6
0
    public Color backgroundColor = Color.white; // 背景色

    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        if (TargetMaterial != null)
        {
            TargetMaterial.SetFloat("_EdgeOnly", edgesOnly);
            TargetMaterial.SetColor("_EdgeColor", edgeColor);
            TargetMaterial.SetColor("_BackgroundColor", backgroundColor);
            Graphics.Blit(src, dest, TargetMaterial);
        }
        else
        {
            Graphics.Blit(src, dest);
        }
    }
 [ImageEffectOpaque]     // 不透明物体渲染完后执行(不影响透明物体)
 void OnRenderImage(RenderTexture src, RenderTexture dest)
 {
     if (TargetMaterial != null)
     {
         TargetMaterial.SetFloat("_EdgeOnly", edgesOnly);
         TargetMaterial.SetColor("_EdgeColor", edgeColor);
         TargetMaterial.SetColor("_BackgroundColor", backgroundColor);
         TargetMaterial.SetFloat("_SampleDistance", sampleDistance);
         TargetMaterial.SetVector("_Sensitivity", new Vector4(sensitivityNormals, sensitivityDepth, 0.0f, 0.0f));
         Graphics.Blit(src, dest, TargetMaterial);
     }
     else
     {
         Graphics.Blit(src, dest);
     }
 }
예제 #8
0
    private void Update()
    {
        if (TargetMaterial != null && _endTime > Time.time)
        {
            _color.a = _alphaCurve.Evaluate((Time.time - _startTime) / (_durationTime));
            TargetMaterial.SetColor("_Color", _color);
            TargetMaterial.SetFloat("_Width", _width);

            if (_meshFilters == null)
            {
                _meshFilters = gameObject.GetComponentsInChildren <MeshFilter>();
            }
            for (int j = 0; j < _meshFilters.Length; j++)
            {
                Graphics.DrawMesh(_meshFilters[j].mesh, _meshFilters[j].transform.localToWorldMatrix, TargetMaterial, 0);   // 对选中物体再次渲染。
            }
        }
    }
예제 #9
0
    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (TargetMaterial != null && drawOccupied != null && additionalCamera != null && targets != null)
        {
            SetupAddtionalCamera();
            tempRT = RenderTexture.GetTemporary(source.width, source.height, 0);
            additionalCamera.targetTexture = tempRT;

            for (int i = 0; i < targets.Length; i++)
            {
                if (targets[i] == null)
                {
                    continue;
                }
                meshFilters = targets[i].GetComponentsInChildren <MeshFilter>();
                for (int j = 0; j < meshFilters.Length; j++)
                {
                    if ((MainCamera.cullingMask & (1 << meshFilters[j].gameObject.layer)) != 0) // 把主相机没渲染的也不加入渲染队列
                    {
                        for (int k = 0; k < meshFilters[j].sharedMesh.subMeshCount; k++)
                        {
                            Graphics.DrawMesh(meshFilters[j].sharedMesh, meshFilters[j].transform.localToWorldMatrix, OccupiedMaterial, LayerMask.NameToLayer("PostEffect"), additionalCamera, k); // 描绘选中物体的所占面积
                        }
                    }
                }
            }
            additionalCamera.Render();  // 需要调用渲染函数,才能及时把描绘物体渲染到纹理中

            TargetMaterial.SetTexture("_SceneTex", source);
            TargetMaterial.SetColor("_Color", outlineColor);
            TargetMaterial.SetInt("_Width", outlineWidth);
            TargetMaterial.SetInt("_Iterations", iterations);
            TargetMaterial.SetFloat("_Gradient", gradient);

            // 使用描边混合材质实现描边效果
            Graphics.Blit(tempRT, destination, TargetMaterial);

            tempRT.Release();
        }
        else
        {
            Graphics.Blit(source, destination);
        }
    }
예제 #10
0
    public float luminanceThreshold = 0.6f;     // 亮度阈值(一般不超过1,开了HDR可以存更高精度)

    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        if (TargetMaterial != null)
        {
            TargetMaterial.SetFloat("_LuminanceThreshold", luminanceThreshold);

            int rtW = src.width / downSample;
            int rtH = src.height / downSample;

            RenderTexture buffer0 = RenderTexture.GetTemporary(rtW, rtH, 0);
            buffer0.filterMode = FilterMode.Bilinear;

            Graphics.Blit(src, buffer0, TargetMaterial, 0);               // 第一个Pass把较亮区域存到buffer0

            // 迭代高斯模糊
            for (int i = 0; i < iterations; i++)
            {
                TargetMaterial.SetFloat("_BlurSize", 1.0f + i * blurSpread);

                RenderTexture buffer1 = RenderTexture.GetTemporary(rtW, rtH, 0);

                Graphics.Blit(buffer0, buffer1, TargetMaterial, 1);       // 第二个Pass,高斯模糊

                RenderTexture.ReleaseTemporary(buffer0);
                buffer0 = buffer1;
                buffer1 = RenderTexture.GetTemporary(rtW, rtH, 0);

                Graphics.Blit(buffer0, buffer1, TargetMaterial, 2);       // 第三个Pass

                RenderTexture.ReleaseTemporary(buffer0);
                buffer0 = buffer1;
            }

            TargetMaterial.SetTexture("_Bloom", buffer0);                // 较亮区域存到_Bloom
            Graphics.Blit(src, dest, TargetMaterial, 3);                 // 第四个Pass最后混合

            RenderTexture.ReleaseTemporary(buffer0);
        }
        else
        {
            Graphics.Blit(src, dest);
        }
    }
    // 也可以在OnPostRender()中更新
    private void Update()
    {
        if (TargetMaterial != null && targets != null)
        {
            TargetMaterial.SetFloat("_Width", width);
            TargetMaterial.SetColor("_Color", color);

            for (int i = 0; i < targets.Length; i++)
            {
                if (targets[i] == null)
                {
                    continue;
                }
                meshFilters = targets[i].GetComponentsInChildren <MeshFilter>();
                for (int j = 0; j < meshFilters.Length; j++)
                {
                    Graphics.DrawMesh(meshFilters[j].sharedMesh, meshFilters[j].transform.localToWorldMatrix, TargetMaterial, 0);   // 对选中物体再次渲染。
                }
            }
        }
    }
예제 #12
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);
        }
    }
예제 #13
0
    private void Update()
    {
        if (TargetMaterial != null && targets != null)
        {
            TargetMaterial.SetFloat("_Width", width);
            TargetMaterial.SetColor("_Color", color);

            for (int i = 0; i < targets.Length; i++)
            {
                if (targets[i] == null)
                {
                    continue;
                }
                meshFilters = targets[i].GetComponentsInChildren <MeshFilter>();
                for (int j = 0; j < meshFilters.Length; j++)
                {
                    for (int k = 0; k < meshFilters[j].sharedMesh.subMeshCount; k++)    // 每个网格还有子网格
                    {
                        Graphics.DrawMesh(meshFilters[j].sharedMesh, meshFilters[j].transform.localToWorldMatrix, TargetMaterial, 0, MainCamera, k);
                    }
                }
            }
        }
    }
예제 #14
0
        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));
                        }
                    }
                }
            }
        }