Advertisement
tonynogo

Demo 32 - Volumetric sphere in a cube

Jul 6th, 2017
4,536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/VolumetricSphere"
  2. {
  3.     Properties
  4.     {
  5.         _Center ("Center", vector) = (0.0, 0.0, 0.0)
  6.         _ColorCube ("Color cube", Color) = (1, 1, 1, 1)
  7.         _ColorSphere ("Color sphere", Color) = (1, 1, 1, 1)
  8.         _Radius ("Radius", float) = 2.0
  9.         _StepNumber ("Step Number", float) = 10.0
  10.         _StepVal ("Step Val", float) = 0.1
  11.     }
  12.     SubShader
  13.     {
  14.         Pass
  15.         {      
  16.             Blend SrcAlpha OneMinusSrcAlpha
  17.  
  18.             CGPROGRAM
  19.             #pragma vertex vert
  20.             #pragma fragment frag
  21.  
  22.             #include "UnityCG.cginc"
  23.  
  24.             struct v2f {
  25.                 float4 pos : SV_POSITION;
  26.                 float3 worldPos : TEXCOORD1;
  27.             };
  28.  
  29.             v2f vert(appdata_base v) {
  30.                 v2f o;
  31.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  32.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
  33.                 return o;
  34.             }
  35.  
  36.             float _StepNumber;
  37.             float _StepVal;
  38.             float3 _Center;
  39.             float _Radius;
  40.             fixed4 _ColorCube;
  41.             fixed4 _ColorSphere;
  42.  
  43.             fixed4 raymarch(float3 worldPos, float3 viewDirection) {
  44.                 for(int i = 0; i < _StepNumber; i++) {
  45.                     if(distance(worldPos, _Center) < _Radius)
  46.                         return _ColorSphere;
  47.                     worldPos += viewDirection * _StepVal;
  48.                 }
  49.                 return _ColorCube;
  50.             }
  51.  
  52.             fixed4 frag(v2f i) : SV_Target {
  53.                 float3 viewDirection = normalize(i.worldPos - _WorldSpaceCameraPos);
  54.                 return raymarch(i.worldPos, viewDirection);
  55.             }
  56.  
  57.             ENDCG
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement