Files
LowPolyRPG/Assets/StixGames/GrassShader/GrassIncludesHDRP.hlsl
Caleb Sandford deQuincey 715fb68744 Initial commitment
2025-06-25 11:10:11 +01:00

136 lines
5.2 KiB
HLSL

#ifndef GRASS_HDRP_INCLUDES
#define GRASS_HDRP_INCLUDES
#define _SPECULAR_SETUP
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialGBufferMacros.hlsl"
// Define stuff from the builtin pipeline
#undef unity_ObjectToWorld
#define unity_ObjectToWorld UNITY_MATRIX_M
#undef unity_WorldToObject
#define unity_WorldToObject UNITY_MATRIX_I_M
#if defined(UNITY_COMPILER_HLSL) || defined(SHADER_API_PSSL) || defined(UNITY_COMPILER_HLSLCC)
#define UNITY_INITIALIZE_OUTPUT(type,name) name = (type)0;
#else
#define UNITY_INITIALIZE_OUTPUT(type,name)
#endif
// Tranforms position from object to homogenous space
inline float4 UnityObjectToClipPos(in float3 pos)
{
// More efficient than computing M*VP matrix product
return mul(_ViewProjMatrix, mul(unity_ObjectToWorld, float4(pos, 1.0)));
}
inline float4 UnityObjectToClipPos(float4 pos) // overload for float4; avoids "implicit truncation" warning for existing shaders
{
return UnityObjectToClipPos(pos.xyz);
}
// Tessellation utility
// ---- utility functions
float UnityCalcDistanceTessFactor (float4 vertex, float minDist, float maxDist, float tess)
{
float3 wpos = mul(unity_ObjectToWorld,vertex).xyz;
float dist = distance (wpos, _WorldSpaceCameraPos);
float f = clamp(1.0 - (dist - minDist) / (maxDist - minDist), 0.01, 1.0) * tess;
return f;
}
float4 UnityCalcTriEdgeTessFactors (float3 triVertexFactors)
{
float4 tess;
tess.x = 0.5 * (triVertexFactors.y + triVertexFactors.z);
tess.y = 0.5 * (triVertexFactors.x + triVertexFactors.z);
tess.z = 0.5 * (triVertexFactors.x + triVertexFactors.y);
tess.w = (triVertexFactors.x + triVertexFactors.y + triVertexFactors.z) / 3.0f;
return tess;
}
float UnityCalcEdgeTessFactor (float3 wpos0, float3 wpos1, float edgeLen)
{
// distance to edge center
float dist = distance (0.5 * (wpos0+wpos1), _WorldSpaceCameraPos);
// length of the edge
float len = distance(wpos0, wpos1);
// edgeLen is approximate desired size in pixels
float f = max(len * _ScreenParams.y / (edgeLen * dist), 1.0);
return f;
}
float UnityDistanceFromPlane (float3 pos, float4 plane)
{
float d = dot (float4(pos,1.0f), plane);
return d;
}
// Returns true if triangle with given 3 world positions is outside of camera's view frustum.
// cullEps is distance outside of frustum that is still considered to be inside (i.e. max displacement)
bool UnityWorldViewFrustumCull (float3 wpos0, float3 wpos1, float3 wpos2, float cullEps)
{
float4 planeTest;
// left
planeTest.x = (( UnityDistanceFromPlane(wpos0, _FrustumPlanes[0]) > -cullEps) ? 1.0f : 0.0f ) +
(( UnityDistanceFromPlane(wpos1, _FrustumPlanes[0]) > -cullEps) ? 1.0f : 0.0f ) +
(( UnityDistanceFromPlane(wpos2, _FrustumPlanes[0]) > -cullEps) ? 1.0f : 0.0f );
// right
planeTest.y = (( UnityDistanceFromPlane(wpos0, _FrustumPlanes[1]) > -cullEps) ? 1.0f : 0.0f ) +
(( UnityDistanceFromPlane(wpos1, _FrustumPlanes[1]) > -cullEps) ? 1.0f : 0.0f ) +
(( UnityDistanceFromPlane(wpos2, _FrustumPlanes[1]) > -cullEps) ? 1.0f : 0.0f );
// top
planeTest.z = (( UnityDistanceFromPlane(wpos0, _FrustumPlanes[2]) > -cullEps) ? 1.0f : 0.0f ) +
(( UnityDistanceFromPlane(wpos1, _FrustumPlanes[2]) > -cullEps) ? 1.0f : 0.0f ) +
(( UnityDistanceFromPlane(wpos2, _FrustumPlanes[2]) > -cullEps) ? 1.0f : 0.0f );
// bottom
planeTest.w = (( UnityDistanceFromPlane(wpos0, _FrustumPlanes[3]) > -cullEps) ? 1.0f : 0.0f ) +
(( UnityDistanceFromPlane(wpos1, _FrustumPlanes[3]) > -cullEps) ? 1.0f : 0.0f ) +
(( UnityDistanceFromPlane(wpos2, _FrustumPlanes[3]) > -cullEps) ? 1.0f : 0.0f );
// has to pass all 4 plane tests to be visible
return !all (planeTest);
}
#define UNITY_domain domain
#define UNITY_partitioning partitioning
#define UNITY_outputtopology outputtopology
#define UNITY_patchconstantfunc patchconstantfunc
#define UNITY_outputcontrolpoints outputcontrolpoints
// Transforms normal from object to world space
inline float3 UnityObjectToWorldNormal( in float3 norm )
{
// mul(IT_M, norm) => mul(norm, I_M) => {dot(norm, I_M.col0), dot(norm, I_M.col1), dot(norm, I_M.col2)}
return normalize(mul(norm, (float3x3)unity_WorldToObject));
}
// ================= Surface shader ======================
struct SurfaceOutputStandardSpecular
{
half3 Albedo; // diffuse color
half3 Specular; // specular color
float3 Normal; // tangent space normal, if written
half3 Emission;
half Smoothness; // 0=rough, 1=smooth
half Occlusion; // occlusion (default 1)
half Alpha; // alpha for transparencies
};
// Grass includes
#include "GrassConfig.hlsl"
#include "GrassDefinitionsAndFunctions.hlsl"
#include "GrassVertex.hlsl"
#include "GrassTessellation.hlsl"
#include "GrassGeom.hlsl"
#include "GrassSurface.hlsl"
#include "GrassFragHDRP.hlsl"
#endif