Advertisement
EugeneFitz

Untitled

Mar 14th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. // Create the scene, camera, and renderer
  2. const scene = new THREE.Scene();
  3. const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
  4. const renderer = new THREE.WebGLRenderer();
  5. renderer.setSize(window.innerWidth, window.innerHeight);
  6. document.body.appendChild(renderer.domElement);
  7.  
  8. // Add a grid helper to the scene
  9. const gridHelper = new THREE.GridHelper(10, 10);
  10. scene.add(gridHelper);
  11.  
  12. // Add orbit controls to the camera
  13. const controls = new THREE.OrbitControls(camera, renderer.domElement);
  14. controls.update();
  15.  
  16. // Set the camera position
  17. camera.position.set(0, 5, 5);
  18. camera.lookAt(scene.position);
  19.  
  20. // Add a cube to the scene
  21. const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
  22. const cubeMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
  23. const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  24. cube.position.set(0.5, 0.5 * cubeGeometry.parameters.height, 0.5);
  25. scene.add(cube);
  26.  
  27. // Add a container div for the text field and button
  28. const container = document.createElement('div');
  29. container.style.position = 'absolute';
  30. container.style.top = '10px';
  31. container.style.left = '10px';
  32. container.style.zIndex = '1';
  33. container.style.height = '10px';
  34. container.style.display = 'flex';
  35. container.style.justifyContent = 'center';
  36. container.style.alignItems = 'center';
  37. document.body.appendChild(container);
  38.  
  39. // Add a text area and button to the container
  40. const textArea = document.createElement('textarea');
  41. textArea.id = 'textInput';
  42. textArea.placeholder = 'Enter turtle commands';
  43. textArea.style.marginRight = '1rem';
  44. container.appendChild(textArea);
  45.  
  46. const activateButton = document.createElement('button');
  47. activateButton.textContent = 'Activate';
  48. activateButton.id = 'activateButton';
  49. container.appendChild(activateButton);
  50.  
  51. // Adjust the renderer's canvas style to fix the position
  52. renderer.domElement.style.position = 'absolute';
  53. renderer.domElement.style.top = '0';
  54. renderer.domElement.style.left = '0';
  55. renderer.domElement.style.zIndex = '0';
  56. document.body.appendChild(renderer.domElement);
  57.  
  58. // Helper function to create a text label
  59. function createTextLabel(text) {
  60. const canvas = document.createElement('canvas');
  61. const context = canvas.getContext('2d');
  62. context.font = '24px Arial';
  63. context.fillStyle = 'rgba(255, 255, 255, 1)';
  64. context.fillText(text, 0, 24);
  65.  
  66. const texture = new THREE.CanvasTexture(canvas);
  67. const material = new THREE.SpriteMaterial({ map: texture });
  68. const sprite = new THREE.Sprite(material);
  69. sprite.scale.set(0.3 * canvas.width, 0.3 * canvas.height, 1); // increase scale
  70.  
  71. return sprite;
  72. }
  73.  
  74. // Parse turtle commands and move the cube
  75. function executeCommands(parsedCommands, index) {
  76. if (index >= parsedCommands.length) {
  77. return;
  78. }
  79.  
  80. const command = parsedCommands[index].trim();
  81. let startPosition, endPosition, startTime, currentPosition;
  82. const duration = 500; // duration of each movement in milliseconds
  83. let movementVector = new THREE.Vector3();
  84.  
  85. if (command.startsWith('print(') && command.endsWith(')')) {
  86. const text = command.substring(6, command.length - 1);
  87.  
  88. const textLabel = createTextLabel(text);
  89. textLabel.position.set(cube.position.x, cube.position.y + 1.5, cube.position.z); // update position
  90. scene.add(textLabel);
  91.  
  92. setTimeout(() => {
  93. scene.remove(textLabel);
  94. executeCommands(parsedCommands, index + 1);
  95. }, 2000);
  96.  
  97.  
  98. } else if (command.startsWith('setColor(') && command.endsWith(')')) {
  99. const color = command.substring(9, command.length - 1);
  100. cube.material.color.set(color);
  101. executeCommands(parsedCommands, index + 1);
  102. } else if (command.startsWith('setScale(') && command.endsWith(')')) {
  103. const scale = parseFloat(command.substring(9, command.length - 1));
  104. cube.scale.set(scale, scale, scale);
  105. executeCommands(parsedCommands, index + 1);
  106. } else if (command === 'reset') {
  107. cube.position.set(0.5, 0.5 * cubeGeometry.parameters.height, 0.5);
  108. cube.rotation.set(0, 0, 0);
  109. cube.material.color.set(0xff0000);
  110. cube.scale.set(1, 1, 1);
  111. executeCommands(parsedCommands, index + 1);
  112. } else {
  113. switch (command) {
  114. case 'forward':
  115. movementVector.set(0, 0, -1);
  116. break;
  117. case 'back':
  118. movementVector.set(0, 0, 1);
  119. break;
  120. case 'left':
  121. movementVector.set(-1, 0, 0);
  122. break;
  123. case 'right':
  124. movementVector.set(1, 0, 0);
  125. break;
  126. case 'up':
  127. startPosition = cube.position.y;
  128. endPosition = startPosition + 1;
  129. break;
  130. case 'down':
  131. startPosition = cube.position.y;
  132. endPosition = startPosition - 1;
  133. break;
  134. case 'turnRight':
  135. case 'turnLeft':
  136. startPosition = cube.rotation.y;
  137. endPosition = startPosition + (command === 'turnRight' ? -1 : 1) * Math.PI / 2;
  138. startTime = performance.now();
  139. currentPosition = cube.rotation.y;
  140.  
  141. const updateRotation = () => {
  142. const elapsedTime = performance.now() - startTime;
  143. const progress = Math.min(elapsedTime / duration, 1);
  144.  
  145. cube.rotation.y = startPosition + progress * (endPosition - startPosition);
  146.  
  147. if (progress < 1) {
  148. requestAnimationFrame(updateRotation);
  149. } else {
  150. setTimeout(() => executeCommands(parsedCommands, index + 1), duration);
  151. }
  152. };
  153.  
  154. requestAnimationFrame(updateRotation);
  155. return;
  156. default:
  157. const textLabel = createTextLabel("Unknown command: ${command}");
  158. textLabel.position.set(cube.position.x, cube.position.y + 1, cube.position.z);
  159. scene.add(textLabel);
  160.  
  161. setTimeout(() => {
  162. scene.remove(textLabel);
  163. executeCommands(parsedCommands, index + 1);
  164. }, 2000);
  165. //console.log(`Unknown command: ${command}`);
  166. }
  167.  
  168. if (movementVector.length() > 0) {
  169. movementVector.applyQuaternion(cube.quaternion);
  170. startPosition = cube.position.clone();
  171. endPosition = startPosition.clone().add(movementVector);
  172. startTime = performance.now();
  173. currentPosition = cube.position.clone();
  174.  
  175. const updatePosition = () => {
  176. const elapsedTime = performance.now() - startTime;
  177. const progress = Math.min(elapsedTime / duration, 1);
  178.  
  179. cube.position.lerpVectors(startPosition, endPosition, progress);
  180.  
  181. if (progress < 1) {
  182. requestAnimationFrame(updatePosition);
  183. } else {
  184. setTimeout(() => executeCommands(parsedCommands, index + 1), duration);
  185. }
  186. };
  187.  
  188. requestAnimationFrame(updatePosition);
  189. } else {
  190. executeCommands(parsedCommands, index + 1);
  191. }
  192. }
  193. }
  194.  
  195. function parseTurtleCommands(commands) {
  196. console.log('Parsing commands:', commands);
  197. const parsedCommands = commands.split('\n');
  198. executeCommands(parsedCommands, 0);
  199. }
  200.  
  201. // Add event listener to activate button
  202. activateButton.addEventListener('click', () => {
  203. console.log('Button clicked');
  204. const textInput = document.getElementById('textInput');
  205. const text = textInput.value;
  206. parseTurtleCommands(text);
  207. textInput.value = '';
  208. });
  209.  
  210. // Render the scene
  211. function animate() {
  212. requestAnimationFrame(animate);
  213. renderer.render(scene, camera);
  214. controls.update();
  215. }
  216. animate();
  217.  
  218. With this code, how do I add
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement