Search Unity

Basic raymarching shader not working, Unity 2019.3.3, Built-in Render Pipeline

Discussion in 'Shaders' started by pepin_the_healer666, Dec 26, 2020.

  1. pepin_the_healer666

    pepin_the_healer666

    Joined:
    Feb 9, 2015
    Posts:
    5
    Hey guys,

    I was following a unity raymarching tutorial. I tried to raymarch a sphere, however it doesn't seem to work. All I can see is a solid red color. I tried everything, spent a few hours on it, read the code 30 times and I'm pretty sure there are no typos and everything seems to be correct. Please tell me what the issue might be. I'm using Unity 2019.3.3 with Built-in Render Pipeline.

    Code (CSharp):
    1. Shader "Unlit/SimpleRaymarching"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags
    10.         {
    11.             "RenderType" = "Opaque"
    12.         }
    13.  
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             #include "UnityCG.cginc"
    20.  
    21.             #define MAX_STEPS 100
    22.             #define MAX_DIST 1000.
    23.             #define SURF_DIST 0.001
    24.  
    25.             struct appdata
    26.             {
    27.                 float4 vertex : POSITION;
    28.                 float2 uv : TEXCOORD0;
    29.             };
    30.  
    31.             struct v2f
    32.             {
    33.                 float2 uv : TEXCOORD0;
    34.                 float4 vertex : SV_POSITION;
    35.             };
    36.  
    37.             sampler2D _MainTex;
    38.             float4 _MainTex_ST;
    39.  
    40.             v2f vert (appdata v)
    41.             {
    42.                 v2f o;
    43.                 o.vertex = UnityObjectToClipPos(v.vertex);
    44.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    45.                 return o;
    46.             }
    47.  
    48.             float GetDist(float3 p) {
    49.                 float d = length(p) - 0.5;
    50.  
    51.                 return d;
    52.             }
    53.  
    54.             float Raymarch(float3 ro, float3 rd) {
    55.                 float dO = 0;
    56.                 float dS;
    57.                 for (int i = 0; i < MAX_STEPS; i++) {
    58.                     float p = ro + dO * rd;
    59.                     dS = GetDist(p);
    60.                     dO += dS;
    61.                     if (dS < SURF_DIST || dO > MAX_DIST) break;
    62.                 }
    63.  
    64.                 return dO;
    65.             }
    66.  
    67.             fixed4 frag(v2f i) : SV_Target
    68.             {
    69.                 float2 uv = i.uv-.5;
    70.  
    71.                 float3 ro = float3(0,0,-3);
    72.                 float3 rd = normalize(float3(uv.x, uv.y, 1));
    73.  
    74.                 float d = Raymarch(ro, rd);
    75.                 fixed4 col = 0;
    76.  
    77.                 if (d < MAX_DIST) {
    78.                     col.r = 1;
    79.                 }
    80.          
    81.                 return col;
    82.             }
    83.             ENDCG
    84.         }
    85.     }
    86. }
    87.  
     

    Attached Files:

    Last edited: Dec 29, 2020
  2. pepin_the_healer666

    pepin_the_healer666

    Joined:
    Feb 9, 2015
    Posts:
    5
    ok, I've found a bug.
    Line 58, should be "float3" instead of "float" because point is a vec3:
    float3 p = ro + dO * rd;