Skip to content
This repository was archived by the owner on Feb 27, 2023. It is now read-only.
Open
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
1 change: 1 addition & 0 deletions packages/entryPoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ export async function startPreview() {
}
})
clientDebug.ToggleSceneBoundingBoxes(sceneData.sceneId, false).catch((e) => defaultLogger.error(e))
clientDebug.ToggleSceneSpawnPoints(sceneData.sceneId, false).catch((e) => defaultLogger.error(e))
unityInterface.SendMessageToUnity('Main', 'TogglePreviewMenu', JSON.stringify({ enabled: true }))
}
})
Expand Down
89 changes: 80 additions & 9 deletions packages/unity-interface/ClientDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { defaultLogger } from 'shared/logger'
import { ErrorContextTypes, ReportFatalErrorWithUnityPayloadAsync } from 'shared/loading/ReportFatalError'
import { getUnityInstance, IUnityInterface } from './IUnityInterface'
import { fetchSceneIds } from 'decentraland-loader/lifecycle/utils/fetchSceneIds'
import { fetchSceneJson } from 'decentraland-loader/lifecycle/utils/fetchSceneJson'
import { SceneJsonData } from 'shared/types'
import { SpawnPoint } from '@dcl/schemas'
import { gridToWorld, parseParcelPosition } from 'atomicHelpers/parcelScenePositions'
import { Vector3 } from '@dcl/ecs-math'

export class ClientDebug {
private unityInterface: IUnityInterface
Expand Down Expand Up @@ -87,22 +92,88 @@ export class ClientDebug {
}

public async ToggleSceneBoundingBoxes(scene: string, enabled: boolean) {
const isInputCoords = scene.match(/^-?[0-9]*([,]-?[0-9]*){1}$/)
let sceneId: string | undefined

if (isInputCoords) {
const ids = await fetchSceneIds([scene])
sceneId = ids[0] ?? undefined
} else {
sceneId = scene
}
const isInputCoords = isValueACoordinate(scene)
const sceneId: string | undefined = isInputCoords ? await getSceneIdFromCoordinates(scene) : scene

if (sceneId) {
this.unityInterface.SendMessageToUnity('Main', 'ToggleSceneBoundingBoxes', JSON.stringify({ sceneId, enabled }))
} else {
throw new Error(`scene not found ${scene}`)
}
}

public async ToggleSceneSpawnPoints(scene: string, enabled?: boolean, sceneJsonData?: SceneJsonData) {
const isInputCoords = isValueACoordinate(scene)
const sceneId: string | undefined = isInputCoords ? await getSceneIdFromCoordinates(scene) : scene

if (!sceneId) {
throw new Error(`scene not found ${scene}`)
}

let sceneJson = sceneJsonData

// if `sceneJsonData` is not in the arguments we fetch the json data
if (!sceneJson) {
const fetchJson = await fetchSceneJson([sceneId])
const fetchedJson = fetchJson[0] ?? undefined

if (!fetchedJson) {
throw new Error(`scene json not found ${scene}`)
}
sceneJson = fetchedJson.sceneJsonData
}

// get base parcel world position to always handle positions in world context
const base = parseParcelPosition(sceneJson.scene.base)
const basePosition = new Vector3()
gridToWorld(base.x, base.y, basePosition)

const spawnPoints: SpawnPoint[] = []

// if no spawnpoint set in scene json, we create the default one (0,0,0)
if (!sceneJson.spawnPoints) {
spawnPoints.push({
name: 'undefined',
position: { x: [basePosition.x], y: [basePosition.y], z: [basePosition.z] },
default: true
})
} else {
const convertPositionComponent = (value: number | number[], sceneWorldPosition: number): number[] => {
if (Array.isArray(value)) {
return value.map((v) => sceneWorldPosition + v)
}
return [sceneWorldPosition + value]
}

// convert vector3 to world position and always use type `number[]` for spawnpoint position
for (const spawnPoint of sceneJson.spawnPoints) {
spawnPoints.push({
...spawnPoint,
position: {
x: convertPositionComponent(spawnPoint.position.x, basePosition.x),
y: convertPositionComponent(spawnPoint.position.y, basePosition.y),
z: convertPositionComponent(spawnPoint.position.z, basePosition.z)
},
cameraTarget: spawnPoint.cameraTarget ? basePosition.add(spawnPoint.cameraTarget) : undefined
})
}
}

this.unityInterface.SendMessageToUnity(
'Main',
'ToggleSceneSpawnPoints',
JSON.stringify({ sceneId, enabled, spawnPoints })
)
}
}

function isValueACoordinate(value: string): boolean {
return value.match(/^-?[0-9]*([,]-?[0-9]*){1}$/) ? true : false
}

async function getSceneIdFromCoordinates(coordinates: string): Promise<string | undefined> {
const ids = await fetchSceneIds([coordinates])
return ids[0] ?? undefined
}

export const clientDebug: ClientDebug = new ClientDebug(getUnityInstance())
26 changes: 19 additions & 7 deletions packages/unity-interface/dcl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,15 @@ export async function startUnitySceneWorkers() {
}

export async function getPreviewSceneId(): Promise<{ sceneId: string | null; sceneBase: string }> {
const result = await fetch('/scene.json?nocache=' + Math.random())
const jsonData = await getPreviewSceneJson()
const [sceneId] = await fetchSceneIds([jsonData.scene.base])
return { sceneId, sceneBase: jsonData.scene.base }
}

async function getPreviewSceneJson() {
const result = await fetch('/scene.json?nocache=' + Math.random())
if (result.ok) {
const scene = (await result.json()) as SceneJsonData

const [sceneId] = await fetchSceneIds([scene.scene.base])
return { sceneId, sceneBase: scene.scene.base }
return (await result.json()) as SceneJsonData
} else {
throw new Error('Could not load scene.json')
}
Expand All @@ -186,13 +188,23 @@ export async function loadPreviewScene(message: sdk.Messages) {
async function oldReload() {
const { sceneId, sceneBase } = await getPreviewSceneId()
if (sceneId) {
await reloadScene(sceneId)
await doReload(sceneId)
} else {
defaultLogger.log(`Unable to load sceneId of ${sceneBase}`)
debugger
}
}

async function doReload(sceneId: string) {
await reloadScene(sceneId)

// We get scene json here to use hot-reload to update displayed spawnpoints
// since the ILand info is not currently changing on hot-reload
getPreviewSceneJson()
.then((sceneJson) => clientDebug.ToggleSceneSpawnPoints(sceneId, undefined, sceneJson))
.catch((e) => defaultLogger.error(e))
}

if (message.type === sdk.SCENE_UPDATE && sdk.SceneUpdate.validate(message)) {
if (message.payload.sceneType === sdk.ProjectType.PORTABLE_EXPERIENCE) {
try {
Expand All @@ -219,7 +231,7 @@ export async function loadPreviewScene(message: sdk.Messages) {
}
} else {
if (message.payload.sceneId) {
await reloadScene(message.payload.sceneId)
await doReload(message.payload.sceneId)
} else {
await oldReload()
}
Expand Down