UCSCharacter.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. THREE.UCSCharacter = function() {
  2. var scope = this;
  3. var mesh;
  4. this.scale = 1;
  5. this.root = new THREE.Object3D();
  6. this.numSkins;
  7. this.numMorphs;
  8. this.skins = [];
  9. this.materials = [];
  10. this.morphs = [];
  11. this.mixer = new THREE.AnimationMixer( this.root );
  12. this.onLoadComplete = function () {};
  13. this.loadCounter = 0;
  14. this.loadParts = function ( config ) {
  15. this.numSkins = config.skins.length;
  16. this.numMorphs = config.morphs.length;
  17. // Character geometry + number of skins
  18. this.loadCounter = 1 + config.skins.length;
  19. // SKINS
  20. this.skins = loadTextures( config.baseUrl + "skins/", config.skins );
  21. this.materials = createMaterials( this.skins );
  22. // MORPHS
  23. this.morphs = config.morphs;
  24. // CHARACTER
  25. var loader = new THREE.JSONLoader();
  26. console.log( config.baseUrl + config.character );
  27. loader.load( config.baseUrl + config.character, function( geometry ) {
  28. geometry.computeBoundingBox();
  29. geometry.computeVertexNormals();
  30. mesh = new THREE.SkinnedMesh( geometry, new THREE.MeshFaceMaterial() );
  31. mesh.name = config.character;
  32. scope.root.add( mesh );
  33. var bb = geometry.boundingBox;
  34. scope.root.scale.set( config.s, config.s, config.s );
  35. scope.root.position.set( config.x, config.y - bb.min.y * config.s, config.z );
  36. mesh.castShadow = true;
  37. mesh.receiveShadow = true;
  38. console.log( geometry );
  39. var clipBones = THREE.AnimationClip.FromJSONLoaderAnimation( geometry.animation, geometry.bones, mesh.uuid );
  40. scope.mixer.addAction( new THREE.AnimationAction( clipBones, 0, 1, 1, true ) );
  41. scope.setSkin( 0 );
  42. scope.checkLoadComplete();
  43. } );
  44. };
  45. this.setSkin = function( index ) {
  46. if ( mesh && scope.materials ) {
  47. mesh.material = scope.materials[ index ];
  48. }
  49. };
  50. this.updateMorphs = function( influences ) {
  51. if ( mesh ) {
  52. for ( var i = 0; i < scope.numMorphs; i ++ ) {
  53. mesh.morphTargetInfluences[ i ] = influences[ scope.morphs[ i ] ] / 100;
  54. }
  55. }
  56. };
  57. function loadTextures( baseUrl, textureUrls ) {
  58. var mapping = THREE.UVMapping;
  59. var textures = [];
  60. for ( var i = 0; i < textureUrls.length; i ++ ) {
  61. textures[ i ] = THREE.ImageUtils.loadTexture( baseUrl + textureUrls[ i ], mapping, scope.checkLoadComplete );
  62. textures[ i ].name = textureUrls[ i ];
  63. }
  64. return textures;
  65. }
  66. function createMaterials( skins ) {
  67. var materials = [];
  68. for ( var i = 0; i < skins.length; i ++ ) {
  69. materials[ i ] = new THREE.MeshLambertMaterial( {
  70. color: 0xeeeeee,
  71. specular: 10.0,
  72. map: skins[ i ],
  73. skinning: true,
  74. morphTargets: true
  75. } );
  76. }
  77. return materials;
  78. }
  79. this.checkLoadComplete = function () {
  80. scope.loadCounter -= 1;
  81. if ( scope.loadCounter === 0 ) {
  82. scope.onLoadComplete();
  83. }
  84. }
  85. };