Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<!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>

              
            
!

CSS

              
                
              
            
!

JS

              
                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);
};

              
            
!
999px

Console