smkplus / ShaderMan

Convert ShaderToy to Unity HLSL/CG
https://smkplus.github.io/ShaderMan.io
MIT License
1.41k stars 197 forks source link

Can't get any shader to work #15

Open corbinyo opened 5 years ago

corbinyo commented 5 years ago

Shaders will not compile - using unity 2018.3.0b10

smkplus commented 5 years ago

Shaders will not compile - using unity 2018.3.0b10

try using web version because It's new version of shader man https://smkplus.github.io/ShaderMan.io/ which shader do you want to convert

corbinyo commented 5 years ago

Hi! Thank you for answering. I am still having the same issues using https://smkplus.github.io/ShaderMan.io/ when I try and convert https://www.shadertoy.com/view/4ddXW4

smkplus commented 5 years ago

Hi! Thank you for answering. I am still having the same issues using https://smkplus.github.io/ShaderMan.io/ when I try and convert https://www.shadertoy.com/view/4ddXW4

Shader "Hidden/Shader"
{

    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

/**
 * Created by Kamil Kolaczynski (revers) - 2016
 *
 * Licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
 *
 * This shader, as always, uses a lot of code (raymarching, noise and lighting) credited to iq 
 * [ https://www.shadertoy.com/view/Xds3zN ]. 
 * Camera path is based on Shane's "Subterranean Fly-Through" [ https://www.shadertoy.com/view/XlXXWj ].
 * Additional specular lighting trick is based on "Wet stone" by TDM [ https://www.shadertoy.com/view/ldSSzV ].
 * Thanks for sharing great code guys!
 * 
 * The shader was created and exported from Synthclipse [ http://synthclipse.sourceforge.net/ ].
 */
 #define FOV 0.4
 #define MarchDumping 0.7579
 #define Far 38.925
 #define MaxSteps 128
 #define CameraSpeed 4.5099998
 #define TunnelSmoothFactor  2.0
 #define TunnelRadius  0.85660005
 #define TunnelFreqA  0.18003
 #define TunnelFreqB  0.25
 #define TunnelAmpA  3.6230998
 #define TunnelAmpB  2.4324
 #define NoiseIsoline  0.319
 #define NoiseScale  2.9980001
 #define Color  float3(0.085, 0.658, 1.0)

#define M_NONE -1.0
#define M_NOISE 1.0

float hash(float h) {
    return frac(sin(h) * 43758.5453123);
}

float noise(float3 x) {
    float3 p = floor(x);
    float3 f = frac(x);
    f = f * f * (3.0 - 2.0 * f);

    float n = p.x + p.y * 157.0 + 113.0 * p.z;
    return lerp(
            lerp(lerp(hash(n + 0.0), hash(n + 1.0), f.x),
                    lerp(hash(n + 157.0), hash(n + 158.0), f.x), f.y),
            lerp(lerp(hash(n + 113.0), hash(n + 114.0), f.x),
                    lerp(hash(n + 270.0), hash(n + 271.0), f.x), f.y), f.z);
}

float fbm(float3 p) {
    float f = 0.0;
    f = 0.5000 * noise(p);
    p *= 2.01;
    f += 0.2500 * noise(p);
    p *= 2.02;
    f += 0.1250 * noise(p);

    return f;
}

// by iq. http://iquilezles.org/www/articles/smin/smin.htm
float smax(float a, float b, float k) {
    float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
    return lerp(a, b, h) + k * h * (1.0 - h);
}

// From "Subterranean Fly-Through" by Shane https://www.shadertoy.com/view/XlXXWj
float2 path(float z) {
    return float2(TunnelAmpA * sin(z * TunnelFreqA), TunnelAmpB * cos(z * TunnelFreqB));
}

float noiseDist(float3 p) {
    p = p / NoiseScale;
    return (fbm(p) - NoiseIsoline) * NoiseScale;
}

float2 map(float3 p) {
    float d = noiseDist(p);
    float d2 = length(p.xy - path(p.z)) - TunnelRadius;
    d = smax(d, -d2, TunnelSmoothFactor);

    float2 res = float2(d, M_NOISE);
    return res;
}

float2 castRay(float3 ro, float3 rd) {
    float tmin = 0.0;
    float tmax = Far;

    float precis = 0.002;
    float t = tmin;
    float m = M_NONE;

    for (int i = 0; i < MaxSteps; i++) {
        float2 res = map(ro + rd * t);
        if (res.x < precis || t > tmax) {
            break;
        }
        t += res.x * MarchDumping;
        m = res.y;
    }
    if (t > tmax) {
        m = M_NONE;
    }
    return float2(t, m);
}

float softshadow(float3 ro, float3 rd, float mint, float tmax) {
    float res = 1.0;
    float t = mint;

    for (int i = 0; i < 16; i++) {
        float h = map(ro + rd * t).x;

        res = min(res, 8.0 * h / t);
        t += clamp(h, 0.02, 0.10);

        if (h < 0.001 || t > tmax) {
            break;
        }
    }
    return clamp(res, 0.0, 1.0);
}

float3 calcNormal(float3 pos) {
    float2 eps = float2(0.001, 0.0);

    float3 nor = float3(map(pos + eps.xyy).x - map(pos - eps.xyy).x,
            map(pos + eps.yxy).x - map(pos - eps.yxy).x,
            map(pos + eps.yyx).x - map(pos - eps.yyx).x);
    return normalize(nor);
}

float calcAO(float3 pos, float3 nor) {
    float occ = 0.0;
    float sca = 1.0;

    for (int i = 0; i < 5; i++) {
        float hr = 0.01 + 0.12 * float(i) / 4.0;
        float3 aopos = nor * hr + pos;
        float dd = map(aopos).x;

        occ += -(dd - hr) * sca;
        sca *= 0.95;
    }
    return clamp(1.0 - 3.0 * occ, 0.0, 1.0);
}

float3 render(float3 ro, float3 rd) {
    float3 col = float3(0,0,0);
    float2 res = castRay(ro, rd);
    float t = res.x;
    float m = res.y;

    if (m > -0.5) {
        float3 pos = ro + t * rd;
        float3 nor = calcNormal(pos);

        // material
        col = Color + sin(t * 0.8) * 0.3;
        col += 0.3 * sin(float3(0.15, 0.02, 0.10) * _Time.y * 6.0);

        // lighitng
        float occ = calcAO(pos, nor);
        float3 lig = -rd;
        float amb = clamp(0.5 + 0.5 * nor.y, 0.0, 1.0);
        float dif = clamp(dot(nor, lig), 0.0, 1.0);

        float fre = pow(clamp(1.0 + dot(nor, rd), 0.0, 1.0), 2.0);

        float3 ref = reflect(rd, nor);
        float spe = pow(clamp(dot(ref, lig), 0.0, 1.0), 100.0);

        dif *= softshadow(pos, lig, 0.02, 2.5);

        float3 brdf = float3(0,0,0);
        brdf += 1.20 * dif * float3(1.00, 0.90, 0.60);
        brdf += 1.20 * spe * float3(1.00, 0.90, 0.60) * dif;

        // Additional specular lighting trick,
        // taken from "Wet stone" by TDM
        // https://www.shadertoy.com/view/ldSSzV
        nor = normalize(nor - normalize(pos) * 0.2);
        ref = reflect(rd, nor);
        spe = pow(clamp(dot(ref, lig), 0.0, 1.0), 100.0);
        brdf += 2.20 * spe * float3(1.00, 0.90, 0.60) * dif;

        brdf += 0.40 * amb * float3(0.50, 0.70, 1.00) * occ;
        brdf += 0.40 * fre * float3(1.00, 1.00, 1.00) * occ;

        col = col * brdf;

        col = lerp(col, float3(0,0,0), 1.0 - exp(-0.005 * t * t));
    }
    return float3(clamp(col, 0.0, 1.0));
}

float3x3 rotationZ(float a) {
    float sa = sin(a);
    float ca = cos(a);

    return float3x3(ca, sa, 0.0, -sa, ca, 0.0, 0.0, 0.0, 1.0);
}

    fixed4 frag (v2f i) : SV_Target
    {
    float2 q = i.uv;
    float2 coord = 2.0 * q - 1.0;
    coord *= FOV;

    float t = _Time.y * CameraSpeed + 4.0 * 60.0;
    float3 ro = float3(path(t), t);

    t += 0.5;
    float3 target = float3(path(t), t);
    float3 dir = normalize(target - ro);
    float3 up = mul(float3(-0.9309864, -0.33987653, 0.1332234) , rotationZ(_Time.y * 0.05));
    float3 upOrtho = normalize(up - dot(dir, up) * dir);
    float3 right = normalize(cross(dir, upOrtho));

    float3 rd = normalize(dir + coord.x * right + coord.y * upOrtho);

    float3 col = render(ro, rd);

    col = pow(col, float3(0.4545,0.4545,0.4545));

   return float4(col, 1.0);
            }
            ENDCG
        }
    }
}
LauraLaureus commented 5 years ago

