Unity-NorthStar / data /Packages /com.unity.render-pipelines.universal /Editor /ShaderGraph /Includes /ShaderPassDecal.hlsl
| void MeshDecalsPositionZBias(inout Varyings input) | |
| { | |
| input.positionCS.z -= _DecalMeshDepthBias; | |
| input.positionCS.z += _DecalMeshDepthBias; | |
| } | |
| void InitializeInputData(Varyings input, float3 positionWS, half3 normalWS, half3 viewDirectionWS, out InputData inputData) | |
| { | |
| inputData = (InputData)0; | |
| inputData.positionWS = positionWS; | |
| inputData.normalWS = normalWS; | |
| inputData.viewDirectionWS = viewDirectionWS; | |
| inputData.shadowCoord = input.shadowCoord; | |
| inputData.shadowCoord = TransformWorldToShadowCoord(positionWS); | |
| inputData.shadowCoord = float4(0, 0, 0, 0); | |
| inputData.fogCoord = InitializeInputDataFog(float4(positionWS, 1.0), input.fogFactorAndVertexLight.x); | |
| inputData.vertexLighting = input.fogFactorAndVertexLight.yzw; | |
| inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV.xy, half3(input.sh), normalWS); | |
| inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV); | |
| inputData.bakedGI = SAMPLE_GI(input.sh, | |
| GetAbsolutePositionWS(inputData.positionWS), | |
| inputData.normalWS, | |
| inputData.viewDirectionWS, | |
| input.positionCS.xy, | |
| input.probeOcclusion, | |
| inputData.shadowMask); | |
| inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, half3(input.sh), normalWS); | |
| inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV); | |
| inputData.dynamicLightmapUV = input.dynamicLightmapUV.xy; | |
| inputData.staticLightmapUV = input.staticLightmapUV; | |
| inputData.vertexSH = input.sh; | |
| inputData.probeOcclusion = input.probeOcclusion; | |
| inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS); | |
| } | |
| void GetSurface(DecalSurfaceData decalSurfaceData, inout SurfaceData surfaceData) | |
| { | |
| surfaceData.albedo = decalSurfaceData.baseColor.rgb; | |
| surfaceData.metallic = saturate(decalSurfaceData.metallic); | |
| surfaceData.specular = 0; | |
| surfaceData.smoothness = saturate(decalSurfaceData.smoothness); | |
| surfaceData.occlusion = decalSurfaceData.occlusion; | |
| surfaceData.emission = decalSurfaceData.emissive; | |
| surfaceData.alpha = saturate(decalSurfaceData.baseColor.w); | |
| surfaceData.clearCoatMask = 0; | |
| surfaceData.clearCoatSmoothness = 1; | |
| } | |
| PackedVaryings Vert(Attributes inputMesh) | |
| { | |
| Varyings output = (Varyings)0; | |
| UNITY_SETUP_INSTANCE_ID(inputMesh); | |
| UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output); | |
| if (_DecalMeshBiasType == DECALMESHDEPTHBIASTYPE_VIEW_BIAS) // TODO: Check performance of branch | |
| { | |
| float3 viewDirectionOS = GetObjectSpaceNormalizeViewDir(inputMesh.positionOS); | |
| inputMesh.positionOS += viewDirectionOS * (_DecalMeshViewBias); | |
| } | |
| output = BuildVaryings(inputMesh); | |
| if (_DecalMeshBiasType == DECALMESHDEPTHBIASTYPE_DEPTH_BIAS) // TODO: Check performance of branch | |
| { | |
| MeshDecalsPositionZBias(output); | |
| } | |
| output = BuildVaryings(inputMesh); | |
| OUTPUT_LIGHTMAP_UV(inputMesh.uv1, unity_LightmapST, output.staticLightmapUV); | |
| output.dynamicLightmapUV.xy = inputMesh.uv2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw; | |
| output.sh = float3(SampleSHVertex(half3(output.normalWS))); | |
| PackedVaryings packedOutput = (PackedVaryings)0; | |
| packedOutput = PackVaryings(output); | |
| return packedOutput; | |
| } | |
| void Frag(PackedVaryings packedInput, | |
| #if defined(DECAL_DBUFFER) | |
| OUTPUT_DBUFFER(outDBuffer) | |
| #elif defined(DECAL_SCREEN_SPACE) | |
| out half4 outColor : SV_Target0 | |
| #elif defined(DECAL_GBUFFER) | |
| out FragmentOutput fragmentOutput | |
| #elif defined(DECAL_FORWARD_EMISSIVE) | |
| out half4 outEmissive : SV_Target0 | |
| #elif defined(SCENEPICKINGPASS) | |
| out float4 outColor : SV_Target0 | |
| #else | |
| #error SHADERPASS_is_not_correctly_define | |
| #endif | |
| ) | |
| { | |
| outColor = _SelectionID; | |
| UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(packedInput); | |
| UNITY_SETUP_INSTANCE_ID(packedInput); | |
| Varyings input = UnpackVaryings(packedInput); | |
| half angleFadeFactor = 1.0; | |
| float2 positionCS = input.positionCS.xy; | |
| // Only screen space needs flip logic, other passes do not setup needed properties so we skip here | |
| TransformScreenUV(positionCS, _ScreenSize.y); | |
| uint surfaceRenderingLayer = DecodeMeshRenderingLayer(LOAD_FRAMEBUFFER_X_INPUT(GBUFFER4, positionCS.xy).r); | |
| uint surfaceRenderingLayer = LoadSceneRenderingLayer(positionCS.xy); | |
| uint projectorRenderingLayer = uint(UNITY_ACCESS_INSTANCED_PROP(Decal, _DecalLayerMaskFromDecal)); | |
| // This is simple trick to clip if there is no matching layers | |
| // Part (surfaceRenderingLayer & projectorRenderingLayer) will produce 0, 1, 2 ... | |
| // Finally we subtract with small value to remmap only zero to negative value | |
| clip((surfaceRenderingLayer & projectorRenderingLayer) - 0.1); | |
| float depth = LOAD_FRAMEBUFFER_X_INPUT(GBUFFER3, positionCS.xy).x; | |
| float depth = LoadSceneDepth(positionCS.xy); | |
| float depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, LOAD_FRAMEBUFFER_X_INPUT(GBUFFER3, positionCS.xy)); | |
| // Adjust z to match NDC for OpenGL | |
| float depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, LoadSceneDepth(positionCS.xy)); | |
| half3 normalWS = half3(ReconstructNormalTap9(positionCS.xy)); | |
| half3 normalWS = half3(ReconstructNormalTap5(positionCS.xy)); | |
| half3 normalWS = half3(ReconstructNormalDerivative(input.positionCS.xy)); | |
| half3 normalWS = half3(LoadSceneNormals(positionCS.xy)); | |
| float2 positionSS = FoveatedRemapNonUniformToLinearCS(input.positionCS.xy) * _ScreenSize.zw; | |
| float3 positionWS = ComputeWorldSpacePosition(positionSS, depth, UNITY_MATRIX_I_VP); | |
| input.positionWS = positionWS; | |
| // Transform from relative world space to decal space (DS) to clip the decal | |
| float3 positionDS = TransformWorldToObject(positionWS); | |
| positionDS = positionDS * float3(1.0, -1.0, 1.0); | |
| // call clip as early as possible | |
| float clipValue = 0.5 - Max3(abs(positionDS).x, abs(positionDS).y, abs(positionDS).z); | |
| clip(clipValue); | |
| float2 texCoord = positionDS.xz + float2(0.5, 0.5); | |
| input.texCoord0.xy = texCoord; | |
| input.texCoord1.xy = texCoord; | |
| input.texCoord2.xy = texCoord; | |
| input.texCoord3.xy = texCoord; | |
| // Check if this decal projector require angle fading | |
| half4x4 normalToWorld = UNITY_ACCESS_INSTANCED_PROP(Decal, _NormalToWorld); | |
| half2 angleFade = half2(normalToWorld[1][3], normalToWorld[2][3]); | |
| if (angleFade.y < 0.0f) // if angle fade is enabled | |
| { | |
| half3 decalNormal = half3(normalToWorld[0].z, normalToWorld[1].z, normalToWorld[2].z); | |
| half dotAngle = dot(normalWS, decalNormal); | |
| // See equation in DecalCreateDrawCallSystem.cs - simplified to a madd mul add here | |
| angleFadeFactor = saturate(angleFade.x + angleFade.y * (dotAngle * (dotAngle - 2.0))); | |
| } | |
| float3 positionWS = input.positionWS.xyz; | |
| half3 viewDirectionWS = GetWorldSpaceNormalizeViewDir(positionWS); | |
| DecalSurfaceData surfaceData; | |
| GetSurfaceData(input, input.positionCS, angleFadeFactor, surfaceData); | |
| ENCODE_INTO_DBUFFER(surfaceData, outDBuffer); | |
| // Blend normal with background | |
| surfaceData.normalWS.xyz = normalize(lerp(normalWS.xyz, surfaceData.normalWS.xyz, surfaceData.normalWS.w)); | |
| InputData inputData; | |
| InitializeInputData(input, positionWS, surfaceData.normalWS.xyz, viewDirectionWS, inputData); | |
| SurfaceData surface = (SurfaceData)0; | |
| GetSurface(surfaceData, surface); | |
| half4 color = UniversalFragmentPBR(inputData, surface); | |
| color.rgb = MixFog(color.rgb, inputData.fogCoord); | |
| outColor = color; | |
| // Need to reconstruct normal here for inputData.bakedGI, but also save off surfaceData.normalWS for correct GBuffer blending | |
| half3 normalToPack = surfaceData.normalWS.xyz; | |
| surfaceData.normalWS.xyz = normalize(lerp(normalWS.xyz, surfaceData.normalWS.xyz, surfaceData.normalWS.w)); | |
| InputData inputData; | |
| InitializeInputData(input, positionWS, surfaceData.normalWS.xyz, viewDirectionWS, inputData); | |
| SurfaceData surface = (SurfaceData)0; | |
| GetSurface(surfaceData, surface); | |
| BRDFData brdfData; | |
| InitializeBRDFData(surface.albedo, surface.metallic, 0, surface.smoothness, surface.alpha, brdfData); | |
| // Skip GI if there is no abledo | |
| Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask); | |
| MixRealtimeAndBakedGI(mainLight, surfaceData.normalWS.xyz, inputData.bakedGI, inputData.shadowMask); | |
| half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surface.occlusion, surfaceData.normalWS.xyz, inputData.viewDirectionWS); | |
| half3 color = 0; | |
| // We can not use usual GBuffer functions (etc. BRDFDataToGbuffer) as we use alpha for blending | |
| half3 packedNormalWS = PackNormal(normalToPack); | |
| fragmentOutput.GBuffer0 = half4(surfaceData.baseColor.rgb, surfaceData.baseColor.a); | |
| fragmentOutput.GBuffer1 = 0; | |
| fragmentOutput.GBuffer2 = half4(packedNormalWS, surfaceData.normalWS.a); | |
| fragmentOutput.GBuffer3 = half4(surfaceData.emissive + color, surfaceData.baseColor.a); | |
| fragmentOutput.GBuffer4 = inputData.shadowMask; // will have unity_ProbesOcclusion value if subtractive lighting is used (baked) | |
| // Emissive need to be pre-exposed | |
| outEmissive.rgb = surfaceData.emissive * GetCurrentExposureMultiplier(); | |
| outEmissive.a = surfaceData.baseColor.a; | |
| } | |