Example #1
0
def vertex_shader(
    index: (RES_INPUT, "VertexId", i32),
    pos: (RES_OUTPUT, "Position", vec4),
    color: (RES_OUTPUT, 0, vec3),
):
    positions = [vec2(+0.0, -0.5), vec2(+0.5, +0.5), vec2(-0.5, +0.7)]
    p = positions[index]
    pos = vec4(p, 0.0, 1.0)  # noqa
    color = vec3(p, 0.5)  # noqa
Example #2
0
def vertex_shader(
        index=("input", "VertexId", i32),
        out_pos=("output", "Position", vec4),
        out_color=("output", 0, vec3),
):
    positions = [vec2(+0.0, -0.5), vec2(+0.5, +0.5), vec2(-0.5, +0.7)]

    p = positions[index]
    out_pos = vec4(p, 0.0, 1.0)  # noqa
    out_color = vec3(p, 0.5)  # noqa
Example #3
0
 def compute_shader(
         index_xyz: ("input", "GlobalInvocationId", ivec3),
         data1: ("buffer", 0, Array(vec4)),
         data2: ("buffer", 1, Array(vec4)),
 ):
     index = index_xyz.x
     v = data1[index]
     v1 = mix(v.x, v.y, v.z)
     v2 = mix(vec2(v.x, v.x), vec2(v.y, v.y), v.z)
     data2[index] = vec4(v1, v2.x, v2.y, 0.0)
Example #4
0
def fragment_shader(
        in_tex: (RES_TEXTURE, 0, "2d f32"),
        in_uv: (RES_INPUT, 0, vec2),
        in_rot: (RES_UNIFORM, 1, vec4),
        out_color: (RES_OUTPUT, 0, vec4),
):
    # Distance squared to center of circle
    d2 = in_uv.x**2 + in_uv.y**2

    # Check if outside of map boundary
    if d2 > 1.0:
        # density = math.exp(50.0 * (1.0 - r))  # Display atmosphere
        out_color = vec4(0, 0, 0, 0)
    else:
        # Camera rotation
        in_uv_rot = vec2(
            in_uv.x * math.cos(in_rot.z) + in_uv.y * math.sin(in_rot.z),
            in_uv.y * math.cos(in_rot.z) - in_uv.x * math.sin(in_rot.z))

        # Map transformation
        c = math.sqrt(1.0 - d2)
        lam = in_rot.x + math.atan2(
            in_uv_rot.x,
            c * math.cos(in_rot.y) - in_uv_rot.y * math.sin(in_rot.y))
        phi = math.asin(c * math.sin(in_rot.y) +
                        in_uv_rot.y * math.cos(in_rot.y))

        # Get coordinates of color on texture
        uvCoord = vec2((lam / math.pi + 1.0) % 2.0 - 1.0, 2.0 * phi / math.pi)
        texCoords = ivec2((uvCoord + vec2(1, -1)) * vec2(800, -400))

        # Get normal in globe reference frame
        norm = math.normalize(
            vec3(
                math.cos(phi) * math.cos(lam),
                math.cos(phi) * math.sin(lam), math.sin(phi)))

        # Set light direction
        light = vec3(math.sin(in_rot.w), math.cos(in_rot.w), -.1 * in_rot.z)
        # Get light intensity
        intensity = norm @ math.normalize(light)
        # Set ambient lighting
        ambient = .1
        # Check if in shadow
        if intensity < 0.0:
            shading = vec4(ambient, ambient, ambient, 1)
        else:
            intensity += ambient
            shading = vec4(intensity, intensity, intensity, 1)

        # Get texture color and apply shading
        out_color = in_tex.read(texCoords).zyxw * shading  # noqa
Example #5
0
 def vertex_shader(
     index: (RES_INPUT, "VertexId", i32),
     pos: (RES_OUTPUT, "Position", vec4),
     pointsize: (RES_OUTPUT, "PointSize", f32),
 ):
     positions = [
         vec2(-0.5, -0.5),
         vec2(-0.5, +0.5),
         vec2(+0.5, -0.5),
         vec2(+0.5, +0.5),
     ]
     p = positions[index]
     pos = vec4(p, 0.0, 1.0)  # noqa
     pointsize = 16.0  # noqa
Example #6
0
def vertex_shader(
        index: ("input", "VertexId", i32),
        pos: ("output", "Position", vec4),
        tcoord: ("output", 0, vec2),
):
    positions = [
        vec2(-0.5, -0.5),
        vec2(-0.5, +0.5),
        vec2(+0.5, -0.5),
        vec2(+0.5, +0.5),
    ]
    p = positions[index]
    pos = vec4(p, 0.0, 1.0)  # noqa
    tcoord = vec2(p + 0.5)  # noqa - map to 0..1
Example #7
0
 def compute_shader(
         index: ("input", "GlobalInvocationId", ivec3),
         data1: ("buffer", 0, Array(ivec2)),
         data2: ("buffer", 1, Array(vec2)),
 ):
     i = index.x
     data2[i] = vec2(data1[i])
Example #8
0
def compute_shader_tex_add(
        index=("input", "GlobalInvocationId", ivec3),
        tex1=("texture", 0, "2d rg16i"),
        tex2=("texture", 1, "2d r32f"),
):
    val = vec2(tex1.read(index.xy).xy)  # ivec2 -> vec2
    tex2.write(index.xy, vec4(val.x + val.y, 0.0, 0.0, 0.0))
