Best JavaScript code snippet using wpt
main.js
Source:main.js
1import {OBJLoader} from './resources/threejs/r122/examples/jsm/loaders/OBJLoader.js';2import {MTLLoader} from './resources/threejs/r122/examples/jsm/loaders/MTLLoader.js';3import {DDSLoader} from './resources/threejs/r122/examples/jsm/loaders/DDSLoader.js';4import {DragControls} from './resources/threejs/r122/examples/jsm/controls/DragControls.js';5var container, scene, camera, renderer;6var controls;7var drag_controls;8var group;9var sphere;10let enableSelection = false;11const objects = [];12const collidableObjects = [];13var collected = [];14var trash = [];15var factory = [];16var player = new THREE.Object3D();17var truck = new THREE.Object3D();18var spotLightPlayer;19var spotLight;20truck.name = "truck";21player.name = "player";22const mouse = new THREE.Vector2(), raycaster = new THREE.Raycaster();23var control_target;24init();25animate();26function init() {27 // Setup28 container = document.getElementById('container');29 scene = new THREE.Scene();30 camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);31 //cem karaca32 const listener = new THREE.AudioListener();33 camera.add( listener );34 // create a global audio source35 const sound = new THREE.Audio( listener );36 const nostalgia = ['cem_karaca_isci_kal.mp3', 'baris_manco_donence.mp3'];37 // load a sound and set it as the Audio object's buffer38 const audioLoader = new THREE.AudioLoader();39 audioLoader.load( nostalgia[randomInt(0,1)], function( buffer ) {40 sound.setBuffer( buffer );41 sound.setLoop( true );42 sound.setVolume( 0.1 );43 sound.play();44 });45 renderer = new THREE.WebGLRenderer({alpha: true});46 renderer.setSize(window.innerWidth, window.innerHeight);47 //48 {49 const planeSize = 2000;50 const loader = new THREE.TextureLoader();51 const texture = loader.load('ground.jpg');52 texture.wrapS = THREE.RepeatWrapping;53 texture.wrapT = THREE.RepeatWrapping;54 texture.magFilter = THREE.NearestFilter;55 const repeats = planeSize / 8;56 texture.repeat.set(repeats, repeats);57 const planeGeo = new THREE.PlaneBufferGeometry(planeSize, planeSize);58 const planeMat = new THREE.MeshPhongMaterial({59 map: texture,60 side: THREE.DoubleSide,61 });62 const mesh = new THREE.Mesh(planeGeo, planeMat);63 mesh.rotation.x = Math.PI * -.5;64 scene.add(mesh);65 }66 {67 const skyColor = 0xB1E1FF; // light blue68 const groundColor = 0xB97A20; // brownish orange69 const intensity = 1;70 const light = new THREE.HemisphereLight( skyColor, groundColor, intensity );71 light.name = "hemisphereLight";72 scene.add(light);73 }74 {75 const color = 0xFFFFFF;76 const intensity = 1;77 const light = new THREE.DirectionalLight(color, intensity);78 light.position.set(0, 10, 0);79 light.target.position.set(-5, 0, 0);80 light.name = "directionalLight"81 scene.add(light);82 scene.add(light.target);83 }84 //85 // Add Objects To the Scene HERE86 const mtlLoader = new MTLLoader();87 // adding player to scene88 mtlLoader.load('obj/character/character.mtl', function (materials) {89 materials.preload();90 const objLoaderExample = new OBJLoader();91 objLoaderExample.setMaterials(materials);92 objLoaderExample.load('obj/character/character.obj', (root) => {93 root.rotation.y = Math.PI * -1;94 player.add(root);95 //scene.add(root);96 });97 });98 // adding truck to scene99 100 mtlLoader.load('obj/vehicle/truck.mtl', function (materials) {101 materials.preload();102 const objLoaderExample = new OBJLoader();103 objLoaderExample.setMaterials(materials);104 objLoaderExample.load('obj/vehicle/truck.obj', (root) => {105 root.rotation.y = Math.PI * -1;106 107 //root.position.x = -10;108 //root.position.y = 0;109 //root.position.z = 5;110 //collidableObjects.push(root);111 //objects.push(root);112 truck.add(root);113 //scene.add(root);114 });115 });116 117 /***************************************************************************/118 loadApartment(15, 15, 1, Math.floor(Math.random() * 10)+1);119 loadApartment(27.5, 15, 1, Math.floor(Math.random() * 10)+1, 0.5);120 loadApartment(27.5, 25, 1, Math.floor(Math.random() * 10)+1, 0.5);121 loadApartment(52.5, 15, 1, Math.floor(Math.random() * 10)+1, -0.5);122 loadApartment(52.5, 25, 1, Math.floor(Math.random() * 10)+1, -0.5);123 loadApartment(52.5, 35, 1, Math.floor(Math.random() * 10)+1, -0.5);124 loadApartment(40, 45, 2, Math.floor(Math.random() * 20)+1);125 126 loadApartment(-15, 10, 1, Math.floor(Math.random() * 10)+1, 0.5);127 loadApartment(-15, 20, 1, Math.floor(Math.random() * 10)+1, 0.5);128 loadApartment(-15, 30, 1, Math.floor(Math.random() * 10)+1, 0.5);129 /*130 objectLoader('obj/trash/bottle1.mtl', 'obj/trash/bottle1.obj', 0, 5, 0, true);131 objectLoader('obj/trash/bottle2.mtl', 'obj/trash/bottle2.obj', -5, 5, 0, true);132 objectLoader('obj/trash/bottle3.mtl', 'obj/trash/bottle3.obj', -10, 5, 0, true);133 objectLoader('obj/trash/trash_bag.mtl', 'obj/trash/trash_bag.obj', 0, 10, 0, true);134 objectLoader('obj/trash/trash_can.mtl', 'obj/trash/trash_can.obj', -5, 10, 0, true);135 objectLoader('obj/trash/trash_can_wlid.mtl', 'obj/trash/trash_can_wlid.obj', -10, 10, 0, true);136 objectLoader('obj/trash/trash_dumpster.mtl', 'obj/trash/trash_dumpster.obj', 0, 15, 0, );137 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', -5, 15, 0);138 objectLoader('obj/character/character.mtl', 'obj/character/character.obj', -10, 15, 0, true);139 objectLoader('obj/vehicle/truck.mtl', 'obj/vehicle/truck.obj', 0, 0, 0, false);140 objectLoader('obj/vehicle/truck.mtl', 'obj/vehicle/truck.obj', -20, 15, 0, true);*/141 objectLoader('obj/trash/trash_can.mtl', 'obj/trash/trash_can.obj', 7, 7, 0, false);142 143 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', -7, 15, 0, false, 0.5);144 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', -7, 55, 0, false, 0.75);145 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', -7, -20, 0, false, 0.5);146 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', -7, -50, 0, false, 0.5);147 148 149 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', 30, 7, 0, false, -1);150 151 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', -40, 7, 0, false, -1);152 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', -70, 7, 0, false, -1);153 154 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', -30, -67.5, 0, false, 0);155 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', -65, -67.5, 0, false, 0);156 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', 30, -67.5, 0, false, 0);157 158 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', 80, 7, 0, false, -1);159 160 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', 30, 45, 0, false, -1);161 162 163 164 165 166 objectLoader('obj/way/connector.mtl', 'obj/way/connector.obj', 0, 0, 0.01, false, 0);167 168 // 0.5 way169 for (var i = 1; i <= 3; i++)170 objectLoader('obj/way/mid.mtl', 'obj/way/mid.obj', 10 * i, 0, 0.01, false, 0.5);171 172 objectLoader('obj/way/connector.mtl', 'obj/way/connector.obj', 40, 0, 0.01, false, 0);173 for (var i = 1; i <= 3; i++)174 objectLoader('obj/way/mid.mtl', 'obj/way/mid.obj', 40 + 10 * i, 0, 0.01, false, 0.5);175 // 0 turn at 0.5 way176 for (var i = 1; i <= 2; i++)177 objectLoader('obj/way/mid.mtl', 'obj/way/mid.obj', 40, 10 * i, 0.01, false, 0);178 objectLoader('obj/way/end.mtl', 'obj/way/end.obj', 40, 30, 0.01, false, 0);179 // end of 0.5 way180 for (var i = 1; i <= 3; i++)181 objectLoader('obj/way/mid.mtl', 'obj/way/mid.obj', 40 + 10 * i, 0, 0.01, false, 0.5);182 objectLoader('obj/way/end.mtl', 'obj/way/end.obj', 80, 0, 0.01, false, 0.5);183 loadApartment(95, 0, 2, 9, -0.5);184 loadApartment(65, 15, 1, 2, -1);185 186 for (var i = 1; i <= 7; i++)187 loadApartment(85 - 10*i, -15, 1, Math.floor(Math.random() * 10), 0);188 189 for (var i = 1; i <= 2; i++)190 loadApartment(-15 -15*i, 15, 1, Math.floor(Math.random() * 10), -1);191 192 loadApartment(-15, -15, 1, Math.floor(Math.random() * 10)+1, 0.5);193 loadApartment(-15, -25, 1, Math.floor(Math.random() * 10)+1, 0.5);194 loadApartment(-15, -35, 1, Math.floor(Math.random() * 10)+1, 0.5);195 loadApartment(-15, -45, 1, Math.floor(Math.random() * 10)+1, 0.5);196 197 loadApartment(15, -25, 1, Math.floor(Math.random() * 10)+1, -0.5);198 loadApartment(15, -35, 1, Math.floor(Math.random() * 10)+1, -0.5);199 loadApartment(15, -45, 1, Math.floor(Math.random() * 10)+1, -0.5);200 201 loadApartment(-25, -45, 1, Math.floor(Math.random() * 10)+1, -1);202 loadApartment(-35, -45, 1, Math.floor(Math.random() * 10)+1, -1);203 loadApartment(-25, -75, 1, Math.floor(Math.random() * 10)+1, 1);204 loadApartment(-35, -75, 1, Math.floor(Math.random() * 10)+1, 1);205 loadApartment(-15, -75, 1, Math.floor(Math.random() * 10)+1, 1);206 loadApartment(-45, -75, 1, Math.floor(Math.random() * 10)+1, 1);207 208 loadApartment(-45, -25, 1, Math.floor(Math.random() * 10)+1, -0.5);209 loadApartment(-45, -35, 1, Math.floor(Math.random() * 10)+1, -0.5);210 loadApartment(-75, -25, 1, Math.floor(Math.random() * 10)+1, 0.5);211 loadApartment(-75, -35, 1, Math.floor(Math.random() * 10)+1, 0.5);212 loadApartment(-75, -15, 1, Math.floor(Math.random() * 10)+1, 0.5);213 loadApartment(-75, -45, 1, Math.floor(Math.random() * 10)+1, 0.5);214 215 216 loadApartment(-45, 30, 1, Math.floor(Math.random() * 10)+1, -0.75);217 loadApartment(-60, 35, 1, Math.floor(Math.random() * 10)+1, -1);218 loadApartment(-75, 30, 1, Math.floor(Math.random() * 10)+1, 0.75);219 220 loadApartment(-75, 15, 1, Math.floor(Math.random() * 10)+1, 0.5);221 222 loadApartment(-25, -15, 1, Math.floor(Math.random() * 10)+1, 0);223 loadApartment(-35, -15, 1, Math.floor(Math.random() * 10)+1, 0);224 225 loadApartment(70, 15, 1, Math.floor(Math.random() * 10)+1, -1);226 227 loadApartment(40, -75, 2, Math.floor(Math.random() * 10)+1, 0);228 loadApartment(-75, -65, 2, Math.floor(Math.random() * 10)+1, 0.5);229 230 loadApartment(25, -75, 1, Math.floor(Math.random() * 10)+1, 0);231 loadApartment(30, -45, 1, Math.floor(Math.random() * 10)+1, -1);232 loadApartment(40, -45, 1, Math.floor(Math.random() * 10)+1, -1);233 234 // -1 way235 for (var i = 1; i <= 5; i++)236 objectLoader('obj/way/mid.mtl', 'obj/way/mid.obj', 0, -10*i, 0.01, false, -1);237 238 objectLoader('obj/way/connector.mtl', 'obj/way/connector.obj', 0, -60, 0.01, false, -1);239 for (var i = 1; i <= 10; i++)240 objectLoader('obj/way/mid.mtl', 'obj/way/mid.obj', 0, -60-10*i, 0.01, false, -1);241 objectLoader('obj/way/end.mtl', 'obj/way/end.obj', 0, -170, 0.01, false, -1);242 for (var i = 1; i <= 3; i++)243 objectLoader('obj/way/mid.mtl', 'obj/way/mid.obj', 10*i, -60, 0.01, false, 0.5);244 objectLoader('obj/way/end.mtl', 'obj/way/end.obj', 40, -60, 0.01, false, 0.5);245 for (var i = 1; i <= 5; i++)246 objectLoader('obj/way/mid.mtl', 'obj/way/mid.obj', -10*i, -60, 0.01, false, -0.5);247 248 249 objectLoader('obj/way/connector.mtl', 'obj/way/connector.obj', -60, -60, 0.01, false, 0);250 for (var i = 1; i <= 5; i++)251 objectLoader('obj/way/mid.mtl', 'obj/way/mid.obj', -60, -60+10*i, 0.01, false, 0);252 253 objectLoader('obj/way/connector.mtl', 'obj/way/connector.obj', -60, 0, 0.01, false, 0);254 objectLoader('obj/way/mid.mtl', 'obj/way/mid.obj', -60, 10, 0.01, false, 0);255 objectLoader('obj/way/end.mtl', 'obj/way/end.obj', -60, 20, 0.01, false, 0);256 for (var i = 1; i <= 5; i++)257 objectLoader('obj/way/mid.mtl', 'obj/way/mid.obj', -60+10*i, 0, 0.01, false, 0.5);258 for (var i = 1; i <= 10; i++)259 objectLoader('obj/way/mid.mtl', 'obj/way/mid.obj', -60-10*i, 0, 0.01, false, -0.5);260 261 262 objectLoader('obj/way/end.mtl', 'obj/way/end.obj', -170, 0, 0.01, false, -0.5);263 264 265 // 0 way266 for (var i = 1; i <= 3; i++)267 objectLoader('obj/way/mid.mtl', 'obj/way/mid.obj', 0, 10*i, 0.01, false, 0);268 269 270 objectLoader('obj/way/end.mtl', 'obj/way/end.obj', 10, 40, 0.01, false, 0.5);271 objectLoader('obj/way/connector.mtl', 'obj/way/connector.obj', 0, 40, 0.01, false, 0);272 objectLoader('obj/way/end.mtl', 'obj/way/end.obj', 0, 50, 0.01, false, 0);273 loadApartment(15, 55, 1, Math.floor(Math.random() * 10)+1, -0.75);274 loadApartment(0, 65, 1, Math.floor(Math.random() * 10)+1, -1);275 loadApartment(-15, 55, 1, Math.floor(Math.random() * 10)+1, 0.75);276 objectLoader('obj/way/end.mtl', 'obj/way/end.obj', -10, 40, 0.01, false, -0.5);277 loadApartment(-25, 40, 1, Math.floor(Math.random() * 10)+1, 0.5);278 279 280 281 282 for (var i = -170; i <= -100; i= i + 2)283 objectLoader('obj/field/plant.mtl', 'obj/field/plant.obj', i, 15, 0, false, Math.random() * 2 - 1);284 for (var i = -170; i <= -100; i= i + 2)285 objectLoader('obj/field/plant.mtl', 'obj/field/plant.obj', i, 20, 0, false, Math.random() * 2 - 1);286 for (var i = -170; i <= -100; i= i + 2)287 objectLoader('obj/field/plant.mtl', 'obj/field/plant.obj', i, 25, 0, false, Math.random() * 2 - 1);288 for (var i = -170; i <= -100; i= i + 2)289 objectLoader('obj/field/plant.mtl', 'obj/field/plant.obj', i, 30, 0, false, Math.random() * 2 - 1);290 291 for (var i = -170; i < -100; i= i + 6.25)292 objectLoader('obj/field/fence.mtl', 'obj/field/fence.obj', i, 10, 0, false, 0);293 for (var i = -170; i < -100; i= i + 6.25)294 objectLoader('obj/field/fence.mtl', 'obj/field/fence.obj', i, 35, 0, false, 0);295 296 for (var i = 10; i < 35; i= i + 6.25)297 objectLoader('obj/field/fence.mtl', 'obj/field/fence.obj', -176.25, i, 0, false, 0.5);298 for (var i = 10; i < 35; i= i + 6.25)299 objectLoader('obj/field/fence.mtl', 'obj/field/fence.obj', -101.25, i, 0, false, 0.5);300 301 302 for (var i = 0; i < 30; i++)303 objectLoader('obj/field/cow.mtl', 'obj/field/cow.obj', randomInt(-190, -100), randomInt(-15, -50), 0, false, Math.random() * 2 - 1);304 305 306 307 308 309 310 311 312 313 for (var i = 0; i < 300; i++){314 var type = Math.floor(Math.random() * 3)+1;315 objectLoader('obj/tree/'+type+'.mtl', 'obj/tree/'+type+'.obj', randomInt(-200, 150), randomInt(75, 200), 0.1, false, Math.random() * 2 - 1);316 } 317 318 for (var i = 0; i < 150; i++){319 var type = Math.floor(Math.random() * 3)+1;320 objectLoader('obj/tree/'+type+'.mtl', 'obj/tree/'+type+'.obj', randomInt(105, 150), randomInt(-200, 75), 0.1, false, Math.random() * 2 - 1);321 }322 323 for (var i = 0; i < 250; i++){324 var type = Math.floor(Math.random() * 3)+1;325 objectLoader('obj/tree/'+type+'.mtl', 'obj/tree/'+type+'.obj', randomInt(-200, 150), randomInt(-325, -200), 0.1, false, Math.random() * 2 - 1);326 }327 328 for (var i = 0; i < 200; i++){329 var type = Math.floor(Math.random() * 3)+1;330 objectLoader('obj/tree/'+type+'.mtl', 'obj/tree/'+type+'.obj', randomInt(-245, -200), randomInt(-325, 200), 0.1, false, Math.random() * 2 - 1);331 }332 333 334 objectLoader('obj/farm.mtl', 'obj/farm.obj', -180, 0, 0.01, false, 0);335 objectLoader('obj/factory.mtl', 'obj/factory.obj', 0, -190, 0.01, false, -0.5);336 337 338 339 340 341 342 343 /************************************************************************/344 objectLoader('obj/trash/bottle1.mtl', 'obj/trash/bottle1.obj', 0, 5, 0, true);345 objectLoader('obj/trash/bottle2.mtl', 'obj/trash/bottle2.obj', -5, 5, 0, true);346 objectLoader('obj/trash/bottle3.mtl', 'obj/trash/bottle3.obj', -10, 5, 0, true);347 objectLoader('obj/trash/trash_bag.mtl', 'obj/trash/trash_bag.obj', 0, 10, 0, true);348 objectLoader('obj/trash/trash_can.mtl', 'obj/trash/trash_can.obj', -5, 10, 0, true);349 objectLoader('obj/trash/trash_can_wlid.mtl', 'obj/trash/trash_can_wlid.obj', -10, 10, 0, true);350 objectLoader('obj/trash/trash_dumpster.mtl', 'obj/trash/trash_dumpster.obj', 0, 15, 0, );351 objectLoader('obj/trash/trash_dumpster_open.mtl', 'obj/trash/trash_dumpster_open.obj', -5, 15, 0);352 //objectLoader('obj/character/character.mtl', 'obj/character/character.obj', -10, 15, 0, true);353 //objectLoader('obj/vehicle/truck.mtl', 'obj/vehicle/truck.obj', -20, 15, 0, true);354 355 356 357 358 // spotlight359 {360 spotLight = new THREE.SpotLight(0xffffff, 1);361 spotLight.position.set(player.position.x, player.position.y, player.position.z);362 spotLight.angle = Math.PI / 30;363 spotLight.penumbra = 0.1;364 spotLight.decay = 2;365 spotLight.distance = 200;366 spotLight.castShadow = true;367 spotLight.shadow.mapSize.width = 50;368 spotLight.shadow.mapSize.height = 50;369 spotLight.shadow.camera.near = 10;370 spotLight.shadow.camera.far = 200;371 spotLight.shadow.focus = 2;372 scene.add(spotLight);373 374 const targetObject = spotLightPlayer;375 scene.add(targetObject);376 377 spotLight.target = targetObject;378 }379 380 player.position.x = 0;381 //objectLoader('obj/vehicle/truck.mtl', 'obj/vehicle/truck.obj', -20, 15, 0, false);382 //383 //player.position.x = 0;384 //player.rotation.y += Math.PI * 0.5;385 truck.position.x = -10;386 scene.add(player);387 scene.add(truck);388 control_target = player;389 controls = new THREE.PlayerControls(camera, player, collidableObjects, raycaster);390 controls.init();391 group = new THREE.Group();392 scene.add(group);393 drag_controls = new DragControls(objects, camera, renderer.domElement);394 drag_controls.addEventListener('drag', render);395 // Events396 controls.addEventListener('change', render, false);397 window.addEventListener('resize', onWindowResize, false);398 399 // Final touches400 container.appendChild(renderer.domElement);401 document.body.appendChild(container);402}403function update() {404 //raycaster.set(player.position, new THREE.Vector3(0, 0, 1));405 //console.log(player.position);406 //const intersects = raycaster.intersectObjects(scene.children, true);407 408 409 410 411 412 /*if(intersects.length > 0){413 for(let i = 0; i< intersects.length; i++){414 if(intersects[i].distance < 0.5){415 console.log("we hit something");416 break;417 }418 else{419 //console.log("object is far away")420 };421 }422 423 //intersects[0].object.material.color.set( 0xff0000 );424 }425 else{426 console.log("no collision")427 }*/428 //console.log(intersects.length);429 let directionalLight = scene.getObjectByName("directionalLight");430 let hemisphereLight = scene.getObjectByName("hemisphereLight");431 directionalLight.intensity = GLOBAL_SERVICE_PROVIDER.lightingIntensity;432 hemisphereLight.intensity = GLOBAL_SERVICE_PROVIDER.lightingIntensity;433 controls.update();434 // Drag Control435 document.addEventListener('oncontextmenu', onClick, false);436 //document.addEventListener('onmouseup', onRelease, false);437}438function animate() {439 //raycaster.set(player.position, new THREE.Vector3(0, 0, 1));440 //console.log(player.position);441 //const intersects = raycaster.intersectObjects(scene.children, true);442 /*if(intersects.length > 0){443 for(let i = 0; i< intersects.length; i++){444 if(intersects[i].distance < 0.5){445 console.log("we hit something");446 break;447 }448 else{449 //console.log("object is far away")450 };451 }452 453 //intersects[0].object.material.color.set( 0xff0000 );454 }455 else{456 console.log("no collision")457 }*/458 //console.log(intersects.length);459 //controls.update();460 requestAnimationFrame(animate);461 render();462 requestAnimationFrame(update);463}464function render() {465 // Render Scene466 /*467 if ( drag_controls.enabled )468 controls.enabled = false;469 else470 controls.enabled = true;471 472 */473 spotLight.position.set(player.position.x, player.position.y + 30, player.position.z);474 spotLight.target = player;475 camera.lookAt(control_target);476/* console.log("player : ", player.position.x, player.position.y, player.position.z);477 console.log("truck : ", truck.position.x, truck.position.y, player.position.z);478 console.log("target : ", control_target.name); */479 renderer.clear();480 controls.update();481 renderer.render(scene, camera);482}483function onWindowResize() {484 camera.aspect = window.innerWidth / window.innerHeight;485 camera.updateProjectionMatrix();486 renderer.setSize(window.innerWidth, window.innerHeight);487}488function loadApartment(x = 0.0, z = 0.0, type = 1, floor = 1, rot=-1.0, y = 0.0) {489 type = type.toString();490 objectLoader('obj/apartment/' + type + '/bot.mtl', 'obj/apartment/' + type + '/bot.obj', x, z, y + 0, false, rot);491 var i = 1;492 for (; i < floor; i++)493 objectLoader('obj/apartment/' + type + '/mid.mtl', 'obj/apartment/' + type + '/mid.obj', x, z, y + 3.417 * i, false, rot);494 objectLoader('obj/apartment/' + type + '/top.mtl', 'obj/apartment/' + type + '/top.obj', x, z, y + 3.417 * floor, false, rot);495}496function objectLoader(mtlUrl, objUrl, x, z, y = 0.0, draggable = false, rotation = - 1) {497 //if we wanna use obj outside and they aren't static, we can add it to a list or smth.498 const mtlLoader = new MTLLoader();499 mtlLoader.load(mtlUrl, function (materials) {500 materials.preload();501 const objLoaderExample = new OBJLoader();502 objLoaderExample.setMaterials(materials);503 objLoaderExample.load(objUrl, (root) => {504 root.rotation.y = Math.PI * rotation;505 root.position.x = x;506 root.position.y = y;507 root.position.z = z;508 collidableObjects.push(root);509 if (objUrl === 'obj/trash/trash_bag.obj'){510 root.isCollectible = true;511 root.collectedValue = 4;512 trash.push(root);513 }514 else if (objUrl === 'obj/trash/bottle2.obj'){515 root.isCollectible = true;516 root.collectedValue = 2;517 trash.push(root);518 }519 else if (objUrl === 'obj/trash/bottle1.obj'){520 root.isCollectible = true;521 root.collectedValue = 2;522 trash.push(root);523 }524 else if (objUrl === 'obj/trash/bottle3.obj'){525 root.isCollectible = true;526 root.collectedValue = 2;527 trash.push(root);528 }529 else if (objUrl === ' obj/field/poo.obj'){530 root.isCollectible = true;531 root.collectedValue = 1;532 trash.push(root);533 }534 if(objUrl === 'obj/factory.obj'){535 factory.push(root);536 }537 538 if (draggable)539 objects.push(root);540 if ( objUrl === 'obj/vehicle/truck.obj')541 truck.add(root);542 else543 scene.add(root);544 545 546 547 });548 });549}550document.addEventListener('keydown', function (event) {551 552 // add to collect datas553 if (event.keyCode === 76)554 {555 if (spotLight.intensity === 1)556 spotLight.intensity = 0;557 else if(spotLight.intensity === 0)558 spotLight.intensity = 1;559 }560 561 var targetChanged = true;562 563 if (event.keyCode == 70) {564 if (control_target == player)565 {566 if(playerCanMountTruck()){567 control_target = truck;568 player.visible = false;569 player.position.x = truck.position.x;570 player.position.y = truck.position.y;571 player.position.z = truck.position.z;572 }573 else{574 targetChanged = false;575 }576 } 577 else578 {579 control_target = player;580 player.visible = true;581 player.position.x = truck.position.x - 8;582 player.position.y = truck.position.y;583 player.position.z = truck.position.z;584 }585 if(targetChanged){586 controls = new THREE.PlayerControls(camera, control_target, collidableObjects, raycaster);587 }588 //controls.init();589 render();590 }591 if(event.keyCode == 88){592 pickTrash();593 }594 }, true);595 function onRelease(event)596 {597 drag_controls.enabled = false;598 controls.enabled = true;599}600function onClick(event) {601 event.preventDefault();602 controls.enabled = false;603 if (enableSelection === true) {604 const draggableObjects = drag_controls.getObjects();605 draggableObjects.length = 0;606 mouse.x = (event.clientX / window.innerWidth) * 2 - 1;607 mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;608 raycaster.setFromCamera(mouse, camera);609 const intersections = raycaster.intersectObjects(objects, true);610 if (intersections.length > 0) {611 const object = intersections[ 0 ].object;612 if (group.children.includes(object) === true) {613 object.material.emissive.set(0x000000);614 scene.attach(object);615 } else {616 object.material.emissive.set(0xaaaaaa);617 group.attach(object);618 }619 drag_controls.transformGroup = true;620 draggableObjects.push(group);621 }622 if (group.children.length === 0) {623 drag_controls.transformGroup = false;624 draggableObjects.push(...objects);625 }626 }627 render();628}629function playerCanMountTruck(){630 631 const rays = [632 new THREE.Vector3(0, 1, 1),633 new THREE.Vector3(1, 1, 1),634 new THREE.Vector3(1, 1, 0),635 new THREE.Vector3(1, 1, -1),636 new THREE.Vector3(0, 1, -1),637 new THREE.Vector3(-1, 1, -1),638 new THREE.Vector3(-1, 1, 0),639 new THREE.Vector3(-1, 1, 1)640 ];641 var distance = 30;642 var nearTruck = false;643 644 645 646 var playerDirection = new THREE.Vector3();647 player.getWorldDirection(playerDirection);648 player.worldDirection = playerDirection;649 650 for (let i = 0; i < rays.length; i += 1) {651 // We reset the raycaster to this direction652 raycaster.set(player.position, rays[i]);653 // Test if we intersect with any obstacle mesh654 const intersects = raycaster.intersectObjects([truck], true);655 // And disable that direction if we do656 if (intersects.length > 0 && intersects[0].distance <= distance) {657 // Yep, this.rays[i] gives us : 0 => up, 1 => up-left, 2 => left, ...658 659 nearTruck = true;660 661 662 }663 }664 665 return nearTruck;666 }667 668function randomInt(min, max) { // min and max included 669 return Math.round(Math.random() * (max - min + 1) + min);670}671// random waste generator672setInterval(function(){673 var rand = Math.floor(Math.random() * 4)674 var rand_x = Math.floor(Math.random() * 140) -70;675 var rand_y = 0;676 var rand_z = Math.floor(Math.random() * 300) -180;677 console.log(rand_x, rand_z);678 if (rand == 0)679 {objectLoader('obj/trash/trash_bag.mtl', 'obj/trash/trash_bag.obj', rand_x, rand_z, rand_y, true);}680 if (rand == 1)681 {objectLoader('obj/trash/bottle2.mtl', 'obj/trash/bottle2.obj', rand_x, rand_z, rand_y, true);}682 if (rand == 2)683 {objectLoader('obj/trash/bottle1.mtl', 'obj/trash/bottle1.obj', rand_x, rand_z, rand_y, true);}684 if (rand == 3)685 { objectLoader('obj/trash/bottle3.mtl', 'obj/trash/bottle3.obj', rand_x, rand_z, rand_y, true);}686 }, 800);687 688 setInterval(function(){689 var rand_x = Math.floor(Math.random() * 100) + -170;690 var rand_z = Math.floor(Math.random() * 30) -40;691 objectLoader('obj/field/poo.mtl', 'obj/field/poo.obj', rand_x, rand_z, 0, true);692 }, 1000);693 function pickTrash(){694 const rays = [695 new THREE.Vector3(0, 1, 1),696 new THREE.Vector3(1, 1, 1),697 new THREE.Vector3(1, 1, 0),698 new THREE.Vector3(1, 1, -1),699 new THREE.Vector3(0, 1, -1),700 new THREE.Vector3(-1, 1, -1),701 new THREE.Vector3(-1, 1, 0),702 new THREE.Vector3(-1, 1, 1)703 ];704 var distance = 30;705 706 707 708 var playerDirection = new THREE.Vector3();709 player.getWorldDirection(playerDirection);710 player.worldDirection = playerDirection;711 712 let addToTruck = false;713 for (let i = 0; i < rays.length; i += 1) {714 // We reset the raycaster to this direction715 raycaster.set(player.position, rays[i]);716 // Test if we intersect with any obstacle mesh717 const intersects = raycaster.intersectObjects(trash, true);718 // And disable that direction if we do719 if (intersects.length > 0 && intersects[0].distance <= distance) {720 // Yep, this.rays[i] gives us : 0 => up, 1 => up-left, 2 => left, ...721 addToTruck = true;722 intersects[0].object.visible = false;723 724 }725 }726 if(addToTruck){727 if(targetProxy.truckLoad <=17){728 targetProxy.truckLoad +=3;729 730 }731 732 }733 for (let i = 0; i < rays.length; i += 1) {734 // We reset the raycaster to this direction735 raycaster.set(player.position, rays[i]);736 // Test if we intersect with any obstacle mesh737 const intersects = raycaster.intersectObjects(factory, true);738 // And disable that direction if we do739 if (intersects.length > 0 && intersects[0].distance <= distance) {740 // Yep, this.rays[i] gives us : 0 => up, 1 => up-left, 2 => left, ...741 targetProxy.unload = true;742 743 }744 }745 746 }...
stone.spec.ts
Source:stone.spec.ts
...5describe("Stone tester should", function () {6 this.timeout(15000);7 let objectLoader: ObjectLoader;8 before(() => {9 objectLoader = new ObjectLoader();10 });11 it("load red stone", done => {12 Stone.createStone(objectLoader, StoneColor.Red, new Vector3(0, 0, 0)).then((stone: Stone) => {13 expect(stone).to.be.instanceof(Stone);14 expect(stone.stoneColor).to.equals(StoneColor.Red);15 done();16 }).catch(() => {17 console.log("rip");18 });19 });20 it("load blue stone", done => {21 Stone.createStone(objectLoader, StoneColor.Blue, new Vector3(0, 0, 0)).then((stone: Stone) => {22 expect(stone).to.be.instanceof(Stone);23 stone.sweeping = true;24 expect(stone.isSweeping).to.equals(true);25 expect(stone.stoneColor).to.equals(StoneColor.Blue);26 done();27 }).catch(() => {28 console.log("rip");29 });30 });31 it("build stone with a clockwise spin then invert its spin", done => {32 Stone.createStone(objectLoader, StoneColor.Blue, new Vector3(0, 0, 0)).then((stone: Stone) => {33 expect(stone).to.be.instanceof(Stone);34 expect(stone.spin).to.equals(StoneSpin.Clockwise);35 stone.spin = StoneSpin.CounterClockwise;36 expect(stone.spin).to.equals(StoneSpin.CounterClockwise);37 expect(stone.stoneColor).to.equals(StoneColor.Blue);38 done();39 });40 });41 it("Make a stone bounce up and down", done => {42 let subject = new Subject();43 Stone.createStone(objectLoader, StoneColor.Blue, new Vector3(0, 0, 0)).then((stone: Stone) => {44 let observer = stone.bounce();45 subject.subscribe(observer);46 expect(stone.position.y).to.equal(0);47 subject.next();48 expect(stone.position.y > 0).to.be.true;49 subject.complete();50 expect(stone.position.y).to.equal(0);51 done();52 });53 });54 it('contains a group of Glow component in a Blue Stone', (done) => {55 Stone.createStone(objectLoader, StoneColor.Blue, new Vector3(0, 0, 0))56 .then((stone: Stone) => {57 stone.setIllumination(true);58 // Should contains the glow object59 let glow = stone.getStoneGlowObject();60 assert(glow.visible === true);61 stone.setIllumination(false);62 done();63 })64 .catch((error: any) => {65 console.log("error", error);66 });67 });68 it('contains a group of Glow component in a Red Stone', (done) => {69 Stone.createStone(objectLoader, StoneColor.Blue, new Vector3(0, 0, 0))70 .then((stone: Stone) => {71 stone.setIllumination(true);72 let glow = stone.getStoneGlowObject();73 assert(glow.visible === true);74 done();75 })76 .catch((error: any) => {77 console.log("error", error);78 });79 });80 it('remove a group of Glow component in a Stone', (done) => {81 Stone.createStone(objectLoader, StoneColor.Blue, new Vector3(0, 0, 0))82 .then((stone: Stone) => {83 stone.setIllumination(true);84 let glow = stone.getStoneGlowObject();85 assert(glow.visible === true);86 stone.setIllumination(false);87 let glowOff = stone.getStoneGlowObject();88 // expect(glowOff).to.be.undefined;89 done();90 })91 .catch((error: any) => {92 console.log("error when stopping the illumination", error);93 });94 });95});96describe("Stone tester physics should", () => {97 let objectLoader: ObjectLoader;98 let stone: Stone;99 let initialPosition: Vector3;100 let frameNumber: number;101 let totalNumberOfFrames: number;102 let timePerFrame: number;103 before(() => {104 objectLoader = new ObjectLoader();105 totalNumberOfFrames = 20;106 timePerFrame = 1 / totalNumberOfFrames;107 initialPosition = new Vector3(0, 0, 0);108 });109 beforeEach(done => {110 frameNumber = 0;111 Stone.createStone(objectLoader, StoneColor.Red, initialPosition).then((stoneCreated: Stone) => {112 stone = stoneCreated;113 done();114 });115 });...
broom.spec.ts
Source:broom.spec.ts
...5describe("Broom class should", () => {6 let objectLoader: ObjectLoader;7 let scene: Scene;8 before(() => {9 objectLoader = new ObjectLoader();10 scene = new Scene();11 });12 it("instantiate a broom object", () => {13 Broom.createBroom(objectLoader, scene, new Vector3(0, 0, -11.4)).then((broom: Broom) => {14 expect(broom).to.be.instanceof(Broom);15 });16 });17 it("change a broom to red correctly", () => {18 Broom.createBroom(objectLoader, scene, new Vector3(0, 0, -11.4)).then((broom: Broom) => {19 broom.changeColourTo(THREE.ColorKeywords.red);20 expect(broom.isRed()).to.be.true;21 });22 });23 it("change a broom to red correctly", () => {...
Using AI Code Generation
1var wptools = require('wptools');2var objLoader = new wptools.ObjectLoader();3obj.then(function (obj) {4 console.log(obj);5});6var wptools = require('wptools');7var objLoader = new wptools.ObjectLoader();8var obj = objLoader.load('Barack_Obama');9obj.then(function (obj) {10 console.log(obj);11});12var wptools = require('wptools');13var objLoader = new wptools.ObjectLoader();14var obj = objLoader.load('Barack Obama');15obj.then(function (obj) {16 console.log(obj);17});18var wptools = require('wptools');19var objLoader = new wptools.ObjectLoader();20var obj = objLoader.load('Barack Obama', 'en');21obj.then(function (obj) {22 console.log(obj);23});24var wptools = require('wptools');25var objLoader = new wptools.ObjectLoader();26var obj = objLoader.load('Barack Obama', 'en', 'wikipedia');27obj.then(function (obj) {28 console.log(obj);29});30var wptools = require('wptools');31var objLoader = new wptools.ObjectLoader();32var obj = objLoader.load('Barack Obama', 'en', 'wikipedia', 'wikipedia');33obj.then(function (obj) {34 console.log(obj);35});36var wptools = require('wptools');37var objLoader = new wptools.ObjectLoader();38var obj = objLoader.load('Barack Obama', 'en', 'wikipedia', 'wikipedia', 'wikipedia');39obj.then(function (obj) {40 console.log(obj);41});
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Albert Einstein');3page.get(function(err, resp) {4 console.log(resp);5});6 at ObjectLoader.get (/Users/rohitkumar/Documents/Projects/wptools/lib/object_loader.js:87:13)7 at ObjectLoader.<anonymous> (/Users/rohitkumar/Documents/Projects/wptools/lib/object_loader.js:41:8)8 at ObjectLoader.emit (events.js:107:17)9 at ObjectLoader.<anonymous> (/Users/rohitkumar/Documents/Projects/wptools/lib/object_loader.js:31:10)10 at ObjectLoader.emit (events.js:107:17)11 at ObjectLoader.<anonymous> (/Users/rohitkumar/Documents/Projects/wptools/lib/object_loader.js:29:10)12 at ObjectLoader.emit (events.js:107:17)13 at ObjectLoader.<anonymous> (/Users/rohitkumar/Documents/Projects/wptools/lib/object_loader.js:27:10)14 at ObjectLoader.emit (events.js:107:17)15 at ObjectLoader.<anonymous> (/Users/rohitkumar/Documents/Projects/wptools/lib/object_loader.js:25:10)16var wptools = require('wptools');17var page = wptools.page('Albert Einstein', {api_endpoint: 'wikipedia.org/w/api.php'});18page.get(function(err, resp) {19 console.log(resp);20});
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var path = require('path');4var obj = new wptools();5var file = fs.createWriteStream('test.json');6var file2 = fs.createWriteStream('test2.json');7var file3 = fs.createWriteStream('test3.json');8var file4 = fs.createWriteStream('test4.json');9var file5 = fs.createWriteStream('test5.json');10var file6 = fs.createWriteStream('test6.json');11var file7 = fs.createWriteStream('test7.json');12var file8 = fs.createWriteStream('test8.json');13var file9 = fs.createWriteStream('test9.json');14var file10 = fs.createWriteStream('test10.json');15var file11 = fs.createWriteStream('test11.json');16var file12 = fs.createWriteStream('test12.json');17var file13 = fs.createWriteStream('test13.json');18var file14 = fs.createWriteStream('test14.json');19var file15 = fs.createWriteStream('test15.json');20var file16 = fs.createWriteStream('test16.json');21var file17 = fs.createWriteStream('test17.json');22var file18 = fs.createWriteStream('test18.json');23var file19 = fs.createWriteStream('test19.json');24var file20 = fs.createWriteStream('test20.json');25var file21 = fs.createWriteStream('test21.json');26var file22 = fs.createWriteStream('test22.json');27var file23 = fs.createWriteStream('test23.json');28var file24 = fs.createWriteStream('test24.json');29var file25 = fs.createWriteStream('test25.json');30var file26 = fs.createWriteStream('test26.json');31var file27 = fs.createWriteStream('test27.json');32var file28 = fs.createWriteStream('test28.json');33var file29 = fs.createWriteStream('test29.json');34var file30 = fs.createWriteStream('test30.json');35var file31 = fs.createWriteStream('test31.json');36var file32 = fs.createWriteStream('test32.json');37var file33 = fs.createWriteStream('test33.json');38var file34 = fs.createWriteStream('test34.json');39var file35 = fs.createWriteStream('test35.json');40var file36 = fs.createWriteStream('test36
Using AI Code Generation
1var obj = new ObjectLoader();2obj.prop1 = 'value1';3obj.prop2 = 'value2';4obj.prop3 = 'value3';5var obj2 = new ObjectLoader();6obj2.load(obj);
Using AI Code Generation
1const wptools = require('wptools');2const fs = require('fs');3var json = JSON.parse(fs.readFileSync('test.json', 'utf8'));4var loader = wptools.loader(json);5loader.get(function(err, page) {6 if (err) {7 console.log('Error: ' + err);8 return;9 }10 console.log('Title: ' + page.title());11 console.log('Content: ' + page.content());12 console.log('Categories: ' + page.categories());13 console.log('Links: ' + page.links());14 console.log('Images: ' + page.images());15 console.log('Infobox: ' + page.infobox());16 console.log('Tables: ' + page.tables());17 console.log('Coordinates: ' + page.coordinates());18 console.log('References: ' + page.references());19 console.log('External Links: ' + page.external_links());20 console.log('Language Links: ' + page.langlinks());21 console.log('Sections: ' + page.sections());22 console.log('Templates: ' + page.templates());23});
Using AI Code Generation
1var wpt = require('webpagetest');2var client = wpt('www.webpagetest.org');3var testId = '140301_4Y_7f2a4f4c7b4a2b2f8ed2f2e0a7a1f1fc';4var test = client.getTestStatus(testId);5console.log(test);6var wpt = require('webpagetest');7var client = wpt('www.webpagetest.org');8var testId = '140301_4Y_7f2a4f4c7b4a2b2f8ed2f2e0a7a1f1fc';9var test = client.getTestResults(testId);10console.log(test);11var wpt = require('webpagetest');12var client = wpt('www.webpagetest.org');13var testId = '140301_4Y_7f2a4f4c7b4a2b2f8ed2f2e0a7a1f1fc';14var test = client.getTestResults(testId);15console.log(test.data.median.firstView);16var wpt = require('webpagetest');17var client = wpt('www.webpagetest.org');18var testId = '140301_4Y_7f2a4f4c7b4a2b2f8ed2f2e0a7a1f1fc';19var test = client.getTestResults(testId);20console.log(test.data.median.firstView.requests);21var wpt = require('webpagetest');22var client = wpt('www.webpagetest.org');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!