@smkplus I cannot make a shader to work with unity It throws: imagen

I tried the same in the web version but I get the following error: imagen

I'm trying to convert the following shader if it help you with the test: https://www.shadertoy.com/view/Xts3WH

smkplus commented 5 years ago

    Shader "ShaderMan/MyShader"
    {
    Properties{

    }
    SubShader
    {
    Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    Pass
    {
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #include "UnityCG.cginc"

    float4 vec4(float x,float y,float z,float w){return float4(x,y,z,w);}
    float4 vec4(float x){return float4(x,x,x,x);}
    float4 vec4(float2 x,float2 y){return float4(float2(x.x,x.y),float2(y.x,y.y));}
    float4 vec4(float3 x,float y){return float4(float3(x.x,x.y,x.z),y);}

    float3 vec3(float x,float y,float z){return float3(x,y,z);}
    float3 vec3(float x){return float3(x,x,x);}
    float3 vec3(float2 x,float y){return float3(float2(x.x,x.y),y);}

    float2 vec2(float x,float y){return float2(x,y);}
    float2 vec2(float x){return float2(x,x);}

    float vec(float x){return float(x);}

    struct VertexInput {
    float4 vertex : POSITION;
    float2 uv:TEXCOORD0;
    float4 tangent : TANGENT;
    float3 normal : NORMAL;
    //VertexInput
    };
    struct VertexOutput {
    float4 pos : SV_POSITION;
    float2 uv:TEXCOORD0;
    //VertexOutput
    };

    VertexOutput vert (VertexInput v)
    {
    VertexOutput o;
    o.pos = UnityObjectToClipPos (v.vertex);
    o.uv = v.uv;
    //VertexFactory
    return o;
    }

    fixed4 frag(VertexOutput vertex_output) : SV_Target
    {

    float2 p = vertex_output.uv/1, a = p*5.; a.y -= _Time.y;
    float2 f = frac(a); a -= f; f = f*f*(3.-2.*f);
    float4 r = frac(sin(vec4(a.x + a.y*1e3) + vec4(0, 1, 1e3, 1001)) * 1e5)*30./p.y;
    float3 rgb = p.y + vec3(1,.5,.2) * clamp(lerp(lerp(r.x, r.y, f.x), lerp(r.z, r.w, f.x), f.y)-30., -.2, 1.);
    return float4(rgb,1);
    }
    ENDCG
    }
  }
  }