diff --git a/src/main.js b/src/main.js index a203e05..9ba4472 100644 --- a/src/main.js +++ b/src/main.js @@ -4,6 +4,7 @@ import {addAssetsOnScene} from './utils/addAssetsOnScene.js'; import * as PlayerMovement from './utils/playerMovement.js'; import * as Animations from './utils/animateMesh.js'; let Clock = new THREE.Clock(); +THREE.Cache.enabled = true;//So we can request several time the same file without worrying : https://threejs.org/docs/#api/en/loaders/FileLoader /* --------------------- */ /* | SCENE CREATION | */ diff --git a/src/utils/addAssetsOnScene.js b/src/utils/addAssetsOnScene.js index 6d8c171..6a5dec1 100644 --- a/src/utils/addAssetsOnScene.js +++ b/src/utils/addAssetsOnScene.js @@ -4,15 +4,20 @@ import { createGround } from '../objects/ground.js'; import {loadModel} from './import.js'; -function alignGround(ground, MovingObject){ +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 MovingObjectHeight = boundingBoxMovingObject.max.z - boundingBoxMovingObject.min.z; - MovingObject.position.y = MovingObjectHeight/2 + boundingBoxGround.max.z; + 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 @@ -32,15 +37,60 @@ export async function addAssetsOnScene(scene){ const groundySize = groundHitBox.max.y - groundHitBox.min.y; let fences = [];//List of created fences - const fence = await loadModel('src/objects/models/fence.gltf');//Loading fence model - fence.scene.scale.set(0.05,0.05,0.05); - fence.scene.rotateOnWorldAxis(new THREE.Vector3(1,0,0), MathUtils.degToRad(90));//Must be standing up - - const fenceHitBox = new THREE.Box3().setFromObject(fence.scene); + 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)); + const fenceHitBox = new THREE.Box3().setFromObject(fenceModel.scene); const fenceWidth = fenceHitBox.max.y - fenceHitBox.min.y; - alignGround(ground, fence.scene); - scene.add(fence.scene); + for(let i = 0; i