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

Preview camera fixes #9544

Merged
merged 4 commits into from
Jan 3, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { TransformComponent } from '@etherealengine/engine/src/transform/compone

import CameraAltIcon from '@mui/icons-material/CameraAlt'

import { ScenePreviewCameraComponent } from '@etherealengine/engine/src/scene/components/ScenePreviewCamera'
import { computeTransformMatrix } from '@etherealengine/engine/src/transform/systems/TransformSystem'
import { EditorControlFunctions } from '../../functions/EditorControlFunctions'
import { previewScreenshot } from '../../functions/takeScreenshot'
Expand Down Expand Up @@ -62,7 +63,12 @@ export const ScenePreviewCameraNodeEditor: EditorComponentType = (props) => {
}

const updateScenePreview = async () => {
const imageBlob = (await previewScreenshot(512 / 2, 320 / 2))!
const imageBlob = (await previewScreenshot(
512 / 2,
320 / 2,
0.9,
getComponent(props.entity, ScenePreviewCameraComponent).camera
))!
const url = URL.createObjectURL(imageBlob)
setBufferUrl(url)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/editor/src/functions/takeScreenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function getResizedCanvas(canvas: HTMLCanvasElement, width: number, height: numb
return tmpCanvas
}

const query = defineQuery([ScenePreviewCameraComponent])
const scenePreviewCameraQuery = defineQuery([ScenePreviewCameraComponent])

const ktx2Encoder = new KTX2Encoder()

Expand All @@ -85,7 +85,7 @@ export async function previewScreenshot(
): Promise<Blob | null> {
// Getting Scene preview camera or creating one if not exists
if (!scenePreviewCamera) {
for (const entity of query()) {
for (const entity of scenePreviewCameraQuery()) {
scenePreviewCamera = getComponent(entity, ScenePreviewCameraComponent).camera
}

Expand Down Expand Up @@ -168,7 +168,7 @@ export async function takeScreenshot(
): Promise<[Blob | null, Blob | null]> {
// Getting Scene preview camera or creating one if not exists
if (!scenePreviewCamera) {
for (const entity of query()) {
for (const entity of scenePreviewCameraQuery()) {
scenePreviewCamera = getComponent(entity, ScenePreviewCameraComponent).camera
}

Expand Down
8 changes: 6 additions & 2 deletions packages/engine/src/scene/components/ScenePreviewCamera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { RendererState } from '../../renderer/RendererState'
import { TransformComponent } from '../../transform/components/TransformComponent'
import { ObjectLayers } from '../constants/ObjectLayers'
import { setObjectLayers } from '../functions/setObjectLayers'
import { addObjectToGroup } from './GroupComponent'
import { addObjectToGroup, removeObjectFromGroup } from './GroupComponent'
import { NameComponent } from './NameComponent'
import { setVisibleComponent } from './VisibleComponent'

Expand Down Expand Up @@ -70,7 +70,11 @@ export const ScenePreviewCameraComponent = defineComponent({
const cameraTransform = getComponent(Engine.instance.cameraEntity, TransformComponent)
cameraTransform.position.copy(transform.position)
cameraTransform.rotation.copy(transform.rotation)
addObjectToGroup(entity, previewCamera.camera.value)
const camera = previewCamera.camera.value
addObjectToGroup(entity, camera)
return () => {
removeObjectFromGroup(entity, camera)
}
}, [])

useEffect(() => {
Expand Down
10 changes: 10 additions & 0 deletions packages/engine/src/transform/systems/TransformSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
RigidBodyFixedTagComponent
} from '../../physics/components/RigidBodyComponent'
import { GroupComponent } from '../../scene/components/GroupComponent'
import { ScenePreviewCameraComponent } from '../../scene/components/ScenePreviewCamera'
import { VisibleComponent } from '../../scene/components/VisibleComponent'
import { XRState } from '../../xr/XRState'
import { TransformSerialization } from '../TransformSerialization'
Expand Down Expand Up @@ -74,6 +75,8 @@ const distanceFromLocalClientQuery = defineQuery([TransformComponent, DistanceFr
const distanceFromCameraQuery = defineQuery([TransformComponent, DistanceFromCameraComponent])
const frustumCulledQuery = defineQuery([TransformComponent, FrustumCullCameraComponent])

const scenePreviewCameraQuery = defineQuery([ScenePreviewCameraComponent])

export const computeTransformMatrix = (entity: Entity) => {
const transform = getComponent(entity, TransformComponent)
updateTransformFromComputedTransform(entity)
Expand Down Expand Up @@ -321,6 +324,13 @@ const execute = () => {
viewCamera.projectionMatrixInverse.copy(camera.projectionMatrixInverse)
}

const scenePreviewCameraDirty = scenePreviewCameraQuery().filter(isDirty)

for (const entity of scenePreviewCameraDirty) {
const camera = getComponent(entity, ScenePreviewCameraComponent).camera
camera.matrixWorldInverse.copy(camera.matrixWorld).invert()
}

const dirtyBoundingBoxes = boundingBoxQuery().filter(isDirty)
for (const entity of dirtyBoundingBoxes) updateBoundingBox(entity)

Expand Down
Loading