Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Add an option to supply a THREE.js instance to the Mesh constructor #5

Merged
merged 1 commit into from over 1 year ago

2 participants

Leo Lännenmäki Max Ogden
Leo Lännenmäki

Because transitive dependencies get inserted in to the browserify bundle multiple times (see substack/node-browserify#186) THREE.js can get loaded and instantiated multiple times in a single voxel-engine app.

This causes problems because for example instanceof checks fail for objects created in one Three instance and passed to another.

In my case

var mesh = new Mesh(voxelData);
game.scene.add(mesh.createSurfaceMesh());

Fails because the

mesh instanceof Object3d

fails inside Three.js

Max Ogden maxogden merged commit 530fe51 into from
Max Ogden
Owner

Thanks! Any idea how we could fix three.js?

Leo Lännenmäki

Sorry, no idea. It seems riddled with instanceof checks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 05, 2013
Leo Lännenmäki leolannenmaki Add an option to supply a THREE.js instance 013421b
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 24 additions and 23 deletions. Show diff stats Hide diff stats

  1. +24 23 index.js
47 index.js
... ... @@ -1,15 +1,16 @@
1 1 var THREE = require('three')
2 2
3   -module.exports = function(data, mesher, scaleFactor) {
4   - return new Mesh(data, mesher, scaleFactor)
  3 +module.exports = function(data, mesher, scaleFactor, three) {
  4 + return new Mesh(data, mesher, scaleFactor, three)
5 5 }
6 6
7 7 module.exports.Mesh = Mesh
8 8
9   -function Mesh(data, mesher, scaleFactor) {
  9 +function Mesh(data, mesher, scaleFactor, three) {
  10 + this.THREE = three || THREE
10 11 this.data = data
11   - var geometry = this.geometry = new THREE.Geometry()
12   - this.scale = scaleFactor || new THREE.Vector3(10, 10, 10)
  12 + var geometry = this.geometry = new this.THREE.Geometry()
  13 + this.scale = scaleFactor || new this.THREE.Vector3(10, 10, 10)
13 14
14 15 var result = mesher( data.voxels, data.dims )
15 16 this.meshed = result
@@ -19,7 +20,7 @@ function Mesh(data, mesher, scaleFactor) {
19 20
20 21 for (var i = 0; i < result.vertices.length; ++i) {
21 22 var q = result.vertices[i]
22   - geometry.vertices.push(new THREE.Vector3(q[0], q[1], q[2]))
  23 + geometry.vertices.push(new this.THREE.Vector3(q[0], q[1], q[2]))
23 24 }
24 25
25 26 for (var i = 0; i < result.faces.length; ++i) {
@@ -27,13 +28,13 @@ function Mesh(data, mesher, scaleFactor) {
27 28
28 29 var q = result.faces[i]
29 30 if (q.length === 5) {
30   - var f = new THREE.Face4(q[0], q[1], q[2], q[3])
31   - f.color = new THREE.Color(q[4])
  31 + var f = new this.THREE.Face4(q[0], q[1], q[2], q[3])
  32 + f.color = new this.THREE.Color(q[4])
32 33 f.vertexColors = [f.color,f.color,f.color,f.color]
33 34 geometry.faces.push(f)
34 35 } else if (q.length == 4) {
35   - var f = new THREE.Face3(q[0], q[1], q[2])
36   - f.color = new THREE.Color(q[3])
  36 + var f = new this.THREE.Face3(q[0], q[1], q[2])
  37 + f.color = new this.THREE.Color(q[3])
37 38 f.vertexColors = [f.color,f.color,f.color]
38 39 geometry.faces.push(f)
39 40 }
@@ -51,7 +52,7 @@ function Mesh(data, mesher, scaleFactor) {
51 52 }
52 53
53 54 Mesh.prototype.createWireMesh = function(hexColor) {
54   - var wireMaterial = new THREE.MeshBasicMaterial({
  55 + var wireMaterial = new this.THREE.MeshBasicMaterial({
55 56 color : hexColor || 0xffffff,
56 57 wireframe : true
57 58 })
@@ -63,8 +64,8 @@ Mesh.prototype.createWireMesh = function(hexColor) {
63 64 }
64 65
65 66 Mesh.prototype.createSurfaceMesh = function(material) {
66   - material = material || new THREE.MeshNormalMaterial()
67   - var surfaceMesh = new THREE.Mesh( this.geometry, material )
  67 + material = material || new this.THREE.MeshNormalMaterial()
  68 + var surfaceMesh = new this.THREE.Mesh( this.geometry, material )
68 69 surfaceMesh.scale = this.scale
69 70 surfaceMesh.doubleSided = false
70 71 this.surfaceMesh = surfaceMesh
@@ -77,8 +78,8 @@ Mesh.prototype.addToScene = function(scene) {
77 78 }
78 79
79 80 Mesh.prototype.setPosition = function(x, y, z) {
80   - if (this.wireMesh) this.wireMesh.position = new THREE.Vector3(x, y, z)
81   - if (this.surfaceMesh) this.surfaceMesh.position = new THREE.Vector3(x, y, z)
  81 + if (this.wireMesh) this.wireMesh.position = new this.THREE.Vector3(x, y, z)
  82 + if (this.surfaceMesh) this.surfaceMesh.position = new this.THREE.Vector3(x, y, z)
82 83 }
83 84
84 85 Mesh.prototype.faceVertexUv = function(i) {
@@ -133,17 +134,17 @@ Mesh.prototype.faceVertexUv = function(i) {
133 134 }
134 135 if ((size.z === 0 && spans.x0 < spans.x1) || (size.x === 0 && spans.y0 > spans.y1)) {
135 136 return [
136   - new THREE.Vector2(height, 0),
137   - new THREE.Vector2(0, 0),
138   - new THREE.Vector2(0, width),
139   - new THREE.Vector2(height, width)
  137 + new this.THREE.Vector2(height, 0),
  138 + new this.THREE.Vector2(0, 0),
  139 + new this.THREE.Vector2(0, width),
  140 + new this.THREE.Vector2(height, width)
140 141 ]
141 142 } else {
142 143 return [
143   - new THREE.Vector2(0, 0),
144   - new THREE.Vector2(0, height),
145   - new THREE.Vector2(width, height),
146   - new THREE.Vector2(width, 0)
  144 + new this.THREE.Vector2(0, 0),
  145 + new this.THREE.Vector2(0, height),
  146 + new this.THREE.Vector2(width, height),
  147 + new this.THREE.Vector2(width, 0)
147 148 ]
148 149 }
149 150 }

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.