@ignifx/editor-host 0.13.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +245 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +720 -89
- package/dist/index.js.map +1 -1
- package/dist/vite.d.ts.map +1 -1
- package/dist/vite.js +1 -0
- package/dist/vite.js.map +1 -1
- package/package.json +10 -10
- package/skills/editor-host/SKILL.md +3 -2
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { n as isEditorOrigin, t as EditorHostErrorCode } from "./error-codes.js";
|
|
2
|
-
import { Camera, Component, CoreErrorCode, IgnifxError, Phase, SCENE_ASSET_TYPE, SCENE_FILE_FORMAT, SCENE_FORMAT_VERSION, Script, Transform, VERSION, addComponentFromRecord, canonicalizeNumber, clamp, createApp, createFrameSample, createRay, createSceneAsset, decodeProps, decodeValue, degToRad, describeRegisteredSchemas, describeSchemas, describeSettings, encodeProps, encodeValue, instantiateScene, isIgnifxError, loadSceneDependencies, parseOverridePath, serializeEntity, serializeScene, settingsJsonSchema, toJsonSchema, transformSchema, validateSceneFile } from "@ignifx/core";
|
|
2
|
+
import { Camera, Component, CoreErrorCode, IgnifxError, Light, Phase, SCENE_ASSET_TYPE, SCENE_FILE_FORMAT, SCENE_FORMAT_VERSION, Script, Transform, VERSION, addComponentFromRecord, canonicalizeNumber, clamp, createApp, createFrameSample, createRay, createSceneAsset, decodeProps, decodeValue, degToRad, describeRegisteredSchemas, describeSchemas, describeSettings, encodeProps, encodeValue, instantiateScene, isIgnifxError, loadSceneDependencies, parseOverridePath, serializeEntity, serializeScene, settingsJsonSchema, toJsonSchema, transformSchema, validateSceneFile } from "@ignifx/core";
|
|
3
3
|
import { addToScene, attachPositionGizmoToNode, attachRotationGizmoToNode, attachScaleGizmoToNode, createLines, createPositionGizmo, createRotationGizmo, createScaleGizmo, isGizmoDragging, isGizmoInteracting, isGizmoPickPending, onBeforeRender, setPositionGizmoLocalCoordinates, setRotationGizmoLocalCoordinates, setScaleGizmoLocalCoordinates } from "@babylonjs/lite";
|
|
4
4
|
//#region src/protocol/rpc.ts
|
|
5
5
|
/**
|
|
@@ -186,9 +186,9 @@ const HostKind = {
|
|
|
186
186
|
* @remarks
|
|
187
187
|
* The play controls send `"heartbeat"`, `"playControls"`, `"logRelay"` and `"queryRuntime"`, plus
|
|
188
188
|
* `"screenshot"` when the game has a canvas and `"sendInput"` when it has `app.input`. The scene
|
|
189
|
-
* host sends `"heartbeat"` and whichever of `"gizmos"`, `"screenshot"`, `"viewMode2d"
|
|
190
|
-
* `"pick2d"` it supports; every `SceneMethod` not tied to one of
|
|
191
|
-
* host.
|
|
189
|
+
* host sends `"heartbeat"` and whichever of `"gizmos"`, `"screenshot"`, `"viewMode2d"`,
|
|
190
|
+
* `"pick2d"`, `"gameView"` and `"thumbnails"` it supports; every `SceneMethod` not tied to one of
|
|
191
|
+
* them is required of a 0.3 scene host.
|
|
192
192
|
*
|
|
193
193
|
* @public
|
|
194
194
|
*/
|
|
@@ -242,7 +242,19 @@ const HostCapability = {
|
|
|
242
242
|
* when the game has `app.input`. From engine ask E49 (additive in 0.3); without it, the play view
|
|
243
243
|
* answers `sendInput` with `RpcErrorCode.methodNotFound`.
|
|
244
244
|
*/
|
|
245
|
-
sendInput: "sendInput"
|
|
245
|
+
sendInput: "sendInput",
|
|
246
|
+
/**
|
|
247
|
+
* The scene host answers `setGameView` and `gameViewFrame`: the open scene through its own main
|
|
248
|
+
* camera, rendered beside the editor camera (`RenderViewMethod`). From engine ask E8 (additive in
|
|
249
|
+
* 0.3); without it, the host answers both with `RpcErrorCode.methodNotFound`.
|
|
250
|
+
*/
|
|
251
|
+
gameView: "gameView",
|
|
252
|
+
/**
|
|
253
|
+
* The scene host answers `renderThumbnail`: a model or prefab asset rendered on its own
|
|
254
|
+
* (`RenderViewMethod`). From engine ask E8 (additive in 0.3); without it, the host answers it with
|
|
255
|
+
* `RpcErrorCode.methodNotFound`.
|
|
256
|
+
*/
|
|
257
|
+
thumbnails: "thumbnails"
|
|
246
258
|
};
|
|
247
259
|
/**
|
|
248
260
|
* The longest string, in UTF-16 code units, the editor accepts from a host in `ready` (the engine
|
|
@@ -1768,6 +1780,163 @@ function isLogParams(value) {
|
|
|
1768
1780
|
return isRecord(value) && isOneOf(LOG_LEVELS, value["level"]) && isHostTextOrNull(value["scope"]) && isHostText(value["message"]) && isFiniteNumber(value["timeMs"]) && optionalField(value, "error", (error) => isRecord(error) && isHostText(error["message"]) && isHostTextOrNull(error["code"]) && isHostTextOrNull(error["stack"]));
|
|
1769
1781
|
}
|
|
1770
1782
|
//#endregion
|
|
1783
|
+
//#region src/protocol/render-views.ts
|
|
1784
|
+
/**
|
|
1785
|
+
* Protocol 0.3, additive (engine ask E8): the scene host's second views, rendered through the
|
|
1786
|
+
* engine's render textures. The Game panel's view is the document's own camera rendered beside the
|
|
1787
|
+
* editor camera (`setGameView`, `gameViewFrame`); a thumbnail is an asset rendered on its own
|
|
1788
|
+
* (`renderThumbnail`). Both answer images in the shape of `screenshot`'s.
|
|
1789
|
+
*/
|
|
1790
|
+
/**
|
|
1791
|
+
* Editor → scene host: the render-view methods, answered after `ready` like every `SceneMethod`.
|
|
1792
|
+
*
|
|
1793
|
+
* @remarks
|
|
1794
|
+
* A host lists `HostCapability.gameView` when it answers the first two and
|
|
1795
|
+
* `HostCapability.thumbnails` when it answers the third; one that does not answers
|
|
1796
|
+
* `RpcErrorCode.methodNotFound`. Additive in 0.3.
|
|
1797
|
+
*
|
|
1798
|
+
* @public
|
|
1799
|
+
*/
|
|
1800
|
+
const RenderViewMethod = {
|
|
1801
|
+
/** Turns the Game panel's view on, off, or to a new size. Answers {@link GameViewState}. */
|
|
1802
|
+
setGameView: "setGameView",
|
|
1803
|
+
/**
|
|
1804
|
+
* Renders the open scene through its own main camera, the one the game would use, and answers
|
|
1805
|
+
* the image: {@link GameViewFrameResult}. Fails with `RpcErrorCode.noScene` while no scene is
|
|
1806
|
+
* open and `RpcErrorCode.invalidRequest` while the game view is off.
|
|
1807
|
+
*/
|
|
1808
|
+
gameViewFrame: "gameViewFrame",
|
|
1809
|
+
/** Renders one model or prefab asset on its own and answers the image: {@link ThumbnailResult}. */
|
|
1810
|
+
renderThumbnail: "renderThumbnail"
|
|
1811
|
+
};
|
|
1812
|
+
/**
|
|
1813
|
+
* The largest width and height, in pixels, of the game view.
|
|
1814
|
+
*
|
|
1815
|
+
* @public
|
|
1816
|
+
*/
|
|
1817
|
+
const GAME_VIEW_MAX_SIZE = 2048;
|
|
1818
|
+
/**
|
|
1819
|
+
* The game view's size until `setGameView` gives one: 640 by 360.
|
|
1820
|
+
*
|
|
1821
|
+
* @public
|
|
1822
|
+
*/
|
|
1823
|
+
const GAME_VIEW_DEFAULT_SIZE = {
|
|
1824
|
+
width: 640,
|
|
1825
|
+
height: 360
|
|
1826
|
+
};
|
|
1827
|
+
/**
|
|
1828
|
+
* The largest side, in pixels, of a thumbnail.
|
|
1829
|
+
*
|
|
1830
|
+
* @public
|
|
1831
|
+
*/
|
|
1832
|
+
const THUMBNAIL_MAX_SIZE = 1024;
|
|
1833
|
+
/**
|
|
1834
|
+
* A thumbnail's side when the request gives none.
|
|
1835
|
+
*
|
|
1836
|
+
* @public
|
|
1837
|
+
*/
|
|
1838
|
+
const THUMBNAIL_DEFAULT_SIZE = 128;
|
|
1839
|
+
/**
|
|
1840
|
+
* Tells whether a value is a whole number from 1 to a maximum.
|
|
1841
|
+
*
|
|
1842
|
+
* @param value - The value to test.
|
|
1843
|
+
* @param max - The largest value allowed.
|
|
1844
|
+
* @returns True for an integer in range.
|
|
1845
|
+
*/
|
|
1846
|
+
function isSide(value, max) {
|
|
1847
|
+
return Number.isSafeInteger(value) && Number(value) >= 1 && Number(value) <= max;
|
|
1848
|
+
}
|
|
1849
|
+
/**
|
|
1850
|
+
* Tells whether a value is an image format and, when present, a quality from 0 to 1.
|
|
1851
|
+
*
|
|
1852
|
+
* @param value - The record to test.
|
|
1853
|
+
* @returns True for a known format and a quality in range when given.
|
|
1854
|
+
*/
|
|
1855
|
+
function hasImageFormat(value) {
|
|
1856
|
+
const format = value["format"];
|
|
1857
|
+
return (format === ScreenshotFormat.png || format === ScreenshotFormat.jpeg) && optionalField(value, "quality", (quality) => isFiniteNumber(quality) && quality >= 0 && quality <= 1);
|
|
1858
|
+
}
|
|
1859
|
+
/**
|
|
1860
|
+
* Tells whether a value is a PNG or JPEG data URL.
|
|
1861
|
+
*
|
|
1862
|
+
* @param value - The value to test.
|
|
1863
|
+
* @returns True for a `data:image/png;base64,…` or `data:image/jpeg;base64,…` string.
|
|
1864
|
+
*/
|
|
1865
|
+
function isImageDataUrl(value) {
|
|
1866
|
+
return isString(value) && /^data:image\/(?:png|jpeg);base64,/u.test(value);
|
|
1867
|
+
}
|
|
1868
|
+
/**
|
|
1869
|
+
* Checks the parameters of `setGameView`.
|
|
1870
|
+
*
|
|
1871
|
+
* @param value - The received `params`.
|
|
1872
|
+
* @returns True for a boolean `enabled` and sizes in range when given.
|
|
1873
|
+
*
|
|
1874
|
+
* @public
|
|
1875
|
+
*/
|
|
1876
|
+
function isSetGameViewParams(value) {
|
|
1877
|
+
return isRecord(value) && typeof value["enabled"] === "boolean" && optionalField(value, "width", (width) => isSide(width, 2048)) && optionalField(value, "height", (height) => isSide(height, 2048));
|
|
1878
|
+
}
|
|
1879
|
+
/**
|
|
1880
|
+
* Checks the result of `setGameView`.
|
|
1881
|
+
*
|
|
1882
|
+
* @param value - The received `result`.
|
|
1883
|
+
* @returns True for a boolean `enabled` and sizes in range.
|
|
1884
|
+
*
|
|
1885
|
+
* @public
|
|
1886
|
+
*/
|
|
1887
|
+
function isGameViewState(value) {
|
|
1888
|
+
return isRecord(value) && typeof value["enabled"] === "boolean" && isSide(value["width"], 2048) && isSide(value["height"], 2048);
|
|
1889
|
+
}
|
|
1890
|
+
/**
|
|
1891
|
+
* Checks the parameters of `gameViewFrame`.
|
|
1892
|
+
*
|
|
1893
|
+
* @param value - The received `params`.
|
|
1894
|
+
* @returns True for a known format and a quality from 0 to 1 when given.
|
|
1895
|
+
*
|
|
1896
|
+
* @public
|
|
1897
|
+
*/
|
|
1898
|
+
function isGameViewFrameParams(value) {
|
|
1899
|
+
return isRecord(value) && hasImageFormat(value);
|
|
1900
|
+
}
|
|
1901
|
+
/**
|
|
1902
|
+
* Checks the result of `gameViewFrame`.
|
|
1903
|
+
*
|
|
1904
|
+
* @param value - The received `result`.
|
|
1905
|
+
* @returns True for an image and a camera uid, or `null` for both, and sizes in range.
|
|
1906
|
+
*
|
|
1907
|
+
* @public
|
|
1908
|
+
*/
|
|
1909
|
+
function isGameViewFrameResult(value) {
|
|
1910
|
+
if (!isRecord(value) || !isSide(value["width"], 2048) || !isSide(value["height"], 2048)) return false;
|
|
1911
|
+
const dataUrl = value["dataUrl"];
|
|
1912
|
+
const camera = value["camera"];
|
|
1913
|
+
return dataUrl === null ? camera === null : isImageDataUrl(dataUrl) && isUidString(camera);
|
|
1914
|
+
}
|
|
1915
|
+
/**
|
|
1916
|
+
* Checks the parameters of `renderThumbnail`.
|
|
1917
|
+
*
|
|
1918
|
+
* @param value - The received `params`.
|
|
1919
|
+
* @returns True for an address or asset reference, a size in range when given, and a known format.
|
|
1920
|
+
*
|
|
1921
|
+
* @public
|
|
1922
|
+
*/
|
|
1923
|
+
function isRenderThumbnailParams(value) {
|
|
1924
|
+
if (!isRecord(value) || !hasImageFormat(value)) return false;
|
|
1925
|
+
const asset = value["asset"];
|
|
1926
|
+
return (isString(asset) && asset.length > 0 || isSceneFileAssetRef(asset)) && optionalField(value, "size", (size) => isSide(size, 1024));
|
|
1927
|
+
}
|
|
1928
|
+
/**
|
|
1929
|
+
* Checks the result of `renderThumbnail`.
|
|
1930
|
+
*
|
|
1931
|
+
* @param value - The received `result`.
|
|
1932
|
+
* @returns True for an image and sizes in range.
|
|
1933
|
+
*
|
|
1934
|
+
* @public
|
|
1935
|
+
*/
|
|
1936
|
+
function isThumbnailResult(value) {
|
|
1937
|
+
return isRecord(value) && isImageDataUrl(value["dataUrl"]) && isSide(value["width"], 1024) && isSide(value["height"], 1024);
|
|
1938
|
+
}
|
|
1939
|
+
//#endregion
|
|
1771
1940
|
//#region src/protocol/version.ts
|
|
1772
1941
|
/**
|
|
1773
1942
|
* The protocol version this package describes. The editor sends it in `hello` and the host in
|
|
@@ -2550,6 +2719,16 @@ function createEditorView(handles, overlay, canvas) {
|
|
|
2550
2719
|
};
|
|
2551
2720
|
}
|
|
2552
2721
|
/**
|
|
2722
|
+
* The world bounds of every visible mesh an entity draws, without a view: for a thumbnail stage.
|
|
2723
|
+
*
|
|
2724
|
+
* @param handles - The app's Lite engine and render scene (`app.lite`).
|
|
2725
|
+
* @param entity - The entity.
|
|
2726
|
+
* @returns The box, or `null` when nothing under the entity is drawn yet.
|
|
2727
|
+
*/
|
|
2728
|
+
function entityWorldBounds(handles, entity) {
|
|
2729
|
+
return computeWorldBounds(entity.transform.lite, handles.scene);
|
|
2730
|
+
}
|
|
2731
|
+
/**
|
|
2553
2732
|
* A node's parent. Lite types it as a bare world-matrix provider, so it is followed by identity only.
|
|
2554
2733
|
*
|
|
2555
2734
|
* @param node - The node.
|
|
@@ -4794,6 +4973,181 @@ function conjugate(q) {
|
|
|
4794
4973
|
];
|
|
4795
4974
|
}
|
|
4796
4975
|
//#endregion
|
|
4976
|
+
//#region src/scene/screenshot.ts
|
|
4977
|
+
/**
|
|
4978
|
+
* `screenshot`, shared by the scene view and the play view: the parameter check, and a captured
|
|
4979
|
+
* frame scaled to the requested size and written as a PNG or JPEG data URL. Encoding needs a
|
|
4980
|
+
* browser's `OffscreenCanvas`, as the capture needs a GPU.
|
|
4981
|
+
*/
|
|
4982
|
+
/** The JPEG quality when the request gives none. */
|
|
4983
|
+
const DEFAULT_JPEG_QUALITY$2 = .9;
|
|
4984
|
+
/** Bytes encoded per `String.fromCharCode` call, to stay under engines' argument limits. */
|
|
4985
|
+
const BASE64_CHUNK = 32768;
|
|
4986
|
+
/**
|
|
4987
|
+
* Encodes bytes as base64.
|
|
4988
|
+
*
|
|
4989
|
+
* @param bytes - The bytes.
|
|
4990
|
+
* @returns The base64 text.
|
|
4991
|
+
*/
|
|
4992
|
+
function toBase64(bytes) {
|
|
4993
|
+
let binary = "";
|
|
4994
|
+
for (let start = 0; start < bytes.length; start += BASE64_CHUNK) binary += String.fromCharCode(...bytes.subarray(start, start + BASE64_CHUNK));
|
|
4995
|
+
return btoa(binary);
|
|
4996
|
+
}
|
|
4997
|
+
/**
|
|
4998
|
+
* Scales a captured frame to a size and encodes it as a data URL.
|
|
4999
|
+
*
|
|
5000
|
+
* @param capture - The frame, RGBA8 with the top row first.
|
|
5001
|
+
* @param size - The image size.
|
|
5002
|
+
* @param format - `"png"` or `"jpeg"`.
|
|
5003
|
+
* @param quality - The JPEG quality, from 0 to 1; ignored for PNG.
|
|
5004
|
+
* @returns A `data:image/png;base64,…` or `data:image/jpeg;base64,…` URL.
|
|
5005
|
+
* @throws Error when the browser has no 2D canvas context.
|
|
5006
|
+
*/
|
|
5007
|
+
async function encodeCapture(capture, size, format, quality) {
|
|
5008
|
+
const source = new OffscreenCanvas(capture.width, capture.height);
|
|
5009
|
+
const sourceContext = source.getContext("2d");
|
|
5010
|
+
const target = new OffscreenCanvas(size.width, size.height);
|
|
5011
|
+
const targetContext = target.getContext("2d");
|
|
5012
|
+
if (sourceContext === null || targetContext === null) throw new Error("This browser has no 2D canvas to encode the screenshot with.");
|
|
5013
|
+
sourceContext.putImageData(new ImageData(new Uint8ClampedArray(capture.data), capture.width, capture.height), 0, 0);
|
|
5014
|
+
targetContext.imageSmoothingQuality = "high";
|
|
5015
|
+
targetContext.drawImage(source, 0, 0, size.width, size.height);
|
|
5016
|
+
const type = format === "png" ? "image/png" : "image/jpeg";
|
|
5017
|
+
const blob = await target.convertToBlob(format === "png" ? { type } : {
|
|
5018
|
+
type,
|
|
5019
|
+
quality
|
|
5020
|
+
});
|
|
5021
|
+
return `data:${type};base64,${toBase64(new Uint8Array(await blob.arrayBuffer()))}`;
|
|
5022
|
+
}
|
|
5023
|
+
/**
|
|
5024
|
+
* Checks `screenshot`'s parameters.
|
|
5025
|
+
*
|
|
5026
|
+
* @param params - The received `params`.
|
|
5027
|
+
* @returns The parameters.
|
|
5028
|
+
* @throws InvalidParamsError for a side outside 1 to {@link SCREENSHOT_MAX_SIZE}, an unknown format, or
|
|
5029
|
+
* a quality outside 0 to 1.
|
|
5030
|
+
*/
|
|
5031
|
+
function checkScreenshotParams(params) {
|
|
5032
|
+
if (typeof params === "object" && params !== null) for (const side of ["width", "height"]) {
|
|
5033
|
+
const value = Reflect.get(params, side);
|
|
5034
|
+
if (value !== void 0 && (!Number.isSafeInteger(value) || Number(value) < 1 || Number(value) > 2048)) throw new InvalidParamsError(`screenshot takes a width and height from 1 to ${String(SCREENSHOT_MAX_SIZE)} pixels.`);
|
|
5035
|
+
}
|
|
5036
|
+
if (!isScreenshotParams(params)) throw new InvalidParamsError("screenshot takes { width?, height?, format: \"png\" | \"jpeg\", quality? }.");
|
|
5037
|
+
return params;
|
|
5038
|
+
}
|
|
5039
|
+
/**
|
|
5040
|
+
* Captures the app's next presented frame and encodes it as `screenshot` answers.
|
|
5041
|
+
*
|
|
5042
|
+
* @param app - The app whose canvas is captured.
|
|
5043
|
+
* @param params - Checked parameters.
|
|
5044
|
+
* @returns The data URL and its size.
|
|
5045
|
+
* @throws IgnifxError `IGX-0707` when the app's render loop does not run.
|
|
5046
|
+
*/
|
|
5047
|
+
async function takeScreenshot(app, params) {
|
|
5048
|
+
const capture = await app.renderer.captureScreenshot();
|
|
5049
|
+
const size = screenshotSize(params, capture);
|
|
5050
|
+
return {
|
|
5051
|
+
dataUrl: await encodeCapture(capture, size, params.format, params.quality ?? DEFAULT_JPEG_QUALITY$2),
|
|
5052
|
+
width: size.width,
|
|
5053
|
+
height: size.height
|
|
5054
|
+
};
|
|
5055
|
+
}
|
|
5056
|
+
//#endregion
|
|
5057
|
+
//#region src/scene/game-view.ts
|
|
5058
|
+
/**
|
|
5059
|
+
* The Game panel's view (engine ask E8): the open scene through its own main camera, rendered into
|
|
5060
|
+
* a render texture beside the editor camera's frame (`setGameView`, `gameViewFrame`).
|
|
5061
|
+
*/
|
|
5062
|
+
/** The JPEG quality when the request gives none, as for `screenshot`. */
|
|
5063
|
+
const DEFAULT_JPEG_QUALITY$1 = .9;
|
|
5064
|
+
/**
|
|
5065
|
+
* The camera the game would render the open scene through: the enabled document camera with the
|
|
5066
|
+
* highest priority, the first one on a tie, as the engine picks its main camera.
|
|
5067
|
+
*
|
|
5068
|
+
* @param app - The scene view's app.
|
|
5069
|
+
* @param mirror - The open document.
|
|
5070
|
+
* @returns The camera, or `null` when the document has no enabled one.
|
|
5071
|
+
*/
|
|
5072
|
+
function documentCamera(app, mirror) {
|
|
5073
|
+
const scene = mirror.scene;
|
|
5074
|
+
let best = null;
|
|
5075
|
+
for (const camera of app.world.components(Camera)) {
|
|
5076
|
+
if (camera.entity.scene !== scene || !camera.isEnabledInHierarchy) continue;
|
|
5077
|
+
if (best === null || camera.priority > best.priority) best = camera;
|
|
5078
|
+
}
|
|
5079
|
+
return best;
|
|
5080
|
+
}
|
|
5081
|
+
/** The game view's state and texture. */
|
|
5082
|
+
var GameView = class {
|
|
5083
|
+
#enabled = false;
|
|
5084
|
+
#width = GAME_VIEW_DEFAULT_SIZE.width;
|
|
5085
|
+
#height = GAME_VIEW_DEFAULT_SIZE.height;
|
|
5086
|
+
#texture = null;
|
|
5087
|
+
/**
|
|
5088
|
+
* `setGameView`: turns the view on or off, or resizes it. Off frees the texture.
|
|
5089
|
+
*
|
|
5090
|
+
* @param app - The scene view's app.
|
|
5091
|
+
* @param params - `{ enabled, width?, height? }`.
|
|
5092
|
+
* @returns The view as it now is.
|
|
5093
|
+
*/
|
|
5094
|
+
set(app, params) {
|
|
5095
|
+
if (!isSetGameViewParams(params)) throw new InvalidParamsError("setGameView takes { enabled: boolean, width?, height? } with sides from 1 to 2048.");
|
|
5096
|
+
const width = params.width ?? this.#width;
|
|
5097
|
+
const height = params.height ?? this.#height;
|
|
5098
|
+
if (!params.enabled || width !== this.#width || height !== this.#height) {
|
|
5099
|
+
this.#texture?.dispose();
|
|
5100
|
+
this.#texture = null;
|
|
5101
|
+
}
|
|
5102
|
+
this.#enabled = params.enabled;
|
|
5103
|
+
this.#width = width;
|
|
5104
|
+
this.#height = height;
|
|
5105
|
+
if (this.#enabled) this.#texture ??= app.renderer.createRenderTexture(width, height);
|
|
5106
|
+
return {
|
|
5107
|
+
enabled: this.#enabled,
|
|
5108
|
+
width,
|
|
5109
|
+
height
|
|
5110
|
+
};
|
|
5111
|
+
}
|
|
5112
|
+
/**
|
|
5113
|
+
* `gameViewFrame`: renders the document camera into the texture on the next frame and answers it
|
|
5114
|
+
* as an image.
|
|
5115
|
+
*
|
|
5116
|
+
* @param app - The scene view's app.
|
|
5117
|
+
* @param mirror - The open document.
|
|
5118
|
+
* @param params - `{ format, quality? }`.
|
|
5119
|
+
* @returns The image and the camera's document uid, or no image when the document has no camera.
|
|
5120
|
+
*/
|
|
5121
|
+
async frame(app, mirror, params) {
|
|
5122
|
+
if (!isGameViewFrameParams(params)) throw new InvalidParamsError("gameViewFrame takes { format: \"png\" | \"jpeg\", quality? }.");
|
|
5123
|
+
const texture = this.#texture;
|
|
5124
|
+
if (!this.#enabled || texture === null) throw new RpcFailure(RpcErrorCode.invalidRequest, "The game view is off; send \"setGameView\" with enabled: true first.");
|
|
5125
|
+
const size = {
|
|
5126
|
+
width: this.#width,
|
|
5127
|
+
height: this.#height
|
|
5128
|
+
};
|
|
5129
|
+
const camera = documentCamera(app, mirror);
|
|
5130
|
+
if (camera === null) return {
|
|
5131
|
+
dataUrl: null,
|
|
5132
|
+
...size,
|
|
5133
|
+
camera: null
|
|
5134
|
+
};
|
|
5135
|
+
const uid = (mirror.documentEntityOf(camera.entity) ?? camera.entity).uid;
|
|
5136
|
+
await app.renderer.renderToTexture(camera, texture);
|
|
5137
|
+
return {
|
|
5138
|
+
dataUrl: await encodeCapture(await texture.readPixels(), size, params.format, params.quality ?? DEFAULT_JPEG_QUALITY$1),
|
|
5139
|
+
...size,
|
|
5140
|
+
camera: uid
|
|
5141
|
+
};
|
|
5142
|
+
}
|
|
5143
|
+
/** Frees the texture. */
|
|
5144
|
+
dispose() {
|
|
5145
|
+
this.#texture?.dispose();
|
|
5146
|
+
this.#texture = null;
|
|
5147
|
+
this.#enabled = false;
|
|
5148
|
+
}
|
|
5149
|
+
};
|
|
5150
|
+
//#endregion
|
|
4797
5151
|
//#region src/scene/gizmo-drag.ts
|
|
4798
5152
|
/**
|
|
4799
5153
|
* Turns one gizmo drag into the scene view's drag notifications. It needs no GPU: the view calls
|
|
@@ -5953,87 +6307,6 @@ function registeredAncestors(registry, type) {
|
|
|
5953
6307
|
return ids;
|
|
5954
6308
|
}
|
|
5955
6309
|
//#endregion
|
|
5956
|
-
//#region src/scene/screenshot.ts
|
|
5957
|
-
/**
|
|
5958
|
-
* `screenshot`, shared by the scene view and the play view: the parameter check, and a captured
|
|
5959
|
-
* frame scaled to the requested size and written as a PNG or JPEG data URL. Encoding needs a
|
|
5960
|
-
* browser's `OffscreenCanvas`, as the capture needs a GPU.
|
|
5961
|
-
*/
|
|
5962
|
-
/** The JPEG quality when the request gives none. */
|
|
5963
|
-
const DEFAULT_JPEG_QUALITY = .9;
|
|
5964
|
-
/** Bytes encoded per `String.fromCharCode` call, to stay under engines' argument limits. */
|
|
5965
|
-
const BASE64_CHUNK = 32768;
|
|
5966
|
-
/**
|
|
5967
|
-
* Encodes bytes as base64.
|
|
5968
|
-
*
|
|
5969
|
-
* @param bytes - The bytes.
|
|
5970
|
-
* @returns The base64 text.
|
|
5971
|
-
*/
|
|
5972
|
-
function toBase64(bytes) {
|
|
5973
|
-
let binary = "";
|
|
5974
|
-
for (let start = 0; start < bytes.length; start += BASE64_CHUNK) binary += String.fromCharCode(...bytes.subarray(start, start + BASE64_CHUNK));
|
|
5975
|
-
return btoa(binary);
|
|
5976
|
-
}
|
|
5977
|
-
/**
|
|
5978
|
-
* Scales a captured frame to a size and encodes it as a data URL.
|
|
5979
|
-
*
|
|
5980
|
-
* @param capture - The frame, RGBA8 with the top row first.
|
|
5981
|
-
* @param size - The image size.
|
|
5982
|
-
* @param format - `"png"` or `"jpeg"`.
|
|
5983
|
-
* @param quality - The JPEG quality, from 0 to 1; ignored for PNG.
|
|
5984
|
-
* @returns A `data:image/png;base64,…` or `data:image/jpeg;base64,…` URL.
|
|
5985
|
-
* @throws Error when the browser has no 2D canvas context.
|
|
5986
|
-
*/
|
|
5987
|
-
async function encodeCapture(capture, size, format, quality) {
|
|
5988
|
-
const source = new OffscreenCanvas(capture.width, capture.height);
|
|
5989
|
-
const sourceContext = source.getContext("2d");
|
|
5990
|
-
const target = new OffscreenCanvas(size.width, size.height);
|
|
5991
|
-
const targetContext = target.getContext("2d");
|
|
5992
|
-
if (sourceContext === null || targetContext === null) throw new Error("This browser has no 2D canvas to encode the screenshot with.");
|
|
5993
|
-
sourceContext.putImageData(new ImageData(new Uint8ClampedArray(capture.data), capture.width, capture.height), 0, 0);
|
|
5994
|
-
targetContext.imageSmoothingQuality = "high";
|
|
5995
|
-
targetContext.drawImage(source, 0, 0, size.width, size.height);
|
|
5996
|
-
const type = format === "png" ? "image/png" : "image/jpeg";
|
|
5997
|
-
const blob = await target.convertToBlob(format === "png" ? { type } : {
|
|
5998
|
-
type,
|
|
5999
|
-
quality
|
|
6000
|
-
});
|
|
6001
|
-
return `data:${type};base64,${toBase64(new Uint8Array(await blob.arrayBuffer()))}`;
|
|
6002
|
-
}
|
|
6003
|
-
/**
|
|
6004
|
-
* Checks `screenshot`'s parameters.
|
|
6005
|
-
*
|
|
6006
|
-
* @param params - The received `params`.
|
|
6007
|
-
* @returns The parameters.
|
|
6008
|
-
* @throws InvalidParamsError for a side outside 1 to {@link SCREENSHOT_MAX_SIZE}, an unknown format, or
|
|
6009
|
-
* a quality outside 0 to 1.
|
|
6010
|
-
*/
|
|
6011
|
-
function checkScreenshotParams(params) {
|
|
6012
|
-
if (typeof params === "object" && params !== null) for (const side of ["width", "height"]) {
|
|
6013
|
-
const value = Reflect.get(params, side);
|
|
6014
|
-
if (value !== void 0 && (!Number.isSafeInteger(value) || Number(value) < 1 || Number(value) > 2048)) throw new InvalidParamsError(`screenshot takes a width and height from 1 to ${String(SCREENSHOT_MAX_SIZE)} pixels.`);
|
|
6015
|
-
}
|
|
6016
|
-
if (!isScreenshotParams(params)) throw new InvalidParamsError("screenshot takes { width?, height?, format: \"png\" | \"jpeg\", quality? }.");
|
|
6017
|
-
return params;
|
|
6018
|
-
}
|
|
6019
|
-
/**
|
|
6020
|
-
* Captures the app's next presented frame and encodes it as `screenshot` answers.
|
|
6021
|
-
*
|
|
6022
|
-
* @param app - The app whose canvas is captured.
|
|
6023
|
-
* @param params - Checked parameters.
|
|
6024
|
-
* @returns The data URL and its size.
|
|
6025
|
-
* @throws IgnifxError `IGX-0707` when the app's render loop does not run.
|
|
6026
|
-
*/
|
|
6027
|
-
async function takeScreenshot(app, params) {
|
|
6028
|
-
const capture = await app.renderer.captureScreenshot();
|
|
6029
|
-
const size = screenshotSize(params, capture);
|
|
6030
|
-
return {
|
|
6031
|
-
dataUrl: await encodeCapture(capture, size, params.format, params.quality ?? DEFAULT_JPEG_QUALITY),
|
|
6032
|
-
width: size.width,
|
|
6033
|
-
height: size.height
|
|
6034
|
-
};
|
|
6035
|
-
}
|
|
6036
|
-
//#endregion
|
|
6037
6310
|
//#region src/scene/settings.ts
|
|
6038
6311
|
/**
|
|
6039
6312
|
* `describeSettings`: the project's settings sections, as the editor's settings panel reads them,
|
|
@@ -6108,6 +6381,311 @@ function settingsSet(app) {
|
|
|
6108
6381
|
};
|
|
6109
6382
|
}
|
|
6110
6383
|
//#endregion
|
|
6384
|
+
//#region src/scene/thumbnails.ts
|
|
6385
|
+
/**
|
|
6386
|
+
* `renderThumbnail` (engine ask E8): a model or prefab rendered on its own, in a second app the
|
|
6387
|
+
* host keeps for the purpose, through `app.renderer.renderToTexture`.
|
|
6388
|
+
*
|
|
6389
|
+
* The stage is a separate app on a 1x1 `OffscreenCanvas`, built from the project's own options, so
|
|
6390
|
+
* a thumbnail never shows the open document, never lights it, and is never lit by it. It is
|
|
6391
|
+
* created on the first request and lives until the host is disposed.
|
|
6392
|
+
*/
|
|
6393
|
+
/** The JPEG quality when the request gives none, as for `screenshot`. */
|
|
6394
|
+
const DEFAULT_JPEG_QUALITY = .9;
|
|
6395
|
+
/** The stage camera's vertical field of view, in degrees. */
|
|
6396
|
+
const STAGE_FOV = 35;
|
|
6397
|
+
/** The texture is this many times the image's side, and scaled down when encoded. */
|
|
6398
|
+
const SUPERSAMPLE = 2;
|
|
6399
|
+
/** How many frames a load is given to build its meshes before the thumbnail is taken anyway. */
|
|
6400
|
+
const MAX_SETTLE_FRAMES = 120;
|
|
6401
|
+
/** How many frames in a row the bounds must hold still before the thumbnail is taken. */
|
|
6402
|
+
const STILL_FRAMES = 2;
|
|
6403
|
+
/** The direction from the subject to the camera: in front, above and to the right. */
|
|
6404
|
+
const VIEW_DIRECTION = {
|
|
6405
|
+
x: .55,
|
|
6406
|
+
y: .45,
|
|
6407
|
+
z: -.7
|
|
6408
|
+
};
|
|
6409
|
+
/**
|
|
6410
|
+
* Types the requested asset.
|
|
6411
|
+
*
|
|
6412
|
+
* @param params - Checked parameters.
|
|
6413
|
+
* @returns The address and what it is.
|
|
6414
|
+
* @throws InvalidParamsError for an asset that is neither a model nor a prefab.
|
|
6415
|
+
*/
|
|
6416
|
+
function thumbnailSubject(params) {
|
|
6417
|
+
const asset = params.asset;
|
|
6418
|
+
const address = typeof asset === "string" ? asset : asset.$asset;
|
|
6419
|
+
const type = typeof asset === "string" ? void 0 : asset.type;
|
|
6420
|
+
const lower = address.toLowerCase();
|
|
6421
|
+
if (type === "model" || type === void 0 && (lower.endsWith(".glb") || lower.endsWith(".gltf"))) return {
|
|
6422
|
+
address,
|
|
6423
|
+
kind: "model"
|
|
6424
|
+
};
|
|
6425
|
+
if (type === "scene" || type === "prefab" || type === void 0 && (lower.endsWith(".prefab.json") || lower.endsWith(".scene.json"))) return {
|
|
6426
|
+
address,
|
|
6427
|
+
kind: "prefab"
|
|
6428
|
+
};
|
|
6429
|
+
throw new InvalidParamsError(`renderThumbnail renders models (.glb, .gltf) and prefabs (.prefab.json); ${address} is neither.`);
|
|
6430
|
+
}
|
|
6431
|
+
/**
|
|
6432
|
+
* The one-entity scene file that shows a subject.
|
|
6433
|
+
*
|
|
6434
|
+
* @param address - The asset address.
|
|
6435
|
+
* @param kind - What it is.
|
|
6436
|
+
* @returns The file.
|
|
6437
|
+
*/
|
|
6438
|
+
function thumbnailScene(address, kind) {
|
|
6439
|
+
const transform = {
|
|
6440
|
+
position: [
|
|
6441
|
+
0,
|
|
6442
|
+
0,
|
|
6443
|
+
0
|
|
6444
|
+
],
|
|
6445
|
+
rotation: [
|
|
6446
|
+
0,
|
|
6447
|
+
0,
|
|
6448
|
+
0,
|
|
6449
|
+
1
|
|
6450
|
+
],
|
|
6451
|
+
scale: [
|
|
6452
|
+
1,
|
|
6453
|
+
1,
|
|
6454
|
+
1
|
|
6455
|
+
]
|
|
6456
|
+
};
|
|
6457
|
+
return {
|
|
6458
|
+
format: "ignifx.scene",
|
|
6459
|
+
formatVersion: 1,
|
|
6460
|
+
name: "thumbnail",
|
|
6461
|
+
entities: [kind === "model" ? {
|
|
6462
|
+
uid: "thumbnail-subject",
|
|
6463
|
+
name: "Thumbnail",
|
|
6464
|
+
parent: null,
|
|
6465
|
+
transform,
|
|
6466
|
+
components: [{
|
|
6467
|
+
uid: "thumbnail-model",
|
|
6468
|
+
type: "ignifx/Model",
|
|
6469
|
+
props: { model: {
|
|
6470
|
+
$asset: address,
|
|
6471
|
+
type: "model"
|
|
6472
|
+
} }
|
|
6473
|
+
}]
|
|
6474
|
+
} : {
|
|
6475
|
+
uid: "thumbnail-subject",
|
|
6476
|
+
name: "Thumbnail",
|
|
6477
|
+
parent: null,
|
|
6478
|
+
transform,
|
|
6479
|
+
instance: { scene: { $asset: address } }
|
|
6480
|
+
}]
|
|
6481
|
+
};
|
|
6482
|
+
}
|
|
6483
|
+
/** Renders thumbnails on a stage app of their own. */
|
|
6484
|
+
var ThumbnailRenderer = class {
|
|
6485
|
+
#createApp;
|
|
6486
|
+
#stage = null;
|
|
6487
|
+
#texture = null;
|
|
6488
|
+
#loads = 0;
|
|
6489
|
+
#isDisposed = false;
|
|
6490
|
+
/**
|
|
6491
|
+
* Keeps the factory; nothing is created until the first thumbnail.
|
|
6492
|
+
*
|
|
6493
|
+
* @param createApp - Creates the stage app from the project's options, unstarted.
|
|
6494
|
+
*/
|
|
6495
|
+
constructor(createApp) {
|
|
6496
|
+
this.#createApp = createApp;
|
|
6497
|
+
}
|
|
6498
|
+
/**
|
|
6499
|
+
* `renderThumbnail`: loads the asset alone on the stage, frames it, renders it and encodes it.
|
|
6500
|
+
*
|
|
6501
|
+
* @param params - `{ asset, size?, format, quality? }`.
|
|
6502
|
+
* @returns The image.
|
|
6503
|
+
*/
|
|
6504
|
+
async render(params) {
|
|
6505
|
+
if (!isRenderThumbnailParams(params)) throw new InvalidParamsError("renderThumbnail takes { asset: address | { $asset, type? }, size?: 1 to 1024, format: \"png\" | \"jpeg\", quality? }.");
|
|
6506
|
+
const { address, kind } = thumbnailSubject(params);
|
|
6507
|
+
const { app, camera } = await (this.#stage ??= this.#open());
|
|
6508
|
+
this.#loads += 1;
|
|
6509
|
+
const instance = await app.world.loadSceneFile(thumbnailScene(address, kind), {
|
|
6510
|
+
address: `__ignifx_thumbnail__/${String(this.#loads)}.scene.json`,
|
|
6511
|
+
mode: "single"
|
|
6512
|
+
});
|
|
6513
|
+
try {
|
|
6514
|
+
frameCamera(camera, await settledBounds(app, instance));
|
|
6515
|
+
await nextFrame();
|
|
6516
|
+
const size = params.size ?? 128;
|
|
6517
|
+
const texture = this.#textureOf(app, size * SUPERSAMPLE);
|
|
6518
|
+
await app.renderer.renderToTexture(camera, texture);
|
|
6519
|
+
return {
|
|
6520
|
+
dataUrl: await encodeCapture(await texture.readPixels(), {
|
|
6521
|
+
width: size,
|
|
6522
|
+
height: size
|
|
6523
|
+
}, params.format, params.quality ?? DEFAULT_JPEG_QUALITY),
|
|
6524
|
+
width: size,
|
|
6525
|
+
height: size
|
|
6526
|
+
};
|
|
6527
|
+
} finally {
|
|
6528
|
+
await app.world.unloadScene(instance);
|
|
6529
|
+
}
|
|
6530
|
+
}
|
|
6531
|
+
/** Disposes the stage app, now or once it has been created. */
|
|
6532
|
+
dispose() {
|
|
6533
|
+
this.#isDisposed = true;
|
|
6534
|
+
this.#texture = null;
|
|
6535
|
+
this.#stage?.then((stage) => {
|
|
6536
|
+
stage.app.dispose();
|
|
6537
|
+
}, () => null);
|
|
6538
|
+
}
|
|
6539
|
+
/**
|
|
6540
|
+
* Creates and starts the stage: a persistent scene with the camera and two lights.
|
|
6541
|
+
*
|
|
6542
|
+
* @returns The stage.
|
|
6543
|
+
*/
|
|
6544
|
+
async #open() {
|
|
6545
|
+
const app = await this.#createApp();
|
|
6546
|
+
const scene = app.world.createScene("thumbnail-stage", { persistent: true });
|
|
6547
|
+
const camera = app.world.createEntity("Thumbnail Camera", { scene }).addComponent(Camera, {
|
|
6548
|
+
fov: STAGE_FOV,
|
|
6549
|
+
near: .01,
|
|
6550
|
+
far: 1e3
|
|
6551
|
+
});
|
|
6552
|
+
app.world.createEntity("Thumbnail Sky", { scene }).addComponent(Light, {
|
|
6553
|
+
type: "hemispheric",
|
|
6554
|
+
intensity: .8
|
|
6555
|
+
});
|
|
6556
|
+
const sun = app.world.createEntity("Thumbnail Sun", { scene });
|
|
6557
|
+
sun.transform.localPosition.set(4, 6, -5);
|
|
6558
|
+
sun.transform.lookAt({
|
|
6559
|
+
x: 0,
|
|
6560
|
+
y: 0,
|
|
6561
|
+
z: 0
|
|
6562
|
+
});
|
|
6563
|
+
sun.addComponent(Light, {
|
|
6564
|
+
type: "directional",
|
|
6565
|
+
intensity: 2
|
|
6566
|
+
});
|
|
6567
|
+
await app.start();
|
|
6568
|
+
if (this.#isDisposed) app.dispose();
|
|
6569
|
+
return {
|
|
6570
|
+
app,
|
|
6571
|
+
camera
|
|
6572
|
+
};
|
|
6573
|
+
}
|
|
6574
|
+
/**
|
|
6575
|
+
* The stage's texture at a side, created again when the side changes.
|
|
6576
|
+
*
|
|
6577
|
+
* @param app - The stage app.
|
|
6578
|
+
* @param side - The side in pixels.
|
|
6579
|
+
* @returns The texture.
|
|
6580
|
+
*/
|
|
6581
|
+
#textureOf(app, side) {
|
|
6582
|
+
const current = this.#texture;
|
|
6583
|
+
if (current !== null && current.width === side && !current.isDisposed) return current;
|
|
6584
|
+
current?.dispose();
|
|
6585
|
+
const texture = app.renderer.createRenderTexture(side, side);
|
|
6586
|
+
this.#texture = texture;
|
|
6587
|
+
return texture;
|
|
6588
|
+
}
|
|
6589
|
+
};
|
|
6590
|
+
/**
|
|
6591
|
+
* Waits one animation frame.
|
|
6592
|
+
*
|
|
6593
|
+
* @returns A promise that settles in the next frame callback.
|
|
6594
|
+
*/
|
|
6595
|
+
function nextFrame() {
|
|
6596
|
+
return new Promise((resolve) => {
|
|
6597
|
+
requestAnimationFrame(() => {
|
|
6598
|
+
resolve();
|
|
6599
|
+
});
|
|
6600
|
+
});
|
|
6601
|
+
}
|
|
6602
|
+
/**
|
|
6603
|
+
* Waits until a loaded subject's meshes exist and hold still, then measures them.
|
|
6604
|
+
*
|
|
6605
|
+
* @param app - The stage app.
|
|
6606
|
+
* @param instance - The loaded subject.
|
|
6607
|
+
* @returns Its world bounds; a unit box at the origin when nothing draws.
|
|
6608
|
+
*/
|
|
6609
|
+
async function settledBounds(app, instance) {
|
|
6610
|
+
let last = null;
|
|
6611
|
+
let still = 0;
|
|
6612
|
+
for (let frame = 0; frame < MAX_SETTLE_FRAMES && still < STILL_FRAMES; frame += 1) {
|
|
6613
|
+
await nextFrame();
|
|
6614
|
+
const bounds = unionBounds(app, instance.roots);
|
|
6615
|
+
still = bounds !== null && last !== null && sameBounds(bounds, last) ? still + 1 : 0;
|
|
6616
|
+
last = bounds;
|
|
6617
|
+
}
|
|
6618
|
+
return last ?? {
|
|
6619
|
+
min: {
|
|
6620
|
+
x: -.5,
|
|
6621
|
+
y: -.5,
|
|
6622
|
+
z: -.5
|
|
6623
|
+
},
|
|
6624
|
+
max: {
|
|
6625
|
+
x: .5,
|
|
6626
|
+
y: .5,
|
|
6627
|
+
z: .5
|
|
6628
|
+
}
|
|
6629
|
+
};
|
|
6630
|
+
}
|
|
6631
|
+
/**
|
|
6632
|
+
* The union of entities' world bounds.
|
|
6633
|
+
*
|
|
6634
|
+
* @param app - The stage app.
|
|
6635
|
+
* @param entities - The entities.
|
|
6636
|
+
* @returns The box, or `null` when none draws.
|
|
6637
|
+
*/
|
|
6638
|
+
function unionBounds(app, entities) {
|
|
6639
|
+
let result = null;
|
|
6640
|
+
for (const entity of entities) {
|
|
6641
|
+
const box = entityWorldBounds(app.lite, entity);
|
|
6642
|
+
if (box === null) continue;
|
|
6643
|
+
result = result === null ? box : {
|
|
6644
|
+
min: {
|
|
6645
|
+
x: Math.min(result.min.x, box.min.x),
|
|
6646
|
+
y: Math.min(result.min.y, box.min.y),
|
|
6647
|
+
z: Math.min(result.min.z, box.min.z)
|
|
6648
|
+
},
|
|
6649
|
+
max: {
|
|
6650
|
+
x: Math.max(result.max.x, box.max.x),
|
|
6651
|
+
y: Math.max(result.max.y, box.max.y),
|
|
6652
|
+
z: Math.max(result.max.z, box.max.z)
|
|
6653
|
+
}
|
|
6654
|
+
};
|
|
6655
|
+
}
|
|
6656
|
+
return result;
|
|
6657
|
+
}
|
|
6658
|
+
/**
|
|
6659
|
+
* Tells whether two boxes are equal.
|
|
6660
|
+
*
|
|
6661
|
+
* @param a - One box.
|
|
6662
|
+
* @param b - The other.
|
|
6663
|
+
* @returns True for the same corners.
|
|
6664
|
+
*/
|
|
6665
|
+
function sameBounds(a, b) {
|
|
6666
|
+
return a.min.x === b.min.x && a.min.y === b.min.y && a.min.z === b.min.z && a.max.x === b.max.x && a.max.y === b.max.y && a.max.z === b.max.z;
|
|
6667
|
+
}
|
|
6668
|
+
/**
|
|
6669
|
+
* Places the stage camera so a box fills the view, with its clip planes around it.
|
|
6670
|
+
*
|
|
6671
|
+
* @param camera - The stage camera.
|
|
6672
|
+
* @param bounds - The subject's bounds.
|
|
6673
|
+
*/
|
|
6674
|
+
function frameCamera(camera, bounds) {
|
|
6675
|
+
const center = {
|
|
6676
|
+
x: (bounds.min.x + bounds.max.x) / 2,
|
|
6677
|
+
y: (bounds.min.y + bounds.max.y) / 2,
|
|
6678
|
+
z: (bounds.min.z + bounds.max.z) / 2
|
|
6679
|
+
};
|
|
6680
|
+
const radius = Math.max(Math.hypot(bounds.max.x - center.x, bounds.max.y - center.y, bounds.max.z - center.z), .01);
|
|
6681
|
+
const distance = radius / Math.sin(STAGE_FOV / 2 * Math.PI / 180) * 1.05;
|
|
6682
|
+
const length = Math.hypot(VIEW_DIRECTION.x, VIEW_DIRECTION.y, VIEW_DIRECTION.z);
|
|
6683
|
+
camera.transform.localPosition.set(center.x + VIEW_DIRECTION.x / length * distance, center.y + VIEW_DIRECTION.y / length * distance, center.z + VIEW_DIRECTION.z / length * distance);
|
|
6684
|
+
camera.transform.lookAt(center);
|
|
6685
|
+
camera.near = Math.max(distance - radius * 1.5, distance / 1e3);
|
|
6686
|
+
camera.far = distance + radius * 1.5;
|
|
6687
|
+
}
|
|
6688
|
+
//#endregion
|
|
6111
6689
|
//#region src/scene/scene-host.ts
|
|
6112
6690
|
/**
|
|
6113
6691
|
* The scene methods and notifications of protocol 0.3: what the scene view's host does with the
|
|
@@ -6150,6 +6728,8 @@ var SceneHost = class {
|
|
|
6150
6728
|
snap: null
|
|
6151
6729
|
};
|
|
6152
6730
|
#drag;
|
|
6731
|
+
#gameView = new GameView();
|
|
6732
|
+
#thumbnails;
|
|
6153
6733
|
#started = false;
|
|
6154
6734
|
/** The field defaults seen at start and after each hot reload, from {@link SceneHost.start} on. */
|
|
6155
6735
|
#defaults = null;
|
|
@@ -6185,6 +6765,24 @@ var SceneHost = class {
|
|
|
6185
6765
|
]);
|
|
6186
6766
|
if (has(HostCapability.gizmos)) methods.set(SceneMethod.setGizmo, (params) => this.#setGizmo(params));
|
|
6187
6767
|
if (has(HostCapability.screenshot)) methods.set(SceneMethod.screenshot, async (params) => this.#screenshot(params));
|
|
6768
|
+
if (has(HostCapability.gameView)) {
|
|
6769
|
+
methods.set(RenderViewMethod.setGameView, (params) => {
|
|
6770
|
+
this.#requireMirror(RenderViewMethod.setGameView);
|
|
6771
|
+
return this.#gameView.set(this.#requireApp(), params);
|
|
6772
|
+
});
|
|
6773
|
+
methods.set(RenderViewMethod.gameViewFrame, async (params) => {
|
|
6774
|
+
const mirror = this.#requireMirror(RenderViewMethod.gameViewFrame);
|
|
6775
|
+
this.#requireOpen(RenderViewMethod.gameViewFrame);
|
|
6776
|
+
return this.#gameView.frame(this.#requireApp(), mirror, params);
|
|
6777
|
+
});
|
|
6778
|
+
}
|
|
6779
|
+
const createThumbnailApp = options.createThumbnailApp;
|
|
6780
|
+
this.#thumbnails = has(HostCapability.thumbnails) && createThumbnailApp !== void 0 ? new ThumbnailRenderer(createThumbnailApp) : null;
|
|
6781
|
+
const thumbnails = this.#thumbnails;
|
|
6782
|
+
if (thumbnails !== null) methods.set(RenderViewMethod.renderThumbnail, async (params) => {
|
|
6783
|
+
this.#requireMirror(RenderViewMethod.renderThumbnail);
|
|
6784
|
+
return thumbnails.render(params);
|
|
6785
|
+
});
|
|
6188
6786
|
this.methods = new Map([...methods].map(([name, handler]) => [name, async (params) => this.#serial(() => handler(params))]));
|
|
6189
6787
|
}
|
|
6190
6788
|
/**
|
|
@@ -6392,6 +6990,8 @@ var SceneHost = class {
|
|
|
6392
6990
|
for (const cleanup of this.#cleanups.splice(0)) cleanup();
|
|
6393
6991
|
this.#view?.dispose();
|
|
6394
6992
|
this.#view = null;
|
|
6993
|
+
this.#gameView.dispose();
|
|
6994
|
+
this.#thumbnails?.dispose();
|
|
6395
6995
|
}
|
|
6396
6996
|
/**
|
|
6397
6997
|
* `openScene`: replaces the open document with the editor's file.
|
|
@@ -7039,7 +7639,9 @@ async function startEditorHost(options) {
|
|
|
7039
7639
|
HostCapability.viewMode2d,
|
|
7040
7640
|
HostCapability.pick2d,
|
|
7041
7641
|
HostCapability.logRelay,
|
|
7042
|
-
HostCapability.hotReloadDefaults
|
|
7642
|
+
HostCapability.hotReloadDefaults,
|
|
7643
|
+
HostCapability.gameView,
|
|
7644
|
+
...canRenderThumbnails(options) ? [HostCapability.thumbnails] : []
|
|
7043
7645
|
] : [
|
|
7044
7646
|
HostCapability.heartbeat,
|
|
7045
7647
|
HostCapability.logRelay,
|
|
@@ -7050,7 +7652,8 @@ async function startEditorHost(options) {
|
|
|
7050
7652
|
capabilities,
|
|
7051
7653
|
notify: (method, params) => {
|
|
7052
7654
|
channel?.notify(method, params);
|
|
7053
|
-
}
|
|
7655
|
+
},
|
|
7656
|
+
createThumbnailApp: async () => createThumbnailApp(options)
|
|
7054
7657
|
});
|
|
7055
7658
|
const opened = openChannel({
|
|
7056
7659
|
target: resolveMessageTarget(options.messageTarget),
|
|
@@ -7113,6 +7716,34 @@ function hasGpuView(options) {
|
|
|
7113
7716
|
return options.canvas !== void 0 && options.headless !== true;
|
|
7114
7717
|
}
|
|
7115
7718
|
/**
|
|
7719
|
+
* Tells whether the host can build the thumbnail stage app: it has `OffscreenCanvas`, and either
|
|
7720
|
+
* a way to build fresh extensions or no extensions to rebuild.
|
|
7721
|
+
*
|
|
7722
|
+
* @param options - The host options.
|
|
7723
|
+
* @returns True when `renderThumbnail` can be answered.
|
|
7724
|
+
*/
|
|
7725
|
+
function canRenderThumbnails(options) {
|
|
7726
|
+
return typeof OffscreenCanvas !== "undefined" && (options.createExtensions !== void 0 || (options.extensions ?? []).length === 0);
|
|
7727
|
+
}
|
|
7728
|
+
/**
|
|
7729
|
+
* Creates the app thumbnails are rendered in: fresh instances of the project's extensions, its
|
|
7730
|
+
* settings, assets and scripts, on a 1x1 offscreen canvas of its own (the thumbnails draw into
|
|
7731
|
+
* render textures).
|
|
7732
|
+
*
|
|
7733
|
+
* @param options - The host options.
|
|
7734
|
+
* @returns The app, not started.
|
|
7735
|
+
*/
|
|
7736
|
+
async function createThumbnailApp(options) {
|
|
7737
|
+
const app = await createApp({
|
|
7738
|
+
...appOptions(options),
|
|
7739
|
+
extensions: options.createExtensions?.() ?? [],
|
|
7740
|
+
canvas: new OffscreenCanvas(1, 1),
|
|
7741
|
+
headless: false
|
|
7742
|
+
});
|
|
7743
|
+
app.registerComponents(options.scripts ?? []);
|
|
7744
|
+
return app;
|
|
7745
|
+
}
|
|
7746
|
+
/**
|
|
7116
7747
|
* Picks the `createApp` options out of the host options, leaving out the ones that were not given.
|
|
7117
7748
|
*
|
|
7118
7749
|
* @param options - The host options.
|
|
@@ -7844,6 +8475,6 @@ function listenForEditorPlayControls(options) {
|
|
|
7844
8475
|
};
|
|
7845
8476
|
}
|
|
7846
8477
|
//#endregion
|
|
7847
|
-
export { ApplyOpFailureCode, COLLIDER_OUTLINES_MAX_ENTITIES, COLLIDER_OUTLINES_MAX_NUMBERS, DEFAULT_HEARTBEAT_INTERVAL_MS, EDITOR_CONNECT_MESSAGE_TYPE, EditorHostErrorCode, GizmoMode, GizmoSpace, HOST_LIST_MAX_LENGTH, HOST_TEXT_MAX_LENGTH, HandshakeMethod, HostCapability, HostKind, HostNotification, JSON_RPC_VERSION, PROTOCOL_VERSION, PickUnit, PlayMethod, PlayNotification, PlayPhase, QUERY_RUNTIME_MAX_COMPONENTS, QUERY_RUNTIME_MAX_JSON_LENGTH, QUERY_RUNTIME_MAX_KEYS, QUERY_RUNTIME_MAX_STRING_LENGTH, RpcErrorCode, SAMPLE_ANIMATION_MAX_NAME_LENGTH, SAMPLE_ANIMATION_MAX_PARAMETERS, SCREENSHOT_MAX_SIZE, SEND_INPUT_MAX_EVENTS, SceneMethod, SceneNotification, SceneOpKind, ScreenshotFormat, TRANSFORM_DRAGGING_INTERVAL_MS, ViewMode, isAppErrorData, isApplyOpsParams, isApplyOpsResult, isAssetReloadedParams, isColliderOutlineRecord, isColliderOutlineSet, isEditorConnectMessage, isEditorOrigin, isEntitySelection, isFrameParams, isHostErrorParams, isHotReloadedParams, isIncompatibleProtocolData, isInvalidSceneFileData, isJsonObject, isJsonValue, isLogParams, isOpenSceneParams, isOpenSceneResult, isPickParams, isPickResult, isPlayState, isPlayStateReport, isPlayStats, isProtocolVersion, isQueryRuntimeParams, isQueryRuntimeResult, isReadBackParams, isReadBackResult, isReadyResult, isRpcMessage, isSampleAnimationParams, isSampleAnimationResult, isSceneFile, isSceneFileAssetRef, isSceneFileComponent, isSceneFileEntity, isSceneFileInstance, isSceneFileOverride, isSceneFileTransform, isSceneIssue, isSceneOp, isSceneOpenedParams, isSchemaSet, isScreenshotParams, isScreenshotResult, isSendInputEvent, isSendInputParams, isSendInputResult, isSetAnimateMaterialsParams, isSetGizmoParams, isSetViewModeParams, isSettingsSet, isTransformDraggedParams, isTransformDraggingParams, listenForEditorPlayControls, protocolVersionIsCompatible, startEditorHost, startEditorPlayControls };
|
|
8478
|
+
export { ApplyOpFailureCode, COLLIDER_OUTLINES_MAX_ENTITIES, COLLIDER_OUTLINES_MAX_NUMBERS, DEFAULT_HEARTBEAT_INTERVAL_MS, EDITOR_CONNECT_MESSAGE_TYPE, EditorHostErrorCode, GAME_VIEW_DEFAULT_SIZE, GAME_VIEW_MAX_SIZE, GizmoMode, GizmoSpace, HOST_LIST_MAX_LENGTH, HOST_TEXT_MAX_LENGTH, HandshakeMethod, HostCapability, HostKind, HostNotification, JSON_RPC_VERSION, PROTOCOL_VERSION, PickUnit, PlayMethod, PlayNotification, PlayPhase, QUERY_RUNTIME_MAX_COMPONENTS, QUERY_RUNTIME_MAX_JSON_LENGTH, QUERY_RUNTIME_MAX_KEYS, QUERY_RUNTIME_MAX_STRING_LENGTH, RenderViewMethod, RpcErrorCode, SAMPLE_ANIMATION_MAX_NAME_LENGTH, SAMPLE_ANIMATION_MAX_PARAMETERS, SCREENSHOT_MAX_SIZE, SEND_INPUT_MAX_EVENTS, SceneMethod, SceneNotification, SceneOpKind, ScreenshotFormat, THUMBNAIL_DEFAULT_SIZE, THUMBNAIL_MAX_SIZE, TRANSFORM_DRAGGING_INTERVAL_MS, ViewMode, isAppErrorData, isApplyOpsParams, isApplyOpsResult, isAssetReloadedParams, isColliderOutlineRecord, isColliderOutlineSet, isEditorConnectMessage, isEditorOrigin, isEntitySelection, isFrameParams, isGameViewFrameParams, isGameViewFrameResult, isGameViewState, isHostErrorParams, isHotReloadedParams, isIncompatibleProtocolData, isInvalidSceneFileData, isJsonObject, isJsonValue, isLogParams, isOpenSceneParams, isOpenSceneResult, isPickParams, isPickResult, isPlayState, isPlayStateReport, isPlayStats, isProtocolVersion, isQueryRuntimeParams, isQueryRuntimeResult, isReadBackParams, isReadBackResult, isReadyResult, isRenderThumbnailParams, isRpcMessage, isSampleAnimationParams, isSampleAnimationResult, isSceneFile, isSceneFileAssetRef, isSceneFileComponent, isSceneFileEntity, isSceneFileInstance, isSceneFileOverride, isSceneFileTransform, isSceneIssue, isSceneOp, isSceneOpenedParams, isSchemaSet, isScreenshotParams, isScreenshotResult, isSendInputEvent, isSendInputParams, isSendInputResult, isSetAnimateMaterialsParams, isSetGameViewParams, isSetGizmoParams, isSetViewModeParams, isSettingsSet, isThumbnailResult, isTransformDraggedParams, isTransformDraggingParams, listenForEditorPlayControls, protocolVersionIsCompatible, startEditorHost, startEditorPlayControls };
|
|
7848
8479
|
|
|
7849
8480
|
//# sourceMappingURL=index.js.map
|