Skip to content

Instantly share code, notes, and snippets.

@kulicuu
Last active June 11, 2016 17:40
Show Gist options
  • Select an option

  • Save kulicuu/89fe18b1c4bbc42a63b95c72cb71c51d to your computer and use it in GitHub Desktop.

Select an option

Save kulicuu/89fe18b1c4bbc42a63b95c72cb71c51d to your computer and use it in GitHub Desktop.
some generative webGL nonsense
attribute vec2 a_position;
void main() {
gl_Position = vec4(a_position, 0, 1);
}
require './globals.coffee'
document.getElementsByTagName('body')[0].style.overflow = 'hidden'
vertex_shader_source = require('./shaders/vertex_shader_001_.glsl')
fragment_shader_source = require('./shaders/fragment_shader_000_.glsl')
canvas = document.getElementById('canvas_000')
canvas.width = window.innerWidth
canvas.height = window.innerHeight
# canvas.width = canvas.clientWidth
# canvas.height = canvas.clientHeight
gl = canvas.getContext 'experimental-webgl'#, preserveDrawingBuffer: true
fragment_shader = get_shader(gl, fragment_shader_source, gl.FRAGMENT_SHADER, 'FRAGMENT')
vertex_shader = get_shader(gl, vertex_shader_source, gl.VERTEX_SHADER, 'VERTEX')
# gl.viewport(0, 0, window.innerWidth, window.innerHeight)
# program = createProgramFromScripts gl, [vertex_shader, fragment_shader]
program = gl.createProgram()
gl.attachShader program, vertex_shader
gl.attachShader program, fragment_shader
gl.linkProgram program
gl.useProgram program
colorLocation = gl.getUniformLocation program, "u_color"
resolutionLocation = gl.getUniformLocation(program, "u_resolution")
gl.uniform2f(resolutionLocation, canvas.width, canvas.height)
positionLocation = gl.getAttribLocation program, "a_position"
buffer = gl.createBuffer()
gl.bindBuffer gl.ARRAY_BUFFER, buffer
gl.enableVertexAttribArray positionLocation
gl.vertexAttribPointer positionLocation, 2, gl.FLOAT, false, 0, 0
# resolutionLocation = gl.getUniformLocation(program, "u_resolution")
# gl.uniform2f(resolutionLocation, canvas.width, canvas.height)
# positionLocation = gl.getAttribLocation program, "a_position"
#
# buffer = gl.createBuffer()
#
# gl.bindBuffer gl.ARRAY_BUFFER, buffer
rayy = new Float32Array [
-1.0, -1.0,
1.0, -1.0,
-1.0, 1.0,
-1.0, 1.0,
1, -1.0,
1.0, 1.0
]
rayy4 = new Float32Array [
10, 20,
80, 20,
10, 50,
10, 50,
80, 20,
80, 50
]
rayy5 = new Float32Array [
50, 60,
480, 60,
50, 1580,
10, 30,
40, 20,
40, 50
]
# gl.enableVertexAttribArray positionLocation
# gl.vertexAttribPointer positionLocation, 2, gl.FLOAT, false, 0, 0
#
# gl.bufferData gl.ARRAY_BUFFER, rayy4s, gl.STATIC_DRAW
#
#
# gl.drawArrays gl.TRIANGLES, 0, 6
gl.bufferData gl.ARRAY_BUFFER, rayy4, gl.STATIC_DRAW
gl.drawArrays gl.TRIANGLES, 0, 6
# rayy2 = new Float32Array [
# -1.0, -1.0,
# 1.0, -1.0,
# -2.0, 1.0,
# -1.0, 1.0,
# 1, -1.0,
# 1.0, 1.0
# ]
set_rectangle = (gl, x, y, width, height) ->
x1 = x
x2 = x + width
y1 = y
y2 = y + height
rayy = new Float32Array [
x1, y1,
x2, y1,
x1, y2,
x1, y2,
x2, y1,
x2, y2
]
gl.bufferData gl.ARRAY_BUFFER, rayy, gl.STATIC_DRAW
random_int = (range) ->
return Math.floor(Math.random() * range)
random_int_negpos = (range) ->
if Math.random() > .5
return Math.floor(Math.random() * range)
else
return -(Math.floor(Math.random() * range))
outer = ->
gl.drawArrays gl.TRIANGLES, 0, 6
@gl = gl
for i in [0 .. 20]
set_rectangle @gl, random_int(200), random_int(200), random_int(50), random_int(50)
@gl.uniform4f colorLocation, Math.random(), Math.random(), Math.random(), 1
# outer()
@gl.drawArrays gl.TRIANGLES, 0, 6
counter = 0
set_rectangle gl, random_int(200), random_int(200), random_int(500), random_int(50)
gl.uniform4f colorLocation, Math.random(), Math.random(), Math.random(), 1
# outer()
gl.drawArrays gl.TRIANGLES, 0, 6
set_rectangle gl, random_int(200), random_int(200), random_int(50), random_int(500)
# @gl.uniform4f colorLocation, Math.random(), Math.random(), Math.random(), 1
# # outer()
# @gl.drawArrays gl.TRIANGLES, 0, 6
interval = setInterval ->
counter++
if counter < 1500
set_rectangle gl, counter + random_int_negpos(100), (.45 * counter) + random_int_negpos(100), random_int(200), random_int(250)
gl.uniform4f colorLocation, Math.random(), Math.random(), Math.random(), 1
gl.drawArrays gl.TRIANGLES, 0, 6
# set_rectangle gl, random_int(200), random_int(200), random_int(50), random_int(50)
# gl.uniform4f colorLocation, Math.random(), Math.random(), Math.random(), 1
# gl.drawArrays gl.TRIANGLES, 0, 6
# set_rectangle gl, random_int(200), random_int(200), random_int(150), random_int(150)
# gl.uniform4f colorLocation, Math.random(), Math.random(), Math.random(), 1
# gl.drawArrays gl.TRIANGLES, 0, 6
else
clearInterval interval
, 20
attribute vec2 a_position;
uniform vec2 u_resolution;
// varying vec4 v_color;
void main() {
//convert the rectangle from pixels to 0.0 to 0.1
vec2 zeroToOne = a_position / u_resolution;
//convert from 0->1 to 0->2
vec2 zeroToTwo = zeroToOne * 2.0;
//convert from 0->2 to -1->+1 (clipspace)
vec2 clipSpace = zeroToTwo - 1.0;
gl_Position = vec4((clipSpace * vec2(1, -1)), 0, 1);
// v_color = gl_Position * 30.385 + 2.8;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment