SkeletonHelper.js 2.9 KB

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