Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Vertice sharing between faces and UV support

  • Loading branch information...
commit 7c67b02eec021e43938ada27538070e9e4a7222e 1 parent 31cffea
@chandlerprall authored
Showing with 61 additions and 34 deletions.
  1. +21 −0 LICENSE
  2. +1 −12 ThreeCSG.js
  3. +39 −22 examples.html
  4. BIN  texture.png
View
21 LICENSE
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2012 Chandler Prall. All rights reserved.
+
+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.
View
13 ThreeCSG.js
@@ -136,18 +136,16 @@ window.ThreeBSP = (function() {
vertice_dict = {},
vertex_idx_a, vertex_idx_b, vertex_idx_c,
vertex, face,
- verticeNormals, verticeUvs;
+ verticeUvs;
for ( i = 0; i < polygon_count; i++ ) {
polygon = polygons[i];
polygon_vertice_count = polygon.vertices.length;
for ( j = 2; j < polygon_vertice_count; j++ ) {
- verticeNormals = [];
verticeUvs = [];
vertex = polygon.vertices[0];
- verticeNormals.push( vertex.normal );
verticeUvs.push( new THREE.UV( vertex.uv.x, vertex.uv.y ) );
vertex = new THREE.Vector3( vertex.x, vertex.y, vertex.z );
matrix.multiplyVector3( vertex );
@@ -160,7 +158,6 @@ window.ThreeBSP = (function() {
}
vertex = polygon.vertices[j-1];
- verticeNormals.push( vertex.normal );
verticeUvs.push( new THREE.UV( vertex.uv.x, vertex.uv.y ) );
vertex = new THREE.Vector3( vertex.x, vertex.y, vertex.z );
matrix.multiplyVector3( vertex );
@@ -172,7 +169,6 @@ window.ThreeBSP = (function() {
}
vertex = polygon.vertices[j];
- verticeNormals.push( vertex.normal );
verticeUvs.push( new THREE.UV( vertex.uv.x, vertex.uv.y ) );
vertex = new THREE.Vector3( vertex.x, vertex.y, vertex.z );
matrix.multiplyVector3( vertex );
@@ -190,11 +186,6 @@ window.ThreeBSP = (function() {
new THREE.Vector3( polygon.normal.x, polygon.normal.y, polygon.normal.z )
);
- face.vertexNormals.push(
- verticeNormals[0],
- verticeNormals[1],
- verticeNormals[2]
- );
geometry.faces.push( face );
geometry.faceVertexUvs[0].push( verticeUvs );
}
@@ -257,7 +248,6 @@ window.ThreeBSP = (function() {
this.w *= -1;
for ( i = this.vertices.length - 1; i >= 0; i-- ) {
- //this.vertices[i].normal.multiplyScalar( -1 );
vertices.push( this.vertices[i] );
};
this.vertices = vertices;
@@ -348,7 +338,6 @@ window.ThreeBSP = (function() {
}
};
-
ThreeBSP.Vertex = function( x, y, z, normal, uv ) {
this.x = x;
this.y = y;
View
61 examples.html
@@ -4,11 +4,11 @@
<head>
-<script type="text/javascript" src="http://chandler.prallfamily.com/threebuilds/builds/r49/ThreeDebug.js"></script>
+<script type="text/javascript" src="http://www.chandlerprall.com/threebuilds/builds/r49/ThreeDebug.js"></script>
<script type="text/javascript" src="ThreeCSG.js"></script>
<script type="text/javascript">
-var renderer, scene, camera;
+var renderer, scene, camera, light;
window.onload = function() {
@@ -18,39 +18,47 @@
scene = new THREE.Scene();
+ light = new THREE.DirectionalLight( 0xffffff );
+ light.position.set( 1, 1, 1 ).normalize();
+ scene.add( light );
+
camera = new THREE.PerspectiveCamera(
35,
window.innerWidth / window.innerHeight,
1,
1000
);
- camera.position.set(15, 15, 35);
- camera.lookAt(scene.position);
- scene.add(camera);
-
-
+ camera.position.set( 5, 5, 15 );
+ camera.lookAt( scene.position );
+ scene.add( camera );
+
// Example #1 - Cube (mesh) subtract Sphere (mesh)
(function() {
+ var start_time = (new Date()).getTime();
+
var cube_geometry = new THREE.CubeGeometry( 3, 3, 3 );
var cube_mesh = new THREE.Mesh( cube_geometry );
- cube_mesh.position.x = -10;
+ cube_mesh.position.x = -7;
var cube_bsp = new ThreeBSP( cube_mesh );
var sphere_geometry = new THREE.SphereGeometry( 1.8, 32, 32 );
var sphere_mesh = new THREE.Mesh( sphere_geometry );
- sphere_mesh.position.x = -9.9;
+ sphere_mesh.position.x = -7;
var sphere_bsp = new ThreeBSP( sphere_mesh );
var subtract_bsp = cube_bsp.subtract( sphere_bsp );
-
- scene.add(
- subtract_bsp.toMesh( new THREE.MeshNormalMaterial )
- );
+ var result = subtract_bsp.toMesh( new THREE.MeshLambertMaterial({ shading: THREE.SmoothShading, map: THREE.ImageUtils.loadTexture('texture.png') }) );
+ result.geometry.computeVertexNormals();
+ scene.add( result );
+
+ console.log( 'Example 1: ' + ((new Date()).getTime() - start_time) + 'ms' );
})();
// Example #2 - Sphere (geometry) union Cube (geometry)
(function() {
+ var start_time = (new Date()).getTime();
+
var sphere_geometry = new THREE.SphereGeometry( 2, 16, 16 );
var sphere_bsp = new ThreeBSP( sphere_geometry );
@@ -59,14 +67,18 @@
var union_bsp = sphere_bsp.union( cube_bsp );
- scene.add(
- union_bsp.toMesh( new THREE.MeshNormalMaterial )
- );
+ var result = union_bsp.toMesh( new THREE.MeshLambertMaterial({ shading: THREE.SmoothShading, map: THREE.ImageUtils.loadTexture('texture.png') }) );
+ result.geometry.computeVertexNormals();
+ scene.add( result );
+
+ console.log( 'Example 2: ' + ((new Date()).getTime() - start_time) + 'ms' );
})();
- // Example #2 - Sphere (geometry) intersect Sphere (mesh)
+ // Example #3 - Sphere (geometry) intersect Sphere (mesh)
(function() {
+ var start_time = (new Date()).getTime();
+
var sphere_geometry_1 = new THREE.SphereGeometry( 2, 64, 8 );
var sphere_bsp_1 = new ThreeBSP( sphere_geometry_1 );
@@ -77,13 +89,18 @@
var intersect_bsp = sphere_bsp_1.intersect( sphere_bsp_2 );
- var result_mesh = intersect_bsp.toMesh( new THREE.MeshNormalMaterial );
- result_mesh.position.x = 10;
- scene.add( result_mesh );
+ var result = intersect_bsp.toMesh( new THREE.MeshLambertMaterial({ shading: THREE.SmoothShading, map: THREE.ImageUtils.loadTexture('texture.png') }) );
+ result.position.x = 6;
+ result.geometry.computeVertexNormals();
+ scene.add( result );
+
+ console.log( 'Example 3: ' + ((new Date()).getTime() - start_time) + 'ms' );
})();
-
- renderer.render(scene, camera);
+ (function render() {
+ requestAnimationFrame( render );
+ renderer.render(scene, camera);
+ })();
}
</script>
View
BIN  texture.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Please sign in to comment.
Something went wrong with that request. Please try again.