镜面反射代码

文件名MirrorReflection.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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.
           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( m_DisablePixelLights )
                 QualitySettings.pixelLightCount = 0;
 
          UpdateCameraModes( cam, reflectionCamera );
 
          // Render reflection
          // Reflect camera around reflection plane
          float d = -Vector3.Dot (normal, pos) - m_ClipPlaneOffset;
          Vector4 reflectionPlane = new Vector4 (normal.x, normal.y, normal.z, d);
 
          Matrix4x4 reflection = Matrix4x4.zero;
          CalculateReflectionMatrix (ref reflection, 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 = CameraSpacePlane( reflectionCamera, pos, normal, 1.0f );
          Matrix4x4 projection = cam.projectionMatrix;
          CalculateObliqueMatrix (ref projection, clipPlane);
          reflectionCamera.projectionMatrix = projection;
 
          reflectionCamera.cullingMask = ~(1<<4) & m_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( m_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();
    }
 
 
    private void UpdateCameraModes( 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;
     }
 
     // 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 != m_TextureSize )
          {
                if( m_ReflectionTexture )
                     DestroyImmediate( m_ReflectionTexture );
                m_ReflectionTexture = new RenderTexture( m_TextureSize, m_TextureSize, 16 );
                m_ReflectionTexture.name = "__MirrorReflection" + GetInstanceID();
                m_ReflectionTexture.isPowerOfTwo = true;
                m_ReflectionTexture.hideFlags = HideFlags.DontSave;
                m_OldReflectionTextureSize = m_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;
          }
     }
 
     // Extended sign: returns -1, 0 or 1 based on sign of a
     private static float sgn(float a)
    {
          if (a > 0.0f) return 1.0f;
              if (a < 0.0f) return -1.0f;
                   return 0.0f;
    }
 
    // Given position/normal of the plane, calculates plane in camera space.
    private Vector4 CameraSpacePlane (Camera cam, Vector3 pos, Vector3 normal, float sideSign)
    {
          Vector3 offsetPos = pos + normal * m_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) );
    }
 
    // Adjusts the given projection matrix so that near plane is the given clipPlane
    // clipPlane is given in camera space. See article in Game Programming Gems 5.
    private static void CalculateObliqueMatrix (ref 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];
    }
 
    // Calculates reflection matrix around the given plane
    private static void CalculateReflectionMatrix (ref 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;
    }
}

文件名MirrorReflection.shader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 }
           }
     }
}

Uinty3d 镜面反射代码的更多相关文章

  1. WebGL展示3D房屋内景

      原文地址:WebGL展示3D房屋内景   由于生活和工作上的原因,从年前开始一直到处奔波,没有太多的时间去关注和学习WebGL图形学相关的技术, 不过陆陆续续都有学习使用blender进行3D建模 ...

  2. CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果

    CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果 开始 一图抵千言.首先来看鼠标拖动太阳(光源)的情形. 然后是鼠标拖拽旋转模型的情形. 然后我们移动摄像 ...

  3. 解读Unity中的CG编写Shader系列十 (光滑的镜面反射(冯氏着色))

    前文完成了最基本的镜面反射着色器,单平行光源下的逐顶点着色(per-vertex lighting),又称为古罗着色(Gouraud shading).这篇文章作为后续讨论更光滑的镜面反射方式,逐像素 ...

  4. 解读Unity中的CG编写Shader系列八(镜面反射)

    转自http://www.itnose.net/detail/6117378.html 讨论完漫反射之后,接下来肯定就是镜面反射了 在开始镜面反射shader的coding之前,要扩充一下前面提到的知 ...

  5. Unity5 GI与PBS渲染从用法到着色代码

    http://www.cnblogs.com/zhouxin/p/5168632.html 本文主要介绍Untiy5以后的GI,PBS,以及光源探头,反射探头的用法以及在着色器代码中如何发挥作用,GI ...

  6. Esfog_UnityShader教程_镜面反射SpecularReflection

    系列教程第四篇,本来打算昨天写的,有些小偷懒就今天写了,这一期我们来讨论一下关于镜面反射的基本原理和具体代码.这一篇是承接着上一篇<Esfog_UnityShader教程_漫反射DiffuseR ...

  7. HLSL之镜面反射光照

    今天学习的是镜面的反射光照,其实一般在场景中基本环境光和漫反射光照已经可以表现出一个不错的照明了,今天的镜面反射光照其实仅仅适合于需要在表面添加抛光或者闪耀的物体上,例如金属.玻璃等等,同时也是基于之 ...

  8. 【Unity Shaders】学习笔记——SurfaceShader(十)镜面反射

    [Unity Shaders]学习笔记——SurfaceShader(十)镜面反射 如果你想从零开始学习Unity Shader,那么你可以看看本系列的文章入门,你只需要稍微有点编程的概念就可以. 水 ...

  9. Unity3d 镜面反射 vertex and frag Shader源代码

    Unity3d 镜面反射 网上能找到的基本上是固定管道或表面渲染的shader. 特此翻译为顶点.片段渲染的Shader, 本源代码仅仅涉及shader与cs部分. Editor部分使用NGUI绘制的 ...

