From 9625531bad2be64a123133bb566055f89ecaf4e4 Mon Sep 17 00:00:00 2001
From: Chris Kelley 
Date: Mon, 13 May 2013 15:46:46 +0200
Subject: [PATCH 001/256] bump 3.js to 0.58.9 and voxel-view to repository
---
 package.json | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/package.json b/package.json
index 7473b3d..0ba2e77 100644
--- a/package.json
+++ b/package.json
@@ -9,7 +9,7 @@
   "dependencies": {
     "voxel": "0.3.1",
     "voxel-mesh": "0.2.1",
-    "voxel-view": "0.0.6",
+    "voxel-view": "git://github.com/snagy/voxel-view.git",
     "voxel-chunks": "0.0.2",
     "voxel-raycast": "0.2.1",
     "voxel-control": "0.0.7",
@@ -17,7 +17,7 @@
     "voxel-physical": "0.0.8",
     "voxel-region-change": "0.1.0",
     "raf": "0.0.1",
-    "three": "0.56.0",
+    "three": "0.58.9",
     "pin-it": "0.0.1",
     "aabb-3d": "0.0.0",
     "inherits": "1.0.0",
From 3e7058da5eaf8c9db4d1e428b6fd6b4bd8dfd3cf Mon Sep 17 00:00:00 2001
From: Chris Kelley 
Date: Mon, 13 May 2013 16:27:13 +0200
Subject: [PATCH 002/256] Checking for browser when loading materials
Blows up in server otherwise.
---
 index.js | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/index.js b/index.js
index 918756e..1437cc5 100644
--- a/index.js
+++ b/index.js
@@ -86,13 +86,15 @@ function Game(opts) {
   this.chunksNeedsUpdate = {}
   // contains new chunks yet to be generated. Handled by game.loadPendingChunks
   this.pendingChunks = []
-
-  this.materials = texture({
-    game: this,
-    texturePath: opts.texturePath || './textures/',
-    materialType: opts.materialType || THREE.MeshLambertMaterial,
-    materialParams: opts.materialParams || {}
-  })
+  
+  if (process.browser) {
+    this.materials = texture({
+      game: this,
+      texturePath: opts.texturePath || './textures/',
+      materialType: opts.materialType || THREE.MeshLambertMaterial,
+      materialParams: opts.materialParams || {}
+    })
+  }
 
   this.materialNames = opts.materials || [['grass', 'dirt', 'grass_dirt'], 'brick', 'dirt']
   
From 0f57d9dfbe095ce86f182d2e70c2c0a1f7927ca9 Mon Sep 17 00:00:00 2001
From: Mikola Lysenko 
Date: Mon, 8 Jul 2013 16:08:29 -0500
Subject: [PATCH 003/256] initial version
---
 .gitignore        | 15 +++++++++
 LICENSE           | 22 +++++++++++++
 index.js          | 80 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/createMesh.js | 73 ++++++++++++++++++++++++++++++++++++++++++
 lib/examples.js   | 72 ++++++++++++++++++++++++++++++++++++++++++
 lib/wireShader.js | 16 ++++++++++
 6 files changed, 278 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 LICENSE
 create mode 100644 index.js
 create mode 100644 lib/createMesh.js
 create mode 100644 lib/examples.js
 create mode 100644 lib/wireShader.js
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0d1b0b0
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,15 @@
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+npm-debug.log
+node_modules/*
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..8ce206a
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+
+The MIT License (MIT)
+
+Copyright (c) 2013 Mikola Lysenko
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..08dfcef
--- /dev/null
+++ b/index.js
@@ -0,0 +1,80 @@
+"use strict"
+
+var shell = require("gl-now")()
+var camera = require("game-shell-orbit-camera")(shell)
+var createTileMap = require("gl-tile-map")
+var ndarray = require("ndarray")
+var terrain = require("isabella-texture-pack")
+var createWireShader = require("./lib/wireShader.js")
+var createAOShader = require("ao-shader")
+var examples = require("./lib/examples.js")
+var createVoxelMesh = require("./lib/createMesh.js")
+var glm = require("gl-matrix")
+var mat4 = glm.mat4
+
+//Tile size parameters
+var TILE_SIZE = Math.floor(terrain.shape[0] / 16)|0
+
+//Config variables
+var texture, shader, mesh, wireShader
+
+function selectMesh(name) {
+  mesh = createVoxelMesh(shell.gl, name, examples[name])
+  var c = mesh.center
+  camera.lookAt([c[0]+mesh.radius*2, c[1], c[2]], c, [0,1,0])
+}
+
+shell.on("gl-init", function() {
+  var gl = shell.gl
+
+  //Create shaders
+  shader = createAOShader(gl)
+  wireShader = createWireShader(gl)
+  
+  //Select bunny mesh
+  selectMesh("terrain")
+  
+  //Create texture atlas
+  var tiles = ndarray(terrain.data,
+    [16,16,terrain.shape[0]>>4,terrain.shape[1]>>4,4],
+    [terrain.stride[0]*16, terrain.stride[1]*16, terrain.stride[0], terrain.stride[1], terrain.stride[2]], 0)
+  texture = createTileMap(gl, tiles, 2)
+  texture.mipSamples = 4
+})
+
+shell.on("gl-render", function(t) {
+  var gl = shell.gl
+ 
+  //Calculation projection matrix
+  var projection = mat4.perspective(new Float32Array(16), Math.PI/4.0, shell.width/shell.height, 1.0, 1000.0)
+  var model = mat4.identity(new Float32Array(16))
+  var view = camera.view()
+  
+  gl.enable(gl.CULL_FACE)
+  gl.enable(gl.DEPTH_TEST)
+
+  //Bind the shader
+  shader.bind()
+  shader.attributes.attrib0.location = 0
+  shader.attributes.attrib1.location = 1
+  shader.uniforms.projection = projection
+  shader.uniforms.view = view
+  shader.uniforms.model = model
+  shader.uniforms.tileSize = TILE_SIZE
+  shader.uniforms.tileMap = texture.bind()
+  
+  mesh.triangleVAO.bind()
+  gl.drawArrays(gl.TRIANGLES, 0, mesh.triangleVertexCount)
+  mesh.triangleVAO.unbind()
+
+  //Bind the wire shader
+  wireShader.bind()
+  wireShader.attributes.position.location = 0
+  wireShader.uniforms.projection = projection
+  wireShader.uniforms.model = model
+  wireShader.uniforms.view = view
+  
+  mesh.wireVAO.bind()
+  gl.drawArrays(gl.LINES, 0, mesh.wireVertexCount)
+  mesh.wireVAO.unbind()
+})
diff --git a/lib/createMesh.js b/lib/createMesh.js
new file mode 100644
index 0000000..10f0d15
--- /dev/null
+++ b/lib/createMesh.js
@@ -0,0 +1,73 @@
+"use strict"
+
+var ndarray = require("ndarray")
+var createBuffer = require("gl-buffer")
+var createVAO = require("gl-vao")
+var createAOMesh = require("ao-mesher")
+var ops = require("ndarray-ops")
+
+var cached = {}
+
+//Creates a mesh from a set of voxels
+function createVoxelMesh(gl, name, voxels) {
+  if(name in cached) {
+    return cached[name]
+  }
+  
+  //Create mesh
+  var vert_data = createAOMesh(voxels)
+  
+  //Upload triangle mesh to WebGL
+  var triangleVertexCount = Math.floor(vert_data.length/8)
+  var vert_buf = createBuffer(gl, vert_data)
+  var triangleVAO = createVAO(gl, undefined, [
+    { "buffer": vert_buf,
+      "type": gl.UNSIGNED_BYTE,
+      "size": 4,
+      "offset": 0,
+      "stride": 8,
+      "normalized": false
+    },
+    { "buffer": vert_buf,
+      "type": gl.UNSIGNED_BYTE,
+      "size": 4,
+      "offset": 4,
+      "stride": 8,
+      "normalized": false
+    }
+  ])
+  
+  //Create wire mesh
+  var wireVertexCount = 2 * triangleVertexCount
+  var wireVertexArray = ndarray(new Uint8Array(wireVertexCount * 3), [triangleVertexCount, 2, 3])
+  var trianglePositions = ndarray(vert_data, [triangleVertexCount, 3], [8, 1], 0)
+  ops.assign(wireVertexArray.pick(undefined, 0, undefined), trianglePositions)
+  var wires = wireVertexArray.pick(undefined, 1, undefined)
+  for(var i=0; i<3; ++i) {
+    ops.assign(wires.lo(i).step(3), trianglePositions.lo((i+1)%3).step(3))
+  }
+  var wireBuf = createBuffer(gl, wireVertexArray.data)
+  var wireVAO = createVAO(gl, undefined, [
+    { "buffer": wireBuf,
+      "type": gl.UNSIGNED_BYTE,
+      "size": 3,
+      "offset": 0,
+      "stride": 3,
+      "normalized": false
+    }
+  ])
+  
+  //Bundle result and return
+  var result = {
+    triangleVertexCount: triangleVertexCount,
+    triangleVAO: triangleVAO,
+    wireVertexCount: wireVertexCount,
+    wireVAO: wireVAO,
+    center: [voxels.shape[0]>>1, voxels.shape[1]>>1, voxels.shape[2]>>1],
+    radius: voxels.shape[2]
+  }
+  cached[name] = result
+  return result
+}
+
+module.exports = createVoxelMesh
\ No newline at end of file
diff --git a/lib/examples.js b/lib/examples.js
new file mode 100644
index 0000000..8a35864
--- /dev/null
+++ b/lib/examples.js
@@ -0,0 +1,72 @@
+"use strict"
+
+var ndarray = require("ndarray")
+var fill = require("ndarray-fill")
+var ops = require("ndarray-ops")
+var lazyProperty = require("lazy-property")
+
+//Fill ndarray
+function makeFill(name, size, func) {
+  lazyProperty(exports, name, function() {
+    var result = ndarray(new Int32Array(size[0]*size[1]*size[2]), size)
+    fill(result, func)
+    return result
+  }, true)
+}
+
+//Sphere
+makeFill("sphere", [32,32,32], function(i,j,k) {
+  var x = i - 16
+  var y = j - 16
+  var z = k - 16
+  return (x*x + y*y + z*z) < 30 ? (1<<15) + 0x81 : 0
+})
+
+//Cuboid
+makeFill("box", [32,32,32], function(i,j,k) {
+  var x = Math.abs(i - 16)
+  var y = Math.abs(j - 16)
+  var z = Math.abs(k - 16)
+  return (x*x + y*y + z*z) < 12 ? (1<<15) + 0x19 : 0
+})
+
+//Terrain
+makeFill("terrain", [33,33,33], function(i,j,k) {
+  if(i <=1 || i>=31 || j <= 1 || j >= 31 || k <= 1 || k >= 31) {
+    return 0
+  }
+  var h0 = 3.0 * Math.sin(Math.PI * i / 12.0 - Math.PI * k * 0.1) + 27
+  if(j > h0+1) {
+    return 0
+  }
+  if(h0 <= j) {
+    return (1<<15)+0x19
+  }
+  var h1 = 2.0 * Math.sin(Math.PI * i * 0.25 - Math.PI * k * 0.3) + 20
+  if(h1 <= j) {
+    return (1<<15)+0x20
+  }
+  if(4 < j) {
+    return Math.random() < 0.1 ? (1<<15)+0x23 : (1<<15)+0x10
+  }
+  return (1<<15)+0xff
+})
+
+//Random
+makeFill("random", [16,16,16], function(i,j,k) {
+  if(Math.random() < 0.6) {
+    return 0
+  }
+  return ((Math.random()*255)|0) + (1<<15)
+})
+
+/*
+//Create a bunny
+var bunny = require("bunny")
+var voxelize = require("voxelize")
+lazyProperty(exports, "bunny", function() {
+  var result = voxelize(bunny.cells, bunny.positions, 0.1)
+  ops.mulseq(result.voxels, (1<<15)+0x10)
+  return result.voxels
+}, true)
+*/
\ No newline at end of file
diff --git a/lib/wireShader.js b/lib/wireShader.js
new file mode 100644
index 0000000..fbef92a
--- /dev/null
+++ b/lib/wireShader.js
@@ -0,0 +1,16 @@
+"use strict"
+var createShader = require("gl-shader")
+
+module.exports = function(gl) {
+  return createShader(gl,
+"attribute vec3 position;\
+uniform mat4 projection;\
+uniform mat4 view;\
+uniform mat4 model;\
+void main() {\
+  gl_Position=projection * view * model * vec4(position, 1.0);\
+}",
+"void main() {\
+  gl_FragColor = vec4(0, 1, 0, 1);\
+}")
+}
\ No newline at end of file
From d6060b00c74720daf4f94c2e65746c329be3491d Mon Sep 17 00:00:00 2001
From: Mikola Lysenko 
Date: Mon, 8 Jul 2013 21:26:08 -0500
Subject: [PATCH 004/256] added github ribbon
---
 index.html          | 31 +++++++++++++++++++
 lib/examples.js     | 50 ++++++++++++++++++++-----------
 main.css            | 12 ++++++++
 index.js => main.js | 73 +++++++++++++++++++++++++++++++++++----------
 package.json        | 47 +++++++++++++++++++++++++++++
 5 files changed, 181 insertions(+), 32 deletions(-)
 create mode 100644 index.html
 create mode 100644 main.css
 rename index.js => main.js (56%)
 create mode 100644 package.json
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..b7d18a1
--- /dev/null
+++ b/index.html
@@ -0,0 +1,31 @@
+
+
+
+  
+  Voxel Mipmap Demo
+  
+
+
+   +
+  
+    
+      
+      
+    
+    
+      
+      
+    
+    
+      
+      
+    
+  
   
+  Your browser can't handle WebGL!