SkinnedMesh.html 5.8 KB

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