本文实例为大家分享了Unity3D Shader实现镜子效果的具体代码,供大家参考,具体内容如下
Shader部分代码:
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
|
Shader "Custom/FanShe" { Properties{ _MainTex( "Albedo" ,2D) = "white" {} _MainTint( "Diffuse Color" ,Color)=(1,1,1,1) _Cubemap( "Cubemap" ,CUBE) = "" {} _ReflAmount( "Reflection Amount" ,Range(0.1,1.0))=0.5 } SubShader{ Tags{ "RenderType" = "Opaque" } LOD 200 CGPROGRAM #pragma surface surf Lambert #pragma target 3.0 struct Input { float2 uv_MainTex; float3 worldRefl; }; sampler2D _MainTex; samplerCUBE _Cubemap; fixed4 _MainTint; half _ReflAmount; void surf(Input IN, inout SurfaceOutput o) { fixed4 c = tex2D(_MainTex, IN.uv_MainTex)*_MainTint; o.Albedo = c.rgb; o.Emission = texCUBE(_Cubemap, IN.worldRefl)*_ReflAmount; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" } |
C#部分代码:
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
|
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Shader_FanShe : MonoBehaviour { public Cubemap cubeMap; public Camera cam; Material curmat; // Use this for initialization void Start () { InvokeRepeating( "change" , 1, 0.1f); curmat = gameObject.GetComponent<Renderer>().material; if (curmat == null ) { Debug.Log( "cw" ); } } // Update is called once per frame void Update () { } void change() { cam.transform.rotation = Quaternion.identity; cam.RenderToCubemap(cubeMap); curmat.SetTexture( "_Cubemap" ,cubeMap); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_42452001/article/details/80681147