index.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>facing cube with lighting</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. margin: 0px;
  10. background-color: #000000;
  11. overflow: hidden;
  12. cursor: none;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="./three.js"></script>
  18. <script src="./WebGL.js"></script>
  19. <script src="./OBJLoader.js"></script>
  20. <script>
  21. var camera, scene, renderer, raycaster, objloader;
  22. var mouse, plane, intersectPoint, cameraHeight, light, mousePoint, ship, spacePlane;
  23. if ( WEBGL.isWebGLAvailable() === false ) {
  24. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  25. }
  26. init();
  27. animate();
  28. function init() {
  29. THREE.Object3D.DefaultUp = new THREE.Vector3(0,0,1);
  30. raycaster = new THREE.Raycaster();
  31. mouse = new THREE.Vector2();
  32. objloader = new THREE.OBJLoader();
  33. cameraHeight = 100;
  34. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  35. //camera.up = new THREE.Vector3(0,0,1);
  36. camera.position.x = 10;
  37. camera.position.y = -50;
  38. camera.position.z = cameraHeight;
  39. camera.updateMatrixWorld();
  40. camera.lookAt(new THREE.Vector3(100,100,0));
  41. scene = new THREE.Scene();
  42. plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
  43. intersectPoint = new THREE.Vector3();
  44. //var texture = new THREE.TextureLoader().load( './greebles.jpg' );
  45. objloader.load(
  46. './simple_ship.obj',
  47. function ( object ) {
  48. object.scale.multiplyScalar(8);
  49. object.position = new THREE.Vector3(100, 100, 0);
  50. mouse.x = 0;
  51. mouse.y = 0.5;
  52. ship = object;
  53. scene.add(ship);
  54. },
  55. function ( xhr ) {
  56. //console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
  57. },
  58. function (error) {
  59. console.error( 'Error loading ship model');
  60. }
  61. );
  62. var geometry = new THREE.BoxBufferGeometry(20, 20, 20);
  63. var material = new THREE.MeshStandardMaterial({color: "blue"});
  64. var sphere = new THREE.SphereBufferGeometry( 0.5, 16, 8 );
  65. mousePoint = new THREE.Mesh(sphere, new THREE.MeshBasicMaterial( { color: "red" } ));
  66. scene.add(mousePoint);
  67. light = new THREE.PointLight(0xffffff, 3, 70, 2);
  68. light.position.set(-10, -20, 30);
  69. scene.add(light);
  70. var spaceTexture = new THREE.TextureLoader().load( './darkPurple.png' );
  71. spaceTexture.wrapS = spaceTexture.wrapT = THREE.RepeatWrapping;
  72. spaceTexture.repeat.set(8, 8);
  73. var spaceMaterial = new THREE.MeshBasicMaterial({map: spaceTexture, color: 0x606060 });
  74. spacePlane = new THREE.Mesh(new THREE.PlaneGeometry(1024, 1024), spaceMaterial);
  75. spacePlane.position = new THREE.Vector3(0, 0, -50);
  76. scene.add(spacePlane);
  77. var skyBoxGeometry = new THREE.CubeGeometry( 1024, 1024, 1024 );
  78. var skyBoxMaterial = new THREE.MeshBasicMaterial( { map: spaceTexture, color: 0x606060, side: THREE.BackSide } );
  79. var skyBox = new THREE.Mesh( skyBoxGeometry, skyBoxMaterial );
  80. scene.add(skyBox);
  81. scene.fog = new THREE.FogExp2( 0x000000, 0.0012 );
  82. renderer = new THREE.WebGLRenderer({antialias: true});
  83. renderer.setPixelRatio(window.devicePixelRatio);
  84. renderer.setSize(window.innerWidth, window.innerHeight);
  85. document.body.appendChild(renderer.domElement);
  86. window.addEventListener('resize', onWindowResize, false);
  87. }
  88. function onWindowResize() {
  89. camera.aspect = window.innerWidth / window.innerHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize(window.innerWidth, window.innerHeight);
  92. }
  93. function animate() {
  94. requestAnimationFrame(animate);
  95. camera.position.z = cameraHeight;
  96. if(ship) { camera.lookAt(ship.position); }
  97. camera.updateMatrixWorld();
  98. raycaster.setFromCamera(mouse, camera);
  99. raycaster.ray.intersectPlane(plane, intersectPoint);
  100. if(ship) { ship.lookAt(intersectPoint); }
  101. mousePoint.position.x = intersectPoint.x;
  102. mousePoint.position.y = intersectPoint.y;
  103. mousePoint.position.z = intersectPoint.z;
  104. if(spacePlane) {
  105. spacePlane.position.z = 4 * Math.sin((new Date().getTime() / 1000) * 1) - 10;
  106. }
  107. renderer.render(scene, camera);
  108. }
  109. document.addEventListener("mousemove", function(event) {
  110. event.preventDefault();
  111. mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  112. mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  113. });
  114. var scaling = false;
  115. var pinchCenter = {x:0, y: 0};
  116. var origDistance = null;
  117. var origCameraHeight = null;
  118. document.addEventListener("touchstart", function(event) {
  119. scaling = false;
  120. if(event.touches.length == 2) {
  121. scaling = true;
  122. pinchCenter = {
  123. x: (event.touches[0].clientX + event.touches[1].clientX) / 2,
  124. y: (event.touches[0].clientY + event.touches[1].clientY) / 2
  125. };
  126. origDistance = Math.hypot(
  127. event.touches[0].clientX - pinchCenter.x,
  128. event.touches[0].clientY - pinchCenter.y
  129. );
  130. origCameraHeight = cameraHeight;
  131. }
  132. });
  133. document.addEventListener("touchmove", function(event) {
  134. if(event.touches.length == 1) {
  135. mouse.x = (event.touches[0].clientX / window.innerWidth) * 2 - 1;
  136. mouse.y = -(event.touches[0].clientY / window.innerHeight) * 2 + 1;
  137. }
  138. if(scaling && event.touches.length == 2) {
  139. var newDist = Math.hypot(
  140. event.touches[0].clientX - pinchCenter.x,
  141. event.touches[0].clientY - pinchCenter.y
  142. );
  143. cameraHeight = origCameraHeight * (origDistance / newDist);
  144. cameraHeight = Math.max(Math.min(cameraHeight, 250), 30);
  145. }
  146. });
  147. document.addEventListener("touchend", function(event) {
  148. if(scaling) {
  149. scaling = false;
  150. }
  151. });
  152. document.addEventListener("wheel", function(event) {
  153. cameraHeight += (event.deltaY / 10);
  154. cameraHeight = Math.max(Math.min(cameraHeight, 250), 30);
  155. });
  156. function lookAtVector( sourcePoint, destPoint ) {
  157. return new THREE.Quaternion().setFromRotationMatrix(
  158. new THREE.Matrix4()
  159. .lookAt( sourcePoint, destPoint, new THREE.Vector3( 0, 0, -1 ) )
  160. );
  161. }
  162. </script>
  163. </body>
  164. </html>