Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
6.7 KiB

  1. // Made with Amplify Shader Editor
  2. // Available at the Unity Asset Store - http://u3d.as/y3X
  3. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  4. Shader "Hidden/PostProcess/Pixelize/CombineLayers"
  5. {
  6. Properties
  7. {
  8. _MainTex ( "Screen", 2D ) = "black" {}
  9. [Toggle(_INTERPOLATE_LAYERS_ON)] _INTERPOLATE_LAYERS("INTERPOLATE_LAYERS", Float) = 0
  10. [Toggle(_DEBUG_MASK_ON)] _DEBUG_MASK("DEBUG_MASK", Float) = 0
  11. }
  12. SubShader
  13. {
  14. ZTest Always
  15. Cull Off
  16. ZWrite Off
  17. Pass
  18. {
  19. CGPROGRAM
  20. #pragma vertex vert_img_custom
  21. #pragma fragment frag
  22. #pragma target 3.0
  23. #include "UnityCG.cginc"
  24. #pragma shader_feature _DEBUG_MASK_ON
  25. #pragma shader_feature _INTERPOLATE_LAYERS_ON
  26. struct appdata_img_custom
  27. {
  28. float4 vertex : POSITION;
  29. half2 texcoord : TEXCOORD0;
  30. };
  31. struct v2f_img_custom
  32. {
  33. float4 pos : SV_POSITION;
  34. half2 uv : TEXCOORD0;
  35. half2 stereoUV : TEXCOORD2;
  36. #if UNITY_UV_STARTS_AT_TOP
  37. half4 uv2 : TEXCOORD1;
  38. half4 stereoUV2 : TEXCOORD3;
  39. #endif
  40. };
  41. uniform sampler2D _MainTex;
  42. uniform half4 _MainTex_TexelSize;
  43. uniform half4 _MainTex_ST;
  44. uniform int _MaxPixelizationLevel;
  45. float4 CombineLayers148( sampler2D ScreenAndMaskTexture , float2 UV , int MaxLayers , out float SphereMask )
  46. {
  47. float4 color = tex2D(ScreenAndMaskTexture, UV);
  48. SphereMask = color.w;
  49. const float layerWidth = rcp(MaxLayers);
  50. float previousThreshold = 0.0f;
  51. for (int i = 1; i <= MaxLayers; ++i)
  52. {
  53. float4 layerColor = tex2Dlod(ScreenAndMaskTexture, float4(UV, 0, i));
  54. float newThreshold = i * layerWidth;
  55. float mask;
  56. #if defined(_INTERPOLATE_LAYERS_ON)
  57. mask = saturate((layerColor.w - previousThreshold) / (newThreshold - previousThreshold)); // clamped inverse lerp
  58. previousThreshold = newThreshold;
  59. #else
  60. mask = step(newThreshold, layerColor.w);
  61. #endif
  62. color = lerp(color, layerColor, mask);
  63. }
  64. return color;
  65. }
  66. v2f_img_custom vert_img_custom ( appdata_img_custom v )
  67. {
  68. v2f_img_custom o;
  69. o.pos = UnityObjectToClipPos ( v.vertex );
  70. o.uv = float4( v.texcoord.xy, 1, 1 );
  71. #if UNITY_UV_STARTS_AT_TOP
  72. o.uv2 = float4( v.texcoord.xy, 1, 1 );
  73. o.stereoUV2 = UnityStereoScreenSpaceUVAdjust ( o.uv2, _MainTex_ST );
  74. if ( _MainTex_TexelSize.y < 0.0 )
  75. o.uv.y = 1.0 - o.uv.y;
  76. #endif
  77. o.stereoUV = UnityStereoScreenSpaceUVAdjust ( o.uv, _MainTex_ST );
  78. return o;
  79. }
  80. half4 frag ( v2f_img_custom i ) : SV_Target
  81. {
  82. #ifdef UNITY_UV_STARTS_AT_TOP
  83. half2 uv = i.uv2;
  84. half2 stereoUV = i.stereoUV2;
  85. #else
  86. half2 uv = i.uv;
  87. half2 stereoUV = i.stereoUV;
  88. #endif
  89. half4 finalColor;
  90. // ase common template code
  91. sampler2D ScreenAndMaskTexture148 = _MainTex;
  92. float2 UV148 = i.uv.xy;
  93. int MaxLayers148 = _MaxPixelizationLevel;
  94. float SphereMask148 = 0.0;
  95. float4 localCombineLayers148 = CombineLayers148( ScreenAndMaskTexture148 , UV148 , MaxLayers148 , SphereMask148 );
  96. #ifdef _INTERPOLATE_LAYERS_ON
  97. float4 staticSwitch153 = localCombineLayers148;
  98. #else
  99. float4 staticSwitch153 = localCombineLayers148;
  100. #endif
  101. float3 temp_cast_0 = (SphereMask148).xxx;
  102. float3 temp_cast_1 = (SphereMask148).xxx;
  103. float3 gammaToLinear154 = GammaToLinearSpace( temp_cast_1 );
  104. #ifdef _DEBUG_MASK_ON
  105. float4 staticSwitch145 = float4( gammaToLinear154 , 0.0 );
  106. #else
  107. float4 staticSwitch145 = staticSwitch153;
  108. #endif
  109. finalColor = staticSwitch145;
  110. return finalColor;
  111. }
  112. ENDCG
  113. }
  114. }
  115. CustomEditor "ASEMaterialInspector"
  116. }
  117. /*ASEBEGIN
  118. Version=16304
  119. 2027;92;856;740;-1823.932;1188.114;1.696211;False;False
  120. Node;AmplifyShaderEditor.TexCoordVertexDataNode;2;2008.631,-476.6076;Float;False;0;2;0;5;FLOAT2;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
  121. Node;AmplifyShaderEditor.TemplateShaderPropertyNode;1;2040.631,-572.6075;Float;False;0;0;_MainTex;Shader;0;5;SAMPLER2D;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
  122. Node;AmplifyShaderEditor.IntNode;151;1960.632,-332.6076;Float;False;Global;_MaxPixelizationLevel;_MaxPixelizationLevel;1;0;Create;True;0;0;False;0;5;5;0;1;INT;0
  123. Node;AmplifyShaderEditor.CustomExpressionNode;148;2280.631,-508.6076;Float;False;float4 color = tex2D(ScreenAndMaskTexture, UV)@$SphereMask = color.w@$const float layerWidth = rcp(MaxLayers)@$float previousThreshold = 0.0f@$$for (int i = 1@ i <= MaxLayers@ ++i)${$ float4 layerColor = tex2Dlod(ScreenAndMaskTexture, float4(UV, 0, i))@$ float newThreshold = i * layerWidth@$ float mask@$ #if defined(_INTERPOLATE_LAYERS_ON)$ mask = saturate((layerColor.w - previousThreshold) / (newThreshold - previousThreshold))@ // clamped inverse lerp $ previousThreshold = newThreshold@$ #else$ mask = step(newThreshold, layerColor.w)@$ #endif$ color = lerp(color, layerColor, mask)@$}$$return color@;4;False;4;True;ScreenAndMaskTexture;SAMPLER2D;;In;;Float;True;UV;FLOAT2;0,0;In;;Float;True;MaxLayers;INT;5;In;;Float;True;SphereMask;FLOAT;0;Out;;Float;Combine Layers;True;False;0;4;0;SAMPLER2D;;False;1;FLOAT2;0,0;False;2;INT;5;False;3;FLOAT;0;False;2;FLOAT4;0;FLOAT;4
  124. Node;AmplifyShaderEditor.GammaToLinearNode;154;2819.606,-438.3885;Float;False;0;1;0;FLOAT3;0,0,0;False;1;FLOAT3;0
  125. Node;AmplifyShaderEditor.StaticSwitch;153;2762.428,-549.3163;Float;False;Property;_INTERPOLATE_LAYERS;INTERPOLATE_LAYERS;0;0;Create;True;0;0;False;0;0;0;0;True;;Toggle;2;Key0;Key1;9;1;FLOAT4;0,0,0,0;False;0;FLOAT4;0,0,0,0;False;2;FLOAT4;0,0,0,0;False;3;FLOAT4;0,0,0,0;False;4;FLOAT4;0,0,0,0;False;5;FLOAT4;0,0,0,0;False;6;FLOAT4;0,0,0,0;False;7;FLOAT4;0,0,0,0;False;8;FLOAT4;0,0,0,0;False;1;FLOAT4;0
  126. Node;AmplifyShaderEditor.StaticSwitch;145;3264.128,-518.7849;Float;False;Property;_DEBUG_MASK;DEBUG_MASK;1;0;Create;True;0;0;False;0;0;0;0;True;;Toggle;2;Key0;Key1;9;1;FLOAT4;0,0,0,0;False;0;FLOAT4;0,0,0,0;False;2;FLOAT4;0,0,0,0;False;3;FLOAT4;0,0,0,0;False;4;FLOAT4;0,0,0,0;False;5;FLOAT4;0,0,0,0;False;6;FLOAT4;0,0,0,0;False;7;FLOAT4;0,0,0,0;False;8;FLOAT4;0,0,0,0;False;1;FLOAT4;0
  127. Node;AmplifyShaderEditor.TemplateMultiPassMasterNode;0;3632,-512;Float;False;True;2;Float;ASEMaterialInspector;0;2;Hidden/PostProcess/Pixelize/CombineLayers;c71b220b631b6344493ea3cf87110c93;True;SubShader 0 Pass 0;0;0;SubShader 0 Pass 0;1;False;False;False;True;2;False;-1;False;False;True;2;False;-1;True;7;False;-1;False;True;0;False;0;False;False;False;False;False;False;False;False;False;False;True;2;0;;0;0;Standard;0;0;1;True;False;1;0;FLOAT4;0,0,0,0;False;0
  128. WireConnection;148;0;1;0
  129. WireConnection;148;1;2;0
  130. WireConnection;148;2;151;0
  131. WireConnection;154;0;148;4
  132. WireConnection;153;1;148;0
  133. WireConnection;153;0;148;0
  134. WireConnection;145;1;153;0
  135. WireConnection;145;0;154;0
  136. WireConnection;0;0;145;0
  137. ASEEND*/
  138. //CHKSM=E249BAD91DA4F83695DA86709273EBF5F3CED785