@@ -88,7 +88,7 @@ export default class Geometry {
8888 }
8989 }
9090
91- static padGPUBufferAlignment ( array :TypedArray , vertexCount : number ) : TypedArray {
91+ static padGPUBufferAlignment ( array : TypedArray , vertexCount : number ) : TypedArray {
9292 const itemBytes = array . byteLength / vertexCount ;
9393 if ( itemBytes % 4 === 0 ) {
9494 return array ;
@@ -226,7 +226,7 @@ export default class Geometry {
226226 if ( ! id ) {
227227 id = attribute . array . buffer [ '__id' ] = GUID ( ) ;
228228 }
229- this . data [ attr ] = {
229+ this . data [ attr ] = {
230230 buffer : id ,
231231 offset : attribute . byteOffset ,
232232 stride : attribute . byteStride ,
@@ -467,9 +467,15 @@ export default class Geometry {
467467 } else {
468468 ctor = this . elements . constructor ;
469469 }
470- this . indices = new ctor ( elements . length ) ;
471- for ( let i = 0 ; i < elements . length ; i ++ ) {
472- this . indices [ i ] = elements [ i ] ;
470+ if ( elements instanceof ctor ) {
471+ //2x faster by new TypeArray(typearray)
472+ //https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array
473+ this . indices = new ctor ( elements ) ;
474+ } else {
475+ this . indices = new ctor ( elements . length ) ;
476+ for ( let i = 0 ; i < elements . length ; i ++ ) {
477+ this . indices [ i ] = elements [ i ] ;
478+ }
473479 }
474480 }
475481 this . elements = this . elements . destroy ? this . elements : Geometry . createElementBuffer ( device , this . elements ) ;
@@ -495,7 +501,7 @@ export default class Geometry {
495501 if ( isArray ( data ) ) {
496502 // 因为data可能被转成regl buffer,需要保存到this._vertexCount
497503 // 在 updateData时再更新
498- this . _vertexCount = Math . ceil ( data . length / positionSize ) ;
504+ this . _vertexCount = Math . ceil ( data . length / positionSize ) ;
499505 } else if ( data && data . count !== undefined ) {
500506 this . _vertexCount = data . count ;
501507 }
@@ -600,6 +606,7 @@ export default class Geometry {
600606 return this ;
601607 }
602608
609+
603610 _updateGPUBuffer ( buffer : GPUBuffer , data : AttributeData , offset : number , byteLength : number ) {
604611 if ( Array . isArray ( data ) ) {
605612 data = new Float32Array ( data ) ;
@@ -899,7 +906,7 @@ export default class Geometry {
899906 //对于POSITION数据,为避免updateBBox时频繁创建临时数组,采用缓存tempPosArray的策略获取interleavedArray,
900907 //对于非POSITION的数据,直接readInterleavedArray读取即可
901908 if ( name === this . desc . positionAttribute ) {
902- if ( ! this . _tempPosArray || ( this . _tempPosArray && this . _tempPosArray . length < size * count ) ) {
909+ if ( ! this . _tempPosArray || ( this . _tempPosArray && this . _tempPosArray . length < size * count ) ) {
903910 this . _tempPosArray = new ctor ( size * count ) ;
904911 return gltf . GLTFLoader . readInterleavedArray ( this . _tempPosArray , attribute , count , size , stride , offset , componentType ) ;
905912 }
@@ -915,18 +922,23 @@ export default class Geometry {
915922 }
916923 }
917924
918- createTangent ( name = 'aTangent' ) {
925+ createTangent ( name = 'aTangent' , tangentsDataArray ?: Float32Array | Array < number > ) {
919926 this . _incrVersion ( ) ;
920927 //TODO data 可能是含stride的interleaved类型
921928 const { normalAttribute, positionAttribute, uv0Attribute } = this . desc ;
922929 const normals = this . _getAttributeData ( normalAttribute ) ;
923930 const positions = this . _getAttributeData ( positionAttribute ) ;
924- const tangents = buildTangents (
925- positions ,
926- normals ,
927- this . data [ uv0Attribute ] ,
928- this . elements
929- ) ;
931+ let tangents ;
932+ if ( tangentsDataArray && tangentsDataArray . length ) {
933+ tangents = tangentsDataArray ;
934+ } else {
935+ tangents = buildTangents (
936+ positions ,
937+ normals ,
938+ this . data [ uv0Attribute ] ,
939+ this . elements
940+ ) ;
941+ }
930942 const aTangent = this . data [ name ] = new Float32Array ( tangents . length ) ;
931943 const t : vec4 = [ 0 , 0 , 0 , 0 ] , n : vec3 = [ 0 , 0 , 0 ] , q : vec4 = [ 0 , 0 , 0 , 0 ] ;
932944 for ( let i = 0 ; i < tangents . length ; i += 4 ) {
@@ -982,7 +994,7 @@ export default class Geometry {
982994 const oldData = { } ;
983995
984996 let pos = data [ this . desc . positionAttribute ] ;
985- pos = pos . length ? pos : pos . array ; //存在两种结构 array或者 { array }
997+ pos = pos . length ? pos : pos . array ; //存在两种结构 array或者 { array }
986998 if ( ! isArray ( pos ) ) {
987999 throw new Error ( this . desc . positionAttribute + ' must be array to build unique vertex.' ) ;
9881000 }
@@ -1016,7 +1028,7 @@ export default class Geometry {
10161028 indices [ i ] = cursor ++ ;
10171029 }
10181030 pos = this . data [ this . desc . positionAttribute ] ;
1019- this . _vertexCount = Math . ceil ( pos . length / this . desc . positionSize ) ;
1031+ this . _vertexCount = Math . ceil ( pos . length / this . desc . positionSize ) ;
10201032 delete this . _reglData ;
10211033 }
10221034
@@ -1048,7 +1060,7 @@ export default class Geometry {
10481060
10491061 //@internal
10501062 _forEachBuffer ( fn : ( buffer : any ) => void ) {
1051- if ( this . elements && this . elements . destroy ) {
1063+ if ( this . elements && this . elements . destroy ) {
10521064 fn ( this . elements ) ;
10531065 }
10541066 for ( const p in this . data ) {
@@ -1283,7 +1295,7 @@ function createGPUBuffer(device, data, usage, label) {
12831295 data = new Float32Array ( data ) ;
12841296 }
12851297 const ctor = data . constructor ;
1286- // f32 in default
1298+ // f32 in default
12871299 const byteLength = data . byteLength ;
12881300 // mappedAtCreation requires size is a multiplier of 4
12891301 // https://github.com/gpuweb/gpuweb/issues/5105
0 commit comments