Skip to content
Dealga McArdle edited this page Dec 6, 2018 · 2 revisions

Example 1

vertex_shader = '''
    uniform mat4 viewProjectionMatrix;

    in vec3 position;
    out vec3 pos;

    void main()
    {
        pos = position;
        gl_Position = viewProjectionMatrix * vec4(position, 1.0f);
    }
'''

fragment_shader = '''
    uniform float brightness;
    uniform vec3 viewEyeVec;

    in vec3 pos;

    void main()
    {
        float a = floor(sin(pos.x * 5.2) * 0.8) + 0.4;
        float a2 = floor(sin(pos.x * 2.2) * 2.1) + 0.2;
        a = a + a2;
        a = 2.0 * sin(a / 4.0 + a2 / 12.0);
        float b = floor(sin(pos.y * 8.2) * 0.7) + 0.4;
        float c = floor(sin(pos.z * 1.6) * 2.2) * brightness / 0.7;
        
        vec3 fa = viewEyeVec - vec3(a, b, c);
                
        gl_FragColor = vec4(fa, 1.0);
    }
'''

def draw_fragment(context, args):
    geom, config = args
    batch = config.batch
    shader = config.shader  #  GPUShader(vertex_shader, fragment_shader) 

    shader.bind()
    matrix = context.region_data.perspective_matrix
    region3d = context.space_data.region_3d
    eye = region3d.view_matrix[2][:3]
    
    shader.uniform_float("viewProjectionMatrix", matrix)
    shader.uniform_float("viewEyeVec", eye)
    shader.uniform_float("brightness", 0.5)
    batch.draw(shader)

image

example 2

adding custom functions into the shader.. This doesn't do what I hoped, but it does make a working function

vertex_shader = '''
    uniform mat4 viewProjectionMatrix;

    in vec3 position;
    out vec3 pos;

    void main()
    {
        pos = position;
        gl_Position = viewProjectionMatrix * vec4(position, 1.0f);
    }
'''

fragment_shader = '''
    uniform float brightness;
    uniform vec3 viewEyeVec;

    in vec3 pos;

    float distance(in vec3 a, in vec3 b) {
     
        vec3 v = vec3(a[0]-b[0], a[1]-b[1], a[2]-b[2]);
        float dist = sqrt((v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]));
        return (dist);
    }


    void main()
    {
        //float a = floor(sin(pos.x * 5.2) * 0.8) + 0.4;
        //float a2 = floor(sin(pos.x * 2.2) * 2.1) + 0.2;
        //a = a + a2;
        //a = 2.0 * sin(a / 4.0 + a2 / 12.0);
        //float b = floor(sin(pos.y * 8.2) * 0.7) + 0.4;
                
        vec3 fa = vec3(0.4, 0.5, brightness);
        float dister = distance(viewEyeVec, pos) * 2; 
        fa.x = fa.x / (dister);
        fa.y = fa.y / (dister);
        fa.z = fa.z / (dister);
                
        gl_FragColor = vec4(fa, 1.0);
    }
'''

def draw_fragment(context, args):
    geom, config = args
    batch = config.batch
    shader = config.shader  #  GPUShader(vertex_shader, fragment_shader) 

    shader.bind()
    matrix = context.region_data.perspective_matrix
    region3d = context.space_data.region_3d
    eye = region3d.view_matrix[2][:3]
    
    shader.uniform_float("viewProjectionMatrix", matrix)
    shader.uniform_float("viewEyeVec", eye)
    shader.uniform_float("brightness", 0.5)
    batch.draw(shader)
Clone this wiki locally