How-to-update-things.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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>
  13. <p>All objects by default automatically update their matrices if they have been added to the scene with</p>
  14. <code>
  15. var object = new THREE.Object3D;
  16. scene.add( object );
  17. </code>
  18. or if they are the child of another object that has been added to the scene:
  19. <code>
  20. var object1 = new THREE.Object3D;
  21. var object2 = new THREE.Object3D;
  22. object1.add( object2 );
  23. scene.add( object1 ); //object1 and object2 will automatically update their matrices
  24. </code>
  25. </div>
  26. <p>However, if you know object will be static, you can disable this and update the transform matrix manually just when needed.</p>
  27. <code>
  28. object.matrixAutoUpdate = false;
  29. object.updateMatrix();
  30. </code>
  31. <h2>Geometries</h2>
  32. <div>
  33. <h3>[page:BufferGeometry]</h3>
  34. <div>
  35. <p>
  36. BufferGeometries store information (such as vertex positions, face indices, normals, colors,
  37. UVs, and any custom attributes) in [page:BufferAttribute buffers] - that is,
  38. [link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays typed arrays].
  39. This makes them generally faster than standard Geometries, at the cost of being somewhat harder to
  40. work with.
  41. </p>
  42. <p>
  43. With regards to updating BufferGeometries, the most important thing to understand is that
  44. you cannot resize buffers (this is very costly, basically the equivalent to creating a new geometry).
  45. You can however update the content of buffers.
  46. </p>
  47. <p>
  48. This means that if you know an attribute of you BufferGeometry will grow, say the number of vertices,
  49. you must pre-allocate a buffer large enough to hold any new vertices that may be created. Of
  50. course, this also means that there will be a maximum size for your BufferGeometry - there is
  51. no way to create a BufferGeometry that can efficiently be extended indefinitely.
  52. </p>
  53. <p>
  54. We'll use the example of a line that gets extended at render time. We'll allocate space
  55. in the buffer for 500 vertices but draw only two at first, using [page:BufferGeometry.drawRange].
  56. </p>
  57. <code>
  58. var MAX_POINTS = 500;
  59. // geometry
  60. var geometry = new THREE.BufferGeometry();
  61. // attributes
  62. var positions = new Float32Array( MAX_POINTS * 3 ); // 3 vertices per point
  63. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  64. // draw range
  65. drawCount = 2; // draw the first 2 points, only
  66. geometry.setDrawRange( 0, drawCount );
  67. // material
  68. var material = new THREE.LineBasicMaterial( { color: 0xff0000, linewidth: 2 } );
  69. // line
  70. line = new THREE.Line( geometry, material );
  71. scene.add( line );
  72. </code>
  73. <p>
  74. Next we'll randomly add points to the line using a pattern like:
  75. </p>
  76. <code>
  77. var positions = line.geometry.attributes.position.array;
  78. var x = y = z = index = 0;
  79. for ( var i = 0, l = MAX_POINTS; i < l; i ++ ) {
  80. positions[ index ++ ] = x;
  81. positions[ index ++ ] = y;
  82. positions[ index ++ ] = z;
  83. x += ( Math.random() - 0.5 ) * 30;
  84. y += ( Math.random() - 0.5 ) * 30;
  85. z += ( Math.random() - 0.5 ) * 30;
  86. }
  87. </code>
  88. <p>
  89. If you want to change the <em>number of points</em> rendered after the first render, do this:
  90. </p>
  91. <code>
  92. line.geometry.setDrawRange( 0, newValue );
  93. </code>
  94. <p>
  95. If you want to change the position data values after the first render, you need to
  96. set the needsUpdate flag like so:
  97. </p>
  98. <code>
  99. line.geometry.attributes.position.needsUpdate = true; // required after the first render
  100. </code>
  101. <p>
  102. [link:http://jsfiddle.net/w67tzfhx/ Here is a fiddle] showing an animated line which you can adapt to your use case.
  103. </p>
  104. <h4>Examples:</h4>
  105. [example:webgl_custom_attributes WebGL / custom / attributes]<br />
  106. [example:webgl_buffergeometry_custom_attributes_particles WebGL / buffergeometry / custom / attributes / particles]
  107. </div>
  108. <h3>[page:Geometry]</h3>
  109. <div>
  110. <p>
  111. The following flag control updating of various geometry attributes. Set flags only
  112. for attributes that you need to update, updates are costly. Once buffers
  113. change, these flags reset automatically back to false. You need to keep setting them to
  114. true if you wanna keep updating buffers. Note that this applies only to [page:Geometry]
  115. and not to [page:BufferGeometry].
  116. </p>
  117. <code>
  118. var geometry = new THREE.Geometry();
  119. geometry.verticesNeedUpdate = true;
  120. geometry.elementsNeedUpdate = true;
  121. geometry.morphTargetsNeedUpdate = true;
  122. geometry.uvsNeedUpdate = true;
  123. geometry.normalsNeedUpdate = true;
  124. geometry.colorsNeedUpdate = true;
  125. geometry.tangentsNeedUpdate = true;
  126. </code>
  127. <p>
  128. In versions prior to [link:https://github.com/mrdoob/three.js/releases/tag/r66 r66] meshes
  129. additionally need the <em>dynamic</em> flag enabled (to keep internal typed arrays):
  130. </p>
  131. <code>
  132. //removed after r66
  133. geometry.dynamic = true;
  134. </code>
  135. <h4>Example:</h4>
  136. [example:webgl_geometry_dynamic WebGL / geometry / dynamic]<br />
  137. </div>
  138. </div>
  139. <h2>Materials</h2>
  140. <div>
  141. <p>All uniforms values can be changed freely (e.g. colors, textures, opacity, etc), values are sent to the shader every frame.</p>
  142. <p>Also GLstate related parameters can change any time (depthTest, blending, polygonOffset, etc).</p>
  143. <p>Flat / smooth shading is baked into normals. You need to reset normals buffer (see above).</p>
  144. <p>The following properties can't be easily changed at runtime (once the material is rendered at least once):</p>
  145. <ul>
  146. <li>numbers and types of uniforms</li>
  147. <li>numbers and types of lights</li>
  148. <li>presence or not of
  149. <ul>
  150. <li>texture</li>
  151. <li>fog</li>
  152. <li>vertex colors</li>
  153. <li>skinning</li>
  154. <li>morphing</li>
  155. <li>shadow map</li>
  156. <li>alpha test</li>
  157. </ul>
  158. </li>
  159. </ul>
  160. <p>Changes in these require building of new shader program. You'll need to set</p>
  161. <code>material.needsUpdate = true</code>
  162. <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>
  163. <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>
  164. <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>
  165. <h3>If you need to have different configurations of materials during runtime:</h3>
  166. <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>
  167. <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>
  168. <h3>Examples:</h3>
  169. [example:webgl_materials_cars WebGL / materials / cars]<br />
  170. [example:webgl_postprocessing_dof WebGL / webgl_postprocessing / dof]
  171. </div>
  172. <h2>Textures</h2>
  173. <div>
  174. <p>Image, canvas, video and data textures need to have the following flag set if they are changed:</p>
  175. <code>
  176. texture.needsUpdate = true;
  177. </code>
  178. <p>Render targets update automatically.</p>
  179. <h3>Examples:</h3>
  180. [example:webgl_materials_video WebGL / materials / video]<br />
  181. [example:webgl_rtt WebGL / rtt]
  182. </div>
  183. <h2>Cameras</h2>
  184. <div>
  185. <p>A camera's position and target is updated automatically. If you need to change</p>
  186. <ul>
  187. <li>
  188. fov
  189. </li>
  190. <li>
  191. aspect
  192. </li>
  193. <li>
  194. near
  195. </li>
  196. <li>
  197. far
  198. </li>
  199. </ul>
  200. <p>
  201. then you'll need to recompute the projection matrix:
  202. </p>
  203. <code>
  204. camera.aspect = window.innerWidth / window.innerHeight;
  205. camera.updateProjectionMatrix();
  206. </code>
  207. </div>
  208. </body>
  209. </html>