SkinnedMesh.html 5.0 KB

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