HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!DOCTYPE html>
<html>
<script id="vertex-shader" type="x-shader/x-vertex">
/* bump map vertex shader */
varying vec3 L; /* light vector in texture-space coordinates */
varying vec3 V; /* view vector in texture-space coordinates */
attribute vec2 vTexCoord;
attribute vec4 vPosition;
uniform vec4 normal;
uniform vec4 lightPosition;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat3 normalMatrix;
uniform vec3 objTangent; /* tangent vector in object coordinates */
varying vec2 fTexCoord;
void main()
{
gl_Position = projectionMatrix*modelViewMatrix*vPosition;
fTexCoord = vTexCoord;
vec3 eyePosition = (modelViewMatrix*vPosition).xyz;
vec3 eyeLightPos = (modelViewMatrix*lightPosition).xyz;
/* normal, tangent and binormal in eye coordinates */
vec3 N = normalize(normalMatrix*normal.xyz);
vec3 T = normalize(normalMatrix*objTangent);
vec3 B = cross(N, T);
/* light vector in texture space */
L.x = dot(T, eyeLightPos-eyePosition);
L.y = dot(B, eyeLightPos-eyePosition);
L.z = dot(N, eyeLightPos-eyePosition);
L = normalize(L);
/* view vector in texture space */
V.x = dot(T, -eyePosition);
V.y = dot(B, -eyePosition);
V.z = dot(N, -eyePosition);
V = normalize(V);
}
</script>
<script id="fragment-shader" type="x-shader/x-fragment">
precision mediump float;
varying vec3 L;
varying vec3 V;
varying vec2 fTexCoord;
uniform sampler2D texMap;
uniform vec4 diffuseProduct;
void main()
{
vec4 N = texture2D(texMap, fTexCoord);
vec3 NN = normalize(2.0*N.xyz-1.0);
vec3 LL = normalize(L);
float Kd = max(dot(NN, LL), 0.0);
gl_FragColor = vec4(Kd*diffuseProduct.xyz, 1.0);
}
</script>
<script type="text/javascript" src="https://www.cs.unm.edu/~angel/WebGL/7E/Common/webgl-utils.js"></script>
<script type="text/javascript" src="https://www.cs.unm.edu/~angel/WebGL/7E/Common/InitShaders.js"></script>
<script type="text/javascript" src="https://www.cs.unm.edu/~angel/WebGL/7E/Common/MV.js"></script>
<body>
<canvas id="gl-canvas" width="512" height="512">
Oops ... your browser doesn't support the HTML5 canvas element
</canvas>
</body>
</html>
var canvas;
var gl;
var texSize = 256;
// Bump Data
var data = new Array();
for (var i = 0; i <= texSize; i++) data[i] = new Array();
for (var i = 0; i <= texSize; i++)
for (var j = 0; j <= texSize; j++) data[i][j] = 0.0;
for (var i = texSize / 4; i < 3 * texSize / 4; i++)
for (var j = texSize / 4; j < 3 * texSize / 4; j++) data[i][j] = 1.0;
// Bump Map Normals
function computeBumpMapNormals(data) {
var normalst = new Array();
for (var i = 0; i < data.length-1; i++) normalst[i] = new Array();
for (var i = 0; i < data.length-1; i++)
for (var j = 0; j < data[0].length-1; j++) normalst[i][j] = new Array();
for (var i = 0; i < data.length-1; i++)
for (var j = 0; j < data[0].length-1; j++) {
normalst[i][j][0] = data[i][j] - data[i + 1][j];
normalst[i][j][1] = data[i][j] - data[i][j + 1];
normalst[i][j][2] = 1;
}
// Scale to [1, 0] Texture Coordinates
for (var i = 0; i < normalst.length; i++)
for (var j = 0; j < normalst[0].length; j++) {
var d = 0;
for (k = 0; k < 3; k++) d += normalst[i][j][k] * normalst[i][j][k];
d = Math.sqrt(d);
for (k = 0; k < 3; k++)
normalst[i][j][k] = 0.5 * normalst[i][j][k] / d + 0.5;
}
// Normal Texture flat array
var normals = new Uint8Array(3 * data.length * data[0].length);
for (var i = 0; i < normalst.length; i++)
for (var j = 0; j < normalst[0].length; j++)
for (var k = 0; k < 3; k++)
normals[3 * normalst[0].length * i + 3 * j + k] = 255 * normalst[i][j][k];
return normals;
}
var numVertices = 6;
var pointsArray = [];
var texCoordsArray = [];
var texCoord = [vec2(0, 0), vec2(0, 1), vec2(1, 1), vec2(1, 0)];
var vertices = [
vec4(0.0, 0.0, 0.0, 1.0),
vec4(1.0, 0.0, 0.0, 1.0),
vec4(1.0, 0.0, 1.0, 1.0),
vec4(0.0, 0.0, 1.0, 1.0)
];
var modelViewMatrix, projectionMatrix, normalMatrix;
var eye = vec3(2.0, 2.0, 2.0);
var at = vec3(0.5, 0.0, 0.5);
var up = vec3(0.0, 1.0, 0.0);
var normal = vec4(0.0, 1.0, 0.0, 0.0);
var tangent = vec3(1.0, 0.0, 0.0);
var lightPosition = vec4(0.0, 2.0, 0.0, 1.0);
var lightDiffuse = vec4(1.0, 1.0, 1.0, 1.0);
var materialDiffuse = vec4(0.7, 0.7, 0.7, 1.0);
var program;
var time = 0;
// from glMatrix.js
// Put in MV.js
function mat4ToInverseMat3(mat) {
dest = mat3();
var a00 = mat[0][0],
a01 = mat[0][1],
a02 = mat[0][2];
var a10 = mat[1][0],
a11 = mat[1][1],
a12 = mat[1][2];
var a20 = mat[2][0],
a21 = mat[2][1],
a22 = mat[2][2];
var b01 = a22 * a11 - a12 * a21;
var b11 = -a22 * a10 + a12 * a20;
var b21 = a21 * a10 - a11 * a20;
var d = a00 * b01 + a01 * b11 + a02 * b21;
if (!d) {
return null;
}
var id = 1 / d;
dest[0][0] = b01 * id;
dest[0][1] = (-a22 * a01 + a02 * a21) * id;
dest[0][2] = (a12 * a01 - a02 * a11) * id;
dest[1][0] = b11 * id;
dest[1][1] = (a22 * a00 - a02 * a20) * id;
dest[1][2] = (-a12 * a00 + a02 * a10) * id;
dest[2][0] = b21 * id;
dest[(2)[1]] = (-a21 * a00 + a01 * a20) * id;
dest[2][2] = (a11 * a00 - a01 * a10) * id;
return dest;
}
function configureTexture(image) {
texture = gl.createTexture();
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGB,
texSize,
texSize,
0,
gl.RGB,
gl.UNSIGNED_BYTE,
image
);
gl.generateMipmap(gl.TEXTURE_2D);
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_MIN_FILTER,
gl.NEAREST_MIPMAP_LINEAR
);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
}
function mesh() {
pointsArray.push(vertices[0]);
texCoordsArray.push(texCoord[0]);
pointsArray.push(vertices[1]);
texCoordsArray.push(texCoord[1]);
pointsArray.push(vertices[2]);
texCoordsArray.push(texCoord[2]);
pointsArray.push(vertices[2]);
texCoordsArray.push(texCoord[2]);
pointsArray.push(vertices[3]);
texCoordsArray.push(texCoord[3]);
pointsArray.push(vertices[0]);
texCoordsArray.push(texCoord[0]);
}
window.onload = function init() {
canvas = document.getElementById("gl-canvas");
gl = WebGLUtils.setupWebGL(canvas);
if (!gl) {
alert("WebGL isn't available");
}
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(1.0, 1.0, 1.0, 1.0);
gl.enable(gl.DEPTH_TEST);
//
// Load shaders and initialize attribute buffers
//
program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
modelViewMatrix = lookAt(eye, at, up);
projectionMatrix = ortho(-0.75, 0.75, -0.75, 0.75, -5.5, 5.5);
var normalMatrix = mat4ToInverseMat3(modelViewMatrix);
mesh();
var vBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(pointsArray), gl.STATIC_DRAW);
var vPosition = gl.getAttribLocation(program, "vPosition");
gl.vertexAttribPointer(vPosition, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vPosition);
var tBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, tBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(texCoordsArray), gl.STATIC_DRAW);
var vTexCoord = gl.getAttribLocation(program, "vTexCoord");
gl.vertexAttribPointer(vTexCoord, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vTexCoord);
var normals = computeBumpMapNormals(data);
configureTexture(normals);
var diffuseProduct = mult(lightDiffuse, materialDiffuse);
gl.uniform4fv(
gl.getUniformLocation(program, "diffuseProduct"),
flatten(diffuseProduct)
);
gl.uniform4fv(
gl.getUniformLocation(program, "lightPosition"),
flatten(lightPosition)
);
gl.uniform4fv(gl.getUniformLocation(program, "normal"), flatten(normal));
gl.uniform3fv(gl.getUniformLocation(program, "objTangent"), flatten(tangent));
gl.uniformMatrix4fv(
gl.getUniformLocation(program, "modelViewMatrix"),
false,
flatten(modelViewMatrix)
);
gl.uniformMatrix4fv(
gl.getUniformLocation(program, "projectionMatrix"),
false,
flatten(projectionMatrix)
);
gl.uniformMatrix3fv(
gl.getUniformLocation(program, "normalMatrix"),
false,
flatten(normalMatrix)
);
render();
};
render = function() {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
lightPosition[0] = 5.5 * Math.sin(0.01 * time);
lightPosition[2] = 5.5 * Math.cos(0.01 * time);
time += 1;
gl.uniform4fv(
gl.getUniformLocation(program, "lightPosition"),
flatten(lightPosition)
);
gl.drawArrays(gl.TRIANGLES, 0, numVertices);
requestAnimFrame(render);
};
Also see: Tab Triggers