Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-introduce camera shake #267

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/common/default-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const defaultSettingsV6: SettingsV6 = {
sceneController: "@titan-reactor-plugins/auto-observer",
vrController: "@titan-reactor-plugins/vr-controller",
dampingFactor: 0.1,
cameraShakeStrength: 1,
cameraShakeStrength: 5,
movementSpeed: 1.15,
rotateSpeed: 0.15,
zoomLevels: [ 0.5, 1, 2 ],
Expand Down
3 changes: 2 additions & 1 deletion src/common/get-app-settings-leva-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ const getInputConfig = (
label: "Camera Shake Strength",
value: input.cameraShakeStrength,
min: 0,
max: 1,
max: 20,
step: 0.5
},
"input.unitSelection": {
label: "Enable Unit Selection",
Expand Down
1 change: 0 additions & 1 deletion src/core/model-effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const overlayEffectsMainImage: { image: Image3D | null } = { image: null
export const applyRenderModeToSprite = (
spriteTypeId: number,
sprite: SpriteType,
terrainY: number
) => {
sprite.rotation.x = 0;
if ( modelSetModifiers.sprites[spriteTypeId] ) {
Expand Down
15 changes: 8 additions & 7 deletions src/core/world/openbw-composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {
speedHandler,
} from "@openbw/speed-handler";
import { buildSound } from "@utils/sound-utils";
import { floor32 } from "common/utils/conversions";
import { SceneComposer } from "./scene-composer";
import { PxToWorld, floor32 } from "common/utils/conversions";
import { MathUtils } from "three";
import { createCompletedUpgradesHelper } from "@openbw/completed-upgrades";
import { ViewControllerComposer } from "@core/world/view-controller-composer";
import { World } from "./world";
import { Timer } from "@utils/timer";
import { borrow, Borrowed } from "@utils/object-utils";
import { SimpleBufferView } from "@openbw/structs/simple-buffer-view";
import { Creep } from "..";

export type OpenBwComposer = ReturnType<typeof createOpenBWComposer>;
export type OpenBwComposerApi = OpenBwComposer["api"];
Expand All @@ -31,7 +31,8 @@ export type OpenBwComposerApi = OpenBwComposer["api"];
*/
export const createOpenBWComposer = (
world: World,
scene: Pick<SceneComposer, "pxToWorld" | "terrainExtra">,
pxToWorld: PxToWorld,
creep: Creep,
viewInput: ViewControllerComposer
) => {
let _currentFrame = 0;
Expand All @@ -55,7 +56,7 @@ export const createOpenBWComposer = (
y,
typeId,
unitTypeId,
scene.pxToWorld,
pxToWorld,
viewInput.primaryViewport!.audioType,
viewInput.primaryViewport!.projectedView,
soundChannels
Expand Down Expand Up @@ -91,7 +92,7 @@ export const createOpenBWComposer = (
const buildCreep = ( frame: number ) => {
_tiles.address = world.openBW.getTilesPtr();
_tiles.viewSize = world.openBW.getTilesSize();
scene.terrainExtra.creep.generate( _tiles, frame );
creep.generate( _tiles, frame );
};

let lastElapsed = 0;
Expand All @@ -113,7 +114,7 @@ export const createOpenBWComposer = (
y,
typeId,
unitTypeId,
scene.pxToWorld,
pxToWorld,
viewInput.primaryViewport!.audioType,
viewInput.primaryViewport!.projectedView,
soundChannels
Expand Down Expand Up @@ -141,7 +142,7 @@ export const createOpenBWComposer = (
world.openBW.generateFrame();
_tiles.address = world.openBW.getTilesPtr();
_tiles.viewSize = world.openBW.getTilesSize();
scene.terrainExtra.creep.generateImmediate( _tiles );
creep.generateImmediate( _tiles );
},
update( elapsed: number, frame: number ) {
lastElapsed = elapsed;
Expand Down
9 changes: 6 additions & 3 deletions src/core/world/overlay-composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Mesh,
PlaneGeometry,
Raycaster,
Texture,
Vector2,
} from "three";
import { SceneComposer } from "./scene-composer";
Expand All @@ -24,6 +25,7 @@ import { SurfaceComposer } from "./surface-composer";
import { PostProcessingComposer } from "./postprocessing-composer";
import { InputsComposer } from "./input-composer";
import { ViewControllerComposer } from "./view-controller-composer";
import { Creep } from "..";

export type OverlayComposer = {
api: {
Expand All @@ -47,13 +49,14 @@ const white = new Color( 0xffffff );
export const createOverlayComposer = (
world: World,
{
terrainExtra,
units,
}: SceneComposer,
surfaces: SurfaceComposer,
inputs: InputsComposer,
post: PostProcessingComposer,
viewports: ViewControllerComposer,
creep: Creep,
minimapTex: Texture,
assets: Assets
): OverlayComposer => {
const janitor = new Janitor( "OverlayComposer" );
Expand Down Expand Up @@ -82,7 +85,7 @@ export const createOverlayComposer = (

const minimapMaterial = new MinimapMaterial(
...world.map.size,
terrainExtra.minimapTex
minimapTex
);

const minimap = new Mesh( new PlaneGeometry( 1, 1 ), minimapMaterial );
Expand Down Expand Up @@ -313,7 +316,7 @@ export const createOverlayComposer = (
onFrame() {
minimapMaterial.update(
world.fogOfWar.buffer,
terrainExtra.creep.minimapImageData,
creep.minimapImageData,
world.fogOfWar.effect.opacity
);

Expand Down
6 changes: 4 additions & 2 deletions src/core/world/postprocessing-composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ViewControllerComposer } from "@core/world/view-controller-composer";
import { World } from "./world";
import { isImageHd, isMesh } from "@utils/image-utils";
import { createTransition } from "./transition";
import { Terrain } from "@core/terrain";

//tank base, minerals
const ignoreRecieveShadow = [250, 253, 347, 349, 351];
Expand All @@ -27,8 +28,9 @@ export type PostProcessingComposerApi = PostProcessingComposer["api"];
// Renders each viewport.
export const createPostProcessingComposer = (
world: World,
{ scene, images, sprites, terrain, ...sceneComposer }: SceneComposer,
{ scene, images, sprites, ...sceneComposer }: SceneComposer,
viewportsComposer: ViewControllerComposer,
terrain: Terrain,
assets: Assets
) => {
const janitor = new Janitor("PostProcessingComposer");
Expand Down Expand Up @@ -147,7 +149,7 @@ export const createPostProcessingComposer = (
return {
precompile(camera: PerspectiveCamera | OrthographicCamera) {
_changeRenderMode(false);
sceneComposer.onFrame(0, 0, false);
sceneComposer.onFrame(0, false);

renderComposer.glRenderer.compile(scene, camera);
},
Expand Down
74 changes: 23 additions & 51 deletions src/core/world/scene-composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from "@core/model-effects";
import { SpriteEntities } from "@core/sprite-entities";
import { UnitEntities } from "@core/unit-entities";
import { terrainComposer } from "@image/generate-map/terrain-composer";
import BaseScene from "@render/base-scene";
import {
imageIsDoodad,
Expand All @@ -24,72 +23,51 @@ import {
import { Janitor, JanitorLogLevel } from "three-janitor";
import { spriteIsHidden, spriteSortOrder } from "@utils/sprite-utils";
import { calculateFollowedUnitsTarget, unitIsCompleted, unitIsFlying } from "@utils/unit-utils";
import { drawFunctions, imageTypes, unitTypes } from "common/enums";
import { ImageStruct, UnitStruct, UnitTileScale } from "common/types";
import { drawFunctions, imageTypes } from "common/enums";
import { ImageStruct, UnitStruct } from "common/types";
import { Assets } from "@image/assets";
import { floor32, makePxToWorld } from "common/utils/conversions";
import { PxToWorld, floor32 } from "common/utils/conversions";
import { Color, MathUtils, Vector2, Vector3 } from "three";
import { World } from "./world";
import { Unit } from "@core/unit";
import { IterableSet } from "@utils/data-structures/iterable-set";
import { borrow, Borrowed } from "@utils/object-utils";
import { getJanitorLogLevel } from "@ipc/global";
import { getMapTiles } from "@utils/chk-utils";
import { ImageBase } from "..";
import { ImageHDMaterial } from "@core/image-hd-material";
import { calculateImagesFromTechTreeUnit } from "@utils/preload-map-units-and-sprites";
import { IterableMap } from "@utils/data-structures/iteratible-map";
import { SimpleQuadtree } from "@utils/data-structures/simple-quadtree";
import { settingsStore } from "@stores/settings-store";
import { HeightMaps } from "@image/generate-map";
import { Terrain } from "@core/terrain";
import { ViewControllerComposer } from "./view-controller-composer";
import gameStore from "@stores/game-store";

export type SceneComposer = Awaited<ReturnType<typeof createSceneComposer>>;
export type SceneComposerApi = SceneComposer["api"];

const white = new Color( 0xffffff );

type AdditionalSceneParams = {
terrain: Terrain;
heightMaps: HeightMaps,
pxToWorld: PxToWorld
}

// Primarily concerned about converting OpenBW state to three objects and animations
export const createSceneComposer = async ( world: World, assets: Assets ) => {
export const createSceneComposer = async ( world: World, assets: Assets, viewController: ViewControllerComposer, { terrain, pxToWorld, heightMaps } : AdditionalSceneParams ) => {
const janitor = new Janitor( "SceneComposer" );

const { terrain, ...terrainExtra } = janitor.mop(
await terrainComposer(
...world.map.size,
world.map.tileset,
getMapTiles( world.map ),
UnitTileScale.HD
),
"terrain"
);

const pxToWorld = makePxToWorld( ...world.map.size, terrain.getTerrainY );
const pxToWorldFlat = makePxToWorld( ...world.map.size, () => 0);

const startLocations = world.map.units
.filter( ( u ) => u.unitId === unitTypes.startLocation )
.map( ( u ) => {
const location = pxToWorld.xyz( u.x, u.y, new Vector3() );

const player = world.players.find( ( p ) => p.id === u.player );
if ( player ) {
player.startLocation = (new Vector3).copy( location );
}

return location
})

const playerWithStartLocation = world.players.find(p => p.startLocation);
const initialStartLocation = playerWithStartLocation ? playerWithStartLocation.startLocation ?? new Vector3() : startLocations[0] ?? new Vector3();

const _world = borrow( world );

const units = new UnitEntities();
units.externalOnClearUnits = () => _world.events!.emit( "units-cleared" );
units.externalOnCreateUnit = ( unit ) => _world.events!.emit( "unit-created", unit );

world.openBW.uploadHeightMap(
terrainExtra.heightMaps.singleChannel,
( terrainExtra.heightMaps.texture.image as ImageData ).width,
( terrainExtra.heightMaps.texture.image as ImageData ).height
heightMaps.singleChannel,
( heightMaps.texture.image as ImageData ).width,
( heightMaps.texture.image as ImageData ).height
);

const scene = janitor.mop(
Expand Down Expand Up @@ -312,6 +290,12 @@ export const createSceneComposer = async ( world: World, assets: Assets ) => {

if ( unit ) {
images.setUnit( image, unit );
if (unit.isAttacking) {
const weapon = gameStore().assets?.bwDat.weapons[unit.extras.dat.groundWeapon] || gameStore().assets?.bwDat.weapons[unit.extras.dat.airWeapon];
if (weapon) {
viewController.doShakeCalculation(weapon.explosionType, weapon.damageType, sprite.position);
}
}
}

if ( isImage3d( image ) ) {
Expand All @@ -331,7 +315,6 @@ export const createSceneComposer = async ( world: World, assets: Assets ) => {
applyRenderModeToSprite(
spriteStruct.typeId,
sprite,
terrain.getTerrainY( sprite.position.x, sprite.position.z )
);

sprite.updateMatrix();
Expand Down Expand Up @@ -370,7 +353,6 @@ export const createSceneComposer = async ( world: World, assets: Assets ) => {
Janitor.logLevel = getJanitorLogLevel();
} );

const pxToWorldInverse = makePxToWorld( ...world.map.size, terrain.getTerrainY, true );

return {
images,
Expand All @@ -381,15 +363,8 @@ export const createSceneComposer = async ( world: World, assets: Assets ) => {
selectedUnits,
followedUnits,
scene,
terrain,
terrainExtra,
pxToWorld,
pxToWorldInverse,
pxToWorldFlat,
startLocations,
onFrame(
delta: number,
elapsed: number,
renderMode3D: boolean,
) {

Expand Down Expand Up @@ -452,16 +427,13 @@ export const createSceneComposer = async ( world: World, assets: Assets ) => {
_world.fogOfWar!.forceInstantUpdate = true;
},
pxToWorld,
pxToWorldFlat,
get units() : IterableMap<number, Unit> {
return units.units
},
imageQuadtree,
unitQuadtree,
scene,
followedUnits,
startLocations,
initialStartLocation,
//TODO: extend followedunits instead
getFollowedUnitsCenterPosition: () => calculateFollowedUnitsTarget( followedUnits, pxToWorld ),
selectedUnits,
Expand Down
Loading