smkplus / ShaderMan

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

HLSL shader compiles, but displays as black #13

Closed KevinCain closed 5 years ago

KevinCain commented 5 years ago

I've successfully translated several shaders via @smkplus' ShaderMan (in Unity, and via your web app).

However, in the attached example this shader compiled with a simple fix (see below), but the resulting compiled shader displays as black in Unity 5.6.x.

If the community has any suggestions I'd love to understand.

The only change I made from the ShaderMan output is this simple adaptation below:

    float3 rd = normalize(camera(ro, la)*vec3(uv, 1.97));

I replaced the explicit multiplication with the mul() method: float3 rd = normalize(mul(camera(ro, la), vec3(uv, 1.97)) );

Thanks again for a great tool!

server.zip

smkplus commented 5 years ago

Testx

Shader "Server"
{
    Subshader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vertex_shader
            #pragma fragment pixel_shader
            #pragma target 3.0

            struct custom_type
            {
                float4 screen_vertex : SV_POSITION;
                float3 world_vertex : TEXCOORD1;
            };

            const float tmax = 20.0;

            float hash(float n) {
                return frac(sin(n)*43758.5453);
            }

            float3 hash(float3 p){ 

                float n = sin(dot(p, float3(7, 157, 113)));    
                return frac(float3(2097152, 262144, 32768)*n); 
            }

            float noise(float g) {
                float p = floor(g);
                float f = frac(g);

                return lerp(hash(p), hash(p + 1.0), f);
            }

            float voronoi(float3 x) {
                float3 p = floor(x);
                float3 f = frac(x);

                float2 res = float2(8.0,8.0);

                [unroll(100)]
            for(int i = -1; i <= 1; i++)
                [unroll(100)]
            for(int j = -1; j <= 1; j++)
                [unroll(100)]
            for(int k = -1; k <= 1; k++) {
                    float3 g = float3(float(i), float(j), float(k));
                    float3 r = g + hash(p + g) - f;

                    float d = max(abs(r.x), max(abs(r.y), abs(r.z)));

                    if(d < res.x) {
                        res.y = res.x;
                        res.x = d;
                    } else if(d < res.y) {
                        res.y = d;
                    }
                }

                return res.y - res.x;
            }

            float2 path(float z) {
                return float2(cos(z/8.0)*sin(z/12.0)*12.0, 0.0);
            }

            float map(float3 p) {
                float4 q = float4(p, 1.0);
                q.x += 1.0;

                [unroll(100)]
            for(int i = 0; i < 6; i++) {
                    q.xyz = -1.0 + 2.0*frac(0.5 + 0.5*q.xyz);
                    q = 1.2*q/max(dot(q.xyz, q.xyz), 0.1);
                }

                float2 tun = abs(p.xy - path(p.z))*float2(0.6, 0.5);

                return min(0.25*abs(q.y)/q.w, 1.0 - max(tun.x, tun.y));
            }

            float ambient_occlusion( 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 );
                    occ += -(dd-hr)*sca;
                    sca *= 0.95;
                }
                return clamp( 1.0 - 3.0*occ, 0.0, 1.0 );    
            }

            float3 set_normal (float3 p)
            {
                float3 x = float3 (0.001,0.00,0.00);
                float3 y = float3 (0.00,0.001,0.00);
                float3 z = float3 (0.00,0.00,0.001);
                return normalize(float3(map(p+x)-map(p-x), map(p+y)-map(p-y), map(p+z)-map(p-z))); 
            }

            float3 lighting (float3 p)
            {
                float3 AmbientLight = float3 (0.1,0.1,0.1);
                float3 LightDirection = normalize(float3 (4.0,10.0,-10.0));
                float3 LightColor = float3 (1.0,1.0,1.0);
                float3 NormalDirection = set_normal(p);
                return (max(dot(LightDirection, NormalDirection),0.0) * LightColor + AmbientLight)*ambient_occlusion(p,NormalDirection);
            }

            float4 raymarch (float3 ro, float3 rd)
            {
                for (int i=0; i<128; i++)
                {
                    float ray = map(ro);
                    if (distance(ro,ray*rd)>250) break;
                    if (ray < 0.001) return float4 (lighting(ro)*float3(1.0,1.0,1.0),1.0); else ro+=ray*rd; 
                }
                return float4 (0.0,0.0,0.0,1.0);
            }

            custom_type vertex_shader (float4 vertex : POSITION)
            {
                custom_type vs;
                vs.screen_vertex = UnityObjectToClipPos (vertex);
                vs.world_vertex = mul (unity_ObjectToWorld, vertex);
                return vs;
            }

            float4 pixel_shader (custom_type ps ) : SV_TARGET
            {
                float3 worldPosition = ps.world_vertex;
                float3 viewDirection = normalize(ps.world_vertex - _WorldSpaceCameraPos.xyz);
                return raymarch (worldPosition,viewDirection);
            }

            ENDCG

        }
    }
}
KevinCain commented 5 years ago

Thanks @smkplus for your fast, helpful reply. I note your key changes:

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,)' with 'UnityObjectToClipPos()'

I'm still unable to render the fractal you show in your screen shot using your proposed snippet, in Unity 5.6, when pasting into a new shader and connecting to a new material.

I'll continue to experiment. Thanks again...

smkplus commented 5 years ago

Thanks @smkplus for your fast, helpful reply. I note your key changes:

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,)' with 'UnityObjectToClipPos()'

I'm still unable to render the fractal you show in your screen shot using your proposed snippet, in Unity 5.6, when pasting into a new shader and connecting to a new material.

I'll continue to experiment. Thanks again...

sorry I haven't unity 5 in my computer you can use unity 2017 make a cube and attach a material with above shader then try to scale cube then you can see that fractal

I think this is what you want https://github.com/i-saint/Unity5Effects

KevinCain commented 5 years ago

Thanks, I'll re-open if needed after trying Unity 2017/2018.