随机推荐

  1. C++中的::operator new, ::operator delete

    一般在使用new  和 delete的时候,做了两件事情,一是空间的配置( new 是分配,delete是回收),而是调用对象的析构函数 但是也有办法将这两个过程分开 那就是显式的调用::operat ...

  2. 获取本机IP非127.0.0.1

    protected function GetiP()    {    $preg="/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5] ...

  3. 安装webmin

    wget http://nchc.dl.sourceforge.net/project/webadmin/webmin/1.740/webmin-1.740.tar.gz 解压缩文件,命令是:tar ...

  4. Qt Charts示例

    Qt 5.7 有一些变化,把原来商业版的几个模块用GPLv3协议放到了社区版本里: Qt Charts (GPLv3) Qt Data Visualization (GPLv3) Qt Virtual ...

  5. HDU 4869 (递推 组合数取模)

    Problem Turn the pokers (HDU 4869) 题目大意 有m张牌,全为正面朝上.进行n次操作,每次可以将任意ai张反面,询问n次操作可能的状态数. 解题分析 记正面朝上为1,朝 ...

  6. PHP 安全

    作为PHP程序员,特别是新手,对于互联网的险恶总是知道的太少,对于外部的入侵有很多时候是素手无策的,他们根本不知道黑客是如何入侵的.提交入侵.上传漏洞.sql 注入.跨脚本攻击等等.作为最基本的防范你 ...

  7. PHP访问REST API上传文件的解决方案

    最近写的一个小功能需要通过rest方式上传文件,因此就在网上找了一些解决方案.接下来说明以下我采用的解决方案:我是利用curl来实现的,其中CURLOPT_POST的值为TRUE代表的是请求类型为PO ...

  8. 1.NSThread

    前言 每个iOS应用程序都有个专门用来更新显示UI界面.处理用户触摸事件的主线程,因此不能将其他太耗时的操作放在主线程中执行,不然会造成主线程堵塞(出现卡机现象),带来极坏的用户体验.一般的解决方案就 ...

  9. centos 输入密码正确进不去系统

    国庆长假之后,上班,第一件事就是连接开发服务器进行开发,结果发现死活进不去系统.一开始以为是用户名和密码错误,后来发现怎么输入都是进不去系统,密码输入是肯定正确的. google了一下,发现有人说是密 ...

  10. shell脚本 gawk语言 综采话单 对账 字段核对

    今天被分配到对账组下的字段程序的了解和修改,在 SecureCRT 下的 run.sh 是字段对账的主程序,其中run.sh文件中含有gawk的代码. 程序的主要功能为 有两个文件夹 source存的 ...