Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Objeto cena é quem gerencia tudo que deve existir em uma cena
- var cena = new THREE.Scene();
- //Camera é uma config sbore como e de que posicao iremos observar a cena
- var camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 1000);
- var render = new THREE.WebGLRenderer();
- //Sombra
- render.shadowMap.enabled = true;
- render.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap
- render.setSize(window.innerWidth, window.innerHeight);
- //O canvas será construido pelo renderizador
- var canvas = render.domElement;
- document.body.appendChild(canvas);
- var materialLinha = new THREE.LineBasicMaterial({color: 0xFFFFFF});
- var geometriaLinha = new THREE.Geometry();
- //Luz ambiente
- var luzAmbiente = new THREE.AmbientLight(0x111111);
- cena.add(luzAmbiente);
- //Luz pontual
- var luzPonto = new THREE.PointLight(0xcccccc);
- luzPonto.position.set(2, 0, 4);
- luzPonto.castShadow = true;
- cena.add(luzPonto);
- //Luz pontual 2
- var luzPonto2 = new THREE.PointLight(0xcccccc);
- luzPonto2.position.set(-2, 0, 4);
- luzPonto2.castShadow = true;
- cena.add(luzPonto2);
- ////Luz pontual 3
- //var luzPonto3 = new THREE.PointLight(0xcccccc);
- //luzPonto3.position.set(0, 2, 4);
- //luzPonto3.castShadow = true;
- //cena.add(luzPonto3);
- ////Luz pontual 4
- //var luzPonto4 = new THREE.PointLight(0xcccccc);
- //luzPonto4.position.set(0, -2, 4);
- //luzPonto4.castShadow = true;
- //cena.add(luzPonto4);
- var posicaoCameraX = 0;
- var posicaoCameraY = 0;
- var posicaoCameraZ = 5;
- camera.position.x = posicaoCameraX;
- camera.position.y = posicaoCameraY;
- camera.position.z = posicaoCameraZ;
- cena.rotation.x = -1.2;
- var curva = new THREE.CatmullRomCurve3([
- new THREE.Vector3(0,0,0),
- new THREE.Vector3(1,1,0),
- new THREE.Vector3(2,0,0),
- new THREE.Vector3(1,-1,0),
- new THREE.Vector3(0,0,0),
- new THREE.Vector3(-1,1,0),
- new THREE.Vector3(-2,0,0),
- new THREE.Vector3(-1,-1,0),
- new THREE.Vector3(0,0,0),
- ]);
- var caminho = new THREE.Path(curva.getPoints(200));
- var geometriaLinha = caminho.createPointsGeometry(200);
- var materialPonto = new THREE.PointsMaterial({size: 10, sizeAttenuation: false});
- for (let p of curva.points) {
- var geometriaPonto = new THREE.Geometry();
- geometriaPonto.vertices.push(new THREE.Vector3( p.x, p.y, p.z));
- var ponto = new THREE.Points(geometriaPonto, materialPonto);
- cena.add(ponto);
- }
- var linha = new THREE.Line(geometriaLinha, materialLinha);
- cena.add(linha);
- var geometriaCarro = new THREE.BoxGeometry(0.4,0.2,0.2);
- var materialCarro = new THREE.MeshPhongMaterial();
- var carro = new THREE.Mesh(geometriaCarro, materialCarro);
- carro.material.color.setHex(0xFF0000);
- //Sombra
- carro.castShadow = true; //default is false
- carro.receiveShadow = false; //default
- cena.add(carro);
- var contadorCarro = 0;
- /* Floor */
- var geometry = new THREE.PlaneGeometry( 100000, 100000, 0, 0 );
- var material = new THREE.MeshPhongMaterial( { color: 0x444444 } );
- var floor = new THREE.Mesh( geometry, material );
- floor.material.side = THREE.DoubleSide;
- floor.position.z = -0.2;
- floor.receiveShadow = true;
- cena.add( floor );
- function desenhar() {
- carro.geometry.verticesNeedUpdate = true;
- var pontoAtual = linha.geometry.vertices[contadorCarro];
- carro.position.x = pontoAtual.x;
- carro.position.y = pontoAtual.y;
- carro.position.z = pontoAtual.z;
- contadorCarro++;
- if(contadorCarro == linha.geometry.vertices.length - 1) {
- var proximoPonto = linha.geometry.vertices[contadorCarro];
- contadorCarro = 0;
- }else{
- var proximoPonto = linha.geometry.vertices[contadorCarro+1];
- }
- var angulo = Math.atan2(proximoPonto.y - pontoAtual.y, proximoPonto.x - pontoAtual.x)
- carro.rotation.z = angulo;
- var pos = new THREE.Vector3(
- carro.position.x,
- carro.position.y,
- carro.position.z
- );
- camera.lookAt(pos);
- render.render(cena, camera);
- requestAnimationFrame(desenhar);
- }
- requestAnimationFrame(desenhar);
- canvas.addEventListener("mousedown", function (e){
- xi = e.offsetX;
- yi = e.offsetY;
- }, false);
- //Evento de movimento do mouse (se há botão pressionado)
- canvas.addEventListener("mousemove", function(e) {
- if(e.buttons > 0){
- camera.position.x = 15 * (xi - e.offsetX) / canvas.width;
- camera.position.y = 2 * (e.offsetY - yi) / canvas.height;
- }
- }, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement