• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Shaders Importing shadertoy to project is giving weird results

ajrdesign

Member
I'm trying to import a shadertoy shader into my project, I've followed a tutorial to troubleshoot compile errors and pass the right uniforms but for some reason the shader is only producing one value for the "noise" it's creating.

What's supposed to happen is it sets up noise at different color values to give a terrain effect but for some reason it's setting all the noise to one color (color<0.1).

Here's the code:

Draw event:
Code:
if enabled //only apply shader if the enabled = 1
{
    shader_params = shader_get_uniform(shader3, "iResolution");
    shader_set_uniform_f(shader_params, 512.0, 512.0);
    shader_set(shader3)
}

draw_background(background,0,0)
shader_reset()
draw_text(16,16,string_hash_to_newline("Enabled: "+string(enabled)))
Vertex Shader
Code:
//
// Simple passthrough vertex shader
//
attribute vec3 in_Position;                  // (x,y,z)
//attribute vec3 in_Normal;                  // (x,y,z)     unused in this shader.
attribute vec4 in_Colour;                    // (r,g,b,a)
attribute vec2 in_TextureCoord;              // (u,v)

varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec2 fragCoord;

void main()
{
    vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
   
    v_vColour = in_Colour;
    v_vTexcoord = in_TextureCoord;
   fragCoord = in_Position.xy;
}
Fragment Shader
Code:
uniform vec2       iResolution;           // viewport resolution (in pixels)
varying vec2       fragCoord;

//reference : https://www.shadertoy.com/view/MtcXDr#
float noise (vec3 n)
{
   return fract(sin(dot(n, vec3(95.43583, 93.323197, 94.993431))) * 65536.32);
}

float perlin_a (vec3 n)
{
    vec3 base = floor(n * 64.0) * 0.015625;
    vec3 dd = vec3(0.015625, 0.0, 0.0);
    float a = noise(base);
    float b = noise(base + dd.xyy);
    float c = noise(base + dd.yxy);
    float d = noise(base + dd.xxy);
    vec3 p = (n - base) * 64.0;
    float t = mix(a, b, p.x);
    float tt = mix(c, d, p.x);
    return mix(t, tt, p.y);
}

float perlin_b (vec3 n)
{
    vec3 base = vec3(n.x, n.y, floor(n.z * 64.0) * 0.015625);
    vec3 dd = vec3(0.015625, 0.0, 0.0);
    vec3 p = (n - base) *  64.0;
    float front = perlin_a(base + dd.yyy);
    float back = perlin_a(base + dd.yyx);
    return mix(front, back, p.z);
}

float fbm(vec3 n)
{
    float total = 0.0;
    float m1 = 1.0;
    float m2 = 0.1;
    for (int i = 0; i < 5; i++)
    {
        total += perlin_b(n * m1) * m2;
        m2 *= 2.0;
        m1 *= 0.5;
    }
    return total;
}

vec3 heightmap (vec3 n)
{
   return vec3(fbm((5.0 * n) + fbm((5.0 * n) * 3.0 - 1000.0) * 0.05),0,0);
}

void main()
{
   vec2 uv = (2.0*fragCoord.xy - iResolution.xy)/iResolution.y;
   float color = clamp(vec4(vec3((heightmap(vec3(uv.xy*5.0,2)*0.02)-1.0)),1.0).r,0.0,1.0);
    if (color<0.1) gl_FragColor=vec4(0.77,0.90,0.98,1.0);
    else
    if (color<0.2) gl_FragColor=vec4(0.82,0.92,0.99,1.0);
    else
    if (color<0.3) gl_FragColor=vec4(0.91,0.97,0.99,1.0);
    else
    if (color<0.55) gl_FragColor=vec4(0.62,0.75,0.59,1.0);
    else
    if (color<0.65) gl_FragColor=vec4(0.86,0.90,0.68,1.0);
    else
    if (color<0.75) gl_FragColor=vec4(0.99,0.99,0.63,1.0);
    else
    if (color<0.85) gl_FragColor=vec4(0.99,0.83,0.59,1.0);
    else
    if (color<0.95) gl_FragColor=vec4(0.98,0.71,0.49,1.0);     
    else
    if (color<0.99) gl_FragColor=vec4(0.98,0.57,0.47,1.0);       
   else     
    gl_FragColor=vec4(0.79,0.48,0.43,1.0);           
}
Anyone see what's happening here?
 

Attachments

Top