124 lines
4.9 KiB
HLSL
124 lines
4.9 KiB
HLSL
#ifndef GRASS_URP_INCLUDES
|
|
#define GRASS_URP_INCLUDES
|
|
|
|
#define _SPECULAR_SETUP
|
|
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
|
|
//#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
|
|
|
|
// Define stuff from the builtin pipeline
|
|
|
|
#define SHADOW_COORDS(idx1) float4 shadowCoord : TEXCOORD##idx1;
|
|
#define UNITY_FOG_COORDS(idx) float fogCoord : TEXCOORD##idx;
|
|
|
|
#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
|
|
|
|
// 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, unity_CameraWorldClipPlanes[0]) > -cullEps) ? 1.0f : 0.0f ) +
|
|
(( UnityDistanceFromPlane(wpos1, unity_CameraWorldClipPlanes[0]) > -cullEps) ? 1.0f : 0.0f ) +
|
|
(( UnityDistanceFromPlane(wpos2, unity_CameraWorldClipPlanes[0]) > -cullEps) ? 1.0f : 0.0f );
|
|
// right
|
|
planeTest.y = (( UnityDistanceFromPlane(wpos0, unity_CameraWorldClipPlanes[1]) > -cullEps) ? 1.0f : 0.0f ) +
|
|
(( UnityDistanceFromPlane(wpos1, unity_CameraWorldClipPlanes[1]) > -cullEps) ? 1.0f : 0.0f ) +
|
|
(( UnityDistanceFromPlane(wpos2, unity_CameraWorldClipPlanes[1]) > -cullEps) ? 1.0f : 0.0f );
|
|
// top
|
|
planeTest.z = (( UnityDistanceFromPlane(wpos0, unity_CameraWorldClipPlanes[2]) > -cullEps) ? 1.0f : 0.0f ) +
|
|
(( UnityDistanceFromPlane(wpos1, unity_CameraWorldClipPlanes[2]) > -cullEps) ? 1.0f : 0.0f ) +
|
|
(( UnityDistanceFromPlane(wpos2, unity_CameraWorldClipPlanes[2]) > -cullEps) ? 1.0f : 0.0f );
|
|
// bottom
|
|
planeTest.w = (( UnityDistanceFromPlane(wpos0, unity_CameraWorldClipPlanes[3]) > -cullEps) ? 1.0f : 0.0f ) +
|
|
(( UnityDistanceFromPlane(wpos1, unity_CameraWorldClipPlanes[3]) > -cullEps) ? 1.0f : 0.0f ) +
|
|
(( UnityDistanceFromPlane(wpos2, unity_CameraWorldClipPlanes[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 "GrassLightingURP.hlsl"
|
|
#include "GrassFragURP.hlsl"
|
|
|
|
#endif |