Unity3d 镜面反射
网上能找到的基本上是固定管道或表面渲染的shader,
特此翻译为顶点、片段渲染的Shader,
本源码只涉及shader与cs部分,
Editor部分使用NGUI绘制的,
请自行下载NGUI
unity3d 版本:v4.3.1
ReflectionEffect.cs
using UnityEngine; using System.Collections; using System; /// <summary> /// 反射效果 /// </summary> [AddComponentMenu("GameCore/SpecialEffect/Reflection")] [ExecuteInEditMode] public class ReflectionEffect : MonoBehaviour { public Camera MainCamera; public bool DisablePixelLights = true; public int TextureSize = 512; public float ClipPlaneOffset = 0; public LayerMask 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 = MainCamera; if (!cam) return; // Safeguard from recursive reflections. if (s_InsideRendering) return; s_InsideRendering = true; Camera reflectionCamera; CreateMirrorObjects(cam, out reflectionCamera); // find out the reflection plane: position and normal in world space Vector3 pos = transform.position; Vector3 normal = transform.up; // Optionally disable pixel lights for reflection int oldPixelLightCount = QualitySettings.pixelLightCount; if (DisablePixelLights) QualitySettings.pixelLightCount = 0; CoreTool.CloneCameraModes(cam, reflectionCamera); // Render reflection // Reflect camera around reflection plane float d = -Vector3.Dot(normal, pos) - ClipPlaneOffset; Vector4 reflectionPlane = new Vector4(normal.x, normal.y, normal.z, d); Matrix4x4 reflection = CoreTool.CalculateReflectionMatrix(Matrix4x4.zero, reflectionPlane); Vector3 oldpos = cam.transform.position; Vector3 newpos = reflection.MultiplyPoint(oldpos); reflectionCamera.worldToCameraMatrix = cam.worldToCameraMatrix * reflection; // Setup oblique projection matrix so that near plane is our reflection // plane. This way we clip everything below/above it for free. Vector4 clipPlane = CoreTool.CameraSpacePlane(reflectionCamera, pos, normal, 1.0f, ClipPlaneOffset); Matrix4x4 projection = cam.projectionMatrix; projection = CoreTool.CalculateObliqueMatrix(projection, clipPlane); reflectionCamera.projectionMatrix = projection; reflectionCamera.cullingMask = ~(1 << 4) & ReflectLayers.value; // never render water layer reflectionCamera.targetTexture = m_ReflectionTexture; GL.SetRevertBackfacing(true); reflectionCamera.transform.position = newpos; Vector3 euler = cam.transform.eulerAngles; reflectionCamera.transform.eulerAngles = new Vector3(0, euler.y, euler.z); reflectionCamera.Render(); reflectionCamera.transform.position = oldpos; GL.SetRevertBackfacing(false); Material[] materials = renderer.sharedMaterials; foreach (Material mat in materials) { if (mat.HasProperty("_ReflectionTex")) mat.SetTexture("_ReflectionTex", m_ReflectionTexture); } // Set matrix on the shader that transforms UVs from object space into screen // space. We want to just project reflection texture on screen. Matrix4x4 scaleOffset = Matrix4x4.TRS( new Vector3(0.5f, 0.5f, 0.5f), Quaternion.identity, new Vector3(0.5f, 0.5f, 0.5f)); Vector3 scale = transform.lossyScale; Matrix4x4 mtx = transform.localToWorldMatrix * Matrix4x4.Scale(new Vector3(1.0f / scale.x, 1.0f / scale.y, 1.0f / scale.z)); mtx = scaleOffset * cam.projectionMatrix * cam.worldToCameraMatrix * mtx; foreach (Material mat in materials) { mat.SetMatrix("_ProjMatrix", mtx); } // Restore pixel light count if (DisablePixelLights) QualitySettings.pixelLightCount = oldPixelLightCount; s_InsideRendering = false; } // Cleanup all the objects we possibly have created void OnDisable() { if (m_ReflectionTexture) { DestroyImmediate(m_ReflectionTexture); m_ReflectionTexture = null; } foreach (DictionaryEntry kvp in m_ReflectionCameras) DestroyImmediate(((Camera)kvp.Value).gameObject); m_ReflectionCameras.Clear(); } // On-demand create any objects we need private void CreateMirrorObjects(Camera currentCamera, out Camera reflectionCamera) { reflectionCamera = null; // Reflection render texture if (!m_ReflectionTexture || m_OldReflectionTextureSize != TextureSize) { if (m_ReflectionTexture) DestroyImmediate(m_ReflectionTexture); m_ReflectionTexture = new RenderTexture(TextureSize, TextureSize,0); m_ReflectionTexture.name = "__MirrorReflection" + GetInstanceID(); m_ReflectionTexture.isPowerOfTwo = true; m_ReflectionTexture.hideFlags = HideFlags.DontSave; m_ReflectionTexture.antiAliasing = 4; m_ReflectionTexture.anisoLevel = 0; m_OldReflectionTextureSize = TextureSize; } // Camera for reflection reflectionCamera = m_ReflectionCameras[currentCamera] as Camera; if (!reflectionCamera) // catch both not-in-dictionary and in-dictionary-but-deleted-GO { GameObject go = new GameObject("Mirror Refl Camera id" + GetInstanceID() + " for " + currentCamera.GetInstanceID(), typeof(Camera), typeof(Skybox)); reflectionCamera = go.camera; reflectionCamera.enabled = false; reflectionCamera.transform.position = transform.position; reflectionCamera.transform.rotation = transform.rotation; reflectionCamera.gameObject.AddComponent("FlareLayer"); go.hideFlags = HideFlags.HideAndDontSave; m_ReflectionCameras[currentCamera] = reflectionCamera; } } }
ReflectionEffectEditor.cs
using System.Collections; using System; using UnityEditor; using UnityEngine; /// <summary> /// 反射效果 /// </summary> [CustomEditor(typeof(ReflectionEffect))] public class ReflectionEffectEditor : Editor { string[] _renderTextureOptions = new string[8] { "16", "32", "64", "128", "256", "512", "1024", "2048" }; int _renderTextureWidthDefaultIndex = 5; int _renderTextureWidthIndex = 5; public override void OnInspectorGUI() { SerializedProperty sp = NGUIEditorTools.DrawProperty("Main Camera", this.serializedObject, "MainCamera"); SetMainCamera(sp); if (NGUIEditorTools.DrawHeader("Reflection Settings")) { NGUIEditorTools.BeginContents(); { NGUIEditorTools.DrawProperty("Disable PixelLights", this.serializedObject, "DisablePixelLights"); NGUIEditorTools.DrawProperty("Reflect Layers", this.serializedObject, "ReflectLayers"); NGUIEditorTools.DrawProperty("ClipPlane Offset", this.serializedObject, "ClipPlaneOffset"); } NGUIEditorTools.EndContents(); } if (NGUIEditorTools.DrawHeader("Render Texture Settings")) { NGUIEditorTools.BeginContents(); { sp = this.serializedObject.FindProperty("TextureSize"); _renderTextureWidthIndex = GetTextureOptionsIndex(sp.intValue.ToString()); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Size:", GUILayout.Width(80)); _renderTextureWidthIndex = EditorGUILayout.Popup(_renderTextureWidthIndex, _renderTextureOptions); EditorGUILayout.EndHorizontal(); if (GUILayout.Button("Make Default Value")) { _renderTextureWidthIndex = _renderTextureWidthDefaultIndex; } sp.intValue = int.Parse(_renderTextureOptions[_renderTextureWidthIndex]); } NGUIEditorTools.EndContents(); } this.serializedObject.ApplyModifiedProperties(); } int GetTextureOptionsIndex(string value) { int index = 0; for (int i = 0; i < _renderTextureOptions.Length; i++) { if (_renderTextureOptions[i].Equals(value, StringComparison.OrdinalIgnoreCase)) { index = i; } } return index; } void SetMainCamera(SerializedProperty sp) { if (sp.objectReferenceValue == null) { if (Camera.main != null) { sp.objectReferenceValue = Camera.main; } else { sp.objectReferenceValue = Camera.allCameras[0]; } } } }
CoreTool.cs
using System.Collections; using System; using UnityEngine; /// <summary> /// 工具类 /// </summary> public static class CoreTool { #region Config配置 /// <summary> /// 验证当前文件是否为配置文件 /// </summary> /// <param name="filePath">文件路径</param> /// <returns></returns> public static bool IsConfig(string filePath) { return true; } #endregion #region Camera /// <summary> /// 将源摄像机状态克隆到目标相机 /// </summary> /// <param name="src">源相机</param> /// <param name="dest">目标相机</param> public static void CloneCameraModes(Camera src, Camera dest) { if (dest == null) return; // set camera to clear the same way as current camera dest.clearFlags = src.clearFlags; dest.backgroundColor = src.backgroundColor; if (src.clearFlags == CameraClearFlags.Skybox) { Skybox sky = src.GetComponent(typeof(Skybox)) as Skybox; Skybox mysky = dest.GetComponent(typeof(Skybox)) as Skybox; if (!sky || !sky.material) { mysky.enabled = false; } else { mysky.enabled = true; mysky.material = sky.material; } } // update other values to match current camera. // even if we are supplying custom camera&projection matrices, // some of values are used elsewhere (e.g. skybox uses far plane) dest.farClipPlane = src.farClipPlane; dest.nearClipPlane = src.nearClipPlane; dest.orthographic = src.orthographic; dest.fieldOfView = src.fieldOfView; dest.aspect = src.aspect; dest.orthographicSize = src.orthographicSize; } /// <summary> /// 计算反射矩阵 /// </summary> /// <param name="reflectionMat">原始矩阵</param> /// <param name="plane">反射平面</param> /// <returns>反射矩阵</returns> public static Matrix4x4 CalculateReflectionMatrix(Matrix4x4 reflectionMat, Vector4 plane) { reflectionMat.m00 = (1F - 2F * plane[0] * plane[0]); reflectionMat.m01 = (-2F * plane[0] * plane[1]); reflectionMat.m02 = (-2F * plane[0] * plane[2]); reflectionMat.m03 = (-2F * plane[3] * plane[0]); reflectionMat.m10 = (-2F * plane[1] * plane[0]); reflectionMat.m11 = (1F - 2F * plane[1] * plane[1]); reflectionMat.m12 = (-2F * plane[1] * plane[2]); reflectionMat.m13 = (-2F * plane[3] * plane[1]); reflectionMat.m20 = (-2F * plane[2] * plane[0]); reflectionMat.m21 = (-2F * plane[2] * plane[1]); reflectionMat.m22 = (1F - 2F * plane[2] * plane[2]); reflectionMat.m23 = (-2F * plane[3] * plane[2]); reflectionMat.m30 = 0F; reflectionMat.m31 = 0F; reflectionMat.m32 = 0F; reflectionMat.m33 = 1F; return reflectionMat; } /// <summary> /// 计算指定平面在摄像机中的空间位置 /// </summary> /// <param name="cam">摄像机</param> /// <param name="pos">平面上的点</param> /// <param name="normal">平面法线</param> /// <param name="sideSign">1:平面正面,-1:平面反面</param> /// <param name="clipPlaneOffset">平面法线位置偏移量</param> /// <returns></returns> public static Vector4 CameraSpacePlane(Camera cam, Vector3 pos, Vector3 normal, float sideSign,float clipPlaneOffset) { Vector3 offsetPos = pos + normal * clipPlaneOffset; Matrix4x4 m = cam.worldToCameraMatrix; Vector3 cpos = m.MultiplyPoint(offsetPos); Vector3 cnormal = m.MultiplyVector(normal).normalized * sideSign; return new Vector4(cnormal.x, cnormal.y, cnormal.z, -Vector3.Dot(cpos, cnormal)); } /// <summary> /// 由剪裁面计算投影倾斜矩阵 /// </summary> /// <param name="projection">投影矩阵</param> /// <param name="clipPlane">剪裁面</param> public static Matrix4x4 CalculateObliqueMatrix(Matrix4x4 projection, Vector4 clipPlane) { Vector4 q = projection.inverse * new Vector4( sgn(clipPlane.x), sgn(clipPlane.y), 1.0f, 1.0f ); Vector4 c = clipPlane * (2.0F / (Vector4.Dot(clipPlane, q))); // third row = clip plane - fourth row projection[2] = c.x - projection[3]; projection[6] = c.y - projection[7]; projection[10] = c.z - projection[11]; projection[14] = c.w - projection[15]; return projection; } private static float sgn(float a) { if (a > 0.0f) return 1.0f; if (a < 0.0f) return -1.0f; return 0.0f; } #endregion }
Shader
Shader "GameCore/SpecialEffect/Reflection" { Properties { _ReflectionTex ("Reflection", 2D) = "white" { TexGen ObjectLinear } _ReflectionColor("Color",Color) = (1,1,1,0.5) } SubShader { Tags { "Queue"="Transparent" "RenderType"="Transparent"} LOD 100 Blend SrcAlpha OneMinusSrcAlpha Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" uniform float4x4 _ProjMatrix; uniform sampler2D _ReflectionTex; float4 _ReflectionColor; struct v2f { float4 pos : SV_POSITION; float3 uv : TEXCOORD0; }; v2f vert(appdata_tan v) { v2f o; o.pos = mul (UNITY_MATRIX_MVP,v.vertex); float3 viewDir = ObjSpaceViewDir(v.vertex); o.uv = mul( _ProjMatrix,float4(viewDir,0)); return o; } float4 frag(v2f i) : COLOR { half4 reflcol = tex2Dproj( _ReflectionTex,i.uv); return reflcol*_ReflectionColor; } ENDCG } } }
源码链接: http://pan.baidu.com/s/1i3HrY7b 密码: dupd
Unity3d 镜面反射 vertex and frag Shader源码,布布扣,bubuko.com
Unity3d 镜面反射 vertex and frag Shader源码
原文:http://blog.csdn.net/baozhenliang/article/details/25991125