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
| // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "UnityShadersLearn/Chapter5/SimpleShader" { Properties { _Color ("Color Tint", Color) = (1.0, 1.0, 1.0, 1.0) } SubShader{ pass{ CGPROGRAM
#pragma vertex vert #pragma fragment frag
// note: // 1.填充到语义中的数据是哪来的: // 首先结构体a2v的含义是从应用阶段传递数据给顶点着色器 // POSITION|TANGENT|NORMAL这些语义中的数据在unity中是从该材质的MeshRender组件提供的 // 每帧调用DrawCall时MeshRender都会把它负责渲染的模型数据发送给UnityShader // 一个模型通常包含了一组三角面片,每个三角面片由三个顶点组成,每个顶点包含了一组数据,如顶点位置、法线、切线、纹理坐标、顶点颜色等 // 2.顶点着色器和片元着色器通信 // 定义一个结构体(可以叫v2f),如果顶点着色器返回结构体,那么必须包含SV_POSITION语义,不然渲染器无法得到 // 裁剪空间中的坐标,导致无法渲染顶点到屏幕。其他语义可以自行发挥,比如存储自定义颜色的COLOR0 // 需要注意的是顶点着色器是逐顶点,而片元着色器是逐片元,所以传给片元着色器的信息是插值过的
// level 1 // // SV_POSITION输出到裁剪空间的顶点坐标 // float4 vert(float4 v : POSITION) : SV_POSITION { // return UnityObjectToClipPos(v); // } // // // SV_Target输出到渲染目标,即帧缓冲 // fixed4 frag() : SV_Target { // return fixed4(1.0, 1.0, 1.0, 1.0); // }
// level 2 // struct a2v // { // // POSITION语义,用模型空间的顶点坐标填充该变量 // float4 vertex : POSITION; // // NORMAL语义,用模型空间的法线方向填充该变量 // float3 normal : NORMAL; // // TEXCOORD0语义,用模型的第一套纹理坐标填充该变量 // float4 texcoord : TEXCOORD0; // }; // // float4 vert(a2v v) : SV_POSITION { // return UnityObjectToClipPos(v.vertex); // } // // fixed4 frag() : SV_Target { // return fixed4(1.0,1.0,1.0,1.0); // }
// level 3
// // 需要和属性名称类型都匹配 // fixed4 _Color; // // struct a2v // { // float4 vertex : POSITION; // float3 normal : NORMAL; // float4 texcoord : TEXCOORD0; // }; // // struct v2f // { // float4 pos : SV_POSITION; // float3 color : COLOR0; // }; // // v2f vert(a2v v) // { // v2f o; // o.pos = UnityObjectToClipPos(v.vertex); // // 法线的范围是[-1.0, 1.0] // // 而颜色值是[0, 1.0] // // 这里做个映射 // o.color = v.normal * 0.5 + fixed3(0.5, 0.5, 0.5); // return o; // } // // fixed4 frag(v2f i) : SV_Target // { // fixed3 c = i.color; // c *= _Color.rgb; // return fixed4(c, 1.0); // }
// level 4 #include "UnityCG.cginc" struct v2f { float4 pos : SV_POSITION; fixed4 color : COLOR0; }; v2f vert(appdata_full v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); // Visualize normal o.color = fixed4(v.normal * 0.5 + fixed3(0.5, 0.5, 0.5), 1.0); // Visualize tangent o.color = fixed4(v.tangent.xyz * 0.5 + fixed3(0.5, 0.5, 0.5), 1.0); // Visualize binormal fixed3 binormal = cross(v.normal, v.tangent.xyz) * v.tangent.w; o.color = fixed4(binormal * 0.5 + fixed3(0.5, 0.5, 0.5), 1.0); // Visualize the first set texcoord o.color = fixed4(v.texcoord.xy, 0.0, 1.0); // Visualize the second set texcoord o.color = fixed4(v.texcoord1.xy, 0.0, 1.0); // Visualize fractional part of the first set texcoord o.color = frac(v.texcoord); if (any(saturate(v.texcoord) - v.texcoord)) { o.color.b = 0.5; } o.color.a = 1.0; // Visualize fractional part of the second set texcoord o.color = frac(v.texcoord1); if (any(saturate(v.texcoord1) - v.texcoord1)) { o.color.b = 0.5; } o.color.a = 1.0; // Visualize vertex color // o.color = v.color; return o; } fixed4 frag(v2f i) : SV_Target { return i.color; }
ENDCG } } FallBack "Diffuse" }
|