Last active
August 31, 2017 14:48
-
-
Save cchang62/4ff94ab1abf92db2acd45a120a59622f to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/fuhitu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<!--Ref. https://jsbin.com/fuhitu/1/edit?html,js,output --> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.3.2/gl-matrix-min.js"></script> | |
<script id="shader-vs" type="x-shader/x-vertex"> | |
attribute vec4 coords; | |
attribute float pointSize; | |
uniform mat4 transformMatrix; | |
attribute vec4 colors; | |
varying vec4 varyingColors; | |
void main(void) { | |
gl_Position = transformMatrix * coords; | |
gl_PointSize = pointSize; | |
varyingColors = colors; | |
} | |
</script> | |
<script id="shader-fs" type="x-shader/x-fragment"> | |
precision mediump float; | |
uniform vec4 color; | |
varying vec4 varyingColors; | |
void main(void) { | |
gl_FragColor = varyingColors; | |
} | |
</script> | |
</head> | |
<body> | |
<canvas id="canvas" width="600" height="600"></canvas> | |
<script id="jsbin-javascript"> | |
var gl, | |
shaderProgram, | |
vertices, | |
matrix = mat4.create(), | |
vertexCount = 30; | |
initGL(); | |
createShaders(); | |
createVertices(); | |
draw(); | |
function initGL() { | |
var canvas = document.getElementById("canvas"); | |
console.log(canvas); | |
gl = canvas.getContext("webgl"); | |
gl.enable(gl.DEPTH_TEST); | |
gl.viewport(0, 0, canvas.width, canvas.height); | |
gl.clearColor(1, 1, 1, 1); | |
} | |
function createShaders() { | |
var vertexShader = getShader(gl, "shader-vs"); | |
var fragmentShader = getShader(gl, "shader-fs"); | |
shaderProgram = gl.createProgram(); | |
gl.attachShader(shaderProgram, vertexShader); | |
gl.attachShader(shaderProgram, fragmentShader); | |
gl.linkProgram(shaderProgram); | |
gl.useProgram(shaderProgram); | |
} | |
function createVertices() { | |
vertices = []; | |
var colors = []; | |
for(var i = 0; i < vertexCount; i++) { | |
vertices.push(Math.random() * 2 - 1); | |
vertices.push(Math.random() * 2 - 1); | |
vertices.push(Math.random() * 2 - 1); | |
colors.push(Math.random()); | |
colors.push(Math.random()); | |
colors.push(Math.random()); | |
colors.push(1); | |
} | |
var buffer = gl.createBuffer(); | |
gl.bindBuffer(gl.ARRAY_BUFFER, buffer); | |
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); | |
var coords = gl.getAttribLocation(shaderProgram, "coords"); | |
gl.vertexAttribPointer(coords, 3, gl.FLOAT, false, 0, 0); | |
gl.enableVertexAttribArray(coords); | |
gl.bindBuffer(gl.ARRAY_BUFFER, null); | |
var colorBuffer = gl.createBuffer(); | |
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); | |
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW); | |
var colorsLocation = gl.getAttribLocation(shaderProgram, "colors"); | |
gl.vertexAttribPointer(colorsLocation, 4, gl.FLOAT, false, 0, 0); | |
gl.enableVertexAttribArray(colorsLocation); | |
gl.bindBuffer(gl.ARRAY_BUFFER, null); | |
var pointSize = gl.getAttribLocation(shaderProgram, "pointSize"); | |
gl.vertexAttrib1f(pointSize, 20); | |
// var color = gl.getUniformLocation(shaderProgram, "color"); | |
// gl.uniform4f(color, 0, 0, 0, 1); | |
} | |
function draw() { | |
mat4.rotateX(matrix, matrix, -0.007); | |
mat4.rotateY(matrix, matrix, 0.013); | |
mat4.rotateZ(matrix, matrix, 0.01); | |
var transformMatrix = gl.getUniformLocation(shaderProgram, "transformMatrix"); | |
gl.uniformMatrix4fv(transformMatrix, false, matrix); | |
gl.clear(gl.COLOR_BUFFER_BIT); | |
gl.drawArrays(gl.TRIANGLES, 0, vertexCount); | |
requestAnimationFrame(draw); | |
} | |
/* | |
* https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Adding_2D_content_to_a_WebGL_context | |
*/ | |
function getShader(gl, id) { | |
var shaderScript, theSource, currentChild, shader; | |
shaderScript = document.getElementById(id); | |
if (!shaderScript) { | |
return null; | |
} | |
theSource = ""; | |
currentChild = shaderScript.firstChild; | |
while (currentChild) { | |
if (currentChild.nodeType == currentChild.TEXT_NODE) { | |
theSource += currentChild.textContent; | |
} | |
currentChild = currentChild.nextSibling; | |
} | |
if (shaderScript.type == "x-shader/x-fragment") { | |
shader = gl.createShader(gl.FRAGMENT_SHADER); | |
} else if (shaderScript.type == "x-shader/x-vertex") { | |
shader = gl.createShader(gl.VERTEX_SHADER); | |
} else { | |
// Unknown shader type | |
return null; | |
} | |
gl.shaderSource(shader, theSource); | |
// Compile the shader program | |
gl.compileShader(shader); | |
// See if it compiled successfully | |
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { | |
alert("An error occurred compiling the shaders: " + gl.getShaderInfoLog(shader)); | |
return null; | |
} | |
return shader; | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var gl, | |
shaderProgram, | |
vertices, | |
matrix = mat4.create(), | |
vertexCount = 30; | |
initGL(); | |
createShaders(); | |
createVertices(); | |
draw(); | |
function initGL() { | |
var canvas = document.getElementById("canvas"); | |
console.log(canvas); | |
gl = canvas.getContext("webgl"); | |
gl.enable(gl.DEPTH_TEST); | |
gl.viewport(0, 0, canvas.width, canvas.height); | |
gl.clearColor(1, 1, 1, 1); | |
} | |
function createShaders() { | |
var vertexShader = getShader(gl, "shader-vs"); | |
var fragmentShader = getShader(gl, "shader-fs"); | |
shaderProgram = gl.createProgram(); | |
gl.attachShader(shaderProgram, vertexShader); | |
gl.attachShader(shaderProgram, fragmentShader); | |
gl.linkProgram(shaderProgram); | |
gl.useProgram(shaderProgram); | |
} | |
function createVertices() { | |
vertices = []; | |
var colors = []; | |
for(var i = 0; i < vertexCount; i++) { | |
vertices.push(Math.random() * 2 - 1); | |
vertices.push(Math.random() * 2 - 1); | |
vertices.push(Math.random() * 2 - 1); | |
colors.push(Math.random()); | |
colors.push(Math.random()); | |
colors.push(Math.random()); | |
colors.push(1); | |
} | |
var buffer = gl.createBuffer(); | |
gl.bindBuffer(gl.ARRAY_BUFFER, buffer); | |
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); | |
var coords = gl.getAttribLocation(shaderProgram, "coords"); | |
gl.vertexAttribPointer(coords, 3, gl.FLOAT, false, 0, 0); | |
gl.enableVertexAttribArray(coords); | |
gl.bindBuffer(gl.ARRAY_BUFFER, null); | |
var colorBuffer = gl.createBuffer(); | |
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); | |
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW); | |
var colorsLocation = gl.getAttribLocation(shaderProgram, "colors"); | |
gl.vertexAttribPointer(colorsLocation, 4, gl.FLOAT, false, 0, 0); | |
gl.enableVertexAttribArray(colorsLocation); | |
gl.bindBuffer(gl.ARRAY_BUFFER, null); | |
var pointSize = gl.getAttribLocation(shaderProgram, "pointSize"); | |
gl.vertexAttrib1f(pointSize, 20); | |
// var color = gl.getUniformLocation(shaderProgram, "color"); | |
// gl.uniform4f(color, 0, 0, 0, 1); | |
} | |
function draw() { | |
mat4.rotateX(matrix, matrix, -0.007); | |
mat4.rotateY(matrix, matrix, 0.013); | |
mat4.rotateZ(matrix, matrix, 0.01); | |
var transformMatrix = gl.getUniformLocation(shaderProgram, "transformMatrix"); | |
gl.uniformMatrix4fv(transformMatrix, false, matrix); | |
gl.clear(gl.COLOR_BUFFER_BIT); | |
gl.drawArrays(gl.TRIANGLES, 0, vertexCount); | |
requestAnimationFrame(draw); | |
} | |
/* | |
* https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Adding_2D_content_to_a_WebGL_context | |
*/ | |
function getShader(gl, id) { | |
var shaderScript, theSource, currentChild, shader; | |
shaderScript = document.getElementById(id); | |
if (!shaderScript) { | |
return null; | |
} | |
theSource = ""; | |
currentChild = shaderScript.firstChild; | |
while (currentChild) { | |
if (currentChild.nodeType == currentChild.TEXT_NODE) { | |
theSource += currentChild.textContent; | |
} | |
currentChild = currentChild.nextSibling; | |
} | |
if (shaderScript.type == "x-shader/x-fragment") { | |
shader = gl.createShader(gl.FRAGMENT_SHADER); | |
} else if (shaderScript.type == "x-shader/x-vertex") { | |
shader = gl.createShader(gl.VERTEX_SHADER); | |
} else { | |
// Unknown shader type | |
return null; | |
} | |
gl.shaderSource(shader, theSource); | |
// Compile the shader program | |
gl.compileShader(shader); | |
// See if it compiled successfully | |
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { | |
alert("An error occurred compiling the shaders: " + gl.getShaderInfoLog(shader)); | |
return null; | |
} | |
return shader; | |
} | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gl, | |
shaderProgram, | |
vertices, | |
matrix = mat4.create(), | |
vertexCount = 30; | |
initGL(); | |
createShaders(); | |
createVertices(); | |
draw(); | |
function initGL() { | |
var canvas = document.getElementById("canvas"); | |
console.log(canvas); | |
gl = canvas.getContext("webgl"); | |
gl.enable(gl.DEPTH_TEST); | |
gl.viewport(0, 0, canvas.width, canvas.height); | |
gl.clearColor(1, 1, 1, 1); | |
} | |
function createShaders() { | |
var vertexShader = getShader(gl, "shader-vs"); | |
var fragmentShader = getShader(gl, "shader-fs"); | |
shaderProgram = gl.createProgram(); | |
gl.attachShader(shaderProgram, vertexShader); | |
gl.attachShader(shaderProgram, fragmentShader); | |
gl.linkProgram(shaderProgram); | |
gl.useProgram(shaderProgram); | |
} | |
function createVertices() { | |
vertices = []; | |
var colors = []; | |
for(var i = 0; i < vertexCount; i++) { | |
vertices.push(Math.random() * 2 - 1); | |
vertices.push(Math.random() * 2 - 1); | |
vertices.push(Math.random() * 2 - 1); | |
colors.push(Math.random()); | |
colors.push(Math.random()); | |
colors.push(Math.random()); | |
colors.push(1); | |
} | |
var buffer = gl.createBuffer(); | |
gl.bindBuffer(gl.ARRAY_BUFFER, buffer); | |
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); | |
var coords = gl.getAttribLocation(shaderProgram, "coords"); | |
gl.vertexAttribPointer(coords, 3, gl.FLOAT, false, 0, 0); | |
gl.enableVertexAttribArray(coords); | |
gl.bindBuffer(gl.ARRAY_BUFFER, null); | |
var colorBuffer = gl.createBuffer(); | |
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); | |
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW); | |
var colorsLocation = gl.getAttribLocation(shaderProgram, "colors"); | |
gl.vertexAttribPointer(colorsLocation, 4, gl.FLOAT, false, 0, 0); | |
gl.enableVertexAttribArray(colorsLocation); | |
gl.bindBuffer(gl.ARRAY_BUFFER, null); | |
var pointSize = gl.getAttribLocation(shaderProgram, "pointSize"); | |
gl.vertexAttrib1f(pointSize, 20); | |
// var color = gl.getUniformLocation(shaderProgram, "color"); | |
// gl.uniform4f(color, 0, 0, 0, 1); | |
} | |
function draw() { | |
mat4.rotateX(matrix, matrix, -0.007); | |
mat4.rotateY(matrix, matrix, 0.013); | |
mat4.rotateZ(matrix, matrix, 0.01); | |
var transformMatrix = gl.getUniformLocation(shaderProgram, "transformMatrix"); | |
gl.uniformMatrix4fv(transformMatrix, false, matrix); | |
gl.clear(gl.COLOR_BUFFER_BIT); | |
gl.drawArrays(gl.TRIANGLES, 0, vertexCount); | |
requestAnimationFrame(draw); | |
} | |
/* | |
* https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Adding_2D_content_to_a_WebGL_context | |
*/ | |
function getShader(gl, id) { | |
var shaderScript, theSource, currentChild, shader; | |
shaderScript = document.getElementById(id); | |
if (!shaderScript) { | |
return null; | |
} | |
theSource = ""; | |
currentChild = shaderScript.firstChild; | |
while (currentChild) { | |
if (currentChild.nodeType == currentChild.TEXT_NODE) { | |
theSource += currentChild.textContent; | |
} | |
currentChild = currentChild.nextSibling; | |
} | |
if (shaderScript.type == "x-shader/x-fragment") { | |
shader = gl.createShader(gl.FRAGMENT_SHADER); | |
} else if (shaderScript.type == "x-shader/x-vertex") { | |
shader = gl.createShader(gl.VERTEX_SHADER); | |
} else { | |
// Unknown shader type | |
return null; | |
} | |
gl.shaderSource(shader, theSource); | |
// Compile the shader program | |
gl.compileShader(shader); | |
// See if it compiled successfully | |
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { | |
alert("An error occurred compiling the shaders: " + gl.getShaderInfoLog(shader)); | |
return null; | |
} | |
return shader; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment