castle-scene/main.js (4636B)
1 import * as t from 'three'; 2 import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'; 3 4 const castleW = 10; 5 const castleD = 10; 6 const castleH = 5; 7 8 const gateW = 2.5; 9 const gateH = 3; 10 const gateD = 0.5; 11 12 const towerR = 1.5; 13 const towerH = castleH + 2.5; 14 15 const towerSpireR = towerR + 0.5; 16 const towerSpireH = 6; 17 18 const lakeR = 6; 19 const treeLogH = 4; 20 21 let scene, camera, renderer, container; 22 23 function setUpThree() { 24 scene = new t.Scene(); 25 scene.background = new t.Color(0x9fcffb); 26 27 camera = new t.PerspectiveCamera(50, 28 window.innerWidth / window.innerHeight, 29 0.1, 30 1000); 31 camera.position.set(40, 35, 30); 32 camera.up.set(0, 0, 1); // Set Z axis facing up 33 34 renderer = new t.WebGLRenderer(); 35 renderer.setSize(window.innerWidth, window.innerHeight); 36 document.body.appendChild(renderer.domElement); 37 38 const controls = new OrbitControls(camera, renderer.domElement); 39 40 window.addEventListener('resize', onResize); 41 onResize(); 42 43 const light = new t.DirectionalLight(0xffffff); 44 light.position.x = 100; 45 light.position.y = 50; 46 light.position.z = 100; 47 scene.add(light); 48 } 49 50 function addGeometries() { 51 const floorGeometry = new t.BoxGeometry(90, 75, 0.1); 52 const floorMaterial = new t.MeshPhongMaterial({color: 0x086f09}); 53 const floor = new t.Mesh(floorGeometry, floorMaterial); 54 floor.position.set(10,0,0); 55 scene.add(floor); 56 57 const castleGeometry = new t.BoxGeometry(castleW, castleD, castleH); 58 const castleMaterial = new t.MeshPhongMaterial({color: 0xfddde6}); 59 const castle = new t.Mesh(castleGeometry, castleMaterial); 60 castle.position.set(0,0,castleH/2) 61 scene.add(castle); 62 63 const towerGeometry = new t.CylinderGeometry(towerR, towerR, towerH, 40, 40); 64 const towerSpireGeometry = new t.ConeGeometry(towerSpireR, towerSpireH, 40); 65 const towerMaterial = new t.MeshPhongMaterial({color: 0xfddde6}); 66 const towerSpireMaterial = new t.MeshPhongMaterial({color: 0x4d8dff}); 67 68 let towersMesh = []; 69 let towersSpireMesh = []; 70 const towers = new t.Group(); 71 for (let i = 0; i < 4; i++) { 72 towersMesh[i] = new t.Mesh(towerGeometry, towerMaterial); 73 towersSpireMesh[i] = new t.Mesh(towerSpireGeometry, towerSpireMaterial); 74 75 // 0 -> 00 76 // 1 -> 01 77 // 2 -> 10 78 // 3 -> 11 79 towersMesh[i].position.set( 80 ((-1)**(i>>1))*(castleW/2-(towerR*2/3)), 81 towerH/2, 82 ((-1)**(i&1))*(castleD/2-towerR*2/3)); 83 84 towersSpireMesh[i].position.set( 85 ((-1)**(i>>1))*(castleW/2-towerR*2/3), 86 towerH + (towerSpireH/2), 87 ((-1)**(i&1))*(castleD/2-towerR*2/3)); 88 89 towers.add(towersMesh[i]); 90 towers.add(towersSpireMesh[i]); 91 } 92 93 towers.rotation.x = Math.PI/2; 94 scene.add(towers); 95 96 const gateGeometry = new t.BoxGeometry(gateD, gateW, gateH); 97 const gateMaterial = new t.MeshPhongMaterial({color: 0x7c3f00}); 98 const gate = new t.Mesh(gateGeometry, gateMaterial); 99 100 // 0.01 previene un error visual 101 gate.position.set((castleW-gateD)/2 + 0.01, 0, gateH/2); 102 scene.add(gate); 103 104 const lakeGeometry = new t.CircleGeometry(lakeR, 40); 105 const lakeMaterial = new t.MeshPhongMaterial({color: 0x62c4ff}); 106 107 let lake = []; 108 for (let i = 0; i < 2; ++i) { 109 lake[i] = new t.Mesh(lakeGeometry, lakeMaterial); 110 lake[i].position.set(24 + (i*lakeR*5/4),0,0.1) 111 scene.add(lake[i]); 112 } 113 114 const Tree = new t.Group(); 115 const treeLogGeometry = new t.CylinderGeometry(0.5, 0.5, treeLogH, 40, 40); 116 const treeLogMaterial = new t.MeshPhongMaterial({color: 0x7c3f00}); 117 const treeLog = new t.Mesh(treeLogGeometry, treeLogMaterial); 118 treeLog.rotation.x = Math.PI/2; 119 treeLog.position.set(14,-12,treeLogH/2) 120 Tree.add(treeLog); 121 122 const treeLeavesGeometry = new t.SphereGeometry(1.75,40,40); 123 const treeLeavesMaterial = new t.MeshPhongMaterial({color: 0x00ff00}); 124 let treeLeaves = []; 125 for (let i = 0; i < 3; i++) { 126 treeLeaves[i] = new t.Mesh(treeLeavesGeometry, treeLeavesMaterial); 127 treeLeaves[i].position.set( 128 14, 129 -11.5-((0.25)*(1+((-1)**i))), 130 treeLogH+i); 131 Tree.add(treeLeaves[i]); 132 } 133 134 scene.add(Tree); 135 } 136 137 function onResize() { 138 camera.aspect = window.innerWidth / window.innerHeight; 139 camera.updateProjectionMatrix(); 140 renderer.setSize(window.innerWidth, window.innerHeight); 141 } 142 143 function mainLoop() { 144 requestAnimationFrame(mainLoop); 145 renderer.render(scene, camera); 146 } 147 148 setUpThree(); 149 addGeometries(); 150 mainLoop();