Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. var container;
  2. var camera, scene, renderer;
  3. var mouse, raycaster, controls, hemiLight;
  4.  
  5. function init(containerID) {
  6. // Will setup all needed variables
  7. container = document.getElementById(containerID);
  8. camera = new THREE.PerspectiveCamera(1, window.innerWidth / window.innerHeight, 1, 2000);
  9. camera.position.z = 400;
  10.  
  11. // Scene
  12. scene = new THREE.Scene();
  13. hemiLight = new THREE.HemisphereLight(0xCC9966, 0xCC9966, 1);
  14. hemiLight.position.set(0, 500, 0);
  15. scene.add(hemiLight);
  16.  
  17. // Camera behaviour
  18. controls = new THREE.TrackballControls(camera);
  19. controls.rotateSpeed = 5.0;
  20. controls.zoomSpeed = 5;
  21. controls.panSpeed = 2;
  22. controls.noZoom = false;
  23. controls.noPan = false;
  24. controls.staticMoving = true;
  25. controls.dynamicDampingFactor = 0.3;
  26. controls.enabled = false;
  27.  
  28. // Model
  29. var onProgress = function ( xhr ) {
  30. // Only necessary if the obj-file-size is very large
  31. };
  32. var onError = function ( xhr ) {
  33. // Something went wrong during loading obj-file
  34. console.log("Error, please report to the devs");
  35. };
  36.  
  37. THREE.Loader.Handlers.add(/\.dds$/i, new THREE.DDSLoader());
  38. var loader = new THREE.OBJMTLLoader();
  39.  
  40. // Load the 3D Files
  41. loader.load( 'objects/head.obj', 'images/head.mtl', function ( object ) {
  42. prepareObj( object, "head" );
  43. }, onProgress, onError );
  44. loader.load( 'objects/torso.obj', 'images/torso.mtl', function ( object ) {
  45. prepareObj( object, "torso" );
  46. }, onProgress, onError );
  47. loader.load( 'objects/arm_left.obj', 'images/arm_left.mtl', function ( object ) {
  48. prepareObj( object, "arm_left" );
  49. }, onProgress, onError );
  50. loader.load( 'objects/arm_right.obj', 'images/arm_right.mtl', function ( object ) {
  51. prepareObj( object, "arm_right" );
  52. }, onProgress, onError );
  53. loader.load( 'objects/leg_right.obj', 'images/leg_right.mtl', function ( object ) {
  54. prepareObj( object, "leg_right" );
  55. }, onProgress, onError );
  56. loader.load( 'objects/leg_left.obj', 'images/leg_left.mtl', function ( object ) {
  57. prepareObj( object, "leg_left" );
  58. }, onProgress, onError );
  59.  
  60. renderer = new THREE.WebGLRenderer();
  61. renderer.setPixelRatio(window.devicePixelRatio);
  62. renderer.setSize(500, 500);
  63. renderer.setClearColor(0xffffff, 1);
  64. container.appendChild(renderer.domElement);
  65.  
  66. document.addEventListener("mouseover", function(e) {
  67. checkBodyActive(e);
  68. }, false);
  69. document.addEventListener("click", onClick, false);
  70. window.addEventListener("resize", onWindowResize, false);
  71.  
  72. raycaster = new THREE.Raycaster();
  73. mouse = new THREE.Vector2();
  74.  
  75. camera.aspect = 1;
  76. camera.updateProjectionMatrix();
  77. renderer.render(scene, camera);
  78. animate();
  79. }
  80.  
  81. function checkBodyActive(event) {
  82. // Checks, if the current body is active
  83. if(document.elementFromPoint(event.clientX, event.clientY) == renderer.domElement) {
  84. controls.enabled = true;
  85. animate();
  86. return true;
  87. }
  88. else {
  89. controls.enabled = false;
  90. return false;
  91. }
  92. }
  93.  
  94. function changeTexture(part) {
  95. // Will change the texture of the selected body part
  96. scene.children[part].traverse( function ( child ) {
  97. if (child instanceof THREE.Mesh) {
  98. // Texture must be changed
  99. if(scene.children[part].children[0].children[0].material.map.sourceFile=="images/texture2.jpg") {
  100. // Texture2 is already equipped, unequip it
  101. child.material.map = THREE.ImageUtils.loadTexture("images/texture.jpg");
  102. } else {
  103. // Equip texture2
  104. child.material.map = THREE.ImageUtils.loadTexture("images/texture2.jpg");
  105. }
  106. child.material.needsUpdate = true;
  107. }
  108. });
  109. }
  110.  
  111. function onWindowResize() {
  112. // This function will make sure that all proportions are correct after resizing the window
  113. windowHalfX = window.innerWidth / 2;
  114. windowHalfY = window.innerHeight / 2;
  115. camera.aspect = 1;
  116. camera.updateProjectionMatrix();
  117. }
  118.  
  119. function onClick(event) {
  120. // Mouse-Click-Event, used to check if intersects where hit
  121. mouse.x = (getOffset(event).x / renderer.domElement.width) * 2 - 1;
  122. mouse.y = -(getOffset(event).y / renderer.domElement.height) * 2 + 1;
  123.  
  124. // Create intersect raycasting
  125. raycaster.setFromCamera(mouse, camera);
  126. var intersects = raycaster.intersectObjects(scene.children, true);
  127. if (intersects.length) {
  128. var name = intersects[0].object.parent.parent.name;
  129. for (var j = 0; j < scene.children.length; j++) {
  130. if (scene.children[j].name == name) {
  131. // Found same model as the intersects, change texture
  132. changeTexture(j);
  133. }
  134. }
  135. }
  136. }
  137.  
  138. function getOffset(event) {
  139. // Calcs layerX / layerY for the clicked element
  140. var el = event.target,
  141. x = 0,
  142. y = 0;
  143.  
  144. while (el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) {
  145. x += el.offsetLeft - el.scrollLeft;
  146. y += el.offsetTop - el.scrollTop;
  147. el = el.offsetParent;
  148. }
  149.  
  150. x = event.clientX - x;
  151. y = event.clientY - y;
  152.  
  153. return { x: x, y: y };
  154. }
  155.  
  156. function prepareObj(object, name){
  157. // General configuration of the .obj files while loading
  158. object.position.y = -3;
  159. object.traverse(function(child) {
  160. if(child instanceof THREE.Mesh) {
  161. child.material.map = THREE.ImageUtils.loadTexture("images/texture.jpg");
  162. child.material.needsUpdate = true;
  163. }
  164. });
  165. object.name = name;
  166. scene.add(object);
  167. }
  168.  
  169. function animate() {
  170. // On every frame we need to calculate the new camera position
  171. // and have it look exactly at the center of our scene.
  172. requestAnimationFrame(animate);
  173. controls.update();
  174. camera.lookAt(scene.position);
  175. renderer.render(scene, camera);
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement