Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
tentone committed Mar 23, 2019
1 parent 32ee214 commit 6750259
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 116 deletions.
140 changes: 26 additions & 114 deletions examples/js/exporters/DracoExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,146 +53,58 @@ THREE.DRACOExporter.prototype = {
var builder = new dracoEncoder.MeshBuilder();
var mesh = new dracoEncoder.Mesh();

if ( geometry.isBufferGeometry === true ) {
if ( geometry.isGeometry === true ) {

var vertices = geometry.getAttribute( 'position' );
builder.AddFloatAttributeToMesh( mesh, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
var bufferGeometry = new THREE.BufferGeometry();
bufferGeometry.fromGeometry(geometry);
geometry = bufferGeometry;

var faces = geometry.getIndex();
builder.AddFacesToMesh( mesh, faces.count, faces.array );

if ( options.exportNormals === true ) {

var normals = geometry.getAttribute( 'normal' );

if ( normals !== undefined ) {

builder.AddFloatAttributeToMesh( mesh, dracoEncoder.NORMAL, normals.count, normals.itemSize, normals.array );

}

}

if ( options.exportUvs === true ) {

var uvs = geometry.getAttribute( 'uv' );

if ( uvs !== undefined ) {

builder.AddFloatAttributeToMesh( mesh, dracoEncoder.TEX_COORD, uvs.count, uvs.itemSize, uvs.array );

}

}

if ( options.exportColor === true ) {

var colors = geometry.getAttribute( 'color' );

if ( colors !== undefined ) {

builder.AddFloatAttributeToMesh( mesh, dracoEncoder.COLOR, colors.count, colors.itemSize, colors.array );

}

}

} else if ( geometry.isGeometry === true ) {

var numFaces = geometry.faces.length;
var numPoints = geometry.vertices.length;
var numIndices = numFaces * 3;

// Faces

var indices = new Uint32Array( numIndices );

for ( var i = 0; i < numFaces; i ++ ) {

var index = i * 3;
indices[ index ] = geometry.faces[ i ].a;
indices[ index + 1 ] = geometry.faces[ i ].b;
indices[ index + 2 ] = geometry.faces[ i ].c;

}

builder.AddFacesToMesh( mesh, numFaces, indices );

// Vertex

var vertices = new Float32Array( geometry.vertices.length * 3 );

for ( var i = 0; i < geometry.vertices.length; i ++ ) {

var index = i * 3;
vertices[ index ] = geometry.vertices[ i ].x;
vertices[ index + 1 ] = geometry.vertices[ i ].y;
vertices[ index + 2 ] = geometry.vertices[ i ].z;

}
}

builder.AddFloatAttributeToMesh( mesh, dracoEncoder.POSITION, numPoints, 3, vertices );
console.log( geometry );

// Normals
var vertices = geometry.getAttribute( 'position' );
builder.AddFloatAttributeToMesh( mesh, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );

if ( options.exportNormals === true ) {
var faces = geometry.getIndex();

var normals = new Float32Array( geometry.vertices.length * 3 );
if ( faces !== null ) {

for ( var face of geometry.faces ) {
builder.AddFacesToMesh( mesh, faces.count, faces.array );

normals[ face.a * 3 ] = face.vertexNormals[ 0 ].x;
normals[ ( face.a * 3 ) + 1 ] = face.vertexNormals[ 0 ].y;
normals[ ( face.a * 3 ) + 2 ] = face.vertexNormals[ 0 ].z;
}

normals[ face.b * 3 ] = face.vertexNormals[ 1 ].x;
normals[ ( face.b * 3 ) + 1 ] = face.vertexNormals[ 1 ].y;
normals[ ( face.b * 3 ) + 2 ] = face.vertexNormals[ 1 ].z;
if ( options.exportNormals === true ) {

normals[ face.c * 3 ] = face.vertexNormals[ 2 ].x;
normals[ ( face.c * 3 ) + 1 ] = face.vertexNormals[ 2 ].y;
normals[ ( face.c * 3 ) + 2 ] = face.vertexNormals[ 2 ].z;
var normals = geometry.getAttribute( 'normal' );

}
if ( normals !== undefined ) {

builder.AddFloatAttributeToMesh( mesh, dracoEncoder.NORMAL, numPoints, 3, normals );
builder.AddFloatAttributeToMesh( mesh, dracoEncoder.NORMAL, normals.count, normals.itemSize, normals.array );

}

// Uvs

if ( options.exportUvs === true ) {

var uvs = new Uint32Array( geometry.vertices.length * 2 );
}

for ( var i = 0; i < geometry.faceVertexUvs.length; i ++ ) {
if ( options.exportUvs === true ) {

var index = i * 2;
uvs[ index ] = geometry.faceVertexUvs[ i ].x;
uvs[ index + 1 ] = geometry.faceVertexUvs[ i ].y;
var uvs = geometry.getAttribute( 'uv' );

}
if ( uvs !== undefined ) {

builder.AddFloatAttributeToMesh( mesh, dracoEncoder.TEX_COORD, numPoints, 2, uvs );
builder.AddFloatAttributeToMesh( mesh, dracoEncoder.TEX_COORD, uvs.count, uvs.itemSize, uvs.array );

}

// Color

if ( options.exportColor === true ) {

var colors = new Uint32Array( geometry.vertices.length * 3 );
}

for ( var i = 0; i < geometry.colors.length; i ++ ) {
if ( options.exportColor === true ) {

var index = i * 3;
colors[ index ] = geometry.colors[ i ].x;
colors[ index + 1 ] = geometry.colors[ i ].y;
colors[ index + 2 ] = geometry.colors[ i ].z;
var colors = geometry.getAttribute( 'color' );

}
if ( colors !== undefined ) {

builder.AddFloatAttributeToMesh( mesh, dracoEncoder.COLOR, numPoints, 3, colors );
builder.AddFloatAttributeToMesh( mesh, dracoEncoder.COLOR, colors.count, colors.itemSize, colors.array );

}

Expand Down
31 changes: 29 additions & 2 deletions examples/misc_exporter_draco.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
</head>
<body>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - exporter - draco<br/><br/> <button onclick="exportFile()">export</button>
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - exporter - draco<br/><br/>
<button onclick="exportFile()">export</button>
<button onclick="createGeometry()">geometry</button>
<button onclick="createBufferGeometry()">buffer geometry</button>
</div>

<script src="../build/three.js"></script>
Expand All @@ -53,6 +56,31 @@
init();
animate();

function createBufferGeometry() {

scene.remove( mesh );

var geometry = new THREE.TorusKnotBufferGeometry( 50, 15, 200, 30 );
var material = new THREE.MeshPhongMaterial( { color: 0x00ff00 } );
mesh = new THREE.Mesh( geometry, material );
mesh.castShadow = true;
mesh.position.y = 25;
scene.add( mesh );

}

function createGeometry() {

scene.remove( mesh );

var geometry = new THREE.TorusKnotGeometry( 50, 15, 200, 30 );
var material = new THREE.MeshPhongMaterial( { color: 0x00ff00 } );
mesh = new THREE.Mesh( geometry, material );
mesh.castShadow = true;
mesh.position.y = 25;
scene.add( mesh );
}

function init() {

camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
Expand Down Expand Up @@ -95,7 +123,6 @@

var geometry = new THREE.TorusKnotBufferGeometry( 50, 15, 200, 30 );
var material = new THREE.MeshPhongMaterial( { color: 0x00ff00 } );

mesh = new THREE.Mesh( geometry, material );
mesh.castShadow = true;
mesh.position.y = 25;
Expand Down

0 comments on commit 6750259

Please sign in to comment.