Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4.  
  5. <head>
  6. <title>Example 01.03 - Materials and light</title>
  7. <script type="text/javascript" src="three.js"></script>
  8. <script type="text/javascript" src="jquery-1.9.0.js"></script>
  9. <script type="text/javascript" src="stats.js"></script>
  10. <script type="text/javascript" src="dat.gui.js"></script>
  11. <script type="text/javascript" src="TrackballControl.js"></script>
  12.  
  13. <style>
  14. body{
  15. /* set margin to 0 and overflow to hidden, to go fullscreen */
  16. margin: 0;
  17. overflow: hidden;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22.  
  23. <!-- Div which will hold the Output -->
  24. <div id="WebGL-output">
  25. </div>
  26.  
  27. <!-- Javascript code that runs our Three.js examples -->
  28. <script type="text/javascript">
  29.  
  30. // once everything is loaded, we run our Three.js stuff.
  31. $(function () {
  32.  
  33. // create a scene, that will hold all our elements such as objects, cameras and lights.
  34. var scene = new THREE.Scene();
  35.  
  36. // create a camera, which defines where we're looking at.
  37. var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
  38.  
  39. // create a render and set the size
  40. var renderer = new THREE.WebGLRenderer();
  41.  
  42. renderer.setClearColorHex(0xEEEEEE, 1.0);
  43. renderer.setSize(window.innerWidth, window.innerHeight);
  44. renderer.shadowMapEnabled = true;
  45.  
  46. // create the ground plane
  47. var planeGeometry = new THREE.PlaneGeometry(60,20);
  48. var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});
  49. var plane = new THREE.Mesh(planeGeometry,planeMaterial);
  50. plane.receiveShadow = true;
  51.  
  52. // rotate and position the plane
  53. plane.rotation.x=-0.5*Math.PI;
  54. plane.position.x=15
  55. plane.position.y=0
  56. plane.position.z=0
  57.  
  58. // add the plane to the scene
  59. scene.add(plane);
  60.  
  61. // position and point the camera to the center of the scene
  62. camera.position.x = -15;
  63. camera.position.y = 2;
  64. camera.position.z = 0;
  65. scene.position.x = -100
  66. scene.position.y = 100;
  67.  
  68. // add spotlight for the shadows
  69. var spotLight = new THREE.SpotLight( 0xffffff );
  70. spotLight.position.set( -40, 60, -10 );
  71. spotLight.castShadow = true;
  72. scene.add( spotLight );
  73.  
  74. var nextX = 0;
  75. var nextY = 0;
  76. var nextZ = 0;
  77. for (var i = 1; i <= 3; i++) {
  78. var construction = getConstruction();
  79. construction = moveConstruction(construction, nextX, nextY, nextZ)
  80. construction.rotateY(degToRad(30*i));
  81. scene.add(construction);
  82. nextX = -(nextX + 2 + Math.cos(-degToRad(30) * i));
  83. nextY = nextY + 2 + Math.sin(-degToRad(30) * i);
  84.  
  85. }
  86.  
  87. // construction = getConstruction();
  88. // construction.rotation.y = -30 * (Math.PI/180);
  89. // construction.position.z = 0.7;
  90. // construction.position.x = -0.4
  91. // construction.position.y = 2
  92. // scene.add(construction);
  93.  
  94. // track-ball camera controls
  95. var controls = new THREE.TrackballControls(camera);
  96.  
  97. // add the output of the renderer to the html element
  98. $("#WebGL-output").append(renderer.domElement);
  99.  
  100. // render the scene
  101. render();
  102.  
  103. function render() {
  104. renderer.render( scene, camera );
  105. requestAnimationFrame( render );
  106. controls.update();
  107. }
  108. });
  109.  
  110. function getCylinder() {
  111. var bigCylinder = new THREE.Object3D();
  112. var cubeMaterial = new THREE.MeshLambertMaterial({color: 0x323334});
  113.  
  114. var cylinderGeometry = new THREE.CylinderGeometry(0.5,0.5,3);
  115. var cylinder = new THREE.Mesh(cylinderGeometry, cubeMaterial);
  116.  
  117. cylinder.position.y=2;
  118. cylinder.castShadow=true;
  119.  
  120. // add the sphere to the scene
  121.  
  122.  
  123. var cylinderGeometry2 = new THREE.CylinderGeometry(0.6,0.6,1);
  124. var cylinder2 = new THREE.Mesh(cylinderGeometry2, cubeMaterial);
  125.  
  126. cylinder2.position.y=3;
  127. cylinder2.castShadow=true;
  128.  
  129. // add the sphere to the scene
  130. bigCylinder.add(cylinder);
  131. bigCylinder.add(cylinder2);
  132. return bigCylinder;
  133. }
  134.  
  135. function getConstruction() {
  136. var cubeGeometry = new THREE.CubeGeometry(4,0.5,2);
  137. var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xffa54f});
  138. var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  139. cube.castShadow = true;
  140.  
  141. // position the cube
  142. cube.position.x=0;
  143. cube.position.y=3;
  144. cube.position.z=0.6;
  145.  
  146. var bigCylinder = getCylinder();
  147. bigCylinder.position.z = -1.5;
  148.  
  149. var connectingPlateGeo = new THREE.CubeGeometry(1, 0.8, 1.5);
  150. var connectingPlate = new THREE.Mesh(connectingPlateGeo, new THREE.MeshLambertMaterial({color: 0x323334}) )
  151. connectingPlate.position.z = -0.75;
  152. connectingPlate.position.y = 1;
  153. connectingPlate.castShadow = true;
  154. var construction = new THREE.Object3D();
  155.  
  156. // construction.add(cube);
  157. construction.add(bigCylinder);
  158. construction.add(connectingPlate);
  159.  
  160. return construction;
  161. }
  162.  
  163. function moveConstruction(obj, x, y, z) {
  164. obj.position.x = x;
  165. obj.position.y = y;
  166. obj.position.z = z;
  167. return obj;
  168. }
  169.  
  170. function degToRad(deg) {
  171. return deg*(Math.PI/180);
  172. }
  173.  
  174. </script>
  175. </body>
  176. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement