SkinnedMesh.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. [page:Object3D] &rarr; [page:Mesh] &rarr;
  11. <h1>[name]</h1>
  12. <p class="desc">
  13. A mesh that has a [page:Skeleton] with [page:Bone bones] that can then be
  14. used to animate the vertices of the geometry.<br /><br />
  15. [name] can only be used with WebGL 2. With WebGL 1 `OES_texture_float` and
  16. vertex textures support is required.
  17. </p>
  18. <iframe id="scene" src="scenes/bones-browser.html"></iframe>
  19. <script>
  20. // iOS iframe auto-resize workaround
  21. if ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) {
  22. const scene = document.getElementById( 'scene' );
  23. scene.style.width = getComputedStyle( scene ).width;
  24. scene.style.height = getComputedStyle( scene ).height;
  25. scene.setAttribute( 'scrolling', 'no' );
  26. }
  27. </script>
  28. <h2>Code Example</h2>
  29. <code>
  30. const geometry = new THREE.CylinderGeometry( 5, 5, 5, 5, 15, 5, 30 );
  31. // create the skin indices and skin weights manually
  32. // (typically a loader would read this data from a 3D model for you)
  33. const position = geometry.attributes.position;
  34. const vertex = new THREE.Vector3();
  35. const skinIndices = [];
  36. const skinWeights = [];
  37. for ( let i = 0; i < position.count; i ++ ) {
  38. vertex.fromBufferAttribute( position, i );
  39. // compute skinIndex and skinWeight based on some configuration data
  40. const y = ( vertex.y + sizing.halfHeight );
  41. const skinIndex = Math.floor( y / sizing.segmentHeight );
  42. const skinWeight = ( y % sizing.segmentHeight ) / sizing.segmentHeight;
  43. skinIndices.push( skinIndex, skinIndex + 1, 0, 0 );
  44. skinWeights.push( 1 - skinWeight, skinWeight, 0, 0 );
  45. }
  46. geometry.setAttribute( 'skinIndex', new THREE.Uint16BufferAttribute( skinIndices, 4 ) );
  47. geometry.setAttribute( 'skinWeight', new THREE.Float32BufferAttribute( skinWeights, 4 ) );
  48. // create skinned mesh and skeleton
  49. const mesh = new THREE.SkinnedMesh( geometry, material );
  50. const skeleton = new THREE.Skeleton( bones );
  51. // see example from THREE.Skeleton
  52. const rootBone = skeleton.bones[ 0 ];
  53. mesh.add( rootBone );
  54. // bind the skeleton to the mesh
  55. mesh.bind( skeleton );
  56. // move the bones and manipulate the model
  57. skeleton.bones[ 0 ].rotation.x = -0.1;
  58. skeleton.bones[ 1 ].rotation.x = 0.2;
  59. </code>
  60. <h2>Constructor</h2>
  61. <h3>
  62. [name]( [param:BufferGeometry geometry], [param:Material material] )
  63. </h3>
  64. <p>
  65. [page:BufferGeometry geometry] - an instance of [page:BufferGeometry].<br />
  66. [page:Material material] - (optional) an instance of [page:Material].
  67. Default is a new [page:MeshBasicMaterial].
  68. </p>
  69. <h2>Properties</h2>
  70. <p>See the base [page:Mesh] class for common properties.</p>
  71. <h3>[property:String bindMode]</h3>
  72. <p>
  73. Either `AttachedBindMode` or `DetachedBindMode`. `AttachedBindMode` means the skinned mesh
  74. shares the same world space as the skeleton. This is not true when using `DetachedBindMode`
  75. which is useful when sharing a skeleton across multiple skinned meshes.
  76. Default is `AttachedBindMode`.
  77. </p>
  78. <h3>[property:Matrix4 bindMatrix]</h3>
  79. <p>The base matrix that is used for the bound bone transforms.</p>
  80. <h3>[property:Matrix4 bindMatrixInverse]</h3>
  81. <p>The base matrix that is used for resetting the bound bone transforms.</p>
  82. <h3>[property:Box3 boundingBox]</h3>
  83. <p>
  84. The bounding box of the [name]. Can be calculated with
  85. [page:.computeBoundingBox](). Default is `null`.
  86. </p>
  87. <h3>[property:Sphere boundingSphere]</h3>
  88. <p>
  89. The bounding sphere of the [name]. Can be calculated with
  90. [page:.computeBoundingSphere](). Default is `null`.
  91. </p>
  92. <h3>[property:Boolean isSkinnedMesh]</h3>
  93. <p>Read-only flag to check if a given object is of type [name].</p>
  94. <h3>[property:Skeleton skeleton]</h3>
  95. <p>[page:Skeleton] representing the bone hierarchy of the skinned mesh.</p>
  96. <h2>Methods</h2>
  97. <p>See the base [page:Mesh] class for common methods.</p>
  98. <h3>
  99. [method:undefined bind]( [param:Skeleton skeleton], [param:Matrix4 bindMatrix] )
  100. </h3>
  101. <p>
  102. [page:Skeleton skeleton] - [page:Skeleton] created from a [page:Bone Bones] tree.<br />
  103. [page:Matrix4 bindMatrix] - [page:Matrix4] that represents the base
  104. transform of the skeleton.<br /><br />
  105. Bind a skeleton to the skinned mesh. The bindMatrix gets saved to
  106. .bindMatrix property and the .bindMatrixInverse gets calculated.
  107. </p>
  108. <h3>[method:SkinnedMesh clone]()</h3>
  109. <p>
  110. This method does currently not clone an instance of [name] correctly.
  111. Please use [page:SkeletonUtils.clone]() in the meanwhile.
  112. </p>
  113. <h3>[method:undefined computeBoundingBox]()</h3>
  114. <p>
  115. Computes the bounding box, updating [page:.boundingBox] attribute.<br />
  116. Bounding boxes aren't computed by default. They need to be explicitly
  117. computed, otherwise they are `null`. If an instance of [name] is animated,
  118. this method should be called per frame to compute a correct bounding box.
  119. </p>
  120. <h3>[method:undefined computeBoundingSphere]()</h3>
  121. <p>
  122. Computes the bounding sphere, updating [page:.boundingSphere]
  123. attribute.<br />
  124. Bounding spheres aren't computed by default. They need to be explicitly
  125. computed, otherwise they are `null`. If an instance of [name] is animated,
  126. this method should be called per frame to compute a correct bounding
  127. sphere.
  128. </p>
  129. <h3>[method:undefined normalizeSkinWeights]()</h3>
  130. <p>Normalizes the skin weights.</p>
  131. <h3>[method:undefined pose]()</h3>
  132. <p>This method sets the skinned mesh in the rest pose (resets the pose).</p>
  133. <h3>
  134. [method:Vector3 applyBoneTransform]( [param:Integer index], [param:Vector3 vector] )
  135. </h3>
  136. <p>
  137. Applies the bone transform associated with the given index to the given
  138. position vector. Returns the updated vector.
  139. </p>
  140. <h2>Source</h2>
  141. <p>
  142. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  143. </p>
  144. </body>
  145. </html>