这里面我转一个镜面反射,再阐述一些遇到的问题,先建立以个Shader,名字可以叫做MirrorReflection,脚本如下:
Shader "FX/Mirror Reflection"
{Properties
{_MainTex("Base(RGB)",2D)="white"{}
_ReflectionTex("Reflection",2D)="white"{TexGen ObjectLinear}
}
// two texture cards: full thing
Subshader
{Pass
{SetTexture[_MainTex]{combine texture}
SetTexture[_ReflectionTex]{matrix [_ProjMatrix] combine texture*previous}
}
}
// fallback: just main texture
Subshader
{Pass
{SetTexture [_MainTex]{combine texture}
}
}
}
在建立一个cs脚本,名字叫做MirrorReflection,脚本如下:
using UnityEngine;
using System.Collections;
//实际上 This is in fact just the Water script from Pro Standard Assets,
// just with refraction stuff removed.
[ExecuteInEditMode]// Make mirror live-update even when not in play mode
public class MirrorReflection:MonoBehaviour
{public bool m_DisablePixelLights=true;
public int m_TextureSize=256;
public float m_ClipPlaneOffset=0.07f;
public LayerMask m_ReflectLayers=-1;
private Hashtable m_ReflectionCameras=new Hashtable(); // Camera ->Camera table
private RenderTexture m_ReflectionTexture=null;
private int m_OldReflectionTextureSize=0;
private static bool s_InsideRendering=false;
// This is called when it's known that the object will be rendered by some
// camera. We render reflections and do other updates here.
// Because the script executes in edit mode,reflections for the scene view
// camera will just work!
public void OnWillRenderObject()
{if(!enabled || !renderer || !renderer.sharedMaterial || !renderer.enabled )return;
Camera cam=Camera.current;
if(!cam )return;
// Safeguard from recursive reflections.