Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "L02_URP/Grass"{
- Properties{
- _TopColor("Top Color", Color) = (1,1,1,1)
- _BottomColor("Bottom Color", Color) = (1,1,1,1)
- _TranslucentGain("Translucent Gain", Range(0,1)) = 0.5
- _BendRotationRandom("Bend Rotation Random", Range(0, 1)) = 0.2
- _BladeWidth("Blade Width", Float) = 0.05
- _BladeWidthRandom("Blade Width Random", Float) = 0.02
- _BladeHeight("Blade Height", Float) = 0.5
- _BladeHeightRandom("Blade Height Random", Float) = 0.3
- _TessellationUniform("Tessellation Uniform", Range(1, 64)) = 1
- _WindDistortionMap("Wind Distortion Map", 2D) = "white" {}
- _WindFrequency("Wind Frequency", Vector) = (0.05, 0.05, 0, 0)
- _WindStrength("Wind Strength", Float) = 1
- _BladeForward("Blade Forward Amount", Float) = 0.38
- _BladeCurve("Blade Curvature Amount", Range(1, 4)) = 2
- }
- SubShader{
- Tags{
- "RenderType" = "Opaque"
- "Queue" = "Geometry"
- "RenderPipeline" = "UniversalPipeline"
- }
- LOD 100
- Cull Off
- HLSLINCLUDE
- #pragma require geometry
- #pragma require tessellation tessHW
- #pragma vertex vert
- #pragma fragment frag
- #pragma hull hull
- #pragma domain domain
- #pragma geometry geo
- #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
- #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
- #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
- #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
- #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
- #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
- #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
- #pragma multi_compile _ _SHADOWS_SOFT
- #define UNITY_PI 3.14159265359f
- #define UNITY_TWO_PI 6.28318530718f
- #define BLADE_SEGMENTS 3
- CBUFFER_START(UnityPerMaterial)
- float4 _BaseColor;
- float4 _BaseMap_ST;
- float _BendRotationRandom;
- float _TessellationUniform;
- float _BladeHeight;
- float _BladeHeightRandom;
- float _BladeWidth;
- float _BladeWidthRandom;
- sampler2D _WindDistortionMap;
- float4 _WindDistortionMap_ST;
- float2 _WindFrequency;
- float _WindStrength;
- float _BladeForward;
- float _BladeCurve;
- float4 _TopColor;
- float4 _BottomColor;
- CBUFFER_END
- TEXTURE2D (_BaseMap);SAMPLER(sampler_BaseMap);
- struct vertexInput
- {
- float4 vertex : POSITION;
- float3 normal : NORMAL;
- float4 tangent : TANGENT;
- };
- struct vertexOutput
- {
- float4 vertex : SV_POSITION;
- float3 normal : NORMAL;
- float4 tangent : TANGENT;
- };
- struct TessellationFactors
- {
- float edge[3] : SV_TessFactor;
- float inside : SV_InsideTessFactor;
- };
- struct geometryOutput
- {
- float4 pos : SV_POSITION;
- float2 uv : TEXCOORD0;
- // unityShadowCoord4 _ShadowCoord : TEXCOORD1;
- float3 worldPos : TEXCOORD1;
- // float3 normal : NORMAL;
- float4 shadowCoord : TEXCOORD2;
- };
- /*
- Tools Function
- */
- float rand(float3 co){
- return frac(sin(dot(co.xyz, float3(12.9898, 78.233, 53.539))) * 43758.5453);
- }
- float3x3 AngleAxis3x3(float angle, float3 axis){
- float c, s;
- sincos(angle, s, c);
- float t = 1 - c;
- float x = axis.x;
- float y = axis.y;
- float z = axis.z;
- return float3x3(
- t * x * x + c, t * x * y - s * z, t * x * z + s * y,
- t * x * y + s * z, t * y * y + c, t * y * z - s * x,
- t * x * z - s * y, t * y * z + s * x, t * z * z + c
- );
- }
- /*
- Vertex Shader
- */
- vertexInput vert(vertexInput v){
- return v;
- }
- /*
- Tesselation Shader
- */
- vertexOutput tessVert(vertexInput v){
- vertexOutput o;
- o.vertex = v.vertex;
- o.normal = v.normal;
- o.tangent = v.tangent;
- return o;
- }
- TessellationFactors patchConstantFunction (InputPatch<vertexInput, 3> patch){
- TessellationFactors f;
- f.edge[0] = _TessellationUniform;
- f.edge[1] = _TessellationUniform;
- f.edge[2] = _TessellationUniform;
- f.inside = _TessellationUniform;
- return f;
- }
- [domain("tri")]
- [outputcontrolpoints(3)]
- [outputtopology("triangle_cw")]
- [partitioning("integer")]
- [patchconstantfunc("patchConstantFunction")]
- vertexInput hull (InputPatch<vertexInput, 3> patch, uint id : SV_OutputControlPointID){
- return patch[id];
- }
- [domain("tri")]
- vertexOutput domain(TessellationFactors factors, OutputPatch<vertexInput, 3> patch, float3 barycentricCoordinates : SV_DomainLocation){
- vertexInput v;
- #define MY_DOMAIN_PROGRAM_INTERPOLATE(fieldName) v.fieldName = \
- patch[0].fieldName * barycentricCoordinates.x + \
- patch[1].fieldName * barycentricCoordinates.y + \
- patch[2].fieldName * barycentricCoordinates.z;
- MY_DOMAIN_PROGRAM_INTERPOLATE(vertex)
- MY_DOMAIN_PROGRAM_INTERPOLATE(normal)
- MY_DOMAIN_PROGRAM_INTERPOLATE(tangent)
- return tessVert(v);
- }
- geometryOutput GenerateGrassVertex(float3 vertexPosition, float width, float height, float forward, float2 uv, float3x3 transformMatrix){
- float3 tangentPoint = float3(width, forward, height);
- float3 localPosition = vertexPosition + mul(transformMatrix, tangentPoint);
- float3 tangentNormal = normalize(float3(0, -1, forward));
- float3 localNormal = mul(transformMatrix, tangentNormal);
- float3 positionWS = TransformObjectToWorld(localPosition);
- geometryOutput o;
- o.pos = TransformObjectToHClip(localPosition);
- o.worldPos = TransformWorldToHClip(localPosition + mul(transformMatrix, tangentPoint)).xyz;
- o.uv = uv;
- // o.normal = TransformObjectToWorldNormal(localNormal);
- o.shadowCoord = TransformWorldToShadowCoord(positionWS);
- return o;
- }
- [maxvertexcount(BLADE_SEGMENTS * 2 + 1)]
- void geo(triangle vertexOutput IN[3], inout TriangleStream<geometryOutput> triStream){
- float3 vNormal = IN[0].normal;
- float4 vTangent = IN[0].tangent;
- float3 vBinormal = cross(vNormal, vTangent.xyz) * vTangent.w;
- float3x3 tangentToLocal = float3x3(
- vTangent.x, vBinormal.x, vNormal.x,
- vTangent.y, vBinormal.y, vNormal.y,
- vTangent.z, vBinormal.z, vNormal.z
- );
- float3 pos = IN[0].vertex.xyz;
- float3x3 facingRotationMatrix = AngleAxis3x3(rand(pos) * UNITY_TWO_PI, float3(0, 0, 1));
- float3x3 bendRotationMatrix = AngleAxis3x3(rand(pos.zzx) * _BendRotationRandom * UNITY_PI * 0.5, float3(-1, 0, 0));
- float2 uv = pos.xz * _WindDistortionMap_ST.xy + _WindDistortionMap_ST.zw + _WindFrequency * _Time.y;
- float2 windSample = (tex2Dlod(_WindDistortionMap, float4(uv, 0, 0)).xy * 2 - 1) * _WindStrength;
- float3 wind = normalize(float3(windSample.x, windSample.y, 0));
- float3x3 windRotation = AngleAxis3x3(UNITY_PI * windSample.x, wind);
- float3x3 transformationMatrix = mul(mul(mul(tangentToLocal, windRotation), facingRotationMatrix), bendRotationMatrix);
- float height = (rand(pos.zyx) * 2 - 1) * _BladeHeightRandom + _BladeHeight;
- float width = (rand(pos.xzy) * 2 - 1) * _BladeWidthRandom + _BladeWidth;
- float forward = rand(pos.yyz) * _BladeForward;
- float3x3 transformationMatrixFacing = mul(tangentToLocal, facingRotationMatrix);
- for (int i = 0; i < BLADE_SEGMENTS; i++){
- float t = i / (float)BLADE_SEGMENTS;
- float segmentHeight = height * t;
- float segmentWidth = width * (1 - t);
- float segmentForward = pow(t, _BladeCurve) * forward;
- float3x3 transformMatrix = i == 0 ? transformationMatrixFacing : transformationMatrix;
- triStream.Append(GenerateGrassVertex(pos, segmentWidth, segmentHeight, segmentForward, float2(0, t), transformMatrix));
- triStream.Append(GenerateGrassVertex(pos, -segmentWidth, segmentHeight, segmentForward, float2(1, t), transformMatrix));
- }
- triStream.Append(GenerateGrassVertex(pos, 0, height, forward, float2(0.5, 1), transformationMatrix));
- }
- ENDHLSL
- Pass{
- Name "GrassPass"
- Tags{ "LightMode" = "UniversalForward" }
- HLSLPROGRAM
- float4 frag (geometryOutput i) : SV_Target{
- Light mainLight = GetMainLight(i.shadowCoord);
- float4 shadow = lerp(0.0, 1.0, mainLight.shadowAttenuation + 0.25f);
- return shadow * lerp(_BottomColor, _TopColor, i.uv.y);
- }
- ENDHLSL
- }
- Pass{
- Name "ShadowCaster"
- Tags{ "LightMode" = "ShadowCaster" }
- ZWrite On
- ZTest LEqual
- HLSLPROGRAM
- half4 frag(geometryOutput input) : SV_TARGET{
- return 1;
- }
- ENDHLSL
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement