Skip to content

Commit

Permalink
chore: virtual camera test scene updates 2 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
pravusjif authored Sep 20, 2024
1 parent 28e6588 commit 4ab0d15
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
13 changes: 11 additions & 2 deletions scenes/2,22-virtual-cameras/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
This example scene makes usage of several `VirtualCamera` components and `MainCamera` to change the active virtual camera in the scene.

### SCENE TESTING
### SCENE TESTING - Global Input Cameras
There are 4 entities with `VirtualCamera` up in the sky, they have a text shape marking the order in which they are used.

A system is listening to the Primary input key press ("E") to pass through those 4 cameras activating one after the other. So press 'E' and the cameras will be changing sequentially, after all the cameras have been toggled, the default character camera returns.

* Camera 1: A statically positioned top-down camera -> 89 degrees was used instead of 90 since the input controls are based on the camera forward/direction so if a perfect 80 degrees rotation is used, the input won't work as expected.
* Camera 2: Another statically positioned camera.
* Camera 3: A static positioned camera that makes use of the `lookAt` property referencing the `engine.PlayerEntity` to always look at the player.
* Camera 4: A camera whose entity transform is tweened using a tween sequence. It uses the `lookAt` property referencing an invisible entity positioned at the center of the scene close to the ground.
* Camera 4: A camera whose entity transform is tweened using a tween sequence. It uses the `lookAt` property referencing an invisible entity positioned at the center of the scene close to the ground.

### SCENE TESTING - "Controllable" Camera

There is a clickable (virtual camera) entity cube in the center of the scene that when clicked it blocks the player movement and allows for controlling the cube rotation and X-Z position.

Controls:
* W,A,S,D: X-Z position
* E,F: Y rotation
* SPACEBAR/JUMP: Reset player controls and camera, leaving the controllabel camera where you left it
5 changes: 3 additions & 2 deletions scenes/2,22-virtual-cameras/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
"devDependencies": {
"@dcl/asset-packs": "^1.20.0",
"@dcl/sdk": "^7.5.8-10926653489.commit-aec9f09"
"@dcl/js-runtime": "latest",
"@dcl/sdk": "latest"
},
"engines": {
"node": ">=16.0.0",
Expand All @@ -23,4 +24,4 @@
"printWidth": 120,
"trailingComma": "none"
}
}
}
10 changes: 4 additions & 6 deletions scenes/2,22-virtual-cameras/src/controllableCamera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export function InstantiateControllableCamera() {
MeshRenderer.setBox(controllableCameraEntity)
MeshCollider.setBox(controllableCameraEntity)
Material.setBasicMaterial(controllableCameraEntity, { diffuseColor: Color4.Green() })
VirtualCamera.create(controllableCameraEntity, {
defaultTransition: { transitionMode: VirtualCamera.Transition.Time(0) }
})
VirtualCamera.create(controllableCameraEntity)
pointerEventsSystem.onPointerDown(
{
entity: controllableCameraEntity,
Expand All @@ -51,7 +49,7 @@ export function InstantiateControllableCamera() {
const mainCamera = MainCamera.getMutableOrNull(engine.CameraEntity)
if (!mainCamera) return

ToggleCharacterInput(true)
ToggleCharacterInput(false)

controllableCameraIsActive = true
mainCamera.virtualCameraEntity = controllableCameraEntity
Expand All @@ -71,9 +69,9 @@ export function InstantiateControllableCamera() {
const cameraLeft = Vector3.rotate(cameraForward, Quaternion.fromEulerDegrees(0, -90, 0))

if (inputSystem.isTriggered(InputAction.IA_JUMP, PointerEventType.PET_DOWN)) {
ToggleCharacterInput(false)
ToggleCharacterInput(true)
controllableCameraIsActive = false
mainCamera.virtualCameraEntity = 0
mainCamera.virtualCameraEntity = undefined
} else if (inputSystem.isPressed(InputAction.IA_FORWARD)) {
const delta = Vector3.scale(cameraForward, controllableCameraMovementSpeed * dt)
cameraTransform.position = Vector3.add(cameraTransform.position, delta)
Expand Down
19 changes: 7 additions & 12 deletions scenes/2,22-virtual-cameras/src/globalInputCameras.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,29 @@ export function InstantiateGlobalInputCameras() {
SpawnVirtualCamera({
position: Vector3.create(8, 16, 8),
rotation: Quaternion.fromEulerDegrees(89, 0, 0)
}, {
defaultTransition: { transitionMode: VirtualCamera.Transition.Time(0) }
}, "1")

const staticVirtualCamera2Pos = Vector3.create(0, 16, 15)
SpawnVirtualCamera({
position: staticVirtualCamera2Pos,
rotation: Quaternion.fromLookAt(staticVirtualCamera2Pos, centerOfScenePosition)
}, {
}, "2", {
defaultTransition: { transitionMode: VirtualCamera.Transition.Time(2) }
}, "2")
})

const staticVirtualCamera3Pos = Vector3.create(15, 16, 0)
SpawnVirtualCamera({
position: staticVirtualCamera3Pos,
rotation: Quaternion.fromLookAt(staticVirtualCamera3Pos, centerOfScenePosition)
}, {
}, "3", {
defaultTransition: { transitionMode: VirtualCamera.Transition.Speed(20) },
lookAtEntity: engine.PlayerEntity
}, "3")
})

const movingVirtualCamera = SpawnVirtualCamera({
position: Vector3.create(2, 16, 2),
rotation: Quaternion.fromEulerDegrees(89, 0, 0)
}, {
defaultTransition: { transitionMode: VirtualCamera.Transition.Speed(0) },
lookAtEntity: sceneCenterEntity
}, "4")
}, "4", { lookAtEntity: sceneCenterEntity })
Tween.create(movingVirtualCamera, {
duration: 4000,
easingFunction: EasingFunction.EF_LINEAR,
Expand Down Expand Up @@ -130,7 +125,7 @@ export function InstantiateGlobalInputCameras() {
currentVirtualCameraIndex = 0

if (virtualCamerasCollection[currentVirtualCameraIndex] == engine.CameraEntity) {
mainCamera.virtualCameraEntity = 0
mainCamera.virtualCameraEntity = undefined
} else {
mainCamera.virtualCameraEntity = virtualCamerasCollection[currentVirtualCameraIndex]
VisibilityComponent.getMutable(virtualCamerasCollection[currentVirtualCameraIndex]).visible = false
Expand All @@ -140,7 +135,7 @@ export function InstantiateGlobalInputCameras() {
})
}

function SpawnVirtualCamera(transformProps: Partial<TransformType>, camProps: PBVirtualCamera, camText: string ): Entity {
function SpawnVirtualCamera(transformProps: Partial<TransformType>, camText: string, camProps: PBVirtualCamera = {} ): Entity {
const virtualCameraEntity = engine.addEntity()
Transform.create(virtualCameraEntity, transformProps)
MeshRenderer.setBox(virtualCameraEntity)
Expand Down
6 changes: 3 additions & 3 deletions scenes/2,22-virtual-cameras/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Vector3, Quaternion, Color4 } from '@dcl/sdk/math'
import { engine, Transform, MeshRenderer, Material } from '@dcl/sdk/ecs'
import { InstantiateGlobalInputCameras } from "./globalInputCameras";
// import { InstantiateControllableCamera } from "./controllableCamera";
import {InstantiateModifierAreas} from "./modifierAreas";
import { InstantiateControllableCamera } from "./controllableCamera";
import { InstantiateModifierAreas } from "./modifierAreas";

// Environment setup
const groundEntity = engine.addEntity()
Expand All @@ -18,4 +18,4 @@ InstantiateGlobalInputCameras()

InstantiateModifierAreas()

// InstantiateControllableCamera()
InstantiateControllableCamera()

0 comments on commit 4ab0d15

Please sign in to comment.