ParticleRenderer.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. THREE.WebGLRenderer.ParticleRenderer = function ( lowlevelrenderer, info ) {
  2. THREE.WebGLRenderer.Object3DRenderer.call( this, lowlevelrenderer, info );
  3. };
  4. THREE.WebGLRenderer.ParticleRenderer.prototype = Object.create( THREE.WebGLRenderer.Object3DRenderer.prototype );
  5. THREE.extend( THREE.WebGLRenderer.ParticleRenderer.prototype, {
  6. createBuffers: function ( geometry ) {
  7. var renderer = this.renderer;
  8. geometry.__webglVertexBuffer = renderer.createBuffer();
  9. geometry.__webglColorBuffer = renderer.createBuffer();
  10. this.info.memory.geometries ++;
  11. },
  12. initBuffers: function ( geometry, object ) {
  13. var nvertices = geometry.vertices.length;
  14. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  15. geometry.__colorArray = new Float32Array( nvertices * 3 );
  16. geometry.__sortArray = [];
  17. geometry.__webglParticleCount = nvertices;
  18. this.initCustomAttributes ( geometry, object );
  19. },
  20. setBuffers: function ( geometry, object , projectionScreenMatrix ) {
  21. var renderer = this.renderer;
  22. var v, c, vertex, offset, index, color,
  23. vertices = geometry.vertices,
  24. vl = vertices.length,
  25. colors = geometry.colors,
  26. cl = colors.length,
  27. vertexArray = geometry.__vertexArray,
  28. colorArray = geometry.__colorArray,
  29. sortArray = geometry.__sortArray,
  30. dirtyVertices = geometry.verticesNeedUpdate,
  31. dirtyElements = geometry.elementsNeedUpdate,
  32. dirtyColors = geometry.colorsNeedUpdate,
  33. customAttributes = geometry.__webglCustomAttributesList,
  34. i, il,
  35. a, ca, cal, value,
  36. customAttribute;
  37. var _projScreenMatrixPS = THREE.WebGLRenderer.ParticleRenderer._m1,
  38. _vector3 = THREE.WebGLRenderer.ParticleRenderer._v1;
  39. if ( object.sortParticles ) {
  40. _projScreenMatrixPS.multiplyMatrices( projectionScreenMatrix, object.matrixWorld );
  41. for ( v = 0; v < vl; v ++ ) {
  42. vertex = vertices[ v ];
  43. _vector3.copy( vertex );
  44. _vector3.applyProjection(_projScreenMatrixPS);
  45. sortArray[ v ] = [ _vector3.z, v ];
  46. }
  47. sortArray.sort( this.numericalSort );
  48. for ( v = 0; v < vl; v ++ ) {
  49. vertex = vertices[ sortArray[v][1] ];
  50. offset = v * 3;
  51. vertexArray[ offset ] = vertex.x;
  52. vertexArray[ offset + 1 ] = vertex.y;
  53. vertexArray[ offset + 2 ] = vertex.z;
  54. }
  55. for ( c = 0; c < cl; c ++ ) {
  56. offset = c * 3;
  57. color = colors[ sortArray[c][1] ];
  58. colorArray[ offset ] = color.r;
  59. colorArray[ offset + 1 ] = color.g;
  60. colorArray[ offset + 2 ] = color.b;
  61. }
  62. if ( customAttributes ) {
  63. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  64. customAttribute = customAttributes[ i ];
  65. if ( ! ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) ) continue;
  66. offset = 0;
  67. cal = customAttribute.value.length;
  68. if ( customAttribute.size === 1 ) {
  69. for ( ca = 0; ca < cal; ca ++ ) {
  70. index = sortArray[ ca ][ 1 ];
  71. customAttribute.array[ ca ] = customAttribute.value[ index ];
  72. }
  73. } else if ( customAttribute.size === 2 ) {
  74. for ( ca = 0; ca < cal; ca ++ ) {
  75. index = sortArray[ ca ][ 1 ];
  76. value = customAttribute.value[ index ];
  77. customAttribute.array[ offset ] = value.x;
  78. customAttribute.array[ offset + 1 ] = value.y;
  79. offset += 2;
  80. }
  81. } else if ( customAttribute.size === 3 ) {
  82. if ( customAttribute.type === "c" ) {
  83. for ( ca = 0; ca < cal; ca ++ ) {
  84. index = sortArray[ ca ][ 1 ];
  85. value = customAttribute.value[ index ];
  86. customAttribute.array[ offset ] = value.r;
  87. customAttribute.array[ offset + 1 ] = value.g;
  88. customAttribute.array[ offset + 2 ] = value.b;
  89. offset += 3;
  90. }
  91. } else {
  92. for ( ca = 0; ca < cal; ca ++ ) {
  93. index = sortArray[ ca ][ 1 ];
  94. value = customAttribute.value[ index ];
  95. customAttribute.array[ offset ] = value.x;
  96. customAttribute.array[ offset + 1 ] = value.y;
  97. customAttribute.array[ offset + 2 ] = value.z;
  98. offset += 3;
  99. }
  100. }
  101. } else if ( customAttribute.size === 4 ) {
  102. for ( ca = 0; ca < cal; ca ++ ) {
  103. index = sortArray[ ca ][ 1 ];
  104. value = customAttribute.value[ index ];
  105. customAttribute.array[ offset ] = value.x;
  106. customAttribute.array[ offset + 1 ] = value.y;
  107. customAttribute.array[ offset + 2 ] = value.z;
  108. customAttribute.array[ offset + 3 ] = value.w;
  109. offset += 4;
  110. }
  111. }
  112. }
  113. }
  114. } else {
  115. if ( dirtyVertices ) {
  116. for ( v = 0; v < vl; v ++ ) {
  117. vertex = vertices[ v ];
  118. offset = v * 3;
  119. vertexArray[ offset ] = vertex.x;
  120. vertexArray[ offset + 1 ] = vertex.y;
  121. vertexArray[ offset + 2 ] = vertex.z;
  122. }
  123. }
  124. if ( dirtyColors ) {
  125. for ( c = 0; c < cl; c ++ ) {
  126. color = colors[ c ];
  127. offset = c * 3;
  128. colorArray[ offset ] = color.r;
  129. colorArray[ offset + 1 ] = color.g;
  130. colorArray[ offset + 2 ] = color.b;
  131. }
  132. }
  133. if ( customAttributes ) {
  134. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  135. customAttribute = customAttributes[ i ];
  136. if ( customAttribute.needsUpdate &&
  137. ( customAttribute.boundTo === undefined ||
  138. customAttribute.boundTo === "vertices") ) {
  139. cal = customAttribute.value.length;
  140. offset = 0;
  141. if ( customAttribute.size === 1 ) {
  142. for ( ca = 0; ca < cal; ca ++ ) {
  143. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  144. }
  145. } else if ( customAttribute.size === 2 ) {
  146. for ( ca = 0; ca < cal; ca ++ ) {
  147. value = customAttribute.value[ ca ];
  148. customAttribute.array[ offset ] = value.x;
  149. customAttribute.array[ offset + 1 ] = value.y;
  150. offset += 2;
  151. }
  152. } else if ( customAttribute.size === 3 ) {
  153. if ( customAttribute.type === "c" ) {
  154. for ( ca = 0; ca < cal; ca ++ ) {
  155. value = customAttribute.value[ ca ];
  156. customAttribute.array[ offset ] = value.r;
  157. customAttribute.array[ offset + 1 ] = value.g;
  158. customAttribute.array[ offset + 2 ] = value.b;
  159. offset += 3;
  160. }
  161. } else {
  162. for ( ca = 0; ca < cal; ca ++ ) {
  163. value = customAttribute.value[ ca ];
  164. customAttribute.array[ offset ] = value.x;
  165. customAttribute.array[ offset + 1 ] = value.y;
  166. customAttribute.array[ offset + 2 ] = value.z;
  167. offset += 3;
  168. }
  169. }
  170. } else if ( customAttribute.size === 4 ) {
  171. for ( ca = 0; ca < cal; ca ++ ) {
  172. value = customAttribute.value[ ca ];
  173. customAttribute.array[ offset ] = value.x;
  174. customAttribute.array[ offset + 1 ] = value.y;
  175. customAttribute.array[ offset + 2 ] = value.z;
  176. customAttribute.array[ offset + 3 ] = value.w;
  177. offset += 4;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. if ( dirtyVertices || object.sortParticles ) {
  185. renderer.setDynamicArrayBuffer(geometry.__webglVertexBuffer,vertexArray);
  186. }
  187. if ( dirtyColors || object.sortParticles ) {
  188. renderer.setDynamicArrayBuffer(geometry.__webglColorBuffer,colorArray);
  189. }
  190. if ( customAttributes ) {
  191. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  192. customAttribute = customAttributes[ i ];
  193. if ( customAttribute.needsUpdate || object.sortParticles ) {
  194. renderer.setDynamicArrayBuffer(customAttribute.buffer,customAttribute.array);
  195. }
  196. }
  197. }
  198. }
  199. } );
  200. THREE.WebGLRenderer.ParticleRenderer._m1 = new THREE.Matrix4();
  201. THREE.WebGLRenderer.ParticleRenderer._v1 = new THREE.Vector3();