2
0

TypedGeometry.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.TypedGeometry = function ( size ) {
  5. THREE.BufferGeometry.call( this );
  6. if ( size !== undefined ) {
  7. this.setArrays(
  8. new Float32Array( size * 3 * 3 ),
  9. new Float32Array( size * 3 * 3 ),
  10. new Float32Array( size * 3 * 2 )
  11. );
  12. }
  13. };
  14. THREE.TypedGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
  15. THREE.TypedGeometry.prototype.setArrays = function ( vertices, normals, uvs ) {
  16. this.vertices = vertices;
  17. this.normals = normals;
  18. this.uvs = uvs;
  19. this.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
  20. this.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  21. this.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );
  22. return this;
  23. };
  24. THREE.TypedGeometry.prototype.merge = ( function () {
  25. var offset = 0;
  26. var normalMatrix = new THREE.Matrix3();
  27. return function ( geometry, matrix, startOffset ) {
  28. if ( startOffset !== undefined ) offset = startOffset;
  29. var offset2 = offset * 2;
  30. var offset3 = offset * 3;
  31. var vertices = this.attributes.position.array;
  32. var normals = this.attributes.normal.array;
  33. var uvs = this.attributes.uv.array;
  34. if ( geometry instanceof THREE.TypedGeometry ) {
  35. var vertices2 = geometry.attributes.position.array;
  36. var normals2 = geometry.attributes.normal.array;
  37. var uvs2 = geometry.attributes.uv.array;
  38. for ( var i = 0, l = vertices2.length; i < l; i += 3 ) {
  39. vertices[ i + offset3 ] = vertices2[ i ];
  40. vertices[ i + offset3 + 1 ] = vertices2[ i + 1 ];
  41. vertices[ i + offset3 + 2 ] = vertices2[ i + 2 ];
  42. normals[ i + offset3 ] = normals2[ i ];
  43. normals[ i + offset3 + 1 ] = normals2[ i + 1 ];
  44. normals[ i + offset3 + 2 ] = normals2[ i + 2 ];
  45. uvs[ i + offset2 ] = uvs2[ i ];
  46. uvs[ i + offset2 + 1 ] = uvs2[ i + 1 ];
  47. }
  48. } else if ( geometry instanceof THREE.IndexedTypedGeometry ) {
  49. var indices2 = geometry.attributes.index.array;
  50. var vertices2 = geometry.attributes.position.array;
  51. var normals2 = geometry.attributes.normal.array;
  52. var uvs2 = geometry.attributes.uv.array;
  53. for ( var i = 0, l = indices2.length; i < l; i ++ ) {
  54. var index = indices2[ i ];
  55. var index3 = index * 3;
  56. var i3 = i * 3;
  57. vertices[ i3 + offset3 ] = vertices2[ index3 ];
  58. vertices[ i3 + offset3 + 1 ] = vertices2[ index3 + 1 ];
  59. vertices[ i3 + offset3 + 2 ] = vertices2[ index3 + 2 ];
  60. normals[ i3 + offset3 ] = normals2[ index3 ];
  61. normals[ i3 + offset3 + 1 ] = normals2[ index3 + 1 ];
  62. normals[ i3 + offset3 + 2 ] = normals2[ index3 + 2 ];
  63. var index2 = index * 2;
  64. var i2 = i * 2;
  65. uvs[ i2 + offset2 ] = uvs2[ index2 ];
  66. uvs[ i2 + offset2 + 1 ] = uvs2[ index2 + 1 ];
  67. }
  68. if ( matrix !== undefined ) {
  69. matrix.applyToVector3Array( vertices, offset3, indices2.length * 3 );
  70. normalMatrix.getNormalMatrix( matrix );
  71. normalMatrix.applyToVector3Array( normals, offset3, indices2.length * 3 );
  72. }
  73. offset += indices2.length;
  74. }
  75. };
  76. } )();