UCSCharacter.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.onLoadComplete = function () {};
  12. this.loadCounter = 0;
  13. this.loadParts = function ( config ) {
  14. this.numSkins = config.skins.length;
  15. this.numMorphs = config.morphs.length;
  16. // Character geometry + number of skins
  17. this.loadCounter = 1 + config.skins.length;
  18. // SKINS
  19. this.skins = loadTextures( config.baseUrl + "skins/", config.skins );
  20. this.materials = createMaterials( this.skins );
  21. // MORPHS
  22. this.morphs = config.morphs;
  23. // CHARACTER
  24. var loader = new THREE.JSONLoader();
  25. console.log( config.baseUrl + config.character );
  26. loader.load( config.baseUrl + config.character, function( geometry ) {
  27. geometry.computeBoundingBox();
  28. geometry.computeVertexNormals();
  29. //THREE.AnimationHandler.add( geometry.animation );
  30. mesh = new THREE.SkinnedMesh( geometry, new THREE.MeshFaceMaterial() );
  31. scope.root.add( mesh );
  32. var bb = geometry.boundingBox;
  33. scope.root.scale.set( config.s, config.s, config.s );
  34. scope.root.position.set( config.x, config.y - bb.min.y * config.s, config.z );
  35. mesh.castShadow = true;
  36. mesh.receiveShadow = true;
  37. animation = new THREE.Animation( mesh, geometry.animation );
  38. animation.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 mapping = THREE.UVMapping;
  57. var textures = [];
  58. for ( var i = 0; i < textureUrls.length; i ++ ) {
  59. textures[ i ] = THREE.ImageUtils.loadTexture( baseUrl + textureUrls[ i ], mapping, scope.checkLoadComplete );
  60. textures[ i ].name = textureUrls[ i ];
  61. }
  62. return textures;
  63. }
  64. function createMaterials( skins ) {
  65. var materials = [];
  66. for ( var i = 0; i < skins.length; i ++ ) {
  67. materials[ i ] = new THREE.MeshLambertMaterial( {
  68. color: 0xeeeeee,
  69. specular: 10.0,
  70. map: skins[ i ],
  71. skinning: true,
  72. morphTargets: true
  73. } );
  74. }
  75. return materials;
  76. }
  77. this.checkLoadComplete = function () {
  78. scope.loadCounter -= 1;
  79. if ( scope.loadCounter === 0 ) {
  80. scope.onLoadComplete();
  81. }
  82. }
  83. };