UCSCharacter.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. THREE.UCSCharacter = function() {
  2. var scope = this;
  3. var mesh;
  4. this.scale = 1;
  5. this.root = new THREE.Object3D();
  6. this.numSkins = undefined;
  7. this.numMorphs = undefined;
  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.MultiMaterial() );
  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. scope.mixer.clipAction( geometry.animations[0], mesh ).play();
  39. scope.setSkin( 0 );
  40. scope.checkLoadComplete();
  41. } );
  42. };
  43. this.setSkin = function( index ) {
  44. if ( mesh && scope.materials ) {
  45. mesh.material = scope.materials[ index ];
  46. }
  47. };
  48. this.updateMorphs = function( influences ) {
  49. if ( mesh ) {
  50. for ( var i = 0; i < scope.numMorphs; i ++ ) {
  51. mesh.morphTargetInfluences[ i ] = influences[ scope.morphs[ i ] ] / 100;
  52. }
  53. }
  54. };
  55. function loadTextures( baseUrl, textureUrls ) {
  56. var textureLoader = new THREE.TextureLoader();
  57. var textures = [];
  58. for ( var i = 0; i < textureUrls.length; i ++ ) {
  59. textures[ i ] = textureLoader.load( baseUrl + textureUrls[ i ], scope.checkLoadComplete );
  60. textures[ i ].mapping = THREE.UVMapping;
  61. textures[ i ].name = textureUrls[ i ];
  62. }
  63. return textures;
  64. }
  65. function createMaterials( skins ) {
  66. var materials = [];
  67. for ( var i = 0; i < skins.length; i ++ ) {
  68. materials[ i ] = new THREE.MeshLambertMaterial( {
  69. color: 0xeeeeee,
  70. specular: 10.0,
  71. map: skins[ i ],
  72. skinning: true,
  73. morphTargets: true
  74. } );
  75. }
  76. return materials;
  77. }
  78. this.checkLoadComplete = function () {
  79. scope.loadCounter -= 1;
  80. if ( scope.loadCounter === 0 ) {
  81. scope.onLoadComplete();
  82. }
  83. }
  84. };