How-to-update-things.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. <h1>[name]</h1>
  11. <div>
  12. <p>All objects by default automatically update their matrices if they have been added to the scene with</p>
  13. <code>
  14. const object = new THREE.Object3D();
  15. scene.add( object );
  16. </code>
  17. or if they are the child of another object that has been added to the scene:
  18. <code>
  19. const object1 = new THREE.Object3D();
  20. const object2 = new THREE.Object3D();
  21. object1.add( object2 );
  22. scene.add( object1 ); //object1 and object2 will automatically update their matrices
  23. </code>
  24. </div>
  25. <p>However, if you know the object will be static, you can disable this and update the transform matrix manually just when needed.</p>
  26. <code>
  27. object.matrixAutoUpdate = false;
  28. object.updateMatrix();
  29. </code>
  30. <h2>BufferGeometry</h2>
  31. <div>
  32. <p>
  33. BufferGeometries store information (such as vertex positions, face indices, normals, colors,
  34. UVs, and any custom attributes) in [page:BufferAttribute buffers] - that is,
  35. [link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays typed arrays].
  36. This makes them generally faster than standard Geometries, at the cost of being somewhat harder to
  37. work with.
  38. </p>
  39. <p>
  40. With regards to updating BufferGeometries, the most important thing to understand is that
  41. you cannot resize buffers (this is very costly, basically the equivalent to creating a new geometry).
  42. You can however update the content of buffers.
  43. </p>
  44. <p>
  45. This means that if you know an attribute of your BufferGeometry will grow, say the number of vertices,
  46. you must pre-allocate a buffer large enough to hold any new vertices that may be created. Of
  47. course, this also means that there will be a maximum size for your BufferGeometry - there is
  48. no way to create a BufferGeometry that can efficiently be extended indefinitely.
  49. </p>
  50. <p>
  51. We'll use the example of a line that gets extended at render time. We'll allocate space
  52. in the buffer for 500 vertices but draw only two at first, using [page:BufferGeometry.drawRange].
  53. </p>
  54. <code>
  55. const MAX_POINTS = 500;
  56. // geometry
  57. const geometry = new THREE.BufferGeometry();
  58. // attributes
  59. const positions = new Float32Array( MAX_POINTS * 3 ); // 3 vertices per point
  60. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  61. // draw range
  62. const drawCount = 2; // draw the first 2 points, only
  63. geometry.setDrawRange( 0, drawCount );
  64. // material
  65. const material = new THREE.LineBasicMaterial( { color: 0xff0000 } );
  66. // line
  67. const line = new THREE.Line( geometry, material );
  68. scene.add( line );
  69. </code>
  70. <p>
  71. Next we'll randomly add points to the line using a pattern like:
  72. </p>
  73. <code>
  74. const positionAttribute = line.geometry.getAttribute( 'position' );
  75. let x = 0, y = 0, z = 0;
  76. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  77. positionAttribute.setXYZ( i, x, y, z );
  78. x += ( Math.random() - 0.5 ) * 30;
  79. y += ( Math.random() - 0.5 ) * 30;
  80. z += ( Math.random() - 0.5 ) * 30;
  81. }
  82. </code>
  83. <p>
  84. If you want to change the <em>number of points</em> rendered after the first render, do this:
  85. </p>
  86. <code>
  87. line.geometry.setDrawRange( 0, newValue );
  88. </code>
  89. <p>
  90. If you want to change the position data values after the first render, you need to
  91. set the needsUpdate flag like so:
  92. </p>
  93. <code>
  94. positionAttribute.needsUpdate = true; // required after the first render
  95. </code>
  96. <p>
  97. If you change the position data values after the initial render, you may need to recompute
  98. bounding volumes so other features of the engine like view frustum culling or helpers properly work.
  99. </p>
  100. <code>
  101. line.geometry.computeBoundingBox();
  102. line.geometry.computeBoundingSphere();
  103. </code>
  104. <p>
  105. [link:https://jsfiddle.net/t4m85pLr/1/ Here is a fiddle] showing an animated line which you can adapt to your use case.
  106. </p>
  107. <h3>Examples</h3>
  108. <p>
  109. [example:webgl_custom_attributes WebGL / custom / attributes]<br />
  110. [example:webgl_buffergeometry_custom_attributes_particles WebGL / buffergeometry / custom / attributes / particles]
  111. </p>
  112. </div>
  113. <h2>Materials</h2>
  114. <div>
  115. <p>All uniforms values can be changed freely (e.g. colors, textures, opacity, etc), values are sent to the shader every frame.</p>
  116. <p>Also GLstate related parameters can change any time (depthTest, blending, polygonOffset, etc).</p>
  117. <p>The following properties can't be easily changed at runtime (once the material is rendered at least once):</p>
  118. <ul>
  119. <li>numbers and types of uniforms</li>
  120. <li>presence or not of
  121. <ul>
  122. <li>texture</li>
  123. <li>fog</li>
  124. <li>vertex colors</li>
  125. <li>morphing</li>
  126. <li>shadow map</li>
  127. <li>alpha test</li>
  128. <li>transparent</li>
  129. </ul>
  130. </li>
  131. </ul>
  132. <p>Changes in these require building of new shader program. You'll need to set</p>
  133. <code>material.needsUpdate = true</code>
  134. <p>Bear in mind this might be quite slow and induce jerkiness in framerate (especially on Windows, as shader compilation is slower in DirectX than OpenGL).</p>
  135. <p>For smoother experience you can emulate changes in these features to some degree by having "dummy" values like zero intensity lights, white textures, or zero density fog.</p>
  136. <p>You can freely change the material used for geometry chunks, however you cannot change how an object is divided into chunks (according to face materials). </p>
  137. <h3>If you need to have different configurations of materials during runtime:</h3>
  138. <p>If the number of materials / chunks is small, you could pre-divide the object beforehand (e.g. hair / face / body / upper clothes / trousers for a human, front / sides / top / glass / tire / interior for a car). </p>
  139. <p>If the number is large (e.g. each face could be potentially different), consider a different solution, such as using attributes / textures to drive different per-face look.</p>
  140. <h3>Examples</h3>
  141. <p>
  142. [example:webgl_materials_car WebGL / materials / car]<br />
  143. [example:webgl_postprocessing_dof WebGL / webgl_postprocessing / dof]
  144. </p>
  145. </div>
  146. <h2>Textures</h2>
  147. <div>
  148. <p>Image, canvas, video and data textures need to have the following flag set if they are changed:</p>
  149. <code>
  150. texture.needsUpdate = true;
  151. </code>
  152. <p>Render targets update automatically.</p>
  153. <h3>Examples</h3>
  154. <p>
  155. [example:webgl_materials_video WebGL / materials / video]<br />
  156. [example:webgl_rtt WebGL / rtt]
  157. </p>
  158. </div>
  159. <h2>Cameras</h2>
  160. <div>
  161. <p>A camera's position and target is updated automatically. If you need to change</p>
  162. <ul>
  163. <li>
  164. fov
  165. </li>
  166. <li>
  167. aspect
  168. </li>
  169. <li>
  170. near
  171. </li>
  172. <li>
  173. far
  174. </li>
  175. </ul>
  176. <p>
  177. then you'll need to recompute the projection matrix:
  178. </p>
  179. <code>
  180. camera.aspect = window.innerWidth / window.innerHeight;
  181. camera.updateProjectionMatrix();
  182. </code>
  183. </div>
  184. <h2>InstancedMesh</h2>
  185. <div>
  186. <p>
  187. `InstancedMesh` is a class for conveniently access instanced rendering in `three.js`. Certain library features like view frustum culling or
  188. ray casting rely on up-to-date bounding volumes (bounding sphere and bounding box). Because of the way how `InstancedMesh` works, the class
  189. has its own [page:InstancedMesh.boundingBox boundingBox] and [page:InstancedMesh.boundingSphere boundingSphere] properties that supersede
  190. the bounding volumes on geometry level.
  191. </p>
  192. <p>
  193. Similar to geometries you have to recompute the bounding box and sphere whenever you change the underlying data. In context of `InstancedMesh`, that
  194. happens when you transform instances via [page:InstancedMesh.setMatrixAt setMatrixAt](). You can use the same pattern like with geometries.
  195. </p>
  196. <code>
  197. instancedMesh.computeBoundingBox();
  198. instancedMesh.computeBoundingSphere();
  199. </code>
  200. </div>
  201. <h2>SkinnedMesh</h2>
  202. <div>
  203. <p>
  204. `SkinnedMesh` follows the same principles like `InstancedMesh` in context of bounding volumes. Meaning the class has its own version of
  205. [page:SkinnedMesh.boundingBox boundingBox] and [page:SkinnedMesh.boundingSphere boundingSphere] to correctly enclose animated meshes.
  206. When calling `computeBoundingBox()` and `computeBoundingSphere()`, the class computes the respective bounding volumes based on the current
  207. bone tranformation (or in other words the current animation state).
  208. </p>
  209. </div>
  210. </body>
  211. </html>