1+ // Copyright (c) Microsoft Corporation.
2+ // Licensed under the MIT License.
3+
4+ #if defined (MATERIAL_QUALITY_LOW)
5+ #include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/UnlitPass.hlsl"
6+ #else
7+
8+ #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl"
9+
10+ void InitializeInputData (Varyings input, SurfaceDescription surfaceDescription, out InputData inputData)
11+ {
12+ inputData = (InputData)0 ;
13+
14+ inputData.positionWS = input.positionWS;
15+
16+ #ifdef _NORMALMAP
17+ // IMPORTANT! If we ever support Flip on double sided materials ensure bitangent and tangent are NOT flipped.
18+ float crossSign = (input.tangentWS.w > 0.0 ? 1.0 : -1.0 ) * GetOddNegativeScale ();
19+ float3 bitangent = crossSign * cross (input.normalWS.xyz, input.tangentWS.xyz);
20+
21+ inputData.tangentToWorld = half3x3 (input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz);
22+ #if _NORMAL_DROPOFF_TS
23+ inputData.normalWS = TransformTangentToWorld (surfaceDescription.NormalTS, inputData.tangentToWorld);
24+ #elif _NORMAL_DROPOFF_OS
25+ inputData.normalWS = TransformObjectToWorldNormal (surfaceDescription.NormalOS);
26+ #elif _NORMAL_DROPOFF_WS
27+ inputData.normalWS = surfaceDescription.NormalWS;
28+ #endif
29+ #else
30+ inputData.normalWS = input.normalWS;
31+ #endif
32+ inputData.normalWS = NormalizeNormalPerPixel (inputData.normalWS);
33+ inputData.viewDirectionWS = GetWorldSpaceNormalizeViewDir (input.positionWS);
34+
35+ #if defined (REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
36+ inputData.shadowCoord = input.shadowCoord;
37+ #elif defined (MAIN_LIGHT_CALCULATE_SHADOWS)
38+ inputData.shadowCoord = TransformWorldToShadowCoord (inputData.positionWS);
39+ #else
40+ inputData.shadowCoord = float4 (0 , 0 , 0 , 0 );
41+ #endif
42+
43+ inputData.fogCoord = InitializeInputDataFog (float4 (input.positionWS, 1.0 ), input.fogFactorAndVertexLight.x);
44+ inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
45+ #if defined (DYNAMICLIGHTMAP_ON)
46+ inputData.bakedGI = SAMPLE_GI (input.staticLightmapUV, input.dynamicLightmapUV.xy, input.sh, inputData.normalWS);
47+ #else
48+ inputData.bakedGI = SAMPLE_GI (input.staticLightmapUV, input.sh, inputData.normalWS);
49+ #endif
50+ inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV (input.positionCS);
51+ inputData.shadowMask = SAMPLE_SHADOWMASK (input.staticLightmapUV);
52+
53+ #if defined (DEBUG_DISPLAY)
54+ #if defined (DYNAMICLIGHTMAP_ON)
55+ inputData.dynamicLightmapUV = input.dynamicLightmapUV.xy;
56+ #endif
57+ #if defined (LIGHTMAP_ON)
58+ inputData.staticLightmapUV = input.staticLightmapUV;
59+ #else
60+ inputData.vertexSH = input.sh;
61+ #endif
62+ #endif
63+ }
64+
65+ PackedVaryings vert (Attributes input)
66+ {
67+ Varyings output = (Varyings)0 ;
68+ output = BuildVaryings (input);
69+ PackedVaryings packedOutput = (PackedVaryings)0 ;
70+ packedOutput = PackVaryings (output);
71+ return packedOutput;
72+ }
73+
74+ void frag (
75+ PackedVaryings packedInput
76+ , out half4 outColor : SV_Target0
77+ #ifdef _WRITE_RENDERING_LAYERS
78+ , out float4 outRenderingLayers : SV_Target1
79+ #endif
80+ )
81+ {
82+ Varyings unpacked = UnpackVaryings (packedInput);
83+ UNITY_SETUP_INSTANCE_ID (unpacked);
84+ UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX (unpacked);
85+ SurfaceDescription surfaceDescription = BuildSurfaceDescription (unpacked);
86+
87+ #if defined (_SURFACE_TYPE_TRANSPARENT)
88+ bool isTransparent = true ;
89+ #else
90+ bool isTransparent = false ;
91+ #endif
92+
93+ #if defined (_ALPHATEST_ON)
94+ half alpha = AlphaDiscard (surfaceDescription.Alpha, surfaceDescription.AlphaClipThreshold);
95+ #elif defined (_SURFACE_TYPE_TRANSPARENT)
96+ half alpha = surfaceDescription.Alpha;
97+ #else
98+ half alpha = half (1.0 );
99+ #endif
100+
101+ #if defined (LOD_FADE_CROSSFADE) && USE_UNITY_CROSSFADE
102+ LODFadeCrossFade (unpacked.positionCS);
103+ #endif
104+
105+ InputData inputData;
106+ InitializeInputData (unpacked, surfaceDescription, inputData);
107+ // TODO: Mip debug modes would require this, open question how to do this on ShaderGraph.
108+ //SETUP_DEBUG_TEXTURE_DATA(inputData, unpacked.texCoord1.xy, _MainTex);
109+
110+ #ifdef _SPECULAR_SETUP
111+ float3 specular = surfaceDescription.Specular;
112+ #if defined (MATERIAL_QUALITY_MEDIUM)
113+ #else
114+ float metallic = 1 ;
115+ #endif //MATERIAL_QUALITY_MEDIUM
116+ #else
117+ float3 specular = 0 ;
118+ #if defined (MATERIAL_QUALITY_MEDIUM)
119+ #else
120+ float metallic = surfaceDescription.Metallic;
121+ #endif //MATERIAL_QUALITY_MEDIUM
122+ #endif
123+
124+ half3 normalTS = half3 (0 , 0 , 0 );
125+ #if defined (_NORMALMAP) && defined (_NORMAL_DROPOFF_TS)
126+ normalTS = surfaceDescription.NormalTS;
127+ #endif
128+
129+ SurfaceData surface;
130+ surface.albedo = surfaceDescription.BaseColor;
131+ #if defined (MATERIAL_QUALITY_MEDIUM)
132+ surface.metallic = 0.0 ;
133+ #else
134+ surface.metallic = saturate (metallic);
135+ #endif //MATERIAL_QUALITY_MEDIUM
136+ surface.specular = specular;
137+ surface.smoothness = saturate (surfaceDescription.Smoothness),
138+ #if defined (MATERIAL_QUALITY_MEDIUM)
139+ surface.occlusion = 1.0 ;
140+ #else
141+ surface.occlusion = surfaceDescription.Occlusion,
142+ #endif //MATERIAL_QUALITY_MEDIUM
143+ surface.emission = surfaceDescription.Emission,
144+ surface.alpha = saturate (alpha);
145+ surface.normalTS = normalTS;
146+ surface.clearCoatMask = 0 ;
147+ surface.clearCoatSmoothness = 1 ;
148+
149+ #ifdef _CLEARCOAT
150+ surface.clearCoatMask = saturate (surfaceDescription.CoatMask);
151+ surface.clearCoatSmoothness = saturate (surfaceDescription.CoatSmoothness);
152+ #endif
153+
154+ surface.albedo = AlphaModulate (surface.albedo, surface.alpha);
155+
156+ #ifdef _DBUFFER
157+ ApplyDecalToSurfaceData (unpacked.positionCS, surface, inputData);
158+ #endif
159+
160+ #if defined (MATERIAL_QUALITY_MEDIUM)
161+ half4 color = UniversalFragmentBlinnPhong (inputData, surface);
162+ #else
163+ half4 color = UniversalFragmentPBR (inputData, surface);
164+ #endif //MATERIAL_QUALITY_MEDIUM
165+ color.rgb = MixFog (color.rgb, inputData.fogCoord);
166+ color.a = OutputAlpha (color.a, isTransparent);
167+
168+ outColor = color;
169+
170+ #ifdef _WRITE_RENDERING_LAYERS
171+ uint renderingLayers = GetMeshRenderingLayer ();
172+ outRenderingLayers = float4 (EncodeMeshRenderingLayer (renderingLayers), 0 , 0 , 0 );
173+ #endif
174+ }
175+ #endif
0 commit comments