import * as THREE from 'three'; import * as MathUtils from 'three/src/math/MathUtils.js'; import { createGround } from '../objects/ground.js'; import {loadModel} from './import.js'; function alignGround(ground, MovingObject) { /*Function used to place an object on top of another object. In fact, this is used only to place correctly objects on the ground*/ const boundingBoxGround = new THREE.Box3().setFromObject(ground); const boundingBoxMovingObject = new THREE.Box3().setFromObject(MovingObject); const groundHeight = boundingBoxGround.max.z - boundingBoxGround.min.z; const objectHeight = boundingBoxMovingObject.max.z - boundingBoxMovingObject.min.z; // Calculating offset, so this function can work with every center position const verticalOffset = (boundingBoxMovingObject.max.z + boundingBoxMovingObject.min.z) / 2; MovingObject.position.z = boundingBoxGround.max.z - verticalOffset + objectHeight / 2; } export async function addAssetsOnScene(scene){ /*This function takes the scene as arg, and is used to load everything needed on the terrain. Player, trees, fences, and everything displayed on the screen is added by this function, at the right place Will return an object containing created objects*/ scene.add(new THREE.AmbientLight(0x404040, 50));//Light, required to see /* -------------------- ADDING GROUND AND FENCES -------------------- */ let fences = [];//List of created fences const fenceModel = await loadModel('src/objects/models/fence.gltf'); //Getting width of one fence fenceModel.scene.scale.set(0.05, 0.05, 0.05); fenceModel.scene.rotateOnWorldAxis(new THREE.Vector3(1, 0, 0), MathUtils.degToRad(90));//Must be standing up const fenceHitBox = new THREE.Box3().setFromObject(fenceModel.scene); const fenceWidth = fenceHitBox.max.y - fenceHitBox.min.y; //Creating the ground const ground = createGround(35*fenceWidth, 35*fenceWidth); scene.add(ground); const groundHitBox = new THREE.Box3().setFromObject(ground); // Calculating ground size const groundXSize = groundHitBox.max.x - groundHitBox.min.x; const groundySize = groundHitBox.max.y - groundHitBox.min.y; for(let i = 0; i