Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions examples/js/exporters/OBJExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ THREE.OBJExporter.prototype = {
var indexNormals = 0;

var vertex = new THREE.Vector3();
var color = new THREE.Color();
var normal = new THREE.Vector3();
var uv = new THREE.Vector2();

Expand Down Expand Up @@ -235,6 +236,69 @@ THREE.OBJExporter.prototype = {

};

var parsePoints = function ( points ) {

var nbVertex = 0;

var geometry = points.geometry;

if ( geometry instanceof THREE.Geometry ) {

geometry = new THREE.BufferGeometry().setFromObject( points );

}

if ( geometry instanceof THREE.BufferGeometry ) {

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

output += 'o ' + points.name + '\n';

if ( vertices !== undefined ) {

for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {

vertex.fromBufferAttribute( vertices, i );
vertex.applyMatrix4( points.matrixWorld );

output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z;

if ( colors !== undefined ) {

color.fromBufferAttribute( colors, i );

output += ' ' + color.r + ' ' + color.g + ' ' + color.b;

}

output += '\n';

}

}

output += 'p ';

for ( j = 1, l = vertices.count; j <= l; j ++ ) {

output += ( indexVertex + j ) + ' ';

}

output += '\n';

} else {

console.warn( 'THREE.OBJExporter.parsePoints(): geometry type unsupported', geometry );

}

// update index
indexVertex += nbVertex;

};

object.traverse( function ( child ) {

if ( child instanceof THREE.Mesh ) {
Expand All @@ -249,6 +313,12 @@ THREE.OBJExporter.prototype = {

}

if ( child instanceof THREE.Points ) {

parsePoints( child );

}

} );

return output;
Expand Down
11 changes: 9 additions & 2 deletions examples/js/loaders/OBJLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,10 @@ THREE.OBJLoader = ( function () {

for ( var vi = 0, l = vertices.length; vi < l; vi ++ ) {

this.addVertexPoint( this.parseVertexIndex( vertices[ vi ], vLen ) );
var index = this.parseVertexIndex( vertices[ vi ], vLen );

this.addVertexPoint( index );
this.addColor( index );

}

Expand Down Expand Up @@ -724,7 +727,11 @@ THREE.OBJLoader = ( function () {

buffergeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( geometry.vertices, 3 ) );

buffergeometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( geometry.normals, 3 ) );
if ( geometry.normals.length > 0 ) {

buffergeometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( geometry.normals, 3 ) );

}

if ( geometry.colors.length > 0 ) {

Expand Down
72 changes: 72 additions & 0 deletions examples/jsm/exporters/OBJExporter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
BufferGeometry,
Color,
Geometry,
Line,
Matrix3,
Mesh,
Points,
Vector2,
Vector3
} from "../../../build/three.module.js";
Expand All @@ -23,6 +25,7 @@ OBJExporter.prototype = {
var indexNormals = 0;

var vertex = new Vector3();
var color = new Color();
var normal = new Vector3();
var uv = new Vector2();

Expand Down Expand Up @@ -245,6 +248,69 @@ OBJExporter.prototype = {

};

var parsePoints = function ( points ) {

var nbVertex = 0;

var geometry = points.geometry;

if ( geometry instanceof Geometry ) {

geometry = new BufferGeometry().setFromObject( points );

}

if ( geometry instanceof BufferGeometry ) {

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

output += 'o ' + points.name + '\n';

if ( vertices !== undefined ) {

for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {

vertex.fromBufferAttribute( vertices, i );
vertex.applyMatrix4( points.matrixWorld );

output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z;

if ( colors !== undefined ) {

color.fromBufferAttribute( colors, i );

output += ' ' + color.r + ' ' + color.g + ' ' + color.b;

}

output += '\n';

}

}

output += 'p ';

for ( j = 1, l = vertices.count; j <= l; j ++ ) {

output += ( indexVertex + j ) + ' ';

}

output += '\n';

} else {

console.warn( 'THREE.OBJExporter.parsePoints(): geometry type unsupported', geometry );

}

// update index
indexVertex += nbVertex;

};

object.traverse( function ( child ) {

if ( child instanceof Mesh ) {
Expand All @@ -259,6 +325,12 @@ OBJExporter.prototype = {

}

if ( child instanceof Points ) {

parsePoints( child );

}

} );

return output;
Expand Down
11 changes: 9 additions & 2 deletions examples/jsm/loaders/OBJLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,10 @@ var OBJLoader = ( function () {

for ( var vi = 0, l = vertices.length; vi < l; vi ++ ) {

this.addVertexPoint( this.parseVertexIndex( vertices[ vi ], vLen ) );
var index = this.parseVertexIndex( vertices[ vi ], vLen );

this.addVertexPoint( index );
this.addColor( index );

}

Expand Down Expand Up @@ -740,7 +743,11 @@ var OBJLoader = ( function () {

buffergeometry.setAttribute( 'position', new Float32BufferAttribute( geometry.vertices, 3 ) );

buffergeometry.setAttribute( 'normal', new Float32BufferAttribute( geometry.normals, 3 ) );
if ( geometry.normals.length > 0 ) {

buffergeometry.setAttribute( 'normal', new Float32BufferAttribute( geometry.normals, 3 ) );

}

if ( geometry.colors.length > 0 ) {

Expand Down
25 changes: 23 additions & 2 deletions examples/misc_exporter_obj.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
<button id="cube">cube</button>
<button id="cylinder">cylinder</button>
<button id="multiple">multiple</button>
<button id="transformed">transformed</button><br /><br />
<button id="transformed">transformed</button>
<button id="points">points</button><br /><br />
<button id="export">export to obj</button>
</div>

Expand Down Expand Up @@ -58,7 +59,7 @@

const child = scene.children[ i ];

if ( child.isMesh ) {
if ( child.isMesh || child.isPoints ) {

child.geometry.dispose();
scene.remove( child );
Expand Down Expand Up @@ -114,6 +115,21 @@

}

} else if ( type === 6 ) {

const points = [ 0, 0, 0, 100, 0, 0, 100, 100, 0, 0, 100, 0 ];
const colors = [ 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0 ];

const geometry = new THREE.BufferGeometry();
geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( points, 3 ) );
geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );

const material = new THREE.PointsMaterial( { size: 10, vertexColors: true } );

const pointCloud = new THREE.Points( geometry, material );
pointCloud.name = 'point cloud';
scene.add( pointCloud );

}

}
Expand Down Expand Up @@ -164,6 +180,11 @@

addGeometry( 5 );

} );
document.getElementById( 'points' ).addEventListener( 'click', function () {

addGeometry( 6 );

} );

exportButton = document.getElementById( 'export' );
Expand Down