How-to-update-things.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 the 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. <p>
  34. You can only update the content of buffers, you cannot resize buffers (this is very costly,
  35. basically equivalent to creating new geometry).
  36. </p>
  37. <p>
  38. You can emulate resizing by pre-allocating a larger buffer and then keeping unneeded vertices
  39. collapsed / hidden.
  40. </p>
  41. <p>
  42. The following flag control updating of various geometry attributes. Set flags only
  43. for attributes that you need to update, updates are costly. Once buffers
  44. change, these flags reset automatically back to false. You need to keep setting them to
  45. true if you wanna keep updating buffers. Note that this applies only to [page:Geometry]
  46. and not to [page:BufferGeometry].
  47. </p>
  48. <code>
  49. var geometry = new THREE.Geometry();
  50. geometry.verticesNeedUpdate = true;
  51. geometry.elementsNeedUpdate = true;
  52. geometry.morphTargetsNeedUpdate = true;
  53. geometry.uvsNeedUpdate = true;
  54. geometry.normalsNeedUpdate = true;
  55. geometry.colorsNeedUpdate = true;
  56. geometry.tangentsNeedUpdate = true;
  57. </code>
  58. <p>
  59. In versions prior to [link:https://github.com/mrdoob/three.js/releases/tag/r66 r66] meshes
  60. additionally need the <em>dynamic</em> flag enabled (to keep internal typed arrays):
  61. </p>
  62. <code>
  63. //removed after r66
  64. geometry.dynamic = true;
  65. </code>
  66. <h3>Examples:</h3>
  67. [example:webgl_geometry_dynamic WebGL / geometry / dynamic]<br />
  68. [example:webgl_custom_attributes WebGL / custom / attributes]<br />
  69. [example:webgl_custom_attributes_particles WebGL / custom / attributes / particles]
  70. </div>
  71. <h2>Materials</h2>
  72. <div>
  73. <p>All uniforms values can be changed freely (e.g. colors, textures, opacity, etc), values are sent to the shader every frame.</p>
  74. <p>Also GLstate related parameters can change any time (depthTest, blending, polygonOffset, etc).</p>
  75. <p>Flat / smooth shading is baked into normals. You need to reset normals buffer (see above).</p>
  76. <p>The following properties can't be easily changed at runtime (once the material is rendered at least once):</p>
  77. <ul>
  78. <li>numbers and types of uniforms</li>
  79. <li>numbers and types of lights</li>
  80. <li>presence or not of
  81. <ul>
  82. <li>texture</li>
  83. <li>fog</li>
  84. <li>vertex colors</li>
  85. <li>skinning</li>
  86. <li>morphing</li>
  87. <li>shadow map</li>
  88. <li>alpha test</li>
  89. </ul>
  90. </li>
  91. </ul>
  92. <p>Changes in these require building of new shader program. You'll need to set</p>
  93. <code>material.needsUpdate = true</code>
  94. <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>
  95. <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>
  96. <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>
  97. <h3>If you need to have different configurations of materials during runtime:</h3>
  98. <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>
  99. <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>
  100. <h3>Examples:</h3>
  101. [example:webgl_materials_cars WebGL / materials / cars]<br />
  102. [example:webgl_postprocessing_dof WebGL / webgl_postprocessing / dof]
  103. </div>
  104. <h2>Textures</h2>
  105. <div>
  106. <p>Image, canvas, video and data textures need to have the following flag set if they are changed:</p>
  107. <code>
  108. texture.needsUpdate = true;
  109. </code>
  110. <p>Render targets update automatically.</p>
  111. <h3>Examples:</h3>
  112. [example:webgl_materials_video WebGL / materials / video]<br />
  113. [example:webgl_rtt WebGL / rtt]
  114. </div>
  115. <h2>Cameras</h2>
  116. <div>
  117. <p>A camera's position and target is updated automatically. If you need to change</p>
  118. <ul>
  119. <li>
  120. fov
  121. </li>
  122. <li>
  123. aspect
  124. </li>
  125. <li>
  126. near
  127. </li>
  128. <li>
  129. far
  130. </li>
  131. </ul>
  132. <p>
  133. then you'll need to recompute the projection matrix:
  134. </p>
  135. <code>
  136. camera.aspect = window.innerWidth / window.innerHeight;
  137. camera.updateProjectionMatrix();
  138. </code>
  139. </div>
  140. </body>
  141. </html>