Advertisement
EugeneFitz

Untitled

Apr 26th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.33 KB | None | 0 0
  1. 'main.js'
  2. import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.module.js";
  3. import { init, animate, scene, camera, controls } from "./scene.js";
  4. import { createTurtle } from "./objects/turtle.js";
  5. import { createGrid } from "./objects/grid.js";
  6. import { Vector2, Raycaster} from "https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.module.js";
  7. import { Computer } from "./objects/computer.js";
  8.  
  9. init();
  10.  
  11. const blinkAnimationSettings = {
  12. animation: {
  13. frametime: 8,
  14. frames: [0, 1],
  15. },
  16. };
  17.  
  18. document.getElementById("menuButton").addEventListener("click", () => {
  19. const menu = document.getElementById("slideMenu");
  20. menu.style.right = menu.style.right === "0px" ? "-250px" : "0px";
  21. });
  22.  
  23. const grid = createGrid(10, 10);
  24.  
  25. let highlightedGridUnit;
  26.  
  27. var listCounters = {
  28. computers: 0,
  29. turtles: 0,
  30. };
  31. const turtlesArray = [];
  32.  
  33. function roundToNearestGridCenter(value, gridSize) {
  34. return Math.round(value / gridSize) * gridSize;
  35. }
  36.  
  37. const computersArray = [];
  38.  
  39. function onMouseDown(event) {
  40. event.preventDefault();
  41.  
  42. if (!placementMode) return;
  43.  
  44. // Calculate mouse position in normalized device coordinates (-1 to +1) for both components
  45. const mouse = new Vector2(
  46. (event.clientX / window.innerWidth) * 2 - 1,
  47. -(event.clientY / window.innerHeight) * 2 + 1
  48. );
  49.  
  50. // Create a raycaster and set its origin and direction based on the mouse position and camera
  51. const raycaster = new Raycaster();
  52. raycaster.setFromCamera(mouse, camera);
  53.  
  54. // Find the intersection with the grid
  55. const intersects = raycaster.intersectObject(grid);
  56.  
  57. // If there is an intersection, create a new turtle or computer and add it to the scene
  58. if (intersects.length > 0) {
  59. const intersect = intersects[0];
  60. const x = Math.round(intersect.point.x - 0.5) + 0.5;
  61. const z = Math.round(intersect.point.z - 0.5) + 0.5;
  62.  
  63. const loader = new THREE.TextureLoader(); // Define the loader here
  64.  
  65. if (currentObject === "turtle") {
  66. const newTurtle = createTurtle(loader, x, z);
  67. scene.add(newTurtle);
  68. addItemToList("turtles", newTurtle);
  69. } else if (currentObject === "./assets/textures/computer/computer_normal_front_blink.png") {
  70. const newComputer = new Computer(blinkAnimationSettings, currentObject);
  71. computersArray.push(newComputer);
  72. scene.add(newComputer.mesh);
  73. newComputer.mesh.position.set(
  74. x,
  75. newComputer.mesh.scale.z / 2,
  76. z
  77. );
  78. addItemToList("computers", newComputer.mesh);
  79. }
  80.  
  81. placementMode = false;
  82. currentObject = null;
  83. }
  84. }
  85.  
  86. function onMouseMove(event) {
  87. if (!placementMode) {
  88. highlightedGridUnit.visible = false;
  89. return;
  90. }
  91.  
  92. // Calculate mouse position in normalized device coordinates (-1 to +1) for both components
  93. const mouse = new Vector2(
  94. (event.clientX / window.innerWidth) * 2 - 1,
  95. -(event.clientY / window.innerHeight) * 2 + 1
  96. );
  97.  
  98. // Create a raycaster and set its origin and direction based on the mouse position and camera
  99. const raycaster = new Raycaster();
  100. raycaster.setFromCamera(mouse, camera);
  101.  
  102. // Find the intersection with the grid
  103. const intersects = raycaster.intersectObject(grid);
  104.  
  105. // If there is an intersection, update the position of the highlighted grid unit
  106. if (intersects.length > 0) {
  107. const intersect = intersects[0];
  108. const x = Math.round(intersect.point.x - 0.5) + 0.5;
  109. const z = Math.round(intersect.point.z - 0.5) + 0.5;
  110. highlightedGridUnit.position.set(x, 0, z);
  111. highlightedGridUnit.visible = true;
  112. } else {
  113. highlightedGridUnit.visible = false;
  114. }
  115. }
  116.  
  117. document.addEventListener("mousemove", onMouseMove);
  118.  
  119. let turtlePlacementMode = false;
  120.  
  121. function createHighlightedGridUnit() {
  122. const geometry = new THREE.PlaneGeometry(1, 1);
  123. const material = new THREE.MeshBasicMaterial({
  124. color: 0xffff00,
  125. side: THREE.DoubleSide,
  126. transparent: true,
  127. opacity: 0.5,
  128. });
  129. const highlightedGridUnit = new THREE.Mesh(geometry, material);
  130. highlightedGridUnit.rotation.x = Math.PI / 2;
  131. highlightedGridUnit.visible = false;
  132. return highlightedGridUnit;
  133. }
  134.  
  135. highlightedGridUnit = createHighlightedGridUnit();
  136. scene.add(highlightedGridUnit);
  137.  
  138. function addItemToList(listId, object) {
  139. var list = document.getElementById(listId);
  140. var listItem = document.createElement("li");
  141.  
  142. // Create the 'X' button
  143. var removeButton = document.createElement("button");
  144. removeButton.textContent = "X";
  145. removeButton.style.position = "absolute";
  146. removeButton.style.right = "5px";
  147. removeButton.style.width = "20px";
  148. removeButton.style.height = "20px";
  149. removeButton.style.lineHeight = "20px";
  150. removeButton.style.padding = "0";
  151. removeButton.style.fontFamily = "Arial, sans-serif";
  152. removeButton.style.fontSize = "14px";
  153. removeButton.style.fontWeight = "bold";
  154. removeButton.style.textAlign = "center";
  155. removeButton.addEventListener("click", (event) => {
  156. event.stopPropagation();
  157. list.removeChild(listItem);
  158. if (object) {
  159. scene.remove(object);
  160. }
  161.  
  162. // Decrement the appropriate counter if a computer or turtle object is removed
  163. if (listId === "computers" || listId === "turtles") {
  164. listCounters[listId]--;
  165. }
  166. });
  167.  
  168. listItem.textContent = listCounters[listId]++;
  169. listItem.style.position = "relative";
  170. listItem.appendChild(removeButton);
  171. listItem.addEventListener("click", () => {
  172. if (object) {
  173. focusOnObject(object);
  174. } else {
  175. focusOnGridCenter();
  176. }
  177. });
  178. list.appendChild(listItem);
  179. }
  180.  
  181. let placementMode = false;
  182. let currentObject = null;
  183.  
  184. function focusOnPosition(position) {
  185. const distance = 5;
  186. const offset = new THREE.Vector3(0, 2, distance);
  187. controls.target.copy(position);
  188. camera.position.copy(position);
  189. camera.position.add(offset);
  190. controls.update();
  191. }
  192.  
  193. // Add the event listener for the mousedown event
  194. document.addEventListener("mousedown", onMouseDown, false);
  195.  
  196. document
  197. .getElementById("addButtonTurtles")
  198. .addEventListener("click", function () {
  199. const loader = new THREE.TextureLoader();
  200. const newX = 0;
  201. const newY = 0;
  202. currentObject = "turtle";
  203. placementMode = true;
  204. highlightedGridUnit.visible = true; // Make the highlightedGridUnit visible
  205. });
  206.  
  207. document
  208. .getElementById("addButtonComputers")
  209. .addEventListener("click", function () {
  210. // Store the selected texture path in currentObject
  211. currentObject = './assets/textures/computer/computer_normal_front_blink.png';
  212. placementMode = true;
  213. highlightedGridUnit.visible = true; // Make the highlightedGridUnit visible
  214. });
  215. // Add event listeners for more lists if needed
  216. animate();
  217.  
  218. ---
  219.  
  220. 'scene.js'
  221. import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.module.js';
  222. import { OrbitControls } from 'https://cdn.skypack.dev/three@0.114.0/examples/jsm/controls/OrbitControls.js';
  223. import { Computer } from './objects/computer.js';
  224. import { createDirectionalLight } from './objects/directionalLight.js';
  225. import { createGrid } from './objects/grid.js';
  226. import { createTurtle } from './objects/turtle.js';
  227.  
  228.  
  229. let camera, scene, renderer, sphere, light, controls, grid;
  230.  
  231. const blinkAnimationSettings = {
  232. "animation": {
  233. "frametime": 8,
  234. "frames": [0, 1],
  235. },
  236. };
  237.  
  238.  
  239. const computersArray = [];
  240.  
  241. const computer = new Computer(blinkAnimationSettings, './assets/textures/computer/computer_normal_front_blink.png');
  242.  
  243. function init() {
  244.  
  245. // Create a new scene
  246. scene = new THREE.Scene();
  247.  
  248. // Set up the camera
  249. camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
  250. camera.position.z = 5;
  251.  
  252. // Create a renderer and add it to the DOM
  253. renderer = new THREE.WebGLRenderer();
  254. renderer.setSize(window.innerWidth, window.innerHeight);
  255. document.body.appendChild(renderer.domElement);
  256. computersArray.push(computer);
  257. scene.add(computer.mesh);
  258.  
  259. const loader = new THREE.TextureLoader();
  260. const turtle = createTurtle(loader, 0, 0);
  261.  
  262. // scene.add(turtle)
  263.  
  264. // Add a directional light
  265. light = createDirectionalLight();
  266. scene.add(light);
  267.  
  268. // Add OrbitControls
  269. controls = new OrbitControls(camera, renderer.domElement);
  270.  
  271. // Add a 10x10 grid
  272. grid = createGrid(10, 10);
  273. scene.add(grid);
  274.  
  275. // Handle window resize
  276. window.addEventListener('resize', onWindowResize, false);
  277. }
  278.  
  279. function animate() {
  280. requestAnimationFrame(animate);
  281.  
  282. // Update animations for all computers
  283. // Update animations for each computer instance
  284. for (const computer of computersArray) {
  285. computer.updateAnimation();
  286. }
  287.  
  288. // Update OrbitControls
  289. controls.update();
  290.  
  291. renderer.render(scene, camera);
  292. }
  293.  
  294. function onWindowResize() {
  295. camera.aspect = window.innerWidth / window.innerHeight;
  296. camera.updateProjectionMatrix();
  297.  
  298. renderer.setSize(window.innerWidth, window.innerHeight);
  299. }
  300.  
  301. export { init, animate, scene, camera, controls};
  302. ----
  303. 'computer.js'
  304. import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.module.js';
  305.  
  306. export class Computer {
  307. constructor(blinkAnimationSettings, texturePath) {
  308. this.blinkAnimationSettings = blinkAnimationSettings;
  309. this.currentFrame = new THREE.Vector2(0, 0);
  310. this.frameCounter = 0;
  311. this.mesh = this.createComputer(texturePath);
  312. }
  313.  
  314. createComputer(texturePath) {
  315. const loader = new THREE.TextureLoader();
  316.  
  317. const texture = loader.load(texturePath, function (texture) {
  318. texture.minFilter = THREE.NearestFilter;
  319. texture.magFilter = THREE.NearestFilter;
  320. });
  321.  
  322. const textureTop = loader.load('./assets/textures/computer/computer_normal_top.png', function (texture) {
  323. texture.minFilter = THREE.NearestFilter;
  324. texture.magFilter = THREE.NearestFilter;
  325. });
  326.  
  327. const textureSides = loader.load('./assets/textures/computer/computer_normal_side.png', function (texture) {
  328. texture.minFilter = THREE.NearestFilter;
  329. texture.magFilter = THREE.NearestFilter;
  330. });
  331.  
  332. const numFrames = this.blinkAnimationSettings.animation.frames.length;
  333. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  334. texture.repeat.set(1 / 1, 1 / 2);
  335.  
  336. const materials = [
  337. new THREE.MeshBasicMaterial({ map: textureSides }),
  338. new THREE.MeshBasicMaterial({ map: textureSides }),
  339. new THREE.MeshBasicMaterial({ map: textureTop }),
  340. new THREE.MeshBasicMaterial({ map: textureTop }),
  341. new THREE.MeshBasicMaterial({ map: texture }),
  342. new THREE.MeshBasicMaterial({ map: textureSides }),
  343. ];
  344.  
  345. const geometry = new THREE.BoxGeometry(1, 1, 1);
  346. const computer = new THREE.Mesh(geometry, materials);
  347.  
  348. computer.scale.set(1, 1, 1);
  349. computer.position.set(-4.5, 0.5, 4.5);
  350.  
  351. return computer;
  352. }
  353.  
  354. updateAnimation() {
  355. if (this.frameCounter % (this.blinkAnimationSettings.animation.frametime * 4) === 0) {
  356. this.currentFrame.y = (this.currentFrame.y + 1) % this.blinkAnimationSettings.animation.frames.length;
  357. this.mesh.material[4].map.offset.y = this.blinkAnimationSettings.animation.frames[this.currentFrame.y] * 0.5;
  358. }
  359. this.frameCounter++;
  360. }
  361. }
  362. ---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement