未验证 提交 5db1eb3a 编写于 作者: M Mr.doob 提交者: GitHub

Merge pull request #19338 from Mugen87/dev49

OBJLoader: Better handle inconsistent face definitions.
...@@ -13,6 +13,13 @@ THREE.OBJLoader = ( function () { ...@@ -13,6 +13,13 @@ THREE.OBJLoader = ( function () {
// usemap map_name // usemap map_name
var map_use_pattern = /^usemap /; var map_use_pattern = /^usemap /;
var vA = new THREE.Vector3();
var vB = new THREE.Vector3();
var vC = new THREE.Vector3();
var ab = new THREE.Vector3();
var cb = new THREE.Vector3();
function ParserState() { function ParserState() {
var state = { var state = {
...@@ -55,7 +62,9 @@ THREE.OBJLoader = ( function () { ...@@ -55,7 +62,9 @@ THREE.OBJLoader = ( function () {
vertices: [], vertices: [],
normals: [], normals: [],
colors: [], colors: [],
uvs: [] uvs: [],
hasNormalIndices: false,
hasUVIndices: false
}, },
materials: [], materials: [],
smooth: true, smooth: true,
...@@ -248,6 +257,27 @@ THREE.OBJLoader = ( function () { ...@@ -248,6 +257,27 @@ THREE.OBJLoader = ( function () {
}, },
addFaceNormal: function ( a, b, c ) {
var src = this.vertices;
var dst = this.object.geometry.normals;
vA.fromArray( src, a );
vB.fromArray( src, b );
vC.fromArray( src, c );
cb.subVectors( vC, vB );
ab.subVectors( vA, vB );
cb.cross( ab );
cb.normalize();
dst.push( cb.x, cb.y, cb.z );
dst.push( cb.x, cb.y, cb.z );
dst.push( cb.x, cb.y, cb.z );
},
addColor: function ( a, b, c ) { addColor: function ( a, b, c ) {
var src = this.colors; var src = this.colors;
...@@ -270,6 +300,16 @@ THREE.OBJLoader = ( function () { ...@@ -270,6 +300,16 @@ THREE.OBJLoader = ( function () {
}, },
addDefaultUV: function () {
var dst = this.object.geometry.uvs;
dst.push( 0, 0 );
dst.push( 0, 0 );
dst.push( 0, 0 );
},
addUVLine: function ( a ) { addUVLine: function ( a ) {
var src = this.uvs; var src = this.uvs;
...@@ -290,26 +330,45 @@ THREE.OBJLoader = ( function () { ...@@ -290,26 +330,45 @@ THREE.OBJLoader = ( function () {
this.addVertex( ia, ib, ic ); this.addVertex( ia, ib, ic );
this.addColor( ia, ib, ic ); this.addColor( ia, ib, ic );
// normals
if ( na !== undefined && na !== '' ) {
var nLen = this.normals.length;
ia = this.parseNormalIndex( na, nLen );
ib = this.parseNormalIndex( nb, nLen );
ic = this.parseNormalIndex( nc, nLen );
this.addNormal( ia, ib, ic );
this.object.geometry.hasNormalIndices = true;
} else {
this.addFaceNormal( ia, ib, ic );
}
// uvs
if ( ua !== undefined && ua !== '' ) { if ( ua !== undefined && ua !== '' ) {
var uvLen = this.uvs.length; var uvLen = this.uvs.length;
ia = this.parseUVIndex( ua, uvLen ); ia = this.parseUVIndex( ua, uvLen );
ib = this.parseUVIndex( ub, uvLen ); ib = this.parseUVIndex( ub, uvLen );
ic = this.parseUVIndex( uc, uvLen ); ic = this.parseUVIndex( uc, uvLen );
this.addUV( ia, ib, ic );
} this.addUV( ia, ib, ic );
if ( na !== undefined && na !== '' ) { this.object.geometry.hasUVIndices = true;
// Normals are many times the same. If so, skip function call and parseInt. } else {
var nLen = this.normals.length;
ia = this.parseNormalIndex( na, nLen );
ib = na === nb ? ia : this.parseNormalIndex( nb, nLen ); // add placeholder values (for inconsistent face definitions)
ic = na === nc ? ia : this.parseNormalIndex( nc, nLen );
this.addNormal( ia, ib, ic ); this.addDefaultUV();
} }
...@@ -652,14 +711,10 @@ THREE.OBJLoader = ( function () { ...@@ -652,14 +711,10 @@ THREE.OBJLoader = ( function () {
buffergeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( geometry.vertices, 3 ) ); buffergeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( geometry.vertices, 3 ) );
if ( geometry.normals.length > 0 ) { if ( geometry.hasNormalIndices === true ) {
buffergeometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( geometry.normals, 3 ) ); buffergeometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( geometry.normals, 3 ) );
} else {
buffergeometry.computeVertexNormals();
} }
if ( geometry.colors.length > 0 ) { if ( geometry.colors.length > 0 ) {
...@@ -669,7 +724,7 @@ THREE.OBJLoader = ( function () { ...@@ -669,7 +724,7 @@ THREE.OBJLoader = ( function () {
} }
if ( geometry.uvs.length > 0 ) { if ( geometry.hasUVIndices === true ) {
buffergeometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( geometry.uvs, 2 ) ); buffergeometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( geometry.uvs, 2 ) );
......
...@@ -14,7 +14,8 @@ import { ...@@ -14,7 +14,8 @@ import {
Mesh, Mesh,
MeshPhongMaterial, MeshPhongMaterial,
Points, Points,
PointsMaterial PointsMaterial,
Vector3
} from "../../../build/three.module.js"; } from "../../../build/three.module.js";
var OBJLoader = ( function () { var OBJLoader = ( function () {
...@@ -28,6 +29,13 @@ var OBJLoader = ( function () { ...@@ -28,6 +29,13 @@ var OBJLoader = ( function () {
// usemap map_name // usemap map_name
var map_use_pattern = /^usemap /; var map_use_pattern = /^usemap /;
var vA = new Vector3();
var vB = new Vector3();
var vC = new Vector3();
var ab = new Vector3();
var cb = new Vector3();
function ParserState() { function ParserState() {
var state = { var state = {
...@@ -70,7 +78,9 @@ var OBJLoader = ( function () { ...@@ -70,7 +78,9 @@ var OBJLoader = ( function () {
vertices: [], vertices: [],
normals: [], normals: [],
colors: [], colors: [],
uvs: [] uvs: [],
hasNormalIndices: false,
hasUVIndices: false
}, },
materials: [], materials: [],
smooth: true, smooth: true,
...@@ -263,6 +273,27 @@ var OBJLoader = ( function () { ...@@ -263,6 +273,27 @@ var OBJLoader = ( function () {
}, },
addFaceNormal: function ( a, b, c ) {
var src = this.vertices;
var dst = this.object.geometry.normals;
vA.fromArray( src, a );
vB.fromArray( src, b );
vC.fromArray( src, c );
cb.subVectors( vC, vB );
ab.subVectors( vA, vB );
cb.cross( ab );
cb.normalize();
dst.push( cb.x, cb.y, cb.z );
dst.push( cb.x, cb.y, cb.z );
dst.push( cb.x, cb.y, cb.z );
},
addColor: function ( a, b, c ) { addColor: function ( a, b, c ) {
var src = this.colors; var src = this.colors;
...@@ -285,6 +316,16 @@ var OBJLoader = ( function () { ...@@ -285,6 +316,16 @@ var OBJLoader = ( function () {
}, },
addDefaultUV: function () {
var dst = this.object.geometry.uvs;
dst.push( 0, 0 );
dst.push( 0, 0 );
dst.push( 0, 0 );
},
addUVLine: function ( a ) { addUVLine: function ( a ) {
var src = this.uvs; var src = this.uvs;
...@@ -305,26 +346,45 @@ var OBJLoader = ( function () { ...@@ -305,26 +346,45 @@ var OBJLoader = ( function () {
this.addVertex( ia, ib, ic ); this.addVertex( ia, ib, ic );
this.addColor( ia, ib, ic ); this.addColor( ia, ib, ic );
// normals
if ( na !== undefined && na !== '' ) {
var nLen = this.normals.length;
ia = this.parseNormalIndex( na, nLen );
ib = this.parseNormalIndex( nb, nLen );
ic = this.parseNormalIndex( nc, nLen );
this.addNormal( ia, ib, ic );
this.object.geometry.hasNormalIndices = true;
} else {
this.addFaceNormal( ia, ib, ic );
}
// uvs
if ( ua !== undefined && ua !== '' ) { if ( ua !== undefined && ua !== '' ) {
var uvLen = this.uvs.length; var uvLen = this.uvs.length;
ia = this.parseUVIndex( ua, uvLen ); ia = this.parseUVIndex( ua, uvLen );
ib = this.parseUVIndex( ub, uvLen ); ib = this.parseUVIndex( ub, uvLen );
ic = this.parseUVIndex( uc, uvLen ); ic = this.parseUVIndex( uc, uvLen );
this.addUV( ia, ib, ic );
} this.addUV( ia, ib, ic );
if ( na !== undefined && na !== '' ) { this.object.geometry.hasUVIndices = true;
// Normals are many times the same. If so, skip function call and parseInt. } else {
var nLen = this.normals.length;
ia = this.parseNormalIndex( na, nLen );
ib = na === nb ? ia : this.parseNormalIndex( nb, nLen ); // add placeholder values (for inconsistent face definitions)
ic = na === nc ? ia : this.parseNormalIndex( nc, nLen );
this.addNormal( ia, ib, ic ); this.addDefaultUV();
} }
...@@ -667,14 +727,10 @@ var OBJLoader = ( function () { ...@@ -667,14 +727,10 @@ var OBJLoader = ( function () {
buffergeometry.setAttribute( 'position', new Float32BufferAttribute( geometry.vertices, 3 ) ); buffergeometry.setAttribute( 'position', new Float32BufferAttribute( geometry.vertices, 3 ) );
if ( geometry.normals.length > 0 ) { if ( geometry.hasNormalIndices === true ) {
buffergeometry.setAttribute( 'normal', new Float32BufferAttribute( geometry.normals, 3 ) ); buffergeometry.setAttribute( 'normal', new Float32BufferAttribute( geometry.normals, 3 ) );
} else {
buffergeometry.computeVertexNormals();
} }
if ( geometry.colors.length > 0 ) { if ( geometry.colors.length > 0 ) {
...@@ -684,7 +740,7 @@ var OBJLoader = ( function () { ...@@ -684,7 +740,7 @@ var OBJLoader = ( function () {
} }
if ( geometry.uvs.length > 0 ) { if ( geometry.hasUVIndices === true ) {
buffergeometry.setAttribute( 'uv', new Float32BufferAttribute( geometry.uvs, 2 ) ); buffergeometry.setAttribute( 'uv', new Float32BufferAttribute( geometry.uvs, 2 ) );
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册