Texture 纹理贴图
基础贴图Shader:只有纹理
1. 在属性中声明纹理贴图: _MainTex ("Texture", 2D) = "white" {}
2. 在Pass中声明变量: sampler2D _MainTex; float4 _MainTex_ST; 这个是成对出现,_MainTex_ST 用与计算坐标偏移offset
3. 在Vertex Function函数中进行纹理坐标采样: o.tex = v.texcoord;
4. 在Fragment Function 函数中采样纹理,并输出: float4 tex = tex2D(_MainTex,i.tex.xy*_MainTex_ST.xy+_MainTex_ST.zw); return float4(tex);
源代码:
Shader "Unlit/Textures_Base" { Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; float4 _MainTex_ST; //输入结构体 struct vertexInput{ float4 vertex:POSITION; float4 texcoord:TEXCOORD0; }; //输出结构体 struct vertexOutput{ float4 pos:SV_POSITION; float4 tex:TEXCOORD0; }; vertexOutput vert (vertexInput v) { vertexOutput o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.tex = v.texcoord; return o; } fixed4 frag (vertexOutput i) : COLOR { //Texture Map float4 tex = tex2D(_MainTex,i.tex.xy*_MainTex_ST.xy+_MainTex_ST.zw); return float4(tex); } ENDCG } } }
纹理(Texture)+高光(Specular)+边缘光(Rim)+漫反射(diffuse)+环境光(ambient)+多光源(Lighting)(1个平行光,1个点光源):
源代码:
Shader "JQM/Textures" { Properties { _Color("Color", color) = (1.0,1.0,1.0,1.0) _MainTex ("Texture", 2D) = "white" {} _SpecColor("Specular Color", color) = (1.0,1.0,1.0,1.0) _Shininess("Shininess",float) = 10 _RimColor("Rim Coloe Color", color) = (1.0,1.0,1.0,1.0) _RimPower("Rim Power",Range(0.1,10.0)) = 3.0 } SubShader { Pass { Tags { "LightMode"="ForwardBase" } CGPROGRAM #pragma vertex vert #pragma fragment frag //#pragma exclude_renderers flash //给指定平台编译 #include "UnityCG.cginc" //使用自定义变量 sampler2D _MainTex; float4 _MainTex_ST; uniform float4 _Color; uniform float4 _SpecColor; uniform float4 _RimColor; uniform float _Shininess; uniform float _RimPower; //使用Unity定义的变量 uniform float4 _LightColor0; //输入结构体 struct vertexInput{ float4 vertex:POSITION; float3 normal:NORMAL; float4 texcoord:TEXCOORD0; }; //输出结构体 struct vertexOutput{ float4 pos:SV_POSITION; float4 tex:TEXCOORD0; float4 posWorld:TEXCOORD1; float3 normalDir:TEXCOORD2; }; vertexOutput vert (vertexInput v) { vertexOutput o; o.posWorld = mul(_Object2World, v.vertex); o.normalDir = normalize( mul(float4(v.normal,0.0),_World2Object).xyz); o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.tex = v.texcoord; return o; } fixed4 frag (vertexOutput i) : COLOR { float3 normalDirection = i.normalDir; float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz- i.posWorld.xyz); float3 lightDirection; float atten; if(_WorldSpaceLightPos0.w==0.0)//平行光 { atten = 1.0; lightDirection = normalize(_WorldSpaceLightPos0.xyz); } else { float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz -i.posWorld.xyz; float distance = length(fragmentToLightSource); atten = 1.0/distance; lightDirection = normalize(fragmentToLightSource); } //灯光 float3 diffuseReflection = atten * _LightColor0.xyz * saturate( dot(normalDirection,lightDirection)); float3 specularReflection = atten * _LightColor0.xyz * _SpecColor.rgb*saturate( dot(normalDirection,lightDirection))*pow(saturate(dot(reflect(-lightDirection,normalDirection),viewDirection)),_Shininess); //Rim Light float rim= 1-dot(normalize(viewDirection),normalDirection); float3 rimLighting = atten * _LightColor0.xyz * _RimColor.rgb*saturate(dot(normalDirection,lightDirection))*pow(rim,_RimPower); float3 lightFinal = rimLighting + diffuseReflection+specularReflection+UNITY_LIGHTMODEL_AMBIENT.xyz; //Texture Map float4 tex = tex2D(_MainTex,i.tex.xy*_MainTex_ST.xy+_MainTex_ST.zw); return float4(tex*lightFinal*_Color.xyz,1.0); } ENDCG } Pass { Tags { "LightMode"="ForwardAdd" } Blend One One CGPROGRAM #pragma vertex vert #pragma fragment frag //#pragma exclude_renderers flash //给指定平台编译 #include "UnityCG.cginc" //使用自定义变量 sampler2D _MainTex; float4 _MainTex_ST; uniform float4 _Color; uniform float4 _SpecColor; uniform float4 _RimColor; uniform float _Shininess; uniform float _RimPower; //使用Unity定义的变量 uniform float4 _LightColor0; //输入结构体 struct vertexInput{ float4 vertex:POSITION; float3 normal:NORMAL; float4 texcoord:TEXCOORD0; }; //输出结构体 struct vertexOutput{ float4 pos:SV_POSITION; float4 tex:TEXCOORD0; float4 posWorld:TEXCOORD1; float3 normalDir:TEXCOORD2; }; vertexOutput vert (vertexInput v) { vertexOutput o; o.posWorld = mul(_Object2World, v.vertex); o.normalDir = normalize( mul(float4(v.normal,0.0),_World2Object).xyz); o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.tex = v.texcoord; return o; } fixed4 frag (vertexOutput i) : COLOR { float3 normalDirection = i.normalDir; float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz- i.posWorld.xyz); float3 lightDirection; float atten; if(_WorldSpaceLightPos0.w==0.0)//平行光 { atten = 1.0; lightDirection = normalize(_WorldSpaceLightPos0.xyz); } else { float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz -i.posWorld.xyz; float distance = length(fragmentToLightSource); atten = 1.0/distance; lightDirection = normalize(fragmentToLightSource); } //灯光 float3 diffuseReflection = atten * _LightColor0.xyz * saturate( dot(normalDirection,lightDirection)); float3 specularReflection = atten * _LightColor0.xyz * _SpecColor.rgb*saturate( dot(normalDirection,lightDirection))*pow(saturate(dot(reflect(-lightDirection,normalDirection),viewDirection)),_Shininess); //Rim Light float rim= 1-dot(normalize(viewDirection),normalDirection); float3 rimLighting = atten * _LightColor0.xyz * _RimColor.rgb*saturate(dot(normalDirection,lightDirection))*pow(rim,_RimPower); float3 lightFinal = rimLighting + diffuseReflection+specularReflection+UNITY_LIGHTMODEL_AMBIENT.xyz; //Texture Map float4 tex = tex2D(_MainTex,i.tex.xy*_MainTex_ST.xy+_MainTex_ST.zw); return float4(tex*lightFinal*_Color.xyz,1.0); } ENDCG } } }