SkinnedMesh.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 used to animate the vertices of the geometry.
  14. </p>
  15. <iframe id="scene" src="scenes/bones-browser.html"></iframe>
  16. <script>
  17. // iOS iframe auto-resize workaround
  18. if ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) {
  19. const scene = document.getElementById( 'scene' );
  20. scene.style.width = getComputedStyle( scene ).width;
  21. scene.style.height = getComputedStyle( scene ).height;
  22. scene.setAttribute( 'scrolling', 'no' );
  23. }
  24. </script>
  25. <h2>Code Example</h2>
  26. <code>
  27. const geometry = new THREE.CylinderGeometry( 5, 5, 5, 5, 15, 5, 30 );
  28. // create the skin indices and skin weights
  29. const position = geometry.attributes.position;
  30. const vertex = new THREE.Vector3();
  31. const skinIndices = [];
  32. const skinWeights = [];
  33. for ( let i = 0; i < position.count; i ++ ) {
  34. vertex.fromBufferAttribute( position, i );
  35. // compute skinIndex and skinWeight based on some configuration data
  36. const y = ( vertex.y + sizing.halfHeight );
  37. const skinIndex = Math.floor( y / sizing.segmentHeight );
  38. const skinWeight = ( y % sizing.segmentHeight ) / sizing.segmentHeight;
  39. skinIndices.push( skinIndex, skinIndex + 1, 0, 0 );
  40. skinWeights.push( 1 - skinWeight, skinWeight, 0, 0 );
  41. }
  42. geometry.setAttribute( 'skinIndex', new THREE.Uint16BufferAttribute( skinIndices, 4 ) );
  43. geometry.setAttribute( 'skinWeight', new THREE.Float32BufferAttribute( skinWeights, 4 ) );
  44. // create skinned mesh and skeleton
  45. const mesh = new THREE.SkinnedMesh( geometry, material );
  46. const skeleton = new THREE.Skeleton( bones );
  47. // see example from THREE.Skeleton
  48. const rootBone = skeleton.bones[ 0 ];
  49. mesh.add( rootBone );
  50. // bind the skeleton to the mesh
  51. mesh.bind( skeleton );
  52. // move the bones and manipulate the model
  53. skeleton.bones[ 0 ].rotation.x = -0.1;
  54. skeleton.bones[ 1 ].rotation.x = 0.2;
  55. </code>
  56. <h2>Constructor</h2>
  57. <h3>[name]( [param:BufferGeometry geometry], [param:Material material] )</h3>
  58. <p>
  59. [page:BufferGeometry geometry] - an instance of [page:BufferGeometry].<br />
  60. [page:Material material] - (optional) an instance of [page:Material]. Default is a new [page:MeshBasicMaterial].
  61. </p>
  62. <h2>Properties</h2>
  63. <p>See the base [page:Mesh] class for common properties.</p>
  64. <h3>[property:String bindMode]</h3>
  65. <p>
  66. Either "attached" or "detached". "attached" uses the [page:SkinnedMesh.matrixWorld]
  67. property for the base transform matrix of the bones. "detached" uses the
  68. [page:SkinnedMesh.bindMatrix]. Default is "attached".
  69. </p>
  70. <h3>[property:Matrix4 bindMatrix]</h3>
  71. <p>
  72. The base matrix that is used for the bound bone transforms.
  73. </p>
  74. <h3>[property:Matrix4 bindMatrixInverse]</h3>
  75. <p>
  76. The base matrix that is used for resetting the bound bone transforms.
  77. </p>
  78. <h3>[property:Skeleton skeleton]</h3>
  79. <p>
  80. [page:Skeleton] representing the bone hierarchy of the skinned mesh.
  81. </p>
  82. <h2>Methods</h2>
  83. <p>See the base [page:Mesh] class for common methods.</p>
  84. <h3>[method:null bind]( [param:Skeleton skeleton], [param:Matrix4 bindMatrix] )</h3>
  85. <p>
  86. [page:Skeleton skeleton] - [page:Skeleton] created from a [page:Bone Bones] tree.<br/>
  87. [page:Matrix4 bindMatrix] - [page:Matrix4] that represents the base transform of the skeleton.<br /><br />
  88. Bind a skeleton to the skinned mesh. The bindMatrix gets saved to .bindMatrix property
  89. and the .bindMatrixInverse gets calculated.
  90. </p>
  91. <h3>[method:SkinnedMesh clone]()</h3>
  92. <p>
  93. Returns a clone of this SkinnedMesh object and any descendants.
  94. </p>
  95. <h3>[method:null normalizeSkinWeights]()</h3>
  96. <p>
  97. Normalizes the skin weights.
  98. </p>
  99. <h3>[method:null pose]()</h3>
  100. <p>
  101. This method sets the skinned mesh in the rest pose (resets the pose).
  102. </p>
  103. <h3>[method:null updateMatrixWorld]( [param:Boolean force] )</h3>
  104. <p>
  105. Updates the [page:Matrix4 MatrixWorld].
  106. </p>
  107. <h3>[method:Vector3 boneTransform]( [index:Integer], [target:Vector3] )</h3>
  108. <p>
  109. Calculates the position of the vertex at the given index relative to the current bone transformations.
  110. </p>
  111. <h2>Source</h2>
  112. <p>
  113. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  114. </p>
  115. </body>
  116. </html>