Shader "Demo/Spinny thing"
{
    Properties
    {
        [Header(Lighting)]
        _SunPos ("Sun position", Vector) = (8, 4, 2)

        [Header(Raymarcher Properties)]
        _MaxSteps ("Max steps", Integer) = 256
        _MaxDist ("Max distance", Float) = 256
        _SurfDist ("Surface distance threshold", Range(0.00001, 0.05)) = 0.001

    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        Cull Front
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #define USE_DYNAMIC_QUALITY
            // #define USE_WORLD_SPACE
            #define USE_REFLECTIONS
            #define MAX_REFLECTIONS 3
            #define DISCARD_ON_MISS
            //#define CONSTRAIN_TO_MESH
            #include "RayMarchLib.cginc"

            float3 _SunPos;

            sdfData scene(float3 p)
            {
                sdfData o;
                const material mGrass = mat(0.001, 0.1, 0.001, 0.7);
                o = sdfPlane(p, 0, mGrass);
                material mDirt = mat(0.1, 0.04, 0.01, 1);
                o = sdfInter(p, o, sdfSphere(p, 9, mDirt), 0.5);

                const material mMetal = mat(0.1, 0);

                o = sdfAdd(p, o, sdfSphere(p - V_Y*2, 2, mMetal));
                material m = mat(0.05, 0.1, 0.2, 1);
                o = sdfAdd(p, o, sdfTorus(rotX(p, _Time* 40+UNITY_PI/2), 5, 0.5, m), 0.5);
                o = sdfAdd(p, o, sdfTorus(rotZ(p, _Time* 40+UNITY_PI/2), 5, 0.5, m), 0.5);
                o = sdfAdd(p, o, sdfTorus(rotX(p, _Time* 40), 5, 0.5, m), 0.5);
                o = sdfAdd(p, o, sdfTorus(rotZ(p, _Time* 40), 5, 0.5, m), 0.5);

                o = sdfAdd(p, o, sdfSphere(abs(rotY(p, -20 * _Time)) - float3(1.5, sin(_Time.y*5) +1,1.5), 0.7, mMetal), 0.2);
                return o;
            }

            fixed4 lightPoint(rayData ray)
            {
                float3 vSunDir = normalize(_SunPos);

                if (ray.bMissed)
                {
                    return sky(ray.vRayDir);
                }

                fixed4 col = 0;

                col = ray.mat.col * lightSun(ray.vNorm, vSunDir);
                col *= lightShadow(ray.vHit + ray.vNorm * _SurfDist, vSunDir, 50);
                col += ray.mat.col * lightSky(ray.vNorm, 1);
                // col *= lightAO(ray.vHit, ray.vNorm);

                // col = pow(col, 0.5);
                return col;
            }
            ENDCG
        }
    }
}
