@@ -29,6 +29,7 @@ export class QuantizedMeshLoader extends QuantizedMeshLoaderBase {
2929 this . ellipsoid = new Ellipsoid ( ) ;
3030 this . skirtLength = 1000 ;
3131 this . smoothSkirtNormals = true ;
32+ this . generateNormals = true ;
3233 this . solid = false ;
3334
3435 // set the range of the tile
@@ -46,6 +47,7 @@ export class QuantizedMeshLoader extends QuantizedMeshLoaderBase {
4647 solid,
4748 skirtLength,
4849 smoothSkirtNormals,
50+ generateNormals,
4951
5052 minLat,
5153 maxLat,
@@ -66,7 +68,8 @@ export class QuantizedMeshLoader extends QuantizedMeshLoaderBase {
6668 const mesh = new Mesh ( geometry , material ) ;
6769 mesh . position . set ( ...header . center ) ;
6870
69- const includeNormals = 'octvertexnormals' in extensions ;
71+ const hasNormalExtension = 'octvertexnormals' in extensions ;
72+ const includeNormals = hasNormalExtension || generateNormals ;
7073 const vertexCount = vertexData . u . length ;
7174 const positions = [ ] ;
7275 const uvs = [ ] ;
@@ -94,10 +97,36 @@ export class QuantizedMeshLoader extends QuantizedMeshLoaderBase {
9497
9598 if ( includeNormals ) {
9699
97- const extNormals = extensions [ 'octvertexnormals' ] . normals ;
98- for ( let i = 0 , l = extNormals . length ; i < l ; i ++ ) {
100+ if ( hasNormalExtension ) {
99101
100- normals . push ( extNormals [ i ] ) ;
102+ const extNormals = extensions [ 'octvertexnormals' ] . normals ;
103+ for ( let i = 0 , l = extNormals . length ; i < l ; i ++ ) {
104+
105+ normals . push ( extNormals [ i ] ) ;
106+
107+ }
108+
109+ } else {
110+
111+ // generate normals using the positions we just created
112+ const tempGeometry = new BufferGeometry ( ) ;
113+ const tempIndexBuffer = indices . length > 21845 ? new Uint32Array ( indices ) : new Uint16Array ( indices ) ;
114+ tempGeometry . setIndex ( new BufferAttribute ( tempIndexBuffer , 1 , false ) ) ;
115+ tempGeometry . setAttribute ( 'position' , new BufferAttribute ( new Float32Array ( positions ) , 3 , false ) ) ;
116+ tempGeometry . computeVertexNormals ( ) ;
117+
118+ const normalAttr = tempGeometry . getAttribute ( 'normal' ) ;
119+ const generatedNormals = normalAttr . array ;
120+
121+ // store in extensions format for consistency
122+ extensions [ 'octvertexnormals' ] = { normals : generatedNormals } ;
123+
124+ // copy to normals array
125+ for ( let i = 0 , l = generatedNormals . length ; i < l ; i ++ ) {
126+
127+ normals . push ( generatedNormals [ i ] ) ;
128+
129+ }
101130
102131 }
103132
0 commit comments