smkplus / ShaderMan

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

Hello,MyShader conver was wrong #31

Open er-ick opened 3 years ago

er-ick commented 3 years ago

https://www.shadertoy.com/view/wtSfDK# I want conver this shader by ShaderMan,but it was wrong,Can you help me? Thank you!!!

er-ick commented 3 years ago

Hello,I am want to fix errors in anther shader,but I'm having some problems,

244 for(int i=0; i<128; i++){ 245 dt = map(camPos + rd*t); 246 if(dt<0.005 || t>150.){ break; 247 }

Shsder Error:syntax error: unexpected end of file at line 245 (on d3d11)

How do I resolve this report error? I really need your help, please,Thank you!!!

smkplus commented 3 years ago

Hello sorry for the late response I'll take a look at the shader...

for the second question try to add a "}" at the end of your shader

for(int i=0; i<128; i++){ dt = map(camPos + rd*t); if(dt<0.005 || t>150.){ break; } }

er-ick commented 3 years ago

WOW,thank you! I am add two "}",It solves!emmm..... 

But there are a lot of mistakes, because I don't know how to write shader......

Thank you very much!

------------------ 原始邮件 ------------------ 发件人: "Seyed Morteza Kamali"<notifications@github.com>; 发送时间: 2021年2月26日(星期五) 中午1:12 收件人: "smkplus/ShaderMan"<ShaderMan@noreply.github.com>; 抄送: "$流星浪子$ "<632621877@qq.com>; "Author"<author@noreply.github.com>; 主题: Re: [smkplus/ShaderMan] Hello,MyShader conver was wrong (#31)

Hello sorry for the late response I'll take a look at the shader...

for the second question try to add a "}" at the end of your shader

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

smkplus commented 3 years ago

I tried to convert your shader

Shader "Hidden/Gravity Racer X "
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        iResolution("iResolution",vector) = (1,1,1,1)
        iMouse("iMouse",vector) = (0,0,0,0)
    }
    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;
            }

            sampler2D _MainTex;
            float iTime;
            float4 iMouse;
            float4 iResolution;

                        #define PI          3.1415926
#define r2(a) float2x2(cos(a),sin(a),-sin(a),cos(a))
//http://mercury.sexy/hg_sdf/

// Sign function that doesn't return 0
float square (float x)  { return x*x;       }
float2  square (float2 x)   { return x*x;       }
float3  square (float3 x)   { return x*x;       }
float lengthSqr(float3 x) { return dot(x, x); }

// Maximum/minumum elements of a floattor
float vmax(float2 v) {  return max(v.x, v.y);                       }
float vmax(float3 v) {  return max(max(v.x, v.y), v.z);             }
float vmax(float4 v) {  return max(max(v.x, v.y), max(v.z, v.w));   }
float vmin(float2 v) {  return min(v.x, v.y);                       }
float vmin(float3 v) {  return min(min(v.x, v.y), v.z);             }
float vmin(float4 v) {  return min(min(v.x, v.y), min(v.z, v.w));   }

// Sign function that doesn't return 0
float sgn(float x) {    return (x<0.)?-1.:1.;                           }
float2 sgn(float2 v)   {    return float2((v.x<0.)?-1.:1., (v.y<0.)?-1.:1.);    }

// Repeat space along one axis.
float pMod(inout float p, float size) {
    float halfsize = size*0.5;
    float c = floor((p + halfsize)/size);
    p = fmod(p + halfsize, size) - halfsize;
    return c;
}

float2 pMod(inout float2 p, float2 size) {
    float2 c = floor((p + size*0.5)/size);
    p = fmod(p + size*0.5,size) - size*0.5;
    return c;
}

float3 pMod(inout float3 p, float3 size) {
    float3 c = floor((p + size*0.5)/size);
    p = fmod(p + size*0.5, size) - size*0.5;
    return c;
}

// Repeat around the origin by a fixed angle.
float pModPolar(inout float2 p, float repetitions) {
    float angle = 2.*PI/repetitions;
    float a = atan2(p.y, p.x) + angle/2.;
    float r = length(p);
    float c = floor(a/angle);
    a = fmod(a,angle) - angle/2.;
    p = float2(cos(a), sin(a))*r;
    // For an odd number of repetitions, fix cell index of the cell in -x direction
    // (cell index would be e.g. -5 and 5 in the two halves of the cell):
    if (abs(c) >= (repetitions/2.)) c = abs(c);
    return c;
}

// Mirror at an axis-aligned plane which is at a specified distance <dist> from the origin.
float pMirror (inout float p, float dist) {
    float s = sgn(p);
    p = abs(p)-dist;
    return s;
}

// @pjkarlik Modified to have angle passed in for post rotation
float2 pMirrorOctant (inout float2 p, float2 dist, float r) {
    float2 s = sgn(p);
    pMirror(p.x, dist.x);
    pMirror(p.y, dist.y);
    p=mul(p,r2(r));
    if (p.y > p.x) p.xy = p.yx;
    return s;
}

            #define R           iResolution
#define M           iMouse
#define T           iTime
#define PI          3.1415926

