简单实现凹面镜与凸面镜效果的思路 VR资源

东方喵 2017-12-05 17:46:33
先上效果:
 
凸面镜:
 
 
 
凹面镜:
 
 
 
 
基于本月的训练营给诸君一点简单的实现思路:
 
1.建造一个副相机,使用RenderTexture,以获取镜中图像。
 
2.建一个quad作为镜
 
3.改变RenderTexture中的uv,以实现凹凸面镜等
 
注:仅供参考。
妈妈说,不做伸手党,自己动手才是乖宝宝~~~
 
code如下:
 
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
凹面镜
*/
Shader "凸面镜与凹面镜/凹面镜" {
        Properties{
        _Tex("MainTex", 2D) = "white" {}
        }
                SubShader{
                Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
                LOD 200
                Cull off
                CGPROGRAM
#pragma surface surf Lambert alpha:blend
                sampler2D _Tex;
 
        struct Input {
                float2 uv_Tex;
        };
 
        void surf(Input IN, inout SurfaceOutput o) {
                half4 c;
                float2 uvTemp;
                half2 offset = IN.uv_Tex.rg - half2(0.5,0.5);
                half2 signOffset = half2(sign(offset.r), sign(offset.g));
                uvTemp.xy = half2(0.5, 0.5)+sqrt(abs(offset))*0.5*signOffset;
                c = tex2D(_Tex,uvTemp);
                o.Emission = c.rgb;
                o.Alpha = 1;
                }
        ENDCG
        }
                FallBack off
}
 
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
凸面镜
*/
Shader "凸面镜与凹面镜/凸面镜" {
        Properties{
        _Tex("MainTex", 2D) = "white" {}
        }
                SubShader{
                Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
                LOD 200
                Cull off
                CGPROGRAM
#pragma surface surf Lambert alpha:blend
                sampler2D _Tex;
 
        struct Input {
                float2 uv_Tex;
        };
 
        void surf(Input IN, inout SurfaceOutput o) {
                half4 c;
                float2 uvTemp;
                half2 offset = IN.uv_Tex.rg - half2(0.5,0.5);
                half2 signOffset = half2(sign(offset.r),sign(offset.g));
                uvTemp.xy = half2(0.5, 0.5)+offset*offset*2*signOffset;
                c = tex2D(_Tex,uvTemp);
                o.Emission = c.rgb;
                o.Alpha = 1;
                }
        ENDCG
        }
                FallBack off
}
 
此上扭曲只是根据偏移量的平方和开方实现。
 
99VR视界二维码
热门推荐
Hot Recommended
在线客服