TypedGeometry.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.constructor = THREE.TypedGeometry;
  16. THREE.TypedGeometry.prototype.setArrays = function ( vertices, normals, uvs ) {
  17. this.vertices = vertices;
  18. this.normals = normals;
  19. this.uvs = uvs;
  20. this.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
  21. this.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  22. this.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );
  23. return this;
  24. };
  25. THREE.TypedGeometry.prototype.merge = ( function () {
  26. var offset = 0;
  27. var normalMatrix = new THREE.Matrix3();
  28. return function ( geometry, matrix, startOffset ) {
  29. if ( startOffset !== undefined ) offset = startOffset;
  30. var offset2 = offset * 2;
  31. var offset3 = offset * 3;
  32. var vertices = this.attributes.position.array;
  33. var normals = this.attributes.normal.array;
  34. var uvs = this.attributes.uv.array;
  35. if ( geometry instanceof THREE.TypedGeometry ) {
  36. var vertices2 = geometry.attributes.position.array;
  37. var normals2 = geometry.attributes.normal.array;
  38. var uvs2 = geometry.attributes.uv.array;
  39. for ( var i = 0, l = vertices2.length; i < l; i += 3 ) {
  40. vertices[ i + offset3 ] = vertices2[ i ];
  41. vertices[ i + offset3 + 1 ] = vertices2[ i + 1 ];
  42. vertices[ i + offset3 + 2 ] = vertices2[ i + 2 ];
  43. normals[ i + offset3 ] = normals2[ i ];
  44. normals[ i + offset3 + 1 ] = normals2[ i + 1 ];
  45. normals[ i + offset3 + 2 ] = normals2[ i + 2 ];
  46. uvs[ i + offset2 ] = uvs2[ i ];
  47. uvs[ i + offset2 + 1 ] = uvs2[ i + 1 ];
  48. }
  49. } else if ( geometry instanceof THREE.IndexedTypedGeometry ) {
  50. var indices2 = geometry.attributes.index.array;
  51. var vertices2 = geometry.attributes.position.array;
  52. var normals2 = geometry.attributes.normal.array;
  53. var uvs2 = geometry.attributes.uv.array;
  54. for ( var i = 0, l = indices2.length; i < l; i ++ ) {
  55. var index = indices2[ i ];
  56. var index3 = index * 3;
  57. var i3 = i * 3;
  58. vertices[ i3 + offset3 ] = vertices2[ index3 ];
  59. vertices[ i3 + offset3 + 1 ] = vertices2[ index3 + 1 ];
  60. vertices[ i3 + offset3 + 2 ] = vertices2[ index3 + 2 ];
  61. normals[ i3 + offset3 ] = normals2[ index3 ];
  62. normals[ i3 + offset3 + 1 ] = normals2[ index3 + 1 ];
  63. normals[ i3 + offset3 + 2 ] = normals2[ index3 + 2 ];
  64. var index2 = index * 2;
  65. var i2 = i * 2;
  66. uvs[ i2 + offset2 ] = uvs2[ index2 ];
  67. uvs[ i2 + offset2 + 1 ] = uvs2[ index2 + 1 ];
  68. }
  69. if ( matrix !== undefined ) {
  70. matrix.applyToVector3Array( vertices, offset3, indices2.length * 3 );
  71. normalMatrix.getNormalMatrix( matrix );
  72. normalMatrix.applyToVector3Array( normals, offset3, indices2.length * 3 );
  73. }
  74. offset += indices2.length;
  75. }
  76. };
  77. } )();