Skip to content

Instantly share code, notes, and snippets.

@psuong
Created August 19, 2020 21:59
Show Gist options
  • Save psuong/fdcc3f4ee9282ad354cf7f94ba1b3bfe to your computer and use it in GitHub Desktop.
Save psuong/fdcc3f4ee9282ad354cf7f94ba1b3bfe to your computer and use it in GitHub Desktop.
ClippedOpaque.hlsl
#ifndef CLIPPED_OPAQUE
#define CLIPPED_OPAQUE
#include "VertexHelpers.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
StructuredBuffer<float4x4> matrixBuffer;
struct Attributes
{
half4 positionOS : POSITION;
half3 normalOS : NORMAL;
half4 tangentOS : TANGENT;
half2 uv : TEXCOORD0;
half2 uvLM : TEXCOORD1;
};
struct Varyings
{
half2 uv : TEXCOORD0;
half2 uvLM : TEXCOORD1;
half4 positionWSAndFogFactor : TEXCOORD2; // xyz: positionWS, w: vertex fog factor
half3 normalWS : TEXCOORD3;
#ifdef _MAIN_LIGHT_SHADOWS
half4 shadowCoord : TEXCOORD6; // compute shadow coord per-vertex for the main light
#endif
half4 positionCS : SV_POSITION;
};
Varyings LitVertexPass(Attributes input, uint instanceID : SV_InstanceID)
{
Varyings output;
float4x4 localToWorld = matrixBuffer[instanceID];
VertexPositionInputs vertexInput = GetVertPosInputs(localToWorld, input.positionOS);
VertexNormalInputs vertexNormalInput = GetVertNorm(localToWorld, input.normalOS, input.tangentOS);
float fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
output.uvLM = input.uvLM.xy * unity_LightmapST.xy + unity_LightmapST.zw;
output.positionWSAndFogFactor = float4(vertexInput.positionWS, fogFactor);
output.normalWS = vertexNormalInput.normalWS;
#ifdef _MAIN_LIGHT_SHADOWS
output.shadowCoord = TransformWorldToShadowCoord(output.positionWSAndFogFactor.xyz);
#endif
output.positionCS = vertexInput.positionCS;
return output;
}
half4 LitFragmentPass(Varyings input) : SV_Target
{
SurfaceData surfaceData;
InitializeStandardLitSurfaceData(input.uv, surfaceData);
half3 normalWS = input.normalWS;
normalWS = normalize(normalWS);
#ifdef LIGHTMAP_ON
half3 bakedGI = SampleLightmap(input.uvLM, normalWS);
#else
half3 bakedGI = SampleSH(normalWS);
#endif
float3 positionWS = input.positionWSAndFogFactor.xyz;
half3 viewDirectionWS = SafeNormalize(GetCameraPositionWS() - positionWS);
BRDFData brdfData;
InitializeBRDFData(
surfaceData.albedo,
surfaceData.metallic,
surfaceData.specular,
surfaceData.smoothness,
surfaceData.alpha,
brdfData);
#ifdef _MAIN_LIGHT_SHADOWS
Light mainLight = GetMainLight(input.shadowCoord);
#else
Light mainLight = GetMainLight();
#endif
half3 color = GlobalIllumination(brdfData, bakedGI, surfaceData.occlusion, normalWS, viewDirectionWS);
color += LightingPhysicallyBased(brdfData, mainLight, normalWS, viewDirectionWS);
// Emission
color += surfaceData.emission;
float fogFactor = input.positionWSAndFogFactor.w;
color = MixFog(color, fogFactor);
return half4(color, 1);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment