Skip to content

Commit

Permalink
#32 - Refactoring main script file, created additional helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbrzeski committed Jun 30, 2024
1 parent 025bd41 commit 42b4bb4
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 335 deletions.
2 changes: 1 addition & 1 deletion client/src/app/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class Controls {

touch;

constructor() {
init() {
this.keyboard = new KeyboardControls();

if ( window.l.config.debug ) {
Expand Down
78 changes: 42 additions & 36 deletions client/src/app/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,53 @@
import * as THREE from 'three';

import { createOfficeRoom } from './scene_assets/office_room';
import { calculateAdjustedGapSize, setCameraFOV } from './main.js';
import { calculateAdjustedGapSize, setCameraFOV } from './helpers/math.js';

import { resetReusables } from './scene_assets/tweens.js';

export default class Events {
constructor() {

}
}

export async function handleViewportChange() {
window.l.current_scene.settings.adjusted_gap = calculateAdjustedGapSize();
window.l.current_scene.room_depth = 8 * window.l.current_scene.settings.adjusted_gap;

var width = window.innerWidth;
var height = window.innerHeight;

window.l.current_scene.renderers.webgl.setSize(width, height);

if (window.l.current_scene.effects.postprocessing && window.l.current_scene.effects.postprocessing.passes.length > 0)
window.l.current_scene.effects.postprocessing.setSize(width, height);


window.l.current_scene.camera.aspect = width / height;

window.l.current_scene.camera.fov = setCameraFOV(window.l.current_scene.camera.aspect);
if (!window.l.current_scene.selected && ! window.l.current_scene.moving) {
let posZ = -20;
//window.l.current_scene.camera.position.z = posZ + (window.l.current_scene.room_depth / 2);
//window.l.current_scene.camera.rotation.x = - (Math.PI / 30) * window.l.current_scene.camera.aspect;
}
window.l.current_scene.camera.updateProjectionMatrix();

const newRoom = await createOfficeRoom();
window.l.current_scene.scene_objects.room.geometry = newRoom.geometry;

if ( ! window.l.current_scene.started ) {
if (window.l.current_scene.camera.aspect < 0.88) {
window.l.current_scene.settings.startPosZ = -5;
window.l.current_scene.settings.adjusted_gap = calculateAdjustedGapSize();
window.l.current_scene.room_depth = 8 * window.l.current_scene.settings.adjusted_gap;

var width = window.innerWidth;
var height = window.innerHeight;

window.l.current_scene.renderers.webgl.setSize( width, height );

if ( window.l.current_scene.effects.postprocessing && window.l.current_scene.effects.postprocessing.passes.length > 0 )
window.l.current_scene.effects.postprocessing.setSize( width, height );


window.l.current_scene.camera.aspect = width / height;

window.l.current_scene.camera.fov = setCameraFOV( window.l.current_scene.camera.aspect );
if ( !window.l.current_scene.selected && !window.l.current_scene.moving ) {
let posZ = -20;
//window.l.current_scene.camera.position.z = posZ + (window.l.current_scene.room_depth / 2);
//window.l.current_scene.camera.rotation.x = - (Math.PI / 30) * window.l.current_scene.camera.aspect;
}
window.l.current_scene.camera.updateProjectionMatrix();

const newRoom = await createOfficeRoom();
window.l.current_scene.scene_objects.room.geometry = newRoom.geometry;

if ( !window.l.current_scene.started ) {
if ( window.l.current_scene.camera.aspect < 0.88 ) {
window.l.current_scene.settings.startPosZ = -5;
}
else {
window.l.current_scene.settings.startPosZ = -10;
}
}
else {
window.l.current_scene.settings.startPosZ = -10;

window.l.current_scene.scene_objects.door.position.z = - 15 + ( window.l.current_scene.room_depth / 2 );
window.l.current_scene.scene_objects.door_frame.position.z = - 15 + ( window.l.current_scene.room_depth / 2 );
}
}
else {

window.l.current_scene.scene_objects.door.position.z = - 15 + (window.l.current_scene.room_depth / 2);
window.l.current_scene.scene_objects.door_frame.position.z = - 15 + (window.l.current_scene.room_depth / 2);
}
}
13 changes: 13 additions & 0 deletions client/src/app/helpers/l.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Provides an interface to the core "l" variable, attached to the browser window.
*/

// Check if window.l is defined
if (typeof window.l === 'undefined') {
throw new Error('window.l is not defined');
}

// Create a shortcut to window.l
const l = window.l;

export default l;
62 changes: 62 additions & 0 deletions client/src/app/helpers/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Set camera FOV based on desired aspect ratio
*
* @param {Float} aspect
* @returns {Float} fov
*/
export function setCameraFOV( aspect ) {
var fov;

var threshold = 0.88;

if ( aspect < threshold ) {
// Portrait or square orientation
fov = mapRange( aspect, 0.5, threshold, 60, 60 );
} else {
// Widescreen orientation
if ( aspect < 2 ) {
// Tolerance for square to widescreen transition
fov = mapRange( aspect, threshold, 2, 60, 45 );
} else {
if ( aspect < 2.25 ) {
fov = mapRange( aspect, 2, 2.25, 45, 40 );
} else {
if ( aspect < 3 ) {
fov = mapRange( aspect, 2.25, 5, 40, 90 );
} else {
fov = 90;
}
}
}
}

return fov;
}



/**
* Function to map a value from one range to another
*/
export function mapRange( value, inMin, inMax, outMin, outMax ) {
return ( ( value - inMin ) * ( outMax - outMin ) ) / ( inMax - inMin ) + outMin;
}

/**
* Helper to calculate the desired gap size
*
* Used to determine office room dimensions on different screen sizes/aspect ratios.
*/
export function calculateAdjustedGapSize() {
var width = window.innerWidth;
var height = window.innerHeight;

// Adjust gap size based on the aspect ratio
var adjustedGapSize =
l.current_scene.settings.gap * l.current_scene.settings.scale;
if ( width < height ) {
adjustedGapSize *= height / width;
}

return adjustedGapSize;
}
23 changes: 23 additions & 0 deletions client/src/app/helpers/select_box.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import l from './l.js';

l.select_box = function ( object ) {
// Remove any existing select boxes
if ( l.current_scene.scene_objects.select_box ) {
l.current_scene.scene.remove( l.current_scene.scene_objects.select_box );
delete l.current_scene.scene_objects.select_box;
}

const box = new THREE.BoxHelper( object, 0xffff00 );
l.current_scene.scene_objects.select_box = box;
l.current_scene.scene.add( l.current_scene.scene_objects.select_box );

l.current_scene.animation_queue.push(
l.select_box_update
);
}

l.select_box_update = function () {
l.current_scene.scene_objects.select_box.position.copy( l.current_scene.scene_objects.select_box.object.position );
l.current_scene.scene_objects.select_box.rotation.copy( l.current_scene.scene_objects.select_box.object.rotation );
l.current_scene.scene_objects.select_box.update();
}
Loading

0 comments on commit 42b4bb4

Please sign in to comment.