BufferGeometry.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../" />
  6. <script src="list.js"></script>
  7. <script src="page.js"></script>
  8. <link type="text/css" rel="stylesheet" href="page.css" />
  9. </head>
  10. <body>
  11. <h1>[name]</h1>
  12. <div class="desc">
  13. <p>
  14. This class is an efficient alternative to [page:Geometry], because it stores all data, including
  15. vertex positions, face indices, normals, colors, UVs, and custom attributes within buffers; this
  16. reduces the cost of passing all this data to the GPU.<br />
  17. This also makes BufferGeometry harder to work with than [page:Geometry]; rather than accessing
  18. position data as [page:Vector3] objects, color data as [page:Color] objects, and so on, you have to
  19. access the raw data from the appropriate [page:BufferAttribute attribute buffer]. This makes
  20. BufferGeometry best-suited for static objects where you don't need to manipulate the geometry much
  21. after instantiating it.
  22. </p>
  23. <h2>Example</h2>
  24. <code>
  25. var geometry = new THREE.BufferGeometry();
  26. // create a simple square shape. We duplicate the top left and bottom right
  27. // vertices because each vertex needs to appear once per triangle.
  28. var vertices = new Float32Array( [
  29. -1.0, -1.0, 1.0,
  30. 1.0, -1.0, 1.0,
  31. 1.0, 1.0, 1.0,
  32. 1.0, 1.0, 1.0,
  33. -1.0, 1.0, 1.0,
  34. -1.0, -1.0, 1.0
  35. ] );
  36. // itemSize = 3 because there are 3 values (components) per vertex
  37. geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
  38. var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  39. var mesh = new THREE.Mesh( geometry, material );
  40. </code>
  41. <div>
  42. [example:webgl_buffergeometry Complex mesh with non-indexed faces]<br />
  43. [example:webgl_buffergeometry_uint Complex mesh with indexed faces]<br />
  44. [example:webgl_buffergeometry_lines Lines]<br />
  45. [example:webgl_buffergeometry_lines_indexed Indexed Lines]<br />
  46. [example:webgl_buffergeometry_custom_attributes_particles Particles]<br />
  47. [example:webgl_buffergeometry_rawshader Raw Shaders]
  48. </div>
  49. <h2>Accessing Attributes</h2>
  50. <p>
  51. WebGL stores data associated with individual vertices of a geometry in <em>attributes</em>.
  52. Examples include the position of the vertex, the normal vector for the vertex, the vertex color,
  53. and so on. When using [page:Geometry], the [page:WebGLRenderer renderer] takes care of wrapping
  54. up this information into typed array buffers and sending this data to the shader. With
  55. BufferGeometry, all of this data is stored in buffers associated with individual attributes.
  56. This means that to get the position data associated with a vertex (for instance), you must call
  57. [page:.getAttribute] to access the 'position' [page:BufferAttribute attribute], then access the individual
  58. x, y, and z coordinates of the position.
  59. </p>
  60. <p>
  61. The following attributes are set by various members of this class:
  62. </p>
  63. <h3>[page:BufferAttribute position] (itemSize: 3)</h3>
  64. <div>
  65. Stores the x, y, and z coordinates of each vertex in this geometry. Set by [page:.fromGeometry]().
  66. </div>
  67. <h3>[page:BufferAttribute normal] (itemSize: 3)</h3>
  68. <div>
  69. Stores the x, y, and z components of the face or vertex normal vector of each vertex in this geometry.
  70. Set by [page:.fromGeometry]().
  71. </div>
  72. <h3>[page:BufferAttribute color] (itemSize: 3)</h3>
  73. <div>
  74. Stores the red, green, and blue channels of vertex color of each vertex in this geometry.
  75. Set by [page:.fromGeometry]().
  76. </div>
  77. <h3>[page:BufferAttribute index] (itemSize: 1)</h3>
  78. Allows for vertices to be re-used across multiple triangles; this is called using "indexed triangles" and
  79. works much the same as it does in [page:Geometry]: each triangle is associated with the indices of three vertices.
  80. This attribute therefore stores the index of each vertex for each triangular face.
  81. If this attribute is not set, the [page:WebGLRenderer renderer] assumes that each three contiguous
  82. positions represent a single triangle.
  83. </div>
  84. <p>
  85. In addition to the the built-in attributes, you can set your own custom attributes using the addAttribute method. With [page:Geometry], these attributes are set and stored on the [page:Material]. In BufferGeometry, the attributes are stored with the geometry itself. Note that you still need to set the attributes information on the material as well, but the value of each attribute is stored in the BufferGeometry.
  86. </p>
  87. <h2>Constructor</h2>
  88. <h3>[name]()</h3>
  89. <div>
  90. This creates a new [name]. It also sets several properties to a default value.
  91. </div>
  92. <h2>Properties</h2>
  93. <h3>[property:Hashmap attributes]</h3>
  94. <div>
  95. This hashmap has as id the name of the attribute to be set and as value the [page:BufferAttribute buffer] to set it to.
  96. Rather than accessing this property directly, use addAttribute and getAttribute to access attributes of this geometry.
  97. </div>
  98. <h3>[property:Box3 boundingBox]</h3>
  99. <div>
  100. Bounding box for the bufferGeometry, which can be calculated with
  101. [page:.computeBoundingBox](). Default is *null*.
  102. </div>
  103. <h3>[property:Sphere boundingSphere]</h3>
  104. <div>
  105. Bounding sphere for the bufferGeometry, which can be calculated with
  106. [page:.computeBoundingSphere](). Default is *null*.
  107. </div>
  108. <h3>[property:Object drawRange]</h3>
  109. <div>
  110. Used to determine what part of the geometry should be rendered. This should not
  111. be set directly, instead use [page:.setDrawRange].<br />
  112. Default is
  113. <code>
  114. { start: 0, count: Infinity }
  115. </code>
  116. </div>
  117. <h3>[property:Array groups]</h3>
  118. <div>
  119. Split the geometry into groups, each of which will be rendered in a separate WebGL draw call.
  120. This allows a [page:MultiMaterial] to be used with the bufferGeometry.<br /><br />
  121. Each group is an object of the form:
  122. <code>{ start: Integer, count: Integer, materialIndex: Integer }</code>
  123. where start specifies the index of the first vertex in this draw call, count specifies
  124. how many vertices are included, and materialIndex specifies the [page:MultiMaterial] index to use.<br /><br />
  125. Use [page:.addGroup] to add groups, rather than modifying this array directly.
  126. </div>
  127. <!-- Note: groups used to be called drawCalls
  128. <h3>[property:Array drawcalls]</h3>
  129. <div>
  130. For geometries that use indexed triangles, this Array can be used to split the object
  131. into multiple WebGL draw calls. Each draw call will draw some subset of the vertices
  132. in this geometry using the configured [page:Material shader]. This may be necessary if,
  133. for instance, you have more than 65535 vertices in your object.
  134. </div> -->
  135. <h3>[property:Integer id]</h3>
  136. <div>Unique number for this bufferGeometry instance.</div>
  137. <h3>[property:BufferAttribute index]</h3>
  138. <div>
  139. See "Accessing Attributes" section above for a description of this property.
  140. Default is *null*.
  141. </div>
  142. <h3>[property:Boolean isBufferGeometry]</h3>
  143. <div>
  144. Used to check whether this or derived classes are BufferGeometries. Default is *true*.<br /><br />
  145. You should not change this, as it used internally for optimisation.
  146. </div>
  147. <h3>[property:Integer MaxIndex]</h3>
  148. <div>Maximum number of vertices allowed, set to *65535*.</div>
  149. <h3>[property:Object morphAttributes]</h3>
  150. <div>
  151. Hashmap of [page:BufferAttribute]s holding details of the geometry's [page:Geometry.morphTargets morphTargets].
  152. </div>
  153. <h3>[property:String name]</h3>
  154. <div>
  155. Optional name for this bufferGeometry instance. Default is an empty string.
  156. </div>
  157. <h3>[property:String uuid]</h3>
  158. <div>
  159. [link:http://en.wikipedia.org/wiki/Universally_unique_identifier UUID] of this object instance.
  160. This gets automatically assigned and shouldn't be edited.
  161. </div>
  162. <h2>Methods</h2>
  163. <h3>[page:EventDispatcher EventDispatcher] methods are available on this class.</h3>
  164. <h3>[property:null addAttribute]( [page:String name], [page:BufferAttribute attribute] )</h3>
  165. <div>
  166. Adds an attribute to this geometry. Use this rather than the attributes property,
  167. because an internal hashmap of [page:.attributes] is maintained to speed up iterating over
  168. attributes.
  169. </div>
  170. <h3>[method:null addGroup]( [page:Integer start], [page:Integer count], [page:Integer materialIndex] )</h3>
  171. <div>
  172. Adds a group to this geometry; see the [page:BufferGeometry.groups groups]
  173. property for details.
  174. </div>
  175. <h3>[method:null applyMatrix]( [page:Matrix4 matrix] )</h3>
  176. <div>Bakes matrix transform directly into vertex coordinates.</div>
  177. <h3>[method:null center] ()</h3>
  178. <div>Center the geometry based on the bounding box.</div>
  179. <h3>[method:BufferGeometry clone]()</h3>
  180. <div>Creates a clone of this BufferGeometry.</div>
  181. <h3>[method:BufferGeometry copy]( [page:BufferGeometry bufferGeometry] )</h3>
  182. <div>Copies another BufferGeometry to this BufferGeometry.</div>
  183. <h3>[method:null clearGroups]( )</h3>
  184. <div>Clears all groups.</div>
  185. <h3>[method:null computeBoundingBox]()</h3>
  186. <div>
  187. Computes bounding box of the geometry, updating [page:.boundingBox] attribute.<br />
  188. Bounding boxes aren't computed by default. They need to be explicitly computed, otherwise they are *null*.
  189. </div>
  190. <h3>[method:null computeBoundingSphere]()</h3>
  191. <div>
  192. Computes bounding sphere of the geometry, updating [page:.boundingSphere] attribute.<br />
  193. Bounding spheres aren't computed by default. They need to be explicitly computed, otherwise they are *null*.
  194. </div>
  195. <h3>[method:null computeVertexNormals]()</h3>
  196. <div>Computes vertex normals by averaging face normals.</div>
  197. <h3>[method:null dispose]()</h3>
  198. <div>
  199. Disposes the object from memory. <br />
  200. You need to call this when you want the bufferGeometry removed while the application is running.
  201. </div>
  202. <h3>[method:BufferGeometry fromDirectGeometry]( [page:Geometry] )</h3>
  203. <div>
  204. Populates this BufferGeometry with data from a [page:DirectGeometry] object.<br /><br />
  205. Note: [page:DirectGeometry] is mainly used as an intermediary object for converting between [page:Geometry]
  206. and BufferGeometry.
  207. </div>
  208. <h3>[method:BufferGeometry fromGeometry]( [page:Geometry] )</h3>
  209. <div>Populates this BufferGeometry with data from a [page:Geometry] object.</div>
  210. <h3>[method:BufferAttribute getAttribute]( [page:String name] )</h3>
  211. <div>Returns the [page:BufferAttribute attribute] with the specified name.</div>
  212. <h3>[method:BufferAttribute getIndex] ()</h3>
  213. <div>Return the [page:.index] buffer.</div>
  214. <h3>[method:BufferGeometry lookAt] ( [page:Vector3 vector] )</h3>
  215. <div>
  216. vector - A world vector to look at.<br /><br />
  217. Rotates the geometry to face a point in space. This is typically done as a one time operation, and not during a loop.
  218. Use [page:Object3D.lookAt] for typical real-time mesh usage.
  219. </div>
  220. <h3>[method:null merge]( [page:BufferGeometry bufferGeometry], [page:Integer offset] )</h3>
  221. <div>Merge in another BufferGeometry with an optional offset of where to start merging in.</div>
  222. <h3>[method:null normalizeNormals]()</h3>
  223. <div>
  224. Every normal vector in a geometry will have a magnitude of 1.
  225. This will correct lighting on the geometry surfaces.
  226. </div>
  227. <h3>[method:BufferAttribute removeAttribute]( [page:String name] )</h3>
  228. <div>Removes the [page:BufferAttribute attribute] with the specified name.</div>
  229. <h3>[method:BufferGeometry rotateX] ( [page:Float radians] )</h3>
  230. <div>
  231. Rotate the geometry about the X axis. This is typically done as a one time operation, and not during a loop.
  232. Use [page:Object3D.rotation] for typical real-time mesh rotation.
  233. </div>
  234. <h3>[method:BufferGeometry rotateY] ( [page:Float radians] )</h3>
  235. <div>
  236. Rotate the geometry about the Y axis. This is typically done as a one time operation, and not during a loop.
  237. Use [page:Object3D.rotation] for typical real-time mesh rotation.
  238. </div>
  239. <h3>[method:BufferGeometry rotateZ] ( [page:Float radians] )</h3>
  240. <div>
  241. Rotate the geometry about the Z axis. This is typically done as a one time operation, and not during a loop.
  242. Use [page:Object3D.rotation] for typical real-time mesh rotation.
  243. </div>
  244. <h3>[method:BufferGeometry scale] ( [page:Float x], [page:Float y], [page:Float z] )</h3>
  245. <div>
  246. Scale the geometry data. This is typically done as a one time operation, and not during a loop.
  247. Use [page:Object3D.scale] for typical real-time mesh scaling.
  248. </div>
  249. <h3>[method:null setIndex] ( [page:BufferAttribute index] )</h3>
  250. <div>Set the [page:.index] buffer.</div>
  251. <h3>[method:null setDrawRange] ( [page:Integer start], [page:Integer count] )</h3>
  252. <div>Set the [page:.drawRange] buffer. See that property for details.</div>
  253. <h3>[method:BufferGeometry setFromObject] ( [page:Object3D object] )</h3>
  254. <div>Sets the attributes for this BufferGeometry from an [page:Object3D].</div>
  255. <h3>[method:Object toJSON]()</h3>
  256. <div>Returns a JSON object representation of the BufferGeometry.</div>
  257. <h3>[method:BufferGeometry toNonIndexed]()</h3>
  258. <div>Return a non-index version of an indexed BufferGeometry.</div>
  259. <h3>[method:BufferGeometry translate] ( [page:Float x], [page:Float y], [page:Float z] )</h3>
  260. <div>
  261. Translate the geometry. This is typically done as a one time operation, and not during a loop.
  262. Use [page:Object3D.position] for typical real-time mesh translation.
  263. </div>
  264. <h3>[method:BufferGeometry updateFromObject] ( [page:Object3D object] )</h3>
  265. <div>Updates the attributes for this BufferGeometry from an [page:Object3D].</div>
  266. <h2>Source</h2>
  267. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  268. </body>
  269. </html>