SkeletonHelper.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @author Sean Griffin / http://twitter.com/sgrif
  3. * @author Michael Guerrero / http://realitymeltdown.com
  4. * @author mrdoob / http://mrdoob.com/
  5. * @author ikerr / http://verold.com
  6. * @author Mugen87 / https://github.com/Mugen87
  7. */
  8. import { LineSegments } from '../objects/LineSegments.js';
  9. import { Matrix4 } from '../math/Matrix4.js';
  10. import { VertexColors } from '../constants.js';
  11. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  12. import { Color } from '../math/Color.js';
  13. import { Vector3 } from '../math/Vector3.js';
  14. import { BufferGeometry } from '../core/BufferGeometry.js';
  15. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  16. import { Object3D } from '../core/Object3D.js';
  17. function getBoneList( object ) {
  18. var boneList = [];
  19. if ( object && object.isBone ) {
  20. boneList.push( object );
  21. }
  22. for ( var i = 0; i < object.children.length; i ++ ) {
  23. boneList.push.apply( boneList, getBoneList( object.children[ i ] ) );
  24. }
  25. return boneList;
  26. }
  27. function SkeletonHelper( object ) {
  28. var bones = getBoneList( object );
  29. var geometry = new BufferGeometry();
  30. var vertices = [];
  31. var colors = [];
  32. var color1 = new Color( 0, 0, 1 );
  33. var color2 = new Color( 0, 1, 0 );
  34. for ( var i = 0; i < bones.length; i ++ ) {
  35. var bone = bones[ i ];
  36. if ( bone.parent && bone.parent.isBone ) {
  37. vertices.push( 0, 0, 0 );
  38. vertices.push( 0, 0, 0 );
  39. colors.push( color1.r, color1.g, color1.b );
  40. colors.push( color2.r, color2.g, color2.b );
  41. }
  42. }
  43. geometry.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  44. geometry.addAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  45. var material = new LineBasicMaterial( { vertexColors: VertexColors, depthTest: false, depthWrite: false, transparent: true } );
  46. LineSegments.call( this, geometry, material );
  47. this.root = object;
  48. this.bones = bones;
  49. this.matrix = object.matrixWorld;
  50. this.matrixAutoUpdate = false;
  51. }
  52. SkeletonHelper.prototype = Object.create( LineSegments.prototype );
  53. SkeletonHelper.prototype.constructor = SkeletonHelper;
  54. SkeletonHelper.prototype.updateMatrixWorld = function () {
  55. var vector = new Vector3();
  56. var boneMatrix = new Matrix4();
  57. var matrixWorldInv = new Matrix4();
  58. return function updateMatrixWorld( force ) {
  59. var bones = this.bones;
  60. var geometry = this.geometry;
  61. var position = geometry.getAttribute( 'position' );
  62. matrixWorldInv.getInverse( this.root.matrixWorld );
  63. for ( var i = 0, j = 0; i < bones.length; i ++ ) {
  64. var bone = bones[ i ];
  65. if ( bone.parent && bone.parent.isBone ) {
  66. boneMatrix.multiplyMatrices( matrixWorldInv, bone.matrixWorld );
  67. vector.setFromMatrixPosition( boneMatrix );
  68. position.setXYZ( j, vector.x, vector.y, vector.z );
  69. boneMatrix.multiplyMatrices( matrixWorldInv, bone.parent.matrixWorld );
  70. vector.setFromMatrixPosition( boneMatrix );
  71. position.setXYZ( j + 1, vector.x, vector.y, vector.z );
  72. j += 2;
  73. }
  74. }
  75. geometry.getAttribute( 'position' ).needsUpdate = true;
  76. Object3D.prototype.updateMatrixWorld.call( this, force );
  77. };
  78. }();
  79. export { SkeletonHelper };