Skip to content

Commit

Permalink
Editor example updates (#7319)
Browse files Browse the repository at this point in the history
* WIP: outline renderer

* Added outline to editor example

* Added destroy handler to viewcube

* Removed mouse and keyboard
  • Loading branch information
kpal81xd authored Jan 30, 2025
1 parent 292802d commit 535f906
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
31 changes: 25 additions & 6 deletions examples/src/examples/misc/editor.example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);

const createOptions = new pc.AppOptions();
createOptions.graphicsDevice = device;
createOptions.mouse = new pc.Mouse(document.body);
createOptions.keyboard = new pc.Keyboard(window);

createOptions.componentSystems = [
pc.RenderComponentSystem,
Expand All @@ -44,7 +42,6 @@ app.setCanvasResolution(pc.RESOLUTION_AUTO);

// load assets
const assets = {
script: new pc.Asset('script', 'script', { url: `${rootPath}/static/scripts/camera/orbit-camera.js` }),
font: new pc.Asset('font', 'font', { url: `${rootPath}/static/assets/fonts/courier.json` })
};
/**
Expand Down Expand Up @@ -126,6 +123,8 @@ camera.addComponent('camera', {
const cameraOffset = 4 * camera.camera?.aspectRatio;
camera.setPosition(cameraOffset, cameraOffset, cameraOffset);
app.root.addChild(camera);

// camera controls
const cameraControls = /** @type {CameraControls} */ (camera.script.create(CameraControls, {
properties: {
focusPoint: pc.Vec3.ZERO,
Expand All @@ -142,6 +141,15 @@ app.on('gizmo:pointer', (/** @type {boolean} */ hasPointer) => {
}
});

// outline renderer
const outlineLayer = new pc.Layer({ name: 'OutlineLayer' });
app.scene.layers.push(outlineLayer);
const immediateLayer = /** @type {pc.Layer} */ (app.scene.layers.getLayerByName('Immediate'));
const outlineRenderer = new pc.OutlineRenderer(app, outlineLayer);
app.on('update', () => {
outlineRenderer.frameUpdate(camera, immediateLayer, false);
});

// grid
const gridEntity = new pc.Entity('grid');
gridEntity.setLocalScale(8, 1, 8);
Expand Down Expand Up @@ -181,8 +189,6 @@ const setGizmoControls = () => {
};
gizmoHandler.switch('translate');
setGizmoControls();
gizmoHandler.add(box);
window.focus();

// view cube
const viewCube = new pc.ViewCube(new pc.Vec4(0, 1, 1, 0));
Expand Down Expand Up @@ -211,11 +217,16 @@ app.on('prerender', () => {
// selector
const layers = app.scene.layers;
const selector = new Selector(app, camera.camera, [layers.getLayerByName('World')]);
selector.on('select', (/** @type {pc.GraphNode} */ node, /** @type {boolean} */ clear) => {
selector.on('select', (/** @type {pc.Entity} */ node, /** @type {boolean} */ clear) => {
gizmoHandler.add(node, clear);
if (clear) {
outlineRenderer.removeAllEntities();
}
outlineRenderer.addEntity(node, pc.Color.WHITE);
});
selector.on('deselect', () => {
gizmoHandler.clear();
outlineRenderer.removeAllEntities();
});

// ensure canvas is resized when window changes size + keep gizmo size consistent to canvas size
Expand Down Expand Up @@ -344,14 +355,22 @@ data.on('*:set', (/** @type {string} */ path, /** @type {any} */ value) => {
}
});

// destroy handlers
app.on('destroy', () => {
gizmoHandler.destroy();
selector.destroy();
viewCube.destroy();

window.removeEventListener('resize', resize);
window.removeEventListener('keydown', keydown);
window.removeEventListener('keyup', keyup);
window.removeEventListener('keypress', keypress);
});

// initial selection
selector.fire('select', box, true);

// focus canvas
window.focus();

export { app };
5 changes: 5 additions & 0 deletions src/extras/gizmo/view-cube.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ class ViewCube extends EventHandler {

this._group.appendChild(fragment);
}

destroy() {
this.dom.remove();
this.off();
}
}

export { ViewCube };

0 comments on commit 535f906

Please sign in to comment.