Example #9
0
 def fragment_shader(
         tex: ("texture", 0, "2d i32"),
         sampler: ("sampler", 1, ""),
         tcoord: ("input", 0, vec2),
         out_color: ("output", 0, vec4),
 ):
     val = vec2(tex.sample(sampler, tcoord).rg)
     out_color = vec4(val.rg / 255.0, 0.0, 1.0)  # noqa
Example #10
0
 def compute_shader(
         index_xyz: ("input", "GlobalInvocationId", ivec3),
         data1: ("buffer", 0, Array(f32)),
         data2: ("buffer", 1, Array(vec2)),
 ):
     index = index_xyz.x
     a = data1[index]
     data2[index] = vec2(a + 1.0, a - 1.0) + 20.0
Example #11
0
 def compute_shader(
         index_xyz: ("input", "GlobalInvocationId", ivec3),
         data1: ("buffer", 0, Array(f32)),
         data2: ("buffer", 1, Array(vec2)),
 ):
     index = index_xyz.x
     v = data1[index]
     data2[index] = normalize(vec2(v, v))
Example #12
0
 def compute_shader(
         index_xyz: ("input", "GlobalInvocationId", ivec3),
         data1: ("buffer", 0, Array(vec2)),
         data2: ("buffer", 1, Array(vec2)),
 ):
     index = index_xyz.x
     a = data1[index]
     data2[index] = vec2(a.x % a.y, math.fmod(a.x, a.y))
Example #13
0
 def compute_shader(
         index_xyz: ("input", "GlobalInvocationId", ivec3),
         data1: ("buffer", 0, Array(f32)),
         data2: ("buffer", 1, Array(f32)),
 ):
     index = index_xyz.x
     a = vec2(data1[index], data1[index])
     data2[index] = a @ a
Example #14
0
 def compute_shader(
         index: ("input", "GlobalInvocationId", ivec3),
         data2: ("buffer", 1, "Array(vec2)"),
 ):
     i = f32(index.x)
     a, b = 1.0, 2.0  # Cover Python storing this as a tuple const
     c, d = a + i, b + 1.0
     data2[index.x] = vec2(c, d)
Example #15
0
 def compute_shader(
         index_xyz: ("input", "GlobalInvocationId", ivec3),
         data1: ("buffer", 0, Array(f32)),
         data2: ("buffer", 1, Array(i32)),
         data3: ("buffer", 2, Array(vec2)),
 ):
     index = index_xyz.x
     v1 = abs(data1[index])  # float
     v2 = abs(data2[index])  # int
     data3[index] = vec2(f32(v1), v2)
Example #16
0
 def compute_shader(
         index_xyz: ("input", "GlobalInvocationId", ivec3),
         data1: ("buffer", 0, Array(f32)),
         data2: ("buffer", 1, Array(vec2)),
 ):
     index = index_xyz.x
     a = data1[index]
     a -= -1.0
     b = vec2(a, a)
     b += 2.0
     data2[index] = b
Example #17
0
def fragment_shader_gaussian(
    in_size: ("input", 0, f32),
    in_point_coord: ("input", "PointCoord", vec2),
    u_points: ("uniform", (1, 0), PointsMaterial.uniform_type),
    out_color: ("output", 0, vec4),
):
    color = u_points.color
    pcoord_pixels = in_point_coord * in_size
    hsize = 0.5 * (in_size - 1.5)
    sigma = hsize / 3.0
    d = distance(pcoord_pixels, vec2(hsize, hsize))
    if d <= hsize:
        t = d / sigma
        a = exp(-0.5 * t * t)
        out_color = vec4(color.rgb, color.a * a)  # noqa - shader output
    else:
        return  # discard
Example #18
0
def fragment_shader_points(
    in_size: ("input", 0, f32),
    in_point_coord: ("input", "PointCoord", vec2),
    u_points: ("uniform", (1, 0), PointsMaterial.uniform_type),
    out_color: ("output", 0, vec4),
):
    # See https://github.com/vispy/vispy/blob/master/vispy/visuals/markers.py
    color = u_points.color
    pcoord_pixels = in_point_coord * in_size
    hsize = 0.5 * (in_size - 1.5)
    aa_width = 1.0
    d = distance(pcoord_pixels, vec2(hsize, hsize))
    if d <= hsize - 0.5 * aa_width:
        out_color = u_points.color.rgba  # noqa - shader output
    elif d <= hsize + 0.5 * aa_width:
        alpha = 0.5 + (hsize - d) / aa_width
        alpha = alpha ** 2  # this works better
        out_color = vec4(color.rgb, color.a * alpha)  # noqa - shader output
    else:
        return  # discard
Example #19
0
def vertex_shader(
        index: (RES_INPUT, "VertexId", i32),  # noqa
        pos: (RES_OUTPUT, "Position", vec4),  # noqa
        uv: (RES_OUTPUT, 0, vec2),
):
    # 6 vertices. TODO: Update with uniform to keep ratio
    positions = [
        vec2(1, 1),
        vec2(-1, 1),
        vec2(-1, -1),
        vec2(1, 1),
        vec2(1, -1),
        vec2(-1, -1)
    ]
    p = positions[index]
    pos = vec4(p, 0.0, 1.0)  # noqa
    uv = p  # noqa
Example #20
0
 def vertex_shader():
     positions = [vec2(0.0, 1.0), vec2(0.0, 1.0), vec2(0.0, 1.0)]  # noqa
Example #21
0
 def compute_shader3(index: ("input", "GlobalInvocationId", ivec3), ):
     v = vec2(3.0, 4.0)
     a, b = v