Guest User

BasicTessellated.shader

a guest
Dec 23rd, 2020
5,622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This shader adds tessellation in URP
  2. Shader "Example/URPUnlitShaderTessallated"
  3. {
  4.  
  5.     // The properties block of the Unity shader. In this example this block is empty
  6.     // because the output color is predefined in the fragment shader code.
  7.     Properties
  8.     {
  9.         _Tess("Tessellation", Range(1, 32)) = 20
  10.         _MaxTessDistance("Max Tess Distance", Range(1, 32)) = 20
  11.         _Noise("Noise", 2D) = "gray" {}
  12.  
  13.     _Weight("Displacement Amount", Range(0, 1)) = 0
  14.     }
  15.  
  16.         // The SubShader block containing the Shader code.
  17.         SubShader
  18.     {
  19.         // SubShader Tags define when and under which conditions a SubShader block or
  20.         // a pass is executed.
  21.         Tags{ "RenderType" = "Opaque" "RenderPipeline" = "UniversalRenderPipeline" }
  22.  
  23.         Pass
  24.     {
  25.         Tags{ "LightMode" = "UniversalForward" }
  26.  
  27.  
  28.         // The HLSL code block. Unity SRP uses the HLSL language.
  29.         HLSLPROGRAM
  30.         // The Core.hlsl file contains definitions of frequently used HLSL
  31.         // macros and functions, and also contains #include references to other
  32.         // HLSL files (for example, Common.hlsl, SpaceTransforms.hlsl, etc.).
  33. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"    
  34. #include "CustomTessellation.hlsl"
  35.  
  36.  
  37. #pragma require tessellation
  38.         // This line defines the name of the vertex shader.
  39. #pragma vertex TessellationVertexProgram
  40.         // This line defines the name of the fragment shader.
  41. #pragma fragment frag
  42.         // This line defines the name of the hull shader.
  43. #pragma hull hull
  44.         // This line defines the name of the domain shader.
  45. #pragma domain domain
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.         sampler2D _Noise;
  53.     float _Weight;
  54.  
  55.     // pre tesselation vertex program
  56.     ControlPoint TessellationVertexProgram(Attributes v)
  57.     {
  58.         ControlPoint p;
  59.  
  60.         p.vertex = v.vertex;
  61.         p.uv = v.uv;
  62.         p.normal = v.normal;
  63.         p.color = v.color;
  64.  
  65.         return p;
  66.     }
  67.  
  68.     // after tesselation
  69.     Varyings vert(Attributes input)
  70.     {
  71.         Varyings output;
  72.         float Noise = tex2Dlod(_Noise, float4(input.uv, 0, 0)).r;
  73.  
  74.         input.vertex.xyz += (input.normal) *  Noise * _Weight;
  75.         output.vertex = TransformObjectToHClip(input.vertex.xyz);
  76.         output.color = input.color;
  77.         output.normal = input.normal;
  78.         output.uv = input.uv;
  79.         return output;
  80.     }
  81.  
  82.     [UNITY_domain("tri")]
  83.     Varyings domain(TessellationFactors factors, OutputPatch<ControlPoint, 3> patch, float3 barycentricCoordinates : SV_DomainLocation)
  84.     {
  85.         Attributes v;
  86.  
  87. #define DomainPos(fieldName) v.fieldName = \
  88.                 patch[0].fieldName * barycentricCoordinates.x + \
  89.                 patch[1].fieldName * barycentricCoordinates.y + \
  90.                 patch[2].fieldName * barycentricCoordinates.z;
  91.  
  92.             DomainPos(vertex)
  93.             DomainPos(uv)
  94.             DomainPos(color)
  95.             DomainPos(normal)
  96.  
  97.             return vert(v);
  98.     }
  99.  
  100.     // The fragment shader definition.            
  101.     half4 frag(Varyings IN) : SV_Target
  102.     {
  103.         half4 tex = tex2D(_Noise, IN.uv);
  104.  
  105.         return tex;
  106.     }
  107.         ENDHLSL
  108.     }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment