Skip to content

Commit 35d1b5b

Browse files
authored
Merge pull request #14844 from Glinkis/patch-1
Update VRMLLoader to support normals.
2 parents 6aeb09f + 0ee70ea commit 35d1b5b

File tree

1 file changed

+71
-10
lines changed

1 file changed

+71
-10
lines changed

examples/js/loaders/VRMLLoader.js

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ THREE.VRMLLoader.prototype = {
218218
break;
219219

220220
case 'point':
221+
case 'vector':
221222
scope.recordingFieldname = fieldName;
222223
scope.isRecordingPoints = true;
223224
scope.points = [];
@@ -303,6 +304,22 @@ THREE.VRMLLoader.prototype = {
303304

304305
}
305306

307+
if ( node.nodeType == 'Normal' ) {
308+
309+
while ( null !== ( parts = float3_pattern.exec( line ) ) ) {
310+
311+
point = {
312+
x: parseFloat( parts[ 1 ] ),
313+
y: parseFloat( parts[ 2 ] ),
314+
z: parseFloat( parts[ 3 ] )
315+
};
316+
317+
scope.points.push( point );
318+
319+
}
320+
321+
}
322+
306323
if ( node.nodeType == 'TextureCoordinate' ) {
307324

308325
while ( null !== ( parts = float2_pattern.exec( line ) ) ) {
@@ -793,9 +810,10 @@ THREE.VRMLLoader.prototype = {
793810
var geometry = new THREE.BufferGeometry();
794811

795812
var positions = [];
813+
var normals = [];
796814
var uvs = [];
797815

798-
var position, uv;
816+
var position, normal, uv;
799817

800818
var i, il, j, jl;
801819

@@ -820,6 +838,23 @@ THREE.VRMLLoader.prototype = {
820838

821839
}
822840

841+
// normals
842+
843+
if ( child.nodeType === 'Normal' ) {
844+
845+
if ( child.points ) {
846+
847+
for ( j = 0, jl = child.points.length; j < jl; j ++ ) {
848+
849+
normal = child.points[ j ];
850+
normals.push( normal.x, normal.y, normal.z );
851+
852+
}
853+
854+
}
855+
856+
}
857+
823858
// positions
824859

825860
if ( child.nodeType === 'Coordinate' ) {
@@ -862,9 +897,11 @@ THREE.VRMLLoader.prototype = {
862897
if ( data.coordIndex ) {
863898

864899
var newPositions = [];
900+
var newNormals = [];
865901
var newUvs = [];
866902

867903
position = new THREE.Vector3();
904+
normal = new THREE.Vector3();
868905
uv = new THREE.Vector2();
869906

870907
for ( i = 0, il = data.coordIndex.length; i < il; i ++ ) {
@@ -886,19 +923,33 @@ THREE.VRMLLoader.prototype = {
886923
// create non indexed geometry, necessary for face normal generation
887924

888925
position.fromArray( positions, i1 * 3 );
889-
uv.fromArray( uvs, i1 * 2 );
890926
newPositions.push( position.x, position.y, position.z );
891-
newUvs.push( uv.x, uv.y );
892-
893927
position.fromArray( positions, i2 * 3 );
894-
uv.fromArray( uvs, i2 * 2 );
895928
newPositions.push( position.x, position.y, position.z );
896-
newUvs.push( uv.x, uv.y );
897-
898929
position.fromArray( positions, i3 * 3 );
899-
uv.fromArray( uvs, i3 * 2 );
900930
newPositions.push( position.x, position.y, position.z );
901-
newUvs.push( uv.x, uv.y );
931+
932+
if ( uvs.length > 0 ) {
933+
934+
uv.fromArray( uvs, i1 * 2 );
935+
newUvs.push( uv.x, uv.y );
936+
uv.fromArray( uvs, i2 * 2 );
937+
newUvs.push( uv.x, uv.y );
938+
uv.fromArray( uvs, i3 * 2 );
939+
newUvs.push( uv.x, uv.y );
940+
941+
}
942+
943+
if ( normals.length > 0 ) {
944+
945+
normal.fromArray( normals, i1 * 3 );
946+
newNormals.push( normal.x, normal.y, normal.z );
947+
normal.fromArray( normals, i2 * 3 );
948+
newNormals.push( normal.x, normal.y, normal.z );
949+
normal.fromArray( normals, i3 * 3 );
950+
newNormals.push( normal.x, normal.y, normal.z );
951+
952+
}
902953

903954
skip ++;
904955

@@ -907,6 +958,7 @@ THREE.VRMLLoader.prototype = {
907958
}
908959

909960
positions = newPositions;
961+
normals = newNormals;
910962
uvs = newUvs;
911963

912964
} else {
@@ -934,7 +986,16 @@ THREE.VRMLLoader.prototype = {
934986

935987
}
936988

937-
geometry.computeVertexNormals();
989+
if ( normals.length > 0 ) {
990+
991+
geometry.addAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
992+
993+
} else {
994+
995+
geometry.computeVertexNormals();
996+
997+
}
998+
938999
geometry.computeBoundingSphere();
9391000

9401001
// see if it's a define

0 commit comments

Comments
 (0)