WebGLObjects.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { Uint16BufferAttribute, Uint32BufferAttribute } from '../../core/BufferAttribute';
  5. import { arrayMax } from '../../utils';
  6. import { WebGLGeometries } from './WebGLGeometries';
  7. function WebGLObjects( gl, attributes, properties, info ) {
  8. var geometries = new WebGLGeometries( gl, attributes, properties, info );
  9. //
  10. function update( object ) {
  11. // TODO: Avoid updating twice (when using shadowMap). Maybe add frame counter.
  12. var geometry = geometries.get( object );
  13. if ( object.geometry.isGeometry ) {
  14. geometry.updateFromObject( object );
  15. }
  16. var index = geometry.index;
  17. var geometryAttributes = geometry.attributes;
  18. if ( index !== null ) {
  19. attributes.update( index, gl.ELEMENT_ARRAY_BUFFER );
  20. }
  21. for ( var name in geometryAttributes ) {
  22. attributes.update( geometryAttributes[ name ], gl.ARRAY_BUFFER );
  23. }
  24. // morph targets
  25. var morphAttributes = geometry.morphAttributes;
  26. for ( var name in morphAttributes ) {
  27. var array = morphAttributes[ name ];
  28. for ( var i = 0, l = array.length; i < l; i ++ ) {
  29. attributes.update( array[ i ], gl.ARRAY_BUFFER );
  30. }
  31. }
  32. return geometry;
  33. }
  34. function getWireframeAttribute( geometry ) {
  35. var property = properties.get( geometry );
  36. if ( property.wireframe !== undefined ) {
  37. return property.wireframe;
  38. }
  39. var indices = [];
  40. var geometryIndex = geometry.index;
  41. var geometryAttributes = geometry.attributes;
  42. // console.time( 'wireframe' );
  43. if ( geometryIndex !== null ) {
  44. var array = geometryIndex.array;
  45. for ( var i = 0, l = array.length; i < l; i += 3 ) {
  46. var a = array[ i + 0 ];
  47. var b = array[ i + 1 ];
  48. var c = array[ i + 2 ];
  49. indices.push( a, b, b, c, c, a );
  50. }
  51. } else {
  52. var array = geometryAttributes.position.array;
  53. for ( var i = 0, l = ( array.length / 3 ) - 1; i < l; i += 3 ) {
  54. var a = i + 0;
  55. var b = i + 1;
  56. var c = i + 2;
  57. indices.push( a, b, b, c, c, a );
  58. }
  59. }
  60. // console.timeEnd( 'wireframe' );
  61. var attribute = new ( arrayMax( indices ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 );
  62. attributes.update( attribute, gl.ELEMENT_ARRAY_BUFFER );
  63. property.wireframe = attribute;
  64. return attribute;
  65. }
  66. return {
  67. getWireframeAttribute: getWireframeAttribute,
  68. update: update
  69. };
  70. }
  71. export { WebGLObjects };