Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. //Objeto cena é quem gerencia tudo que deve existir em uma cena
  2. var cena = new THREE.Scene();
  3.  
  4. //Camera é uma config sbore como e de que posicao iremos observar a cena
  5. var camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 1000);
  6. var render = new THREE.WebGLRenderer();
  7.  
  8. //Sombra
  9. render.shadowMap.enabled = true;
  10. render.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap
  11.  
  12. render.setSize(window.innerWidth, window.innerHeight);
  13.  
  14. //O canvas será construido pelo renderizador
  15. var canvas = render.domElement;
  16. document.body.appendChild(canvas);
  17.  
  18. var materialLinha = new THREE.LineBasicMaterial({color: 0xFFFFFF});
  19. var geometriaLinha = new THREE.Geometry();
  20.  
  21. //Luz ambiente
  22. var luzAmbiente = new THREE.AmbientLight(0x111111);
  23. cena.add(luzAmbiente);
  24.  
  25. //Luz pontual
  26. var luzPonto = new THREE.PointLight(0xcccccc);
  27. luzPonto.position.set(2, 0, 4);
  28. luzPonto.castShadow = true;
  29. cena.add(luzPonto);
  30.  
  31. //Luz pontual 2
  32. var luzPonto2 = new THREE.PointLight(0xcccccc);
  33. luzPonto2.position.set(-2, 0, 4);
  34. luzPonto2.castShadow = true;
  35. cena.add(luzPonto2);
  36.  
  37. ////Luz pontual 3
  38. //var luzPonto3 = new THREE.PointLight(0xcccccc);
  39. //luzPonto3.position.set(0, 2, 4);
  40. //luzPonto3.castShadow = true;
  41. //cena.add(luzPonto3);
  42.  
  43. ////Luz pontual 4
  44. //var luzPonto4 = new THREE.PointLight(0xcccccc);
  45. //luzPonto4.position.set(0, -2, 4);
  46. //luzPonto4.castShadow = true;
  47. //cena.add(luzPonto4);
  48.  
  49.  
  50. var posicaoCameraX = 0;
  51. var posicaoCameraY = 0;
  52. var posicaoCameraZ = 5;
  53.  
  54.  
  55.  
  56. camera.position.x = posicaoCameraX;
  57. camera.position.y = posicaoCameraY;
  58. camera.position.z = posicaoCameraZ;
  59.  
  60.  
  61. cena.rotation.x = -1.2;
  62.  
  63. var curva = new THREE.CatmullRomCurve3([
  64.  
  65. new THREE.Vector3(0,0,0),
  66. new THREE.Vector3(1,1,0),
  67. new THREE.Vector3(2,0,0),
  68. new THREE.Vector3(1,-1,0),
  69. new THREE.Vector3(0,0,0),
  70. new THREE.Vector3(-1,1,0),
  71. new THREE.Vector3(-2,0,0),
  72. new THREE.Vector3(-1,-1,0),
  73. new THREE.Vector3(0,0,0),
  74.  
  75. ]);
  76.  
  77. var caminho = new THREE.Path(curva.getPoints(200));
  78. var geometriaLinha = caminho.createPointsGeometry(200);
  79.  
  80.  
  81. var materialPonto = new THREE.PointsMaterial({size: 10, sizeAttenuation: false});
  82.  
  83. for (let p of curva.points) {
  84. var geometriaPonto = new THREE.Geometry();
  85. geometriaPonto.vertices.push(new THREE.Vector3( p.x, p.y, p.z));
  86. var ponto = new THREE.Points(geometriaPonto, materialPonto);
  87. cena.add(ponto);
  88. }
  89.  
  90. var linha = new THREE.Line(geometriaLinha, materialLinha);
  91. cena.add(linha);
  92.  
  93. var geometriaCarro = new THREE.BoxGeometry(0.4,0.2,0.2);
  94.  
  95. var materialCarro = new THREE.MeshPhongMaterial();
  96.  
  97. var carro = new THREE.Mesh(geometriaCarro, materialCarro);
  98. carro.material.color.setHex(0xFF0000);
  99.  
  100. //Sombra
  101. carro.castShadow = true; //default is false
  102. carro.receiveShadow = false; //default
  103.  
  104. cena.add(carro);
  105.  
  106. var contadorCarro = 0;
  107.  
  108. /* Floor */
  109. var geometry = new THREE.PlaneGeometry( 100000, 100000, 0, 0 );
  110. var material = new THREE.MeshPhongMaterial( { color: 0x444444 } );
  111. var floor = new THREE.Mesh( geometry, material );
  112. floor.material.side = THREE.DoubleSide;
  113. floor.position.z = -0.2;
  114. floor.receiveShadow = true;
  115. cena.add( floor );
  116.  
  117. function desenhar() {
  118.  
  119. carro.geometry.verticesNeedUpdate = true;
  120. var pontoAtual = linha.geometry.vertices[contadorCarro];
  121.  
  122. carro.position.x = pontoAtual.x;
  123. carro.position.y = pontoAtual.y;
  124. carro.position.z = pontoAtual.z;
  125.  
  126. contadorCarro++;
  127. if(contadorCarro == linha.geometry.vertices.length - 1) {
  128. var proximoPonto = linha.geometry.vertices[contadorCarro];
  129. contadorCarro = 0;
  130. }else{
  131. var proximoPonto = linha.geometry.vertices[contadorCarro+1];
  132. }
  133.  
  134.  
  135. var angulo = Math.atan2(proximoPonto.y - pontoAtual.y, proximoPonto.x - pontoAtual.x)
  136. carro.rotation.z = angulo;
  137.  
  138. var pos = new THREE.Vector3(
  139. carro.position.x,
  140. carro.position.y,
  141. carro.position.z
  142. );
  143. camera.lookAt(pos);
  144.  
  145. render.render(cena, camera);
  146. requestAnimationFrame(desenhar);
  147.  
  148. }
  149.  
  150. requestAnimationFrame(desenhar);
  151.  
  152. canvas.addEventListener("mousedown", function (e){
  153. xi = e.offsetX;
  154. yi = e.offsetY;
  155. }, false);
  156.  
  157. //Evento de movimento do mouse (se há botão pressionado)
  158. canvas.addEventListener("mousemove", function(e) {
  159. if(e.buttons > 0){
  160. camera.position.x = 15 * (xi - e.offsetX) / canvas.width;
  161. camera.position.y = 2 * (e.offsetY - yi) / canvas.height;
  162. }
  163. }, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement