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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
| Shader "HHF/Tutorial/Texture" { Properties { _MainTex ("Texture", 2D) = "white" {} [KeywordEnum(Repeat, Clamp)]_WrapMode("WrapMode",int) = 0 [IntRange]_Mipmap("Mipmap",Range(0, 12)) = 0
[Toggle]_NormalTexEnable("NormalTexEnabled", int) = 0 [Normal]_NormalTex("NormalTex", 2D) = "bump"{}
[Toggle]_CubeTexEnable("CubeTexEnabled",int) = 0 _CubeTex("CubeTex", Cube) = "white"{}
[Toggle]_ReflectionProbeEnable("ReflectionProbeEnabled",int) = 0 } SubShader { Tags { "RenderPipeline"="UniversalPipeline" }
Pass { Tags {"LightMode" = "UniversalForward"} HLSLPROGRAM #pragma prefer_hlslcc gles #pragma exclude_renderers d3d11_9x #pragma vertex vert #pragma fragment frag #pragma shader_feature _WRAPMODE_REPEAT _WRAPMODE_CLAMP #pragma shader_feature _ _NORMALTEXENABLE_ON #pragma shader_feature _ _CUBETEXENABLE_ON #pragma shader_feature _ _REFLECTIONPROBEENABLE_ON #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal:NORMAL; float4 tangent:TANGENT; };
struct v2f { float2 uv : TEXCOORD0; float4 positionCS : SV_POSITION; float3 positionWS:TEXCOORD1; half3 normalWS:TEXCOORD2;
float3 tSpace0:TEXCOORD4; float3 tSpace1:TEXCOORD5; float3 tSpace2:TEXCOORD6; };
CBUFFER_START(UnityPerMaterial) float4 _MainTex_ST; half _Mipmap; CBUFFER_END
TEXTURE2D (_MainTex);SAMPLER(sampler_MainTex); TEXTURE2D (_NormalTex);SAMPLER(sampler_NormalTex); TEXTURECUBE (_CubeTex);SAMPLER(sampler_CubeTex);
v2f vert (appdata v) { v2f o; o.positionCS = TransformObjectToHClip(v.vertex.xyz); o.uv = TRANSFORM_TEX(v.uv, _MainTex); o.positionWS = TransformObjectToWorld(v.vertex.xyz); o.normalWS = TransformObjectToWorldNormal(v.normal);
half3 worldTangent = TransformObjectToWorldDir(v.tangent.xyz); half sign = v.tangent.w * GetOddNegativeScale(); half3 worldBinormal = cross(o.normalWS, worldTangent) * sign; o.tSpace0 = float3(worldTangent.x,worldBinormal.x,o.normalWS.x); o.tSpace1 = float3(worldTangent.y,worldBinormal.y,o.normalWS.y); o.tSpace2 = float3(worldTangent.z,worldBinormal.z,o.normalWS.z); return o; }
half4 frag (v2f i) : SV_Target { #if _WRAPMODE_REPEAT i.uv = frac(i.uv); #elif _WRAPMODE_CLAMP i.uv = saturate (i.uv); #endif
half4 c = SAMPLE_TEXTURE2D_LOD(_MainTex, sampler_MainTex, i.uv, _Mipmap);
half3 worldNormal = i.normalWS; #if _NORMALTEXENABLE_ON Light mainLight = GetMainLight(); half3 normalTex = UnpackNormalScale(SAMPLE_TEXTURE2D(_NormalTex, sampler_NormalTex, i.uv), 1); half3 N1 = normalize(normalTex); half3 L = mainLight.direction; worldNormal = half3(dot(i.tSpace0, normalTex), dot(i.tSpace1,normalTex), dot(i.tSpace2,normalTex)); half3 diffuse = mainLight.color * max(0, dot(N1, L)); c.rgb += diffuse; #endif half3 V = normalize(GetWorldSpaceViewDir(i.positionWS)); half3 N = normalize(worldNormal); half3 R = reflect(-V, N);
#if _CUBETEXENABLE_ON half4 cubemap = SAMPLE_TEXTURECUBE(_CubeTex, sampler_CubeTex, R); c += cubemap; #endif
#if _REFLECTIONPROBEENABLE_ON half4 cubemapProbe = SAMPLE_TEXTURECUBE (unity_SpecCube0, samplerunity_SpecCube0, R); half3 probeColor = DecodeHDREnvironment (cubemapProbe, unity_SpecCube0_HDR); c.rgb += probeColor; #endif
return c; }
ENDHLSL } }
SubShader { Tags { "RenderType"="Opaque" }
Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma shader_feature _WRAPMODE_REPEAT _WRAPMODE_CLAMP #pragma shader_feature _ _NORMALTEXENABLE_ON #pragma shader_feature _ _CUBETEXENABLE_ON #pragma shader_feature _ _REFLECTIONPROBEENABLE_ON
#include "UnityCG.cginc" #include "AutoLight.cginc" #include "Lighting.cginc"
struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal:NORMAL; float4 tangent:TANGENT; };
struct v2f { float2 uv : TEXCOORD0; float4 positionCS : SV_POSITION; float3 positionWS:TEXCOORD1; half3 normalWS:TEXCOORD2;
float3 tSpace0:TEXCOORD4; float3 tSpace1:TEXCOORD5; float3 tSpace2:TEXCOORD6; };
sampler2D _MainTex; float4 _MainTex_ST; sampler2D _NormalTex; half _Mipmap; samplerCUBE _CubeTex;
v2f vert (appdata v) { v2f o; o.positionCS = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); o.positionWS = mul(unity_ObjectToWorld,v.vertex); o.normalWS = UnityObjectToWorldNormal(v.normal);
half3 worldTangent = UnityObjectToWorldDir(v.tangent); half tangentSign = v.tangent.w * unity_WorldTransformParams.w; half3 worldBinormal = cross(o.normalWS, worldTangent) * tangentSign; o.tSpace0 = float3(worldTangent.x,worldBinormal.x,o.normalWS.x); o.tSpace1 = float3(worldTangent.y,worldBinormal.y,o.normalWS.y); o.tSpace2 = float3(worldTangent.z,worldBinormal.z,o.normalWS.z); return o; }
half4 frag (v2f i) : SV_Target { #if _WRAPMODE_REPEAT i.uv = frac(i.uv); #elif _WRAPMODE_CLAMP i.uv = saturate (i.uv); #endif
float4 uvMipmap = float4(i.uv, 0, _Mipmap); half4 c = tex2Dlod(_MainTex, uvMipmap);
half3 worldNormal = i.normalWS;
#if _NORMALTEXENABLE_ON half3 normalTex = UnpackNormal(tex2D(_NormalTex, i.uv)); half3 N1 = normalize(normalTex); half3 L = _WorldSpaceLightPos0.xyz; half3 diffuse = _LightColor0.rgb * max(0, dot(N1, L)); worldNormal = half3(dot(i.tSpace0, normalTex), dot(i.tSpace1,normalTex), dot(i.tSpace2,normalTex)); c.rgb += diffuse; #endif
half3 V = normalize(UnityWorldSpaceViewDir(i.positionWS)); half3 N = normalize(worldNormal); half3 R = reflect(-V, N);
#if _CUBETEXENABLE_ON half4 cubemap = texCUBE(_CubeTex, R); c += cubemap; #endif
#if _REFLECTIONPROBEENABLE_ON half4 cubemapProbe = UNITY_SAMPLE_TEXCUBE (unity_SpecCube0, R); half3 probeColor = DecodeHDR (cubemapProbe, unity_SpecCube0_HDR); c.rgb += probeColor; #endif
return c; } ENDCG } } }
|