SkeletonHelper.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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';
  9. import { Matrix4 } from '../math/Matrix4';
  10. import { VertexColors } from '../constants';
  11. import { LineBasicMaterial } from '../materials/LineBasicMaterial';
  12. import { Color } from '../math/Color';
  13. import { Vector3 } from '../math/Vector3';
  14. import { BufferGeometry } from '../core/BufferGeometry';
  15. import { Float32BufferAttribute } from '../core/BufferAttribute';
  16. function getBoneList( object ) {
  17. var boneList = [];
  18. if ( object && object.isBone ) {
  19. boneList.push( object );
  20. }
  21. for ( var i = 0; i < object.children.length; i ++ ) {
  22. boneList.push.apply( boneList, getBoneList( object.children[ i ] ) );
  23. }
  24. return boneList;
  25. }
  26. function SkeletonHelper( object ) {
  27. var bones = getBoneList( object );
  28. var geometry = new BufferGeometry();
  29. var vertices = [];
  30. var colors = [];
  31. var color1 = new Color( 0, 0, 1 );
  32. var color2 = new Color( 0, 1, 0 );
  33. for ( var i = 0; i < bones.length; i ++ ) {
  34. var bone = bones[ i ];
  35. if ( bone.parent && bone.parent.isBone ) {
  36. vertices.push( 0, 0, 0 );
  37. vertices.push( 0, 0, 0 );
  38. colors.push( color1.r, color1.g, color1.b );
  39. colors.push( color2.r, color2.g, color2.b );
  40. }
  41. }
  42. geometry.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  43. geometry.addAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  44. var material = new LineBasicMaterial( { vertexColors: VertexColors, depthTest: false, depthWrite: false, transparent: true } );
  45. LineSegments.call( this, geometry, material );
  46. this.root = object;
  47. this.bones = bones;
  48. this.matrix = object.matrixWorld;
  49. this.matrixAutoUpdate = false;
  50. this.onBeforeRender();
  51. }
  52. SkeletonHelper.prototype = Object.create( LineSegments.prototype );
  53. SkeletonHelper.prototype.constructor = SkeletonHelper;
  54. SkeletonHelper.prototype.onBeforeRender = function () {
  55. var vector = new Vector3();
  56. var boneMatrix = new Matrix4();
  57. var matrixWorldInv = new Matrix4();
  58. return function onBeforeRender() {
  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. };
  77. }();
  78. export { SkeletonHelper };