index.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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>
  20. var camera, scene, renderer, raycaster;
  21. var mesh, mouse, plane, intersectPoint, cameraHeight, light, mousePoint;
  22. if ( WEBGL.isWebGLAvailable() === false ) {
  23. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  24. }
  25. init();
  26. animate();
  27. function init() {
  28. raycaster = new THREE.Raycaster();
  29. mouse = new THREE.Vector2();
  30. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  31. camera.position.z = 100;
  32. camera.position.y = -50;
  33. cameraHeight = 100;
  34. scene = new THREE.Scene();
  35. plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
  36. intersectPoint = new THREE.Vector3();
  37. //var texture = new THREE.TextureLoader().load( './greebles.jpg' );
  38. var geometry = new THREE.BoxBufferGeometry(20, 20, 20);
  39. var material = new THREE.MeshStandardMaterial({color: "gray"});
  40. var sphere = new THREE.SphereBufferGeometry( 0.5, 16, 8 );
  41. mousePoint = new THREE.Mesh(sphere, new THREE.MeshBasicMaterial( { color: "red" } ));
  42. scene.add(mousePoint);
  43. mesh = new THREE.Mesh(geometry, material);
  44. scene.add( mesh );
  45. light = new THREE.PointLight(0xffffff, 5, 50, 1);
  46. light.position.set(-10, -20, 30);
  47. //light.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffffff } ) ) );
  48. scene.add(light);
  49. renderer = new THREE.WebGLRenderer({antialias: true});
  50. renderer.setPixelRatio(window.devicePixelRatio);
  51. renderer.setSize(window.innerWidth, window.innerHeight);
  52. document.body.appendChild(renderer.domElement);
  53. window.addEventListener('resize', onWindowResize, false);
  54. }
  55. function onWindowResize() {
  56. camera.aspect = window.innerWidth / window.innerHeight;
  57. camera.updateProjectionMatrix();
  58. renderer.setSize(window.innerWidth, window.innerHeight);
  59. }
  60. function animate() {
  61. requestAnimationFrame(animate);
  62. camera.position.z = cameraHeight;
  63. camera.lookAt(mesh.position);
  64. camera.updateMatrixWorld();
  65. raycaster.setFromCamera(mouse, camera);
  66. raycaster.ray.intersectPlane(plane, intersectPoint);
  67. mesh.lookAt(intersectPoint);
  68. mousePoint.position.x = intersectPoint.x;
  69. mousePoint.position.y = intersectPoint.y;
  70. mousePoint.position.z = intersectPoint.z;
  71. //light.position.z = 30 + Math.sin((new Date().getTime() / 1000)) * 5;
  72. renderer.render(scene, camera);
  73. }
  74. document.addEventListener("mousemove", function(event) {
  75. event.preventDefault();
  76. mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  77. mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  78. });
  79. var scaling = false;
  80. var pinchCenter = {x:0, y: 0};
  81. var origDistance = null;
  82. var origCameraHeight = null;
  83. document.addEventListener("touchstart", function(event) {
  84. scaling = false;
  85. if(event.touches.length == 2) {
  86. scaling = true;
  87. pinchCenter = {
  88. x: (event.touches[0].clientX + event.touches[1].clientX) / 2,
  89. y: (event.touches[0].clientY + event.touches[1].clientY) / 2
  90. };
  91. origDistance = Math.hypot(
  92. event.touches[0].clientX - pinchCenter.x,
  93. event.touches[0].clientY - pinchCenter.y
  94. );
  95. origCameraHeight = cameraHeight;
  96. }
  97. });
  98. document.addEventListener("touchmove", function(event) {
  99. if(event.touches.length == 1) {
  100. mouse.x = (event.touches[0].clientX / window.innerWidth) * 2 - 1;
  101. mouse.y = -(event.touches[0].clientY / window.innerHeight) * 2 + 1;
  102. }
  103. if(scaling && event.touches.length == 2) {
  104. var newDist = Math.hypot(
  105. event.touches[0].clientX - pinchCenter.x,
  106. event.touches[0].clientY - pinchCenter.y
  107. );
  108. cameraHeight = origCameraHeight * (origDistance / newDist);
  109. cameraHeight = Math.max(Math.min(cameraHeight, 250), 50);
  110. }
  111. });
  112. document.addEventListener("touchend", function(event) {
  113. if(scaling) {
  114. scaling = false;
  115. }
  116. });
  117. document.addEventListener("wheel", function(event) {
  118. cameraHeight += (event.deltaY / 10);
  119. cameraHeight = Math.max(Math.min(cameraHeight, 250), 50);
  120. });
  121. </script>
  122. </body>
  123. </html>