Unity3D教程宝典之Shader篇:第六讲TexGen
coord = P1*X + P2*Y + P3*Z + P4*W
xyzw即物体的本地坐标,P1–P4 为系数
4组结果分别对应OpenGL里的 S, T, R, Q,即 coord.xyzw
而通常的纹理映射只用到s,t 也就是x y
以正方体为例,中心点(0,0,0) x=0 y=0,对应贴图的(0,0)点也就是贴图的左下角
而右上角的前点(0.5,0.5,0.5)后点(0.5,0.5,-0.5)均对应贴图的(0.5,0.5)点也就是贴图的中心点。以此类推得到右下图的结果。
Shader "Texgen" { Properties { _MainTex ("Base", 2D) = "white" { TexGen ObjectLinear } } SubShader { Pass { SetTexture [_MainTex] { combine texture } } } } Shader "Custom/Texgen_Obj_FragMine" { Properties { _MainTex ("Base", 2D) = "white" } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; float4 _MainTex_ST; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; } ; v2f vert (appdata_base v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP,v.vertex); o.uv = v.vertex.xy; return o; } float4 frag (v2f i) : COLOR { float4 texCol = tex2D(_MainTex,i.uv); return texCol; } ENDCG } } }
Shader "Texgen" { Properties { _MainTex ("Base", 2D) = "white" { TexGen EyeLinear } } SubShader { Pass { SetTexture [_MainTex] { combine texture } } } } Shader "Custom/Eye" { Properties { _MainTex ("Base", 2D) = "white" } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; float4 _MainTex_ST; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; } ; v2f vert (appdata_base v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP,v.vertex); o.uv =mul(UNITY_MATRIX_MV,v.vertex); return o; } float4 frag (v2f i) : COLOR { float4 texCol = tex2D(_MainTex,i.uv);∂ return outp; } ENDCG } } }
Shader "Texgen" { Properties { _Tex ("Cube", 2D) = "white" { TexGen SphereMap } } SubShader { Pass { SetTexture [_MainTex] { combine texture } } } }
Shader "Texgen" { Properties { _Tex ("Cube", Cube) = "white" { TexGen CubeReflect } } SubShader { Pass { SetTexture [_MainTex] { combine texture } } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。