void OnRenderImage

来源:互联网 发布:全知之眼纹身意义 编辑:程序博客网 时间:2024/05/20 05:27

<span style="color:#145d7b;font-family: Verdana, Geneva, sans-serif, 宋体; background-color: rgb(255, 255, 255);"><a target=_blank href="http://game.ceeger.com/Script/Camera/Camera.html" style="color: rgb(20, 93, 123); text-decoration: none;">Camera</a></span><span style="font-family: Verdana, Geneva, sans-serif, 宋体; background-color: rgb(255, 255, 255);">.OnRenderImage 在渲染图像之后</span>

function OnRenderImage (source : RenderTexture, destination : RenderTexture) : void

Description描述

OnRenderImage is called after all rendering is complete to render image

postprocessing effects (Unity Pro only).

OnRenderImage在所有渲染完成后被调用,来渲染图片的后期处理效果(仅限UnityPro)。

It allows you to modify final image by processing it with shader based filters. The incoming image is source render texture. The result should end up in destination render texture. When there are multiple image filters attached to the camera, they process image sequentially, by passing first filter's destination as the source to the next filter.

这允许你使用基于shader的过滤器来处理最后的图片,进入的图片是source渲染纹理,结果是destination渲染纹理。当有锁个图片过滤器附加在相机上时,它们序列化的处理图片,将第一个过滤器的目标作为下一个过滤器的源。

This message is sent to all scripts attached to the camera.

这个消息被发送到所有附加在相机上的脚本。


实例:

void OnRenderImage(RenderTexture sourceTexture,RenderTexture destTexture)//最终纹理
{

Graphics.Blit(sourceTexture,destTexture,material);  //拷贝源纹理到目的渲染纹理。在材质上设置source作为_MainTex属性,并且绘制一个全屏方块。

sourceTexture应该是摄像机看到的东西,然后把看到的纹理传给material材质的_MainTex属性。经过material的着色器处理后返还给destTexture。destTexture之前是null对象。


整个c#参考代码:

using UnityEngine;using System.Collections;using UnityEngine.UI;[ExecuteInEditMode]public class TestRenderImage : MonoBehaviour {#region Variablespublic Shader curShader;public float grayScaleAmount = 1.0f;public RawImage ri;private Material curMaterial;#endregion#region PropertiesMaterial material{get{if(curMaterial==null){curMaterial = new Material(curShader);curMaterial.hideFlags=HideFlags.HideAndDontSave;//隐藏并且不自动销毁 保存到下个场景、程序结束也不销毁}return curMaterial; }}#endregionvoid Start () {if(!SystemInfo.supportsImageEffects)//系统找不到这个类的信息。就关闭{enabled = false;return;}if(!curShader && !curShader.isSupported)//如果shader是空的。并且找不到shader帮助。{enabled =false;}}// Update is called once per framevoid Update () {grayScaleAmount = Mathf.Clamp(grayScaleAmount,0.0f,1.0f);}RenderTexture r;void OnRenderImage(RenderTexture sourceTexture,RenderTexture destTexture)//最终纹理{if(curShader!=null){material.SetFloat("_LuminosityAmount",grayScaleAmount);//RenderTexture r;//=new RenderTexture(1000,1000,1000);//if(sourceTexture==null) Debug.Log("1"); else Debug.Log("you1");Debug.Log(sourceTexture.width);Graphics.Blit(sourceTexture,destTexture,material);  //拷贝源纹理到目的渲染纹理。Blit设置dest到激活的渲染纹理,在材质上设置source作为_MainTex属性,并且绘制一个全屏方块。//if(sourceTexture==null) Debug.Log("2"); else Debug.Log("you2");}else{Graphics.Blit(sourceTexture,destTexture);}}void OnDisable(){if(curMaterial){DestroyImmediate(curMaterial);}}}


简便CG代码:

Shader "Custom/ImageEffect" {Properties {_MainTex ("Albedo (RGB)", 2D) = "white" {}_LuminosityAmount("GrayScale Amount",Range(0.0,1)) = 1}SubShader {Pass{CGPROGRAM#pragma vertex vert_img#pragma fragment frag#pragma fragmentoption ARB_precision_hint_fastest#include "UnityCG.cginc"uniform sampler2D _MainTex;fixed _LuminosityAmount;fixed4 frag(v2f_img i) : COLOR{fixed4 renderTex = tex2D(_MainTex,i.uv);float luminosity = 0.299 * renderTex.r + 0.587 * renderTex.g + 0.114 * renderTex.b;fixed4 finalColor = lerp(renderTex,luminosity,_LuminosityAmount);//finalColor.a=0.1;return finalColor;}ENDCG} }}


0 0
原创粉丝点击