Advertisement
Guest User

Unity Stochastic Texture Sampling Example

a guest
Oct 14th, 2019
3,565
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/StochasticSample"
  2. {
  3. Properties
  4. {
  5. _Color ("Color", Color) = (1,1,1,1)
  6. _MainTex ("Albedo (RGB)", 2D) = "white" {}
  7. _BumpMap ("BumpMap", 2D) = "bump" {}
  8. _BumpScale ("BumpScale", Float) = 1
  9. _Smoothness ("Smoothness", 2D) = "white" {}
  10. [Toggle]_Stochastic("Stochastic", Float) = 0
  11. }
  12. SubShader
  13. {
  14. Tags { "RenderType"="Opaque" }
  15. LOD 200
  16.  
  17. CGPROGRAM
  18. // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices
  19. #pragma exclude_renderers gles
  20. // Physically based Standard lighting model, and enable shadows on all light types
  21. #pragma surface surf Standard fullforwardshadows
  22.  
  23. // Use shader model 3.0 target, to get nicer looking lighting
  24. #pragma target 3.0
  25.  
  26. fixed4 _Color;
  27.  
  28. sampler2D _MainTex;
  29. sampler2D _BumpMap;
  30. sampler2D _Smoothness;
  31.  
  32. uniform float _BumpScale;
  33.  
  34. uniform float _Stochastic;
  35.  
  36. struct Input
  37. {
  38. float2 uv_MainTex;
  39. };
  40.  
  41. // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
  42. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
  43. // #pragma instancing_options assumeuniformscaling
  44. UNITY_INSTANCING_BUFFER_START(Props)
  45. // put more per-instance properties here
  46. UNITY_INSTANCING_BUFFER_END(Props)
  47.  
  48. //hash for randomness
  49. float2 hash2D2D (float2 s)
  50. {
  51. //magic numbers
  52. return frac(sin(fmod(float2(dot(s, float2(127.1,311.7)), dot(s, float2(269.5,183.3))), 3.14159))*43758.5453);
  53. }
  54.  
  55. //stochastic sampling
  56. float4 tex2DStochastic(sampler2D tex, float2 UV)
  57. {
  58. //triangle vertices and blend weights
  59. //BW_vx[0...2].xyz = triangle verts
  60. //BW_vx[3].xy = blend weights (z is unused)
  61. float4x3 BW_vx;
  62.  
  63. //uv transformed into triangular grid space with UV scaled by approximation of 2*sqrt(3)
  64. float2 skewUV = mul(float2x2 (1.0 , 0.0 , -0.57735027 , 1.15470054), UV * 3.464);
  65.  
  66. //vertex IDs and barycentric coords
  67. float2 vxID = float2 (floor(skewUV));
  68. float3 barry = float3 (frac(skewUV), 0);
  69. barry.z = 1.0-barry.x-barry.y;
  70.  
  71. BW_vx = ((barry.z>0) ?
  72. float4x3(float3(vxID, 0), float3(vxID + float2(0, 1), 0), float3(vxID + float2(1, 0), 0), barry.zyx) :
  73. float4x3(float3(vxID + float2 (1, 1), 0), float3(vxID + float2 (1, 0), 0), float3(vxID + float2 (0, 1), 0), float3(-barry.z, 1.0-barry.y, 1.0-barry.x)));
  74.  
  75. //calculate derivatives to avoid triangular grid artifacts
  76. float2 dx = ddx(UV);
  77. float2 dy = ddy(UV);
  78.  
  79. //blend samples with calculated weights
  80. return mul(tex2D(tex, UV + hash2D2D(BW_vx[0].xy), dx, dy), BW_vx[3].x) +
  81. mul(tex2D(tex, UV + hash2D2D(BW_vx[1].xy), dx, dy), BW_vx[3].y) +
  82. mul(tex2D(tex, UV + hash2D2D(BW_vx[2].xy), dx, dy), BW_vx[3].z);
  83. }
  84.  
  85. void surf (Input IN, inout SurfaceOutputStandard o)
  86. {
  87. float4 bumpSample;
  88. float4 albedoSample = 1;
  89. float4 smoothnessSample;
  90.  
  91. if (_Stochastic)
  92. {
  93. albedoSample = tex2DStochastic(_MainTex, IN.uv_MainTex);
  94. bumpSample = tex2DStochastic(_BumpMap, IN.uv_MainTex);
  95. smoothnessSample = tex2DStochastic(_Smoothness, IN.uv_MainTex);
  96. //etc.
  97. }
  98. else
  99. {
  100. albedoSample = tex2D(_MainTex, IN.uv_MainTex);
  101. bumpSample = tex2D(_BumpMap, IN.uv_MainTex);
  102. smoothnessSample = tex2D(_Smoothness, IN.uv_MainTex);
  103. //etc.
  104. }
  105.  
  106. o.Alpha = albedoSample.a;
  107.  
  108. o.Albedo = albedoSample.rgb;
  109. o.Normal = UnpackScaleNormal(bumpSample, _BumpScale);
  110. o.Smoothness = smoothnessSample.r;
  111. //etc.
  112. }
  113. ENDCG
  114. }
  115. FallBack "Diffuse"
  116. }
Advertisement
RAW Paste Data Copied
Advertisement