A Pen by Joshua Boyd on CodePen.
Last active
December 15, 2015 23:15
-
-
Save jd-boyd/9992ba44778d3701ea81 to your computer and use it in GitHub Desktop.
Shader Template
This file contains 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
<canvas id="gl" style="border: none;" width="400" height="400"></canvas> | |
<script id="shader-fs" type="x-shader/x-fragment"> | |
// from https://www.shadertoy.com/view/MdX3zr | |
precision mediump float; | |
uniform vec3 iResolution; // viewport resolution (in pixels) | |
uniform float iGlobalTime; // shader playback time (in seconds) | |
float noise(vec3 p) //Thx to Las^Mercury | |
{ | |
vec3 i = floor(p); | |
vec4 a = dot(i, vec3(1., 57., 21.)) + vec4(0., 57., 21., 78.); | |
vec3 f = cos((p-i)*acos(-1.))*(-.5)+.5; | |
a = mix(sin(cos(a)*a),sin(cos(1.+a)*(1.+a)), f.x); | |
a.xy = mix(a.xz, a.yw, f.y); | |
return mix(a.x, a.y, f.z); | |
} | |
float sphere(vec3 p, vec4 spr) | |
{ | |
return length(spr.xyz-p) - spr.w; | |
} | |
float flame(vec3 p) | |
{ | |
float d = sphere(p*vec3(1.,.5,1.), vec4(.0,-1.,.0,1.)); | |
return d + (noise(p+vec3(.0,iGlobalTime*2.,.0)) + noise(p*3.)*.5)*.25*(p.y) ; | |
} | |
float scene(vec3 p) | |
{ | |
return min(100.-length(p) , abs(flame(p)) ); | |
} | |
vec4 raymarch(vec3 org, vec3 dir) | |
{ | |
float d = 0.0, glow = 0.0, eps = 0.02; | |
vec3 p = org; | |
bool glowed = false; | |
for(int i=0; i<64; i++) | |
{ | |
d = scene(p) + eps; | |
p += d * dir; | |
if( d>eps ) | |
{ | |
if(flame(p) < .0) | |
glowed=true; | |
if(glowed) | |
glow = float(i)/64.; | |
} | |
} | |
return vec4(p,glow); | |
} | |
void main() | |
{ | |
vec2 v = -1.0 + 2.0 * gl_FragCoord.xy / iResolution.xy; | |
v.x *= iResolution.x/iResolution.y; | |
vec3 org = vec3(0., -2., 4.); | |
vec3 dir = normalize(vec3(v.x*1.6, -v.y, -1.5)); | |
vec4 p = raymarch(org, dir); | |
float glow = p.w; | |
//blue to orange gradiant | |
vec4 col = mix(vec4(1.0,0.5,0.1,1.0), vec4(0.1,0.5,1.0,1.0), p.y*0.02+0.4); | |
gl_FragColor = mix(vec4(vec3(0.0), 1.0), col, pow(glow*2.0,4.0)); | |
} | |
</script> | |
<script id="shader-vs" type="x-shader/x-vertex"> | |
attribute vec3 aVertexPosition; | |
uniform mat4 uMVMatrix; | |
uniform mat4 uPMatrix; | |
void main(void) { | |
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); | |
} | |
</script> |
This file contains 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; | |
function initGL(canvas) { | |
try { | |
gl = canvas.getContext("webgl"); | |
gl.viewportWidth = canvas.width; | |
gl.viewportHeight = canvas.height; | |
} catch (e) { | |
} | |
if (!gl) { | |
console.log("Could not initialise WebGL, sorry :-("); | |
} | |
} | |
//from 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)) { | |
console.error("An error occurred compiling the shaders: ", | |
gl.getShaderInfoLog(shader)); | |
return null; | |
} | |
return shader; | |
} | |
var shaderProgram; | |
function initShaders() { | |
var fragmentShader = getShader(gl, "shader-fs"); | |
var vertexShader = getShader(gl, "shader-vs"); | |
shaderProgram = gl.createProgram(); | |
gl.attachShader(shaderProgram, vertexShader); | |
gl.attachShader(shaderProgram, fragmentShader); | |
gl.linkProgram(shaderProgram); | |
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { | |
console.error("Could not initialise shaders"); | |
} | |
gl.useProgram(shaderProgram); | |
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition"); | |
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute); | |
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, | |
"uPMatrix"); | |
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, | |
"uMVMatrix"); | |
shaderProgram.iResolution = gl.getUniformLocation(shaderProgram, | |
"iResolution"); | |
shaderProgram.iGlobalTime = gl.getUniformLocation(shaderProgram, | |
"iGlobalTime"); | |
} | |
var mvMatrix = mat4.create(); | |
var pMatrix = mat4.create(); | |
var iResolution = vec3.fromValues(400, 400, 0); | |
function setMatrixUniforms() { | |
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix); | |
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix); | |
gl.uniform3fv(shaderProgram.iResolution, iResolution); | |
} | |
var squareVertexPositionBuffer; | |
function initBuffers() { | |
squareVertexPositionBuffer = gl.createBuffer(); | |
gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer); | |
vertices = [ | |
1.0, 1.0, 0.0, | |
-1.0, 1.0, 0.0, | |
1.0, -1.0, 0.0, | |
-1.0, -1.0, 0.0 | |
]; | |
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), | |
gl.STATIC_DRAW); | |
squareVertexPositionBuffer.itemSize = 3; | |
squareVertexPositionBuffer.numItems = 4; | |
} | |
function drawScene() { | |
var time = ((new Date()).getTime() - orig_time) / 1000.0; | |
//console.log("draw", time); | |
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight); | |
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); | |
mat4.perspective(pMatrix, 45, | |
gl.viewportWidth / gl.viewportHeight, 0.1, 100.0); | |
mat4.identity(mvMatrix); | |
mvMatrix = mat4.translate(mvMatrix,mvMatrix, [0, 0.0, -2.0]); | |
gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer); | |
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, | |
squareVertexPositionBuffer.itemSize, | |
gl.FLOAT, false, 0, 0); | |
setMatrixUniforms(); | |
gl.uniform1f(shaderProgram.iGlobalTime, time); | |
gl.drawArrays(gl.TRIANGLE_STRIP, 0, | |
squareVertexPositionBuffer.numItems); | |
requestAnimationFrame(drawScene); | |
} | |
var date_obj = new Date(); | |
var orig_time = date_obj.getTime(); | |
function webGLStart() { | |
var canvas = document.getElementById("gl"); | |
initGL(canvas); | |
initShaders(); | |
initBuffers(); | |
gl.clearColor(0.0, 0.0, 0.0, 1.0); | |
gl.enable(gl.DEPTH_TEST); | |
drawScene(); | |
} | |
webGLStart(); |
This file contains 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
<script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.3.1/gl-matrix-min.js"></script> |
A Pen by Joshua Boyd on CodePen.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment