Skip to content

Shader compiles but shows everything black #21

Closed
@tarasrng

Description

@tarasrng

The shader has been successfully converted by the web tool (https://smkplus.github.io/ShaderMan.io/) but everything is black in Unity (using Unity 2020.1), and it's really hard to debug what's wrong.

I'm trying to convert this shader: https://www.shadertoy.com/view/WtyXzG
I've replaced 3D texture by a constant because I have no idea how to convert it to unity.

Here is a simplified version that still works in shadertoy:

void layer (out vec4 col, in vec3 uv, in float zoom, in float music) {
    
    vec2 baseUv = uv.xy;
    
    uv.xy *= mix(1.2, 0.001, zoom);
    
    float deCenter = length(uv);
    
    deCenter = min(1., deCenter * deCenter);

    vec2 smokeDin = vec2(-iTime + sin(uv.z*23.)*12.34, iTime + cos(uv.z*11.))*0.02;
    
    vec2 smokeUV = uv.xy;

    float texC = texture(iChannel3, smokeUV*13.+ smokeDin.yx*0.5).r
                *texture(iChannel3, smokeUV*15.*zoom*uv.y - smokeDin.yx*1.2).r*0.015;
    
    
    float texA = (1.- texture(iChannel3, smokeUV*0.6 - texC * 0.5 + smokeDin*0.2).r);
    
    float texB = 1.-texture(iChannel3, smokeUV *  (1.-texA*0.1 + texC * 0.4 * (.5-texA)) + smokeDin*0.1).r;
        

    texA *= (0.5 + texB)*0.25;

    texA *= texA * 8.* zoom * zoom * (1.-col.a) * deCenter;
    
    
    vec2 grid = uv.xy*10.;
    
    uv.xy = mod(grid,1.0);

    grid -= uv.xy;
    
    uv.xy-=0.5;
    
    
    vec4 add = vec4(0,0,0, texA);
    
    float depthMod = uv.z*0.123;
    
    float iTimeAndZoom = iTime + zoom;
    
    float cutMod = col.a * (64.) * music;
    
    float upscale =  (music * col.a * 20.);
    
    const float cuttingMod = 1.;
    
    float distMod =  (1. + texA* 50.  + cutMod)* max(0.1,  zoom - col.a*8.) ;
        
    for(int x=-1;x<=1;x++){
        for(int y=-1;y<=1;y++){
        
            vec2 dUv = uv.xy - vec2(x,y);
            vec2 dGrid = grid + vec2(x,y);

            vec3 vol = vec3(0.8);//texture(iChannel2, vec3(dGrid.xy*0.1+depthMod, (dGrid.x + iTimeAndZoom)*0.003 )).rgb ;

            float big = vol.b*vol.b * upscale;
            
            dUv += (vol.xy - .5)*1.5;
            
            
        
            // CIRCLES
            //dUv += normalize(-dUv.xy) * 0.2 * music  * vol.b;
            
            
            float len = length(dUv) +0.0001;
            
            float dist = big * distMod/len; 

            
            float cut = smoothstep(cuttingMod,.5,len);
            
            float ray = max(0., 1.-abs(dUv.x*dUv.y*300.))* cut * big;
            
            dist += ray;
            
            add.rgb +=  dist * cut * vol;
        }
    }

col += add * zoom * (1.-zoom);
    
    
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = fragCoord/iResolution.xy;

    vec3 noise =  vec3(0.6);
        //texture(iChannel2, vec3(uv*123.123+iTime, iTime+uv.x*uv.y*543.21)).rgb;
    
    vec4 col = vec4(0,0,0,0);
    
    uv -= 0.5;
    
    vec2 M = (iMouse.xy - iResolution.xy * .5)/iResolution.y;
    
    uv += M;
    
    float aspect = iResolution.x/iResolution.y; 
    
    uv.x*=aspect;
        
    
    const float stepCnt =5.;
    const int steCntInt = 5;
    const float oneStep = 1./stepCnt;
    const float SPEED = 0.0;
    
    float zoom = iTime*SPEED;
    
    float index = floor(mod(zoom, 1.)*stepCnt);
    
    zoom = mod(zoom, oneStep);
    
    float off = 0.;
    
    float music = 0.;  
    
    for(int i=0;i<steCntInt;i++){
        
        float totalZoom = zoom + off;
        
        //music = texture(iChannel0, vec2(mix(0.01, 0.8, totalZoom),0.05)).x;

        // for Unseen: smoothstep(0.01,.4, music);    
        //music = smoothstep(-.2,1., music);
        music = 0.075;
        
        layer(col, vec3(uv.xy,index), totalZoom, music);
        off += oneStep;
        index = mod(index-1., stepCnt);
        
    }    
    
    col.rgb*=vec3(0.5,0.9,1) * 5. * oneStep;
    
    vec3 mixing = col.gbr+col.brg;
    
    col.rgb+= mixing * mixing * 0.1;
    
    col.rgb += noise*col.rgb*0.15;

    //col = clamp(col,0.,1.);
    
    fragColor = vec4(col.rgb,1.0);
}

Converted version with few issues fixed:

    Shader "ShaderMan/MyShader"
    {
    Properties{
    _MainTex("MainTex",2D) = "white"{} 
    _iMouse("Mouse", Vector) = (0,0,0,0)
_ThirdTex("ThirdTex",2D) = "white"{} 
_FourthTex("FourthTex",2D) = "white"{} 

    }
    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
    };
    sampler2D _MainTex; 
sampler2D _ThirdTex; 
sampler2D _FourthTex; 
float4 _iMouse;

    
    VertexOutput vert (VertexInput v)
    {
    VertexOutput o;
    o.pos = UnityObjectToClipPos (v.vertex);
    o.uv = v.uv;
    //VertexFactory
    return o;
    }
    
    
void layer (out float4 col, in float3 uv, in float zoom, in float music)
{
    UNITY_INITIALIZE_OUTPUT(float4, col);
    float2 baseUv = uv.xy;
    
    uv.xy *= lerp(1.2, 0.001, zoom);
    
    float deCenter = length(uv);
    
    deCenter = min(1., deCenter * deCenter);

    float2 smokeDin = vec2(-_Time.y + sin(uv.z*23.)*12.34, _Time.y + cos(uv.z*11.))*0.02;
    
    float2 smokeUV = uv.xy;

    float texC = tex2D(_FourthTex, smokeUV*13.+ smokeDin.yx*0.5).r
                *tex2D(_FourthTex, smokeUV*15.*zoom*uv.y - smokeDin.yx*1.2).r*0.015;
    
    
    float texA = (1.- tex2D(_FourthTex, smokeUV*0.6 - texC * 0.5 + smokeDin*0.2).r);
    
    float texB = 1.-tex2D(_FourthTex, smokeUV *  (1.-texA*0.1 + texC * 0.4 * (.5-texA)) + smokeDin*0.1).r;
        

    texA *= (0.5 + texB)*0.25;

    texA *= texA * 8.* zoom * zoom * (1.-col.a) * deCenter;
    
    
    float2 grid = uv.xy*10.;
    
    uv.xy = fmod(grid,1.0);

    grid -= uv.xy;
    
    uv.xy-=0.5;
    
    
    float4 add = vec4(0,0,0, texA);
    
    float depthMod = uv.z*0.123;
    
    float timeAndZOom = _Time.y + zoom;
    
    float cutMod = col.a * (64.) * music;
    
    float upscale =  (music * col.a * 20.);
    
    const float cuttingMod = 1.;
    
    float distMod =  (1. + texA* 50.  + cutMod)* max(0.1,  zoom - col.a*8.) ;
        
    [unroll(100)]
for(int x=-1;x<=1;x++){
        [unroll(100)]
for(int y=-1;y<=1;y++){
        
            float2 dUv = uv.xy - vec2(x,y);
            float2 dGrid = grid + vec2(x,y);

            float3 vol = vec3(0.7);//tex2D(_ThirdTex, vec3(dGrid.xy*0.1+depthMod, (dGrid.x + _Time.yAndZoom)*0.003 )).rgb ;

            float big = vol.b*vol.b * upscale;
            
            dUv += (vol.xy - .5)*1.5;
            
            
        
            // CIRCLES
            //dUv += normalize(-dUv.xy) * 0.2 * music  * vol.b;
            
            
            
            
            
            float len = length(dUv) +0.0001;
            
            float dist = big * distMod/len; 

            
            float cut = smoothstep(cuttingMod,.5,len);
            
            float ray = max(0., 1.-abs(dUv.x*dUv.y*300.))* cut * big;
            
            dist += ray;
            
            add.rgb +=  dist * cut * vol;
        }
    }

col += add * zoom * (1.-zoom);
    
    
}
    fixed4 frag(VertexOutput vertex_output) : SV_Target
    {
    
    float2 uv = vertex_output.uv/1;

    float3 noise =  vec3(0.6);
        //tex2D(_ThirdTex, vec3(uv*123.123+_Time.y, _Time.y+uv.x*uv.y*543.21)).rgb;
    
    float4 col = vec4(0,0,0,0);
    
    uv -= 0.5;
    
    float2 M = (_iMouse.xy - 1 * .5)/1;
    
    uv += M;
    
    float aspect = 1/1; 
    
    uv.x*=aspect;
        
    
    const float stepCnt =5.;
    const int steCntInt = 5;
    const float oneStep = 1./stepCnt;
    const float SPEED = 0.0;
    
    float zoom = _Time.y*SPEED;
    
    float index = floor(fmod(zoom, 1.)*stepCnt);
    
    zoom = fmod(zoom, oneStep);
    
    float off = 0.;
    
    float music = 0.;  
    
    [unroll(100)]
for(int i=0;i<steCntInt;i++){
        
        float totalZoom = zoom + off;
        
        //music = tex2D(_MainTex, vec2(lerp(0.01, 0.8, totalZoom),0.05)).x;

        // for Unseen: smoothstep(0.01,.4, music);    
        //music = smoothstep(-.2,1., music);
        music = 0.075;
        
        layer(col, vec3(uv.xy,index), totalZoom, music);
        off += oneStep;
        index = fmod(index-1., stepCnt);
        
    }    
    
    col.rgb*=vec3(0.5,0.9,1) * 5. * oneStep;
    
    float3 lerping = col.gbr+col.brg;
    
    col.rgb+= lerping * lerping * 0.1;
    
    col.rgb += noise*col.rgb*0.15;

    //col = clamp(col,0.,1.);
    
    return vec4(col.rgb,1.0);

    }
    ENDCG
    }
}
}

This compiles but gives a black screen.
Could you please advise what could go wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions