Guest User

Untitled

a guest
May 25th, 2023
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.68 KB | None | 0 0
  1. //Base Parameters
  2. var renderer = new THREE.WebGLRenderer({ antialias: true });
  3. renderer.setSize(window.innerWidth, window.innerHeight);
  4.  
  5. console.log(renderer);
  6. document.body.appendChild(renderer.domElement);
  7.  
  8. if (window.innerWidth > 800) {
  9. renderer.shadowMap.enabled = true;
  10. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  11. };
  12.  
  13. document.addEventListener("DOMContentLoaded", function(event) {
  14. var camera = new THREE.PerspectiveCamera(20, window.innerWidth / window.innerHeight, 1, 5000);
  15. camera.position.set(0, 2, 25);
  16.  
  17. // Add event listener for mouse wheel events
  18. document.addEventListener('wheel', handleMouseWheel, false);
  19.  
  20. function handleMouseWheel(event) {
  21. // Adjust the camera's position based on the scroll direction
  22. var delta = event.deltaY;
  23. var zoomSpeed = 0.1; // Adjust this value to control the zoom speed
  24.  
  25. if (delta < 0) {
  26. // Zoom in
  27. camera.position.z -= zoomSpeed;
  28. } else {
  29. // Zoom out
  30. camera.position.z += zoomSpeed;
  31. }
  32. }
  33.  
  34. var scene = new THREE.Scene();
  35. var city = new THREE.Object3D();
  36. var smoke = new THREE.Object3D();
  37. var town = new THREE.Object3D();
  38. var createCarPos = true;
  39. var uSpeed = 0.0002; // Speed for Left and Right camera movement, see below.
  40. var vSpeed = 0.0010; // Speed for Up and Down camera movement, see below.
  41.  
  42. // Make it responsive
  43. window.addEventListener("resize", onWindowResize, false);
  44.  
  45. function onWindowResize(){
  46. camera.aspect = window.innerWidth / window.innerHeight;
  47. camera.updateProjectionMatrix();
  48. renderer.setSize(window.innerWidth, window.innerHeight);
  49. }
  50.  
  51. var scene = new THREE.Scene();
  52. var city = new THREE.Object3D();
  53. var smoke = new THREE.Object3D();
  54. var town = new THREE.Object3D();
  55. var createCarPos = true;
  56. var uSpeed = 0.0002; // Speed for Left and Right camera movement, see below.
  57. var vSpeed = 0.0010; // Speed for Up and Down camera movement, see below.
  58.  
  59. // Make it responsive
  60. window.addEventListener("resize", onWindowResize, false);
  61.  
  62. function onWindowResize(){
  63. camera.aspect = window.innerWidth / window.innerHeight;
  64. camera.updateProjectionMatrix();
  65. renderer.setSize(window.innerWidth, window.innerHeight);
  66. }
  67.  
  68. ////////////////////////////////////////////////// //////////////////////////////////////////////////
  69. // // // //
  70. // How to change the HORIZONTAL (Left/Right) // // How to change the VERTICAL (Up/Down) //
  71. // speed of the Camera: // // speed of the Camera: //
  72. // ------------------------- // // ------------------------- //
  73. // Change the uSpeed value on Line 39 above. // // Change the vSpeed value on Line 40 above. //
  74. // For example: 0.001; > 0.002; // // For example: 0.001; > 0.002; //
  75. // ------------------------- // // ------------------------- //
  76. // (Default value is/was 0.001) // // (Default value is/was 0.001) //
  77. // // // //
  78. ////////////////////////////////////////////////// //////////////////////////////////////////////////
  79.  
  80. // FOG Background (Single Color & Multi-Color)
  81. ////////////////////////////////////////////////////////////////////////////////////////////////////
  82. // var setcolor = 0xF02050; <- This is the default example used in AsmrProg's Video/Tutorial. //
  83. ////////////////////////////////////////////////////////////////////////////////////////////////////
  84. // Single Color Method: //
  85. // -------------------- //
  86. // var setcolor = 0x1A9CBC; //
  87. // scene.background = new THREE.Color(setcolor); //
  88. // scene.fog = new THREE.Fog(setcolor, 10, 30); //
  89. ////////////////////////////////////////////////////////////////////////////////////////////////////
  90. // Multi-Color Method: //
  91. var colors = [0x62323A, 0x724638, 0xA28030]; // Array of color values //
  92. // //
  93. // Create a gradient texture //
  94. var canvas = document.createElement('canvas'); //
  95. var context = canvas.getContext('2d'); //
  96. canvas.width = 256; //
  97. canvas.height = 1; //
  98. // //
  99. var gradient = context.createLinearGradient(0, 0, 256, 0); //
  100. gradient.addColorStop(0, '#' + colors[0].toString(16)); //
  101. gradient.addColorStop(0.5, '#' + colors[1].toString(16)); //
  102. gradient.addColorStop(1, '#' + colors[2].toString(16)); //
  103. // //
  104. context.fillStyle = gradient; //
  105. context.fillRect(0, 0, 256, 1); //
  106. // //
  107. // Create a texture from the gradient //
  108. var texture = new THREE.CanvasTexture(canvas); //
  109. texture.wrapS = THREE.ClampToEdgeWrapping; //
  110. texture.wrapT = THREE.ClampToEdgeWrapping; //
  111. texture.minFilter = THREE.LinearFilter; //
  112. texture.magFilter = THREE.LinearFilter; //
  113. // //
  114. // Set the fog background //
  115. scene.background = texture; //
  116. scene.fog = new THREE.Fog(colors[0], 10, 32); //
  117. ////////////////////////////////////////////////// //
  118. // // //
  119. // How to change the Fog Density: // //
  120. // ------------------------- // //
  121. // Change the second value on Line [] above. // //
  122. // The higher the value = Less density. // //
  123. // The lower the more = MORE Density. // //
  124. // // //
  125. // // //
  126. ////////////////////////////////////////////////// //
  127. ////////////////////////////////////////////////////////////////////////////////////////////////////
  128.  
  129. // Random Function
  130. function mathRandom(num = 50){
  131. var numValue = - Math.random() * num + Math.random() * num;
  132. return numValue;
  133. };
  134. //////////////////////////////////////////////////
  135. // //
  136. // How to change the buildings in //
  137. // terms of their Height/Density/Spacing: //
  138. // ------------------------- //
  139. // Change the value on Line 57 above. //
  140. // ------------------------- //
  141. // (Default value is/was 1) //
  142. // ("(num = 1)" shows weird effects) //
  143. // ------------------------- //
  144. // (Any value that exceeds 100 is.. //
  145. // ...idiotic/pointless in My opinion!) //
  146. // //
  147. //////////////////////////////////////////////////
  148.  
  149. // Change Building Colors
  150. var setTintNum = true;
  151. function setTintColor(){
  152. if(setTintNum){
  153. setTintNum = false;
  154. var setColor = 0x000000;
  155. }else{
  156. setTintNum = true;
  157. var setColor = 0x000000;
  158. };
  159. return setColor;
  160. };
  161.  
  162. var cubeposition = {x: 0, z: 0};
  163.  
  164. // Create City
  165. function init() {
  166. var segments = 2;
  167. var planeSize = 60;
  168. var boundary = planeSize / 2;
  169.  
  170. // Function to create a building on top of the plane
  171. function createBuilding(x, y, z, width, height, depth) {
  172. var geometry = new THREE.BoxGeometry(width, height, depth);
  173. var material = new THREE.MeshStandardMaterial({
  174. color: setTintColor(),
  175. wireframe: false,
  176. shading: THREE.SmoothShading,
  177. side: THREE.DoubleSide,
  178. });
  179. var building = new THREE.Mesh(geometry, material);
  180.  
  181. // Adjust the y position to place the building on top of the plane
  182. building.position.set(x, y + height / 2, z);
  183.  
  184. return building;
  185. }
  186.  
  187. for (var i = 1; i < 100; i++) {
  188. var cubeWidth = 0.9;
  189. var width = cubeWidth + mathRandom(1 - cubeWidth);
  190. var height = 0.1 + Math.abs(mathRandom(8));
  191. var depth = cubeWidth + mathRandom(1 - cubeWidth);
  192. var x = Math.round(mathRandom(boundary - width)) * (Math.random() < 0.5 ? -1 : 1);
  193. var y = 0;
  194. var z = Math.round(mathRandom(boundary - depth)) * (Math.random() < 0.5 ? -1 : 1);
  195.  
  196. // Check if building is within the boundaries
  197. if (
  198. Math.abs(x) + width / 2 > boundary ||
  199. Math.abs(z) + depth / 2 > boundary ||
  200. y + height / 2 > planeSize / 2
  201. ) {
  202. continue;
  203. // Skip this iteration if building is outside the boundaries
  204. }
  205.  
  206. var floorGeometry = new THREE.BoxGeometry(width, 0.05, depth);
  207. var floorMaterial = new THREE.MeshLambertMaterial({
  208. color: 0xffffff,
  209. wireframe: true,
  210. transparent: true,
  211. opacity: 0.03,
  212. side: THREE.DoubleSide,
  213. });
  214. var floor = new THREE.Mesh(floorGeometry, floorMaterial);
  215.  
  216. var building = createBuilding(x, y, z, width, height, depth);
  217.  
  218. floor.position.set(building.position.x, 0, building.position.z);
  219.  
  220. town.add(floor);
  221. town.add(building);
  222. }
  223. }
  224.  
  225. //Particulars
  226. var gmaterial = new THREE.MeshToonMaterial({ color: 0xFFFF00, side: THREE.DoubleSide });
  227. var gparticular = new THREE.CircleGeometry(0.01, 3);
  228. var aparticular = 5;
  229.  
  230. for (var h = 1; h < 300; h++) {
  231. var particular = new THREE.Mesh(gparticular, gmaterial);
  232. particular.position.set(mathRandom(aparticular), mathRandom(aparticular), mathRandom(aparticular));
  233. particular.rotation.set(mathRandom(), mathRandom(), mathRandom());
  234. smoke.add(particular);
  235. };
  236.  
  237. var pmaterial = new THREE.MeshPhongMaterial({
  238. color: 0x000000,
  239. side: THREE.FrontSide,
  240. shininess: 10,
  241. opacity: 0.9,
  242. transparent: true
  243. });
  244.  
  245. var pgeometry = new THREE.PlaneGeometry(60, 60);
  246. var pelement = new THREE.Mesh(pgeometry, pmaterial);
  247. pelement.rotation.x = -Math.PI / 2; // Rotate the plane element to face upwards
  248. pelement.position.y = 0; // Set the plane element's position at the same level as the buildings
  249. pelement.receiveShadow = true;
  250. city.add(pelement);
  251.  
  252. // Mouse Functions
  253. var raycaster = new THREE.Raycaster();
  254. var mouse = new THREE.Vector2(), INTERSECTED;
  255. var touchX, touchY;
  256. var threshold = 5; // or whatever value you want to use
  257.  
  258. function onMouseMove(e){
  259. e.preventDefault();
  260. mouse.x = (e.clientX / window.innerWidth) * 2 - 1;
  261. mouse.y = - (e.clientY / window.innerHeight) * 2 + 1;
  262.  
  263. city.rotation.y -= ((mouse.x * 8) - camera.rotation.y) * uSpeed;
  264. city.rotation.x -= ((-(mouse).y * 2) - camera.position.x) * vSpeed;
  265. };
  266.  
  267. function rotateObject(deltaX, deltaY) {
  268. // Rotate the city based on the touch movement
  269. city.rotation.y -= deltaX * uSpeed;
  270. city.rotation.x -= deltaY * vSpeed;
  271.  
  272. // Limit the vertical rotation angle of the city
  273. city.rotation.x = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, city.rotation.x));
  274. }
  275.  
  276. function onDocumentTouchStart(e){
  277. if(e.touches.length == 1){
  278. e.preventDefault();
  279. touchX = e.touches[0].pageX;
  280. touchY = e.touches[0].pageY;
  281. };
  282. };
  283.  
  284. function onDocumentTouchMove(event){
  285. // Get the first touch point
  286. var touch = event.touches[0];
  287.  
  288. // Calculate the distance moved from the previous touch point
  289. var deltaX = touch.clientX - touchX;
  290. var deltaY = touch.clientY - touchY;
  291.  
  292. // Check if the touch has moved beyond a certain threshold
  293. if (Math.abs(deltaX) > threshold || Math.abs(deltaY) > threshold) {
  294. // Prevent the default touch behavior (e.g. scrolling)
  295. event.preventDefault();
  296.  
  297. // Rotate the object based on the touch movement
  298. rotateObject(deltaX, deltaY);
  299.  
  300. // Update the touch point for the next touch move event
  301. touchX = touch.clientX;
  302. touchY = touch.clientY;
  303. }
  304. }
  305.  
  306. window.addEventListener('mousemove', onMouseMove, false);
  307. window.addEventListener('touchstart', onDocumentTouchStart, false);
  308. window.addEventListener('touchmove', onDocumentTouchMove, false);
  309.  
  310. // Create Lights
  311. var ambientLight = new THREE.AmbientLight(0xFFFFFF, 4);
  312. var lightFront = new THREE.SpotLight(0xFFFFFF, 20, 10);
  313. var lightBack = new THREE.PointLight(0xFFFFFF, 0.5);
  314. var spotLightHelper = new THREE.SpotLightHelper(lightFront);
  315.  
  316. lightFront.rotation.x = 45 * THREE.MathUtils.DEG2RAD;
  317. lightFront.rotation.z = -45 * THREE.MathUtils.DEG2RAD;
  318. lightFront.position.set(5, 5, 5);
  319. lightFront.castShadow = true;
  320. lightFront.shadow.mapSize.width = 6000;
  321. lightFront.shadow.mapSize.height = lightFront.shadow.mapSize.width;
  322. lightFront.penumbra = 0.1;
  323. lightBack.position.set(0, 6, 0);
  324.  
  325. smoke.position.y = 2;
  326.  
  327. scene.add(ambientLight);
  328. city.add(lightFront);
  329. scene.add(lightBack);
  330. scene.add(city);
  331. city.add(smoke);
  332. city.add(town);
  333.  
  334. // Grid Helper
  335. var gridHelper = new THREE.GridHelper(60, 120, 0xFF0000, 0x000000);
  336. city.add(gridHelper);
  337.  
  338. // Cars World - Code to create Car mesh
  339. var createCars = function (cScale = 2, cPos = 20, cColor = 0xFFFF00) {
  340. var cMat = new THREE.MeshToonMaterial({color: cColor, side: THREE.DoubleSide});
  341. var cGeo = new THREE.BoxGeometry(1, cScale / 40, cScale / 40);
  342. var cElem = new THREE.Mesh(cGeo, cMat),
  343. w = cElem.geometry.parameters.width,
  344. h = cElem.geometry.parameters.height
  345. var cAmp = 3;
  346.  
  347. if (createCarPos) {
  348. createCarPos = false;
  349. cElem.position.x = -cPos;
  350. cElem.position.z = (mathRandom(cAmp));
  351.  
  352. TweenMax.to(cElem.position, 3, {x: cPos, repeat: -1, yoyo: true, delay: mathRandom(3)});
  353. }else{
  354. createCarPos = true;
  355. cElem.position.x = (mathRandom(cAmp));
  356. cElem.position.z = -cPos;
  357. cElem.rotation.y = 90 * THREE.MathUtils.DEG2RAD;
  358.  
  359. TweenMax.to(cElem.position, 5, {z: cPos, repeat: -1, yoyo: true, delay: mathRandom(3), ease: Power1.easeInOut});
  360. };
  361. cElem.receiveShadow = true;
  362. cElem.castShadow = true;
  363. cElem.position.y = Math.abs(mathRandom(5));
  364. city.add(cElem);
  365. return cElem;
  366. };
  367.  
  368. var myCar = createCars(); // Assign the returned (Car) mesh to a variable
  369. console.log(myCar); // Now I can access > log the Car mesh
  370. console.log(myCar.position);
  371. console.log(myCar.rotation);
  372. console.log(myCar.scale);
  373.  
  374. var generateLines = function(){
  375. for(var i = 0; i < 60; i++) {
  376. createCars(0, 1, 20);
  377. };
  378. };
  379.  
  380. // Camera Position
  381. var cameraSet = function () {
  382. createCars(0.1, 20, 0xFFFFFF);
  383. };
  384.  
  385. //Animate Functions
  386. var animate = function () {
  387. var time = Date.now() * 0.00005;
  388. requestAnimationFrame(animate);
  389.  
  390. city.rotation.y -= ((mouse.x * 8) - camera.rotation.y) * uSpeed;
  391. city.rotation.x -= (-(mouse.y * 2) - camera.position.x) * vSpeed;
  392. if (city.rotation.x < -0.05) {
  393. city.rotation.x = -0.05;
  394. } else if (city.rotation.x > 1) {
  395. city.rotation.x = 1;
  396. }
  397. var cityRotation = Math.sin(Date.now() / 5000) * 13;
  398.  
  399. for (let i = 0, l = town.children.length; i < l; i++) {
  400. var object = town.children[i];
  401. }
  402.  
  403. smoke.rotation.y += 0.01;
  404. smoke.rotation.x += 0.01;
  405.  
  406. camera.lookAt(city.position);
  407. renderer.render(scene, camera);
  408. };
  409.  
  410. //const container = document.createElement('div');
  411. //container.id = 'text-container';
  412. //document.body.appendChild(container);
  413.  
  414. //const h1 = document.createElement('h1');
  415. //h1.innerText = 'YuGiOh! Capsule Monster Coliseum';
  416. //container.appendChild(h1);
  417.  
  418. //const p = document.createElement('p');
  419. //p.innerText = '- Pure Society Edition -';
  420. //container.appendChild(p);
  421.  
  422. //const span = document.createElement('span');
  423. //span.innerText = ' ';
  424. //h1.appendChild(span);
  425.  
  426. container.style.position = 'absolute';
  427. container.style.top = '50%';
  428. container.style.left = '50%';
  429. container.style.transform = 'translate(-50%, -50%)';
  430. container.style.zIndex = '1';
  431.  
  432. // Calling Main Functions
  433. generateLines();
  434. init();
  435.  
  436. // Add a callback to the animate function to transition to the black screen
  437. animate();
  438. // Create the black screen element
  439. const blackScreen = document.createElement('div');
  440. blackScreen.className = 'black-screen';
  441. document.body.appendChild(blackScreen);
  442.  
  443. // Remove the animation container and under-panels elements
  444. const animationContainer = document.querySelector('.animation-container');
  445. const underPanels = document.querySelector('.under-panels');
  446. animationContainer.remove();
  447. underPanels.remove();
  448. });
Advertisement
Add Comment
Please, Sign In to add comment