void OnRenderImage

<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 : RenderTexturedestination : 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 Variables
	public Shader curShader;
	public float grayScaleAmount = 1.0f;
	public RawImage ri;
	private Material curMaterial;
	#endregion
	#region Properties
	Material material
	{
		get
		{
			if(curMaterial==null)
			{
				curMaterial = new Material(curShader);
				curMaterial.hideFlags=HideFlags.HideAndDontSave;//隐藏并且不自动销毁 保存到下个场景、程序结束也不销毁
			}
			return curMaterial; 
		}
	}
	#endregion
	void Start () {
	if(!SystemInfo.supportsImageEffects)//系统找不到这个类的信息。就关闭
		{
			enabled = false;
			return;
		}
		if(!curShader && !curShader.isSupported)//如果shader是空的。并且找不到shader帮助。
		{
			enabled =false;
		}
	}
	
	// Update is called once per frame
	void 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值