#define MINDIST     .001
#define MAXDIST     45.

#define r2(a) float2x2(cos(a),sin(a),-sin(a),cos(a))
#define hash(a, b) frac(sin(a*1.2664745 + b*.9560333 + 3.) * 14958.5453)
//@iq of hsv2rgb - updated
float3 hsv2rgb( float h ) {
    float3 c = float3(h,1.,.5);
    float3 rgb = clamp( abs(fmod(c.x*6.0+float3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
    return c.z * lerp( float3(1.0,1.0,1.0), rgb, c.y);
}
float3 getMouse(float3 ro) {
    float x = M.xy == float2(0,0) ? 0. : -(M.y/R.y * 1. - .5) * PI;
    float y = M.xy == float2(0,0) ? 0. : (M.x/R.x * 1. - .5) * PI;
    ro.zy =mul(ro.zy,r2(x));
    ro.xz =mul(ro.xz,r2(y));
    return ro;   
}

// path functions 
float2 path(in float z){ 
    float2 p1 =float2(2.3*sin(z * .15), 1.4*cos(z * .25));
    float2 p2 =float2(1.2*sin(z * .39), 2.1*sin(z * .15));
    return p1 - p2;
}

float fBox(float3 p, float3 b) {
    float3 d = abs(p) - b;
    return length(max(d, float3(0,0,0))) + vmax(min(d, float3(0,0,0)));
}

float fBox2(float2 p, float2 b) {
    float2 d = abs(p) - b;
    return length(max(d, float2(0,0))) + vmax(min(d, float2(0,0)));
}

float time;

float2 fragtail(float3 pos, float z) {
    float scale = 3.12;
    float twave = 1.5+1.5*sin(z*.5);
    float3 cxz = float3(3.4,2.75,2.+twave);
    float r = length(pos);
    float t = 0.0;
    float ss=.55;

    for (int i = 0;i<3;i++) {
        pos=abs(pos);

        if ( pos.x- pos.y<0.) pos.yx = pos.xy;
        if ( pos.x- pos.z<0.) pos.zx = pos.xz;
        if ( pos.y- pos.z<0.) pos.zy = pos.yz;
        pos.x=scale * pos.x-cxz.x*(scale-1.);
        pos.y=scale * pos.y-cxz.y*(scale-1.);
        pos.z=scale * pos.z;

        if (pos.z>0.5*cxz.z*(scale-1.)) pos.z-=cxz.z*(scale-1.);
        r = fBox2(pos.xy,float2(scale,scale));

        ss*=1./scale;
    }
    float rl = log2(ss*.255);
    return float2(r*ss,rl);
}

float glow,iqd,travelSpeed,carWave;
float3 hp;
float2x2 tax,tay;
// float sg = toggle | to record or not change specfic
// values like hitpoint or glow. this prevents it from
// distorting items or textures for extra passes like
// ao or shadow
float2 map (in float3 pos, float sg) {
    // map stuff
    float3 p = pos-float3(0.,0.,0);
    float2 res = float2(100.,-1.);
    float msize = 4.;

    // set path(s) floattor(s)
    float2 tun = p.xy - path(p.z);
    float3 px = float3(tun+float2(0.,-.1),pos.z+travelSpeed+carWave);
    float3 q = float3(tun,p.z);
    float3 s = q;
    float3 r = float3(abs(q.x),abs(q.y),q.z);

    // mods and floattors
    pModPolar(q.xy,6.);
    pModPolar(s.xy,3.);
    pMod(s.z,msize);
    float3  qid = pMod(q,float3(msize,msize,msize));
    float twave = .15+.15*sin(qid.z*.5);
    iqd=qid.z;
    // panels
    float d3 = fBox(s-float3(.75,0,0),float3(.001,.3,.75));
    if(d3<res.x) {
        //sg prevents hp from changing for ao
        if(sg>0.) hp = s;
        res = float2(d3,3.);
    }

    // stuff
    float3 qr = float3(q.x,abs(q.y),abs(q.z));
    float d6 = fBox(qr-float3(1.2,.25-twave,1.25),float3(.05,.075,.45));
    if(d6<res.x) res = float2(d6,4.);

    // fracal
    float2 d1 = fragtail(q,qid.z);
    if(d1.x<res.x)  res = d1;

    // beams
    float d4 = length(r.xy-float2(.52,.33))-.005+.015*sin(q.z*3.-T*3.5);
    if(d4<res.x && sg > 0.) res = float2(d4,12.);

    // car
    float3 ax = float3(abs(px.x),px.yz);
    ax.xy=mul(ax.xy,tax);
    ax.zy=mul(ax.zy,tay);
    float d7 = fBox(ax-float3(0.3,.0,-.5),float3((frac(px.z-.245)*.4)-.09,.005,.25));
    d7 = min(fBox(px+float3(0.,.22,.3),float3((frac(px.z-.25)*.5)-.15,.0175,.15)),d7);
    if(d7<res.x && sg > 0.) res = float2(d7,5.);

    //sg prevents glow from changing for ao
    if(sg>0.){
        glow += .0001/(.000025+d4*d4);
        glow += .000085/(.000025+d7*d7);
    }

    return res;
}

float2 marcher(float3 ro, float3 rd, float sg, int maxstep){
    float d =  .0,
          m = -1.;
        int i = 0;
        for(i=0;i<maxstep;i++){
            float3 p = ro + rd * d;
            float2 t = map(p, sg);
            if(abs(t.x)<d*MINDIST)break;
            d += t.x*.85;
            m  = t.y;
            if(d>MAXDIST)break;
        }
    return float2(d,m);
}

// Tetrahedron technique @iq
// https://www.iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
float3 getNormal(float3 p, float t){
    float e = (MINDIST + .0001) *t;
    float2 h = float2(1.,-1.)*.5773;
    return normalize( h.xyy*map( p + h.xyy*e, 0. ).x + 
                      h.yyx*map( p + h.yyx*e, 0. ).x + 
                      h.yxy*map( p + h.yxy*e, 0. ).x + 
                      h.xxx*map( p + h.xxx*e, 0. ).x );
}

//camera setup
float3 camera(float3 lp, float3 ro, float2 uv) {
    float3 f=normalize(lp-ro),//camera forward
         r=normalize(cross(float3(0,1,0),f)),//camera right
         u=normalize(cross(f,r)),//camera up
         c=ro+f*.85,//zoom
         i=c+uv.x*r+uv.y*u,//screen coords
        rd=i-ro;//ray direction
    return rd;
}

float3 get_stripes(float2 uv){
    uv.y -= tan(radians(45.)) * uv.x;
    float sd = fmod(floor(uv.y * 2.5), 2.);
    float3 background = (sd<1.) ? float3(1.,1.,1.) : float3(0.,0.,0.);
    return background;
}

float getDiff(float3 p, float3 n, float3 lpos) {
    float3 l = normalize(lpos-p);
    return clamp(dot(n,l),0. , 1.);  
}

//@Shane low cost AO
float calcAO(in float3 p, in float3 n){
    float sca = 2., occ = 0.;
    for( int i = 0; i<5; i++ ){
        float hr = float(i + 1)*.17/5.; 
        // map(pos/dont record hit point)
        float d = map(p + n*hr, 0.).x;
        occ += (hr - d)*sca;
        sca *= .9;
        // Deliberately redundant line 
        // that may or may not stop the 
        // compiler from unrolling.
        if(sca>1e5) break;
    }
    return clamp(1. - occ, 0., 1.);
}

float iTimeDelta;
float3 getCol(float3 n){
  return hsv2rgb(float3(fmod(n.z*.5,1.0),0.8,0.8));
}

            fixed4 frag (v2f i) : SV_Target
            {
    //mblur
    float time = _Time.y + tex2D(_MainTex,i.uv/8.).r * iTimeDelta;
    // precal for ship
    tax=r2(-45.*PI/180.);
    tay=r2(8.*PI/180.);
    travelSpeed = (time * 2.25);
    carWave = sin(time*.3)+.75;
    //
    // pixel screen coordinates
    float2 uv = (i.uv.xy - R.xy*0.5)/R.y;
    float3 C = float3(0.,0.,0.);//default color
    float3 FC = float3(.8,.8,.8);//fade color
    // ray origin / look at point based on path
    float tm = travelSpeed;
    float md = fmod(time*.1,2.);
    float zoom = md<1. ? .25 : -.25;
    float3 lp = float3(0.,0.,0.-tm);
    float3 ro = float3(0.,.01,zoom);
    ro = getMouse(ro);
    ro +=lp; 
    lp.xy += path(lp.z);
    ro.xy += path(ro.z);
    // solve for Ray direction
    float3 rd = camera(lp,ro,uv);

    // trace scene (ro/rd/record hit point/steps)
    float2 t = marcher(ro,rd,1.,192);
    float d = t.x,
          m = t.y;

    // if visible 
    if(d<45.){
        // step next point
        float3 p = ro + rd * d;
        float3 n = getNormal(p,d);

        float3 lpos  = float3(.0,0,0)+lp; 
             lpos.xy = path(lpos.z);
        float dif = getDiff(p,n,lpos);
        float ao = calcAO(p, n);
        float3 h = lerp(hsv2rgb(iqd*.025),float3(.95,.95,.95),get_stripes(normalize(hp.yz)*.28));

        if(m==3.) {
            hp.z+=T*.75;
            hp.y=abs(hp.y)-1.5;
            h = get_stripes(hp.yz*4.)*float3(2.,2.,2.);
        }
        if(m==4.) h= float3(.8,.7,.0);
        if(m==5.) h= float3(.75,.75,.75);
        C += h*dif*ao;
    } 

    C = lerp(FC,C,  exp(-.00045*t.x*t.x*t.x));
    C += glow*.3;   
    return float4(pow(C, float3(0.4545,0.4545,0.4545)),1.0);
            }
            ENDCG
        }
    }
}
er-ick commented 3 years ago

WOW.Thank you!You are so awesome! Its working!!But the color is missing....Do you know how to solve it?