UCSCharacter.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.name );
  38. animation.JITCompile = false;
  39. animation.interpolationType = THREE.AnimationHandler.LINEAR;
  40. animation.play();
  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 = new 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( {"colorDiffuse" : [0.5880, 0.5880, 0.5880],
  70. "colorAmbient" : [0.5880, 0.5880, 0.5880],
  71. "colorSpecular" : [0.5000, 0.5000, 0.5000],
  72. "color" : 0xeeeeee,
  73. "transparency" : 1.0,
  74. "specularCoef" : 10.0,
  75. "wireframe" : false,
  76. "vertexColors" : false,
  77. "map" : skins[i]} );
  78. materials[i].skinning = true;
  79. materials[i].morphTargets = true;
  80. materials[i].wrapAround = true;
  81. materials[i].perPixel = true;
  82. }
  83. return materials;
  84. }
  85. this.checkLoadComplete = function () {
  86. scope.loadCounter -= 1;
  87. if ( scope.loadCounter === 0 ) {
  88. scope.onLoadComplete();
  89. }
  90. }
  91. }