Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <meta charset=utf-8>
  6. <title>S110646 Assignment Unit 2</title>
  7. <style>
  8. body {
  9. margin: 0;
  10. }
  11.  
  12. canvas {
  13. width: 100%;
  14. height: 100%
  15. }
  16. </style>
  17. </head>
  18.  
  19. <body>
  20.  
  21. <!-- Load three.min.js-->
  22. <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/96/three.min.js"></script>
  23.  
  24. <script>
  25.  
  26. //Prepares scene and camera
  27. var scene = new THREE.Scene();
  28. var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
  29.  
  30. var renderer = new THREE.WebGLRenderer();
  31. renderer.setSize(window.innerWidth, window.innerHeight);
  32. document.body.appendChild(renderer.domElement);
  33.  
  34.  
  35. //Material snippet from example code
  36. var sphereMaterial = new THREE.MeshLambertMaterial(
  37. {
  38. color: 0xBBCC00, wireframe: false, side: THREE.DoubleSide
  39. });
  40.  
  41.  
  42. // create the sphere's material
  43. var sphereMaterial = new THREE.MeshNormalMaterial( {color: 0x3794cf, wireframe: false });
  44.  
  45. // set up the sphere vars
  46. var radius = 15, segments = 20, rings = 20;
  47.  
  48. // create a new mesh with sphere geometry -
  49. // we will cover the sphereMaterial next!
  50. var sphere = new THREE.Mesh(
  51. new THREE.SphereGeometry(radius, segments, rings),
  52. sphereMaterial);
  53.  
  54. // add the sphere to the scene
  55. scene.add(sphere);
  56. console.log ("y:", sphere.position.y)
  57.  
  58. //Add ambient light (red / blue) to scene
  59. var directionalLight = new THREE.DirectionalLightShadow(0xff0000);
  60. //directionalLight.position.set( 0, 0, 2 ); //default; light shining from top
  61. directionalLight.castShadow = true;
  62. scene.add(directionalLight);
  63.  
  64. //Set camera very far away so to visualize the scaling, rotation and translation of the polygon
  65. camera.position.z = 100;
  66. // Set the dimensions of the screen
  67. var top = window.innerHeight/2;
  68. var right = window.innerWidth/2;
  69. var bottom = -1 * window.innerHeight/2;
  70. var left = -1 * window.innerWidth/2;
  71.  
  72. var xdirection = 1;
  73. var ydirection = 1;
  74. var speed = 4;
  75.  
  76. function animate() {
  77. requestAnimationFrame(animate);
  78.  
  79. //Translate the sphere
  80. // Update the position of the shape
  81. var xpos = sphere.position.x + ( speed * xdirection );
  82. sphere.position.x = xpos;
  83. var ypos = sphere.position.y + ( speed * ydirection );
  84. sphere.position.y = ypos;
  85.  
  86.  
  87. // Test to see if the shape exceeds the boundaries of the screen
  88. // If it does, reverse its direction by multiplying by -1
  89.  
  90. if (xpos + radius > right || xpos - radius < left) {
  91. xdirection *= -1;
  92. }
  93. if (ypos + radius > top || ypos - radius < bottom) {
  94. ydirection *= -1;
  95. }
  96.  
  97. renderer.render(scene, camera);
  98.  
  99. }
  100.  
  101. animate();
  102. </script>
  103. </body>
  104.  
  105. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement