main.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. import * as THREE from '../threejs/build/three.module.js';
  2. import { KeyCode } from './KeyCode.js';
  3. import { Bee } from './Bee.js';
  4. import { Hive } from './Hive.js';
  5. import { Flower } from './Flower.js';
  6. import { Waypoint } from './Waypoint.js';
  7. // scene size
  8. var WIDTH = window.innerWidth;
  9. var HEIGHT = window.innerHeight;
  10. // camera
  11. var VIEW_ANGLE = 45;
  12. var ASPECT = WIDTH / HEIGHT;
  13. var NEAR = 1;
  14. var FAR = 10000;
  15. var camera, scene, renderer, raycaster;
  16. var clock = new THREE.Clock();
  17. var intersects;
  18. export var keys = [];
  19. var mouse, dragStart, dragOffset;
  20. var selected;
  21. var ground;
  22. var draggingOnGround;
  23. function init() {
  24. for (let i = 0; i < 256; i++) {
  25. keys[i] = false;
  26. }
  27. raycaster = new THREE.Raycaster();
  28. mouse = new THREE.Vector2(-1, -1);
  29. dragStart = new THREE.Vector3(0, 0, 0);
  30. dragOffset = new THREE.Vector3(0, 0, 0);
  31. intersects = [];
  32. // renderer
  33. renderer = new THREE.WebGLRenderer();
  34. renderer.setPixelRatio(window.devicePixelRatio);
  35. renderer.setSize(WIDTH, HEIGHT);
  36. renderer.shadowMap.enabled = true;
  37. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  38. renderer.outputEncoding = THREE.sRGBEncoding;
  39. // scene
  40. scene = new THREE.Scene();
  41. // camera
  42. camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
  43. camera.focus = new THREE.Vector3(0, 0, 0);
  44. camera.focusTarget = new THREE.Vector3(0, 0, 0);
  45. camera.offset = new THREE.Vector3(160, 220, 160);
  46. camera.position.set(camera.focus.x + camera.offset.x, camera.focus.y + camera.offset.y, camera.focus.z + camera.offset.z);
  47. camera.follow = false;
  48. camera.lookAt(camera.focus);
  49. draggingOnGround = false;
  50. var container = document.getElementById('container');
  51. container.appendChild(renderer.domElement);
  52. }
  53. function fillScene() {
  54. //sky
  55. let cubeloader = new THREE.CubeTextureLoader();
  56. let texture = cubeloader.load([
  57. './cubemapsimple/posx.png',
  58. './cubemapsimple/negx.png',
  59. './cubemapsimple/posy.png',
  60. './cubemapsimple/negy.png',
  61. './cubemapsimple/posz.png',
  62. './cubemapsimple/negz.png',
  63. ]);
  64. scene.background = texture;
  65. //ground
  66. let textureLoader = new THREE.TextureLoader();
  67. let lawnTex = textureLoader.load("textures/lawn.jpg");
  68. var planeGeo = new THREE.PlaneBufferGeometry(1000, 1000, 32, 32);
  69. let floorMat = new THREE.MeshPhongMaterial({ map: lawnTex });
  70. floorMat.flatShading = true;
  71. floorMat.shininess = 0;
  72. lawnTex.wrapS = lawnTex.wrapT = THREE.RepeatWrapping;
  73. lawnTex.repeat.set(5, 5);
  74. ground = new THREE.Mesh(planeGeo, floorMat);
  75. ground.rotateX(- Math.PI / 2);
  76. ground.receiveShadow = true;
  77. scene.add(ground);
  78. // lights
  79. var sunlight = new THREE.PointLight(0xffffff, 0.8, 0, 2);
  80. sunlight.position.x = 400;
  81. sunlight.position.y = 400;
  82. sunlight.position.z = 100;
  83. sunlight.castShadow = true;
  84. sunlight.shadow.camera.far = 1600;
  85. scene.add(sunlight);
  86. var mainLight = new THREE.HemisphereLight(0xB1E1FF, 0xB97A20, 0.6);
  87. mainLight.position.x = 1;
  88. mainLight.position.y = 1;
  89. mainLight.position.z = 1;
  90. // mainLight.castShadow = true;
  91. mainLight.lookAt(0, 0, 0);
  92. scene.add(mainLight);
  93. // var light = new THREE.AmbientLight(0xcccccc, 1);
  94. // scene.add(light);
  95. //set pieces
  96. Hive.collection = [];
  97. Flower.collection = [];
  98. Bee.collection = [];
  99. Promise.all([Hive.loadModel(), Flower.loadModel(), Bee.loadModel()]).then(async result => {
  100. for (let i = 0; i < 3; i++) {
  101. let hive = new Hive(scene);
  102. await hive.load(result[0]);
  103. hive.init(i);
  104. Hive.collection.push(hive);
  105. }
  106. Hive.collection.forEach(hive => hive.addToScene(scene));
  107. for (let i = 0; i < 24; i++) {
  108. let flower = new Flower();
  109. await flower.load(result[1]);
  110. flower.position.x = ((i % 6) / 6 * 400) - 200;
  111. flower.position.z = (Math.floor(i / 6) / 6 * 300) + 150;
  112. flower.rotation.y = (2 * Math.PI) * Math.random();
  113. Flower.collection.push(flower);
  114. }
  115. Flower.collection.forEach(flower => flower.addToScene(scene));
  116. Bee.baseModel = result[2];
  117. Hive.collection[0].setSwarm(1);
  118. Hive.collection[0].honeyReserve = 20;
  119. Hive.collection[0].toggleFlower(Flower.collection[0]);
  120. Hive.collection[0].spawnBee();
  121. Hive.collection[2].setSwarm(2);
  122. Hive.collection[2].honeyReserve = 20;
  123. Hive.collection[2].toggleFlower(Flower.collection[5]);
  124. Hive.collection[2].spawnBee();
  125. for (let i = 0; i < Waypoint.collection.length; i++) {
  126. let waypoint = Waypoint.collection[i];
  127. waypoint.rebuildLines();
  128. }
  129. update();
  130. });
  131. }
  132. function setFocus(focus) {
  133. selected = focus;
  134. camera.focusTarget.set(selected.position.x, selected.position.y, selected.position.z);
  135. camera.follow = true;
  136. }
  137. function render() {
  138. renderer.render(scene, camera);
  139. }
  140. function update() {
  141. requestAnimationFrame(update);
  142. var delta = clock.getDelta();
  143. var timer = Date.now();
  144. let origin = new THREE.Vector3(0, camera.offset.y, 0);
  145. let distance = camera.offset.distanceTo(origin);
  146. var delta = camera.offset.clone().sub(origin);
  147. var theta = Math.atan2(delta.z, delta.x);
  148. let cameraSpeed = 3;
  149. if (keys[KeyCode.Left] || keys[KeyCode.A]) { //left
  150. let vector = new THREE.Vector3();
  151. camera.getWorldDirection(vector);
  152. vector.multiply(new THREE.Vector3(cameraSpeed, 0, -cameraSpeed));
  153. camera.focusTarget.add(vector);
  154. }
  155. if (keys[KeyCode.Right] || keys[KeyCode.D]) { //right
  156. let vector = new THREE.Vector3();
  157. camera.getWorldDirection(vector);
  158. vector.multiply(new THREE.Vector3(-cameraSpeed, 0, cameraSpeed));
  159. camera.focusTarget.add(vector);
  160. }
  161. if (keys[KeyCode.Up] || keys[KeyCode.W]) { //up
  162. let vector = new THREE.Vector3();
  163. camera.getWorldDirection(vector);
  164. vector.multiply(new THREE.Vector3(cameraSpeed, 0, cameraSpeed));
  165. camera.focusTarget.add(vector);
  166. }
  167. if (keys[KeyCode.Down] || keys[KeyCode.S]) { //down
  168. let vector = new THREE.Vector3();
  169. camera.getWorldDirection(vector);
  170. vector.multiply(new THREE.Vector3(-cameraSpeed, 0, -cameraSpeed));
  171. camera.focusTarget.add(vector);
  172. }
  173. var rotation = THREE.Math.degToRad(6);
  174. let cameraOffset = new THREE.Vector3(camera.offset.x, camera.offset.y, camera.offset.z);
  175. if (keys[KeyCode.Q]) {
  176. cameraOffset.x = distance * Math.cos(theta + rotation);
  177. cameraOffset.z = distance * Math.sin(theta + rotation);
  178. }
  179. if (keys[KeyCode.E]) {
  180. cameraOffset.x = distance * Math.cos(theta - rotation);
  181. cameraOffset.z = distance * Math.sin(theta - rotation);
  182. }
  183. if (camera.follow) {
  184. camera.focusTarget = selected.position.clone();
  185. }
  186. camera.offset.lerp(cameraOffset, 0.1);
  187. camera.focus.lerp(camera.focusTarget, 0.1);
  188. camera.position.set(camera.focus.x + camera.offset.x, camera.focus.y + camera.offset.y, camera.focus.z + camera.offset.z);
  189. camera.lookAt(camera.focus);
  190. raycaster.setFromCamera(mouse, camera);
  191. intersects = raycaster.intersectObjects(scene.children, true);
  192. Bee.collection.forEach(bee => bee.update(delta, timer));
  193. Hive.collection.forEach(hive => hive.update(delta, timer));
  194. Flower.collection.forEach(flower => flower.update(delta, timer));
  195. render();
  196. }
  197. document.addEventListener("DOMContentLoaded", () => {
  198. init();
  199. fillScene();
  200. });
  201. window.addEventListener("keydown", e => keys[e.keyCode] = true);
  202. window.addEventListener("keyup", e => keys[e.keyCode] = false);
  203. window.addEventListener("mousemove", event => {
  204. mouse.x = normalize(event.clientX / window.innerWidth);
  205. mouse.y = -normalize(event.clientY / window.innerHeight);
  206. event.target.style.cursor = "default";
  207. let hiveIntersects = getIntersectFromCollection(Hive.collection, intersects);
  208. if (hiveIntersects.length > 0) {
  209. event.target.style.cursor = "pointer";
  210. }
  211. let flowerIntersects = getIntersectFromCollection(Flower.collection, intersects);
  212. if (flowerIntersects.length > 0) {
  213. event.target.style.cursor = "pointer";
  214. }
  215. let beeIntersects = getIntersectFromCollection(Bee.collection, intersects);
  216. if (beeIntersects.length > 0) {
  217. event.target.style.cursor = "pointer";
  218. }
  219. let groundIntersect = intersects.filter(i => i.object == ground);
  220. if (groundIntersect.length > 0) {
  221. if (draggingOnGround) {
  222. event.target.style.cursor = "move";
  223. dragOffset = camera.focus.clone().sub(groundIntersect[0].point);
  224. camera.focusTarget = dragStart.clone().add(dragOffset);
  225. }
  226. }
  227. });
  228. window.addEventListener("mousedown", event => {
  229. camera.follow = false;
  230. mouse.x = normalize(event.clientX / window.innerWidth);
  231. mouse.y = -normalize(event.clientY / window.innerHeight);
  232. let hiveIntersects = getIntersectFromCollection(Hive.collection, intersects);
  233. if (hiveIntersects.length > 0) {
  234. return;
  235. }
  236. let flowerIntersects = getIntersectFromCollection(Flower.collection, intersects);
  237. if (flowerIntersects.length > 0) {
  238. return;
  239. }
  240. let beeIntersects = getIntersectFromCollection(Bee.collection, intersects);
  241. if (beeIntersects.length > 0) {
  242. setFocus(beeIntersects[0]);
  243. return;
  244. }
  245. let groundIntersect = intersects.filter(i => i.object == ground);
  246. if (groundIntersect.length > 0) {
  247. draggingOnGround = true;
  248. dragStart = groundIntersect[0].point.clone();
  249. dragOffset = camera.focus.clone().sub(groundIntersect[0].point);
  250. camera.focusTarget = dragStart.clone().add(dragOffset);
  251. }
  252. });
  253. window.addEventListener("contextmenu", event => {
  254. event.preventDefault();
  255. });
  256. window.addEventListener("mouseup", event => {
  257. if (draggingOnGround) {
  258. draggingOnGround = false;
  259. return;
  260. }
  261. draggingOnGround = false;
  262. if (event.button == 2) {
  263. if (selected instanceof Hive) {
  264. let flowerIntersects = getIntersectFromCollection(Flower.collection, intersects);
  265. if (flowerIntersects.length > 0) {
  266. selected.toggleFlower(flowerIntersects[0]);
  267. addLines(selected);
  268. return;
  269. }
  270. return;
  271. }
  272. }
  273. let hiveIntersects = getIntersectFromCollection(Hive.collection, intersects);
  274. if (hiveIntersects.length > 0) {
  275. if (selected == hiveIntersects[0]) {
  276. setFocus(hiveIntersects[0]);
  277. }
  278. selected = hiveIntersects[0];
  279. addLines(selected);
  280. return;
  281. }
  282. let flowerIntersects = getIntersectFromCollection(Flower.collection, intersects);
  283. if (flowerIntersects.length > 0) {
  284. if (selected == flowerIntersects[0]) {
  285. setFocus(flowerIntersects[0]);
  286. }
  287. selected = flowerIntersects[0];
  288. addLines(selected);
  289. return;
  290. }
  291. let beeIntersects = getIntersectFromCollection(Bee.collection, intersects);
  292. if (beeIntersects.length > 0) {
  293. setFocus(beeIntersects[0]);
  294. addLines(selected);
  295. return;
  296. }
  297. });
  298. window.addEventListener("wheel", event => {
  299. let vector = new THREE.Vector3();
  300. camera.getWorldDirection(vector);
  301. vector.multiplyScalar(-event.deltaY / 4);
  302. camera.offset.add(vector);
  303. });
  304. let addLines = (focus) => {
  305. for (let i = 0; i < Waypoint.collection.length; i++) {
  306. let waypoint = Waypoint.collection[i];
  307. for (let j = 0; j < waypoint.lines.length; j++) {
  308. let line = waypoint.lines[j];
  309. scene.remove(line);
  310. }
  311. }
  312. if (focus.hasOwnProperty("entranceWaypoint")) {
  313. for (let i = 0; i < focus.entranceWaypoint.lines.length; i++) {
  314. scene.add(focus.entranceWaypoint.lines[i]);
  315. }
  316. }
  317. for (let i = 0; i < Hive.collection.length; i++) {
  318. let hive = Hive.collection[i];
  319. scene.remove(hive.billboard);
  320. if (hive == focus) {
  321. scene.add(hive.billboard);
  322. }
  323. }
  324. }
  325. let getIntersectFromCollection = (collection, intersectCollection) => {
  326. return collection.filter(target => intersectCollection.filter(i => crawlParent(target.model, i.object)).length > 0);
  327. }
  328. let crawlParent = (lookingFor, questionObj) => {
  329. if (questionObj == lookingFor) {
  330. return true;
  331. }
  332. if (questionObj.parent == null) {
  333. return false;
  334. }
  335. if (questionObj.parent == lookingFor) {
  336. return true;
  337. }
  338. return crawlParent(lookingFor, questionObj.parent);
  339. }
  340. window.addEventListener("touchmove", event => {
  341. mouse.x = normalize(event.touches[0].clientX / window.innerWidth);
  342. mouse.y = -normalize(event.touches[0].clientY / window.innerHeight);
  343. });
  344. function normalize(value) {
  345. return (2 * value) - 1;
  346. }