Example #1
0
    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (TargetMaterial != null && drawOccupied != null && additionalCamera != null)
        {
            tempRT = RenderTexture.GetTemporary(source.width, source.height, 0);
            additionalCamera.targetTexture = tempRT;

            // 额外相机中使用shader,绘制出物体所占面积
            additionalCamera.RenderWithShader(drawOccupied, "");

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

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

            tempRT.Release();
        }
        else
        {
            Graphics.Blit(source, destination);
        }
    }
Example #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);
        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);
            }
        }
Example #4
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);
        }
    }
        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));
                        }
                    }
                }
            }
        }