Merge branch 'main' into add-inspector-v2

This commit is contained in:
rewin
2024-02-29 22:02:00 +03:00
116 changed files with 5010 additions and 1297 deletions
@@ -0,0 +1,88 @@
:host {
display: flex;
border-radius: 8px;
background: #0006;
backdrop-filter: blur(16px);
}
.sidebar, .timeline { padding: 8px; }
.sidebar { padding-inline-end: 0; }
.timeline { padding-inline-start: 0; }
.sidebar {
flex: 0 0 auto;
width: 256px;
display: flex;
flex-direction: column;
gap: 8px;
overflow: hidden;
}
.btn-keyframe {
width: 40px;
height: 36px;
border-radius: 4px;
padding: 0;
justify-content: center;
background: #7A2C34E3;
color: #FFF;
&:hover {
background: #90363F;
}
}
.timeline-nav {
gap: 0;
margin-top: 2px;
sts-button {
width: 32px;
height: 32px;
padding: 0;
justify-content: center;
background: transparent !important;
backdrop-filter: initial;
color: #FFFC;
font-size: 14px;
&:hover {
color: #FFFF;
}
&.play {
font-size: 20px;
}
}
}
.divider {
position: relative;
flex: 0 0 8px;
margin: {
right: -3px;
left: -3px;
}
cursor: ew-resize;
&::before {
content: "";
position: absolute;
top: 0;
right: 3px;
bottom: 0;
left: 3px;
background: #FFF2;
opacity: 0;
transition: opacity 66.666ms linear;
}
&:hover::before {
opacity: 1;
}
}
.timeline {
flex: 1 0 0;
}
@@ -0,0 +1,183 @@
import * as studio from "@storyteller/studio";
import { type CameraKeyframesEvent } from "@storyteller/studio";
import { drag, on } from "@storyteller/framework";
import { LitElement, type PropertyValues, html, unsafeCSS } from "lit";
import { customElement, state } from "lit/decorators.js";
import { styleMap } from "lit/directives/style-map.js";
import "@storyteller/studio-ui/button";
import "@storyteller/studio-ui/toolbar";
import "@storyteller/studio-ui/tree";
import "./anim-timeline.element";
import styles from "./anim-editor.element.scss?inline";
@customElement("sts-anim-editor")
export class AnimEditorElement extends LitElement {
static override styles = unsafeCSS(styles);
@state() sidebarWidth = 256;
@state() timelineStart = 0;
@state() timelineEnd = 5_000;
@state() clipStart = 0;
@state() clipEnd = 15_000;
@state() playHead = 0;
@state() keyframes: Record<string, number> = {};
protected override update(changes: PropertyValues<this>): void {
if (changes.has("playHead"))
studio.seekTime(this.playHead);
super.update(changes);
}
@on("window:camera-keyframes")
onCameraKeyframeChanges({ detail: keyframes }: CameraKeyframesEvent): void {
this.keyframes = keyframes;
}
onSidebarResize(event: PointerEvent): void {
this.sidebarWidth = Math.max(Math.min(this.sidebarWidth + event.movementX, 512), 128);
}
addKeyframe(): void {
studio.addCameraKeyframe(this.playHead);
}
jumpToStart(): void {
this.playHead = this.clipStart;
}
prevKeyframe(): void {
let pos = this.playHead;
const keyframes = Array.from(Object.values(this.keyframes)).sort((a, b) => b - a);
for (let keyframe of keyframes) {
pos = keyframe;
if (keyframe < this.playHead)
break;
}
this.playHead = pos;
}
nextKeyframe(): void {
let pos = this.playHead;
const keyframes = Array.from(Object.values(this.keyframes)).sort((a, b) => a - b);
for (let keyframe of keyframes) {
pos = keyframe;
if (keyframe > this.playHead)
break;
}
this.playHead = pos;
}
jumpToEnd(): void {
this.playHead = this.clipEnd;
}
#onTimelineStartChange(event: CustomEvent<number>): void {
this.timelineStart = event.detail;
}
#onTimelineEndChange(event: CustomEvent<number>): void {
this.timelineEnd = event.detail;
}
#onPlayHeadChange(event: CustomEvent<number>): void {
this.playHead = event.detail;
}
#onKeyframesChange(event: CustomEvent<Record<string, number>>): void {
this.keyframes = event.detail;
}
protected override render = () => html`
<div class="sidebar"
style=${styleMap({
width: `${this.sidebarWidth}px`,
})}
>
<sts-toolbar>
<sts-toolbar-group>
<sts-button class="btn-keyframe"
icon="key"
aria-label="Add Keyframe"
title="Add Keyframe"
@click=${this.addKeyframe}
></sts-button>
</sts-toolbar-group>
<sts-toolbar-group class="timeline-nav">
<sts-button
icon="bwd-fast"
aria-label="Jump to Start"
title="Jump to Start"
@click=${this.jumpToStart}
></sts-button>
<sts-button
icon="bwd-step"
aria-label="Previous Keyframe"
title="Previous Keyframe"
@click=${this.prevKeyframe}
></sts-button>
<sts-button class="play"
icon="play"
aria-label="Play"
title="Play"
></sts-button>
<sts-button
icon="fwd-step"
aria-label="Next Keyframe"
title="Next Keyframe"
@click=${this.nextKeyframe}
></sts-button>
<sts-button
icon="fwd-fast"
aria-label="Jump to End"
title="Jump to End"
@click=${this.jumpToEnd}
></sts-button>
</sts-toolbar-group>
</sts-toolbar>
<div class="tracklist" role="list">
<sts-tree class="track"
role="listitem"
name="Camera"
icon="video"
></sts-tree>
<sts-tree class="track"
role="listitem"
name="Camera Target"
icon="crosshairs"
></sts-tree>
</div>
</div>
<div class="divider"
${drag({
move: this.onSidebarResize,
})}
></div>
<sts-anim-timeline class="timeline"
.rangeStart=${this.timelineStart}
@rangeStartChange=${this.#onTimelineStartChange}
.rangeEnd=${this.timelineEnd}
@rangeEndChange=${this.#onTimelineEndChange}
.clipStart=${this.clipStart}
.clipEnd=${this.clipEnd}
.playHead=${this.playHead}
@playHeadChange=${this.#onPlayHeadChange}
.keyframes=${this.keyframes}
@keyframesChange=${this.#onKeyframesChange}
></sts-anim-timeline>
`;
}
@@ -0,0 +1,120 @@
:host {
position: relative;
overflow: hidden;
}
*:focus-visible {
outline: none;
}
.wrapper {
position: absolute;
inset: 0;
&.panning {
cursor: grabbing;
}
}
.clip-region {
position: absolute;
top: 0;
bottom: 0;
background: #0004;
}
.hash {
position: absolute;
top: 0;
bottom: 0;
width: 2px;
background: #FFF1;
&.strong {
background: #FFF3;
}
}
.timestamp {
position: absolute;
top: 8px;
color: #FFF6;
font: {
family: "Fira Sans", sans-serif;
size: 14px;
}
user-select: none;
pointer-events: none;
z-index: 1;
.frac {
color: #FFF3;
}
}
.playhead {
position: absolute;
top: 0;
bottom: 0;
width: 8px;
transform: translateX(-3px);
cursor: ew-resize;
color: #7A2C34E3;
transition: color 66.667ms linear;
&:hover {
color: #90363F;
}
.head {
position: absolute;
top: 0;
left: 0;
width: 8px;
height: 32px;
background: currentColor;
&::after {
content: "";
box-sizing: border-box;
position: absolute;
bottom: -12px;
display: block;
width: 8px;
height: 12px;
border: 4px solid transparent {
top: 8px solid currentColor;
}
}
}
.bar {
position: absolute;
top: 36px;
bottom: 0;
left: 3px;
right: 3px;
background: currentColor;
}
}
.keyframe {
position: absolute;
top: 50%;
transform: translateX(-4.5px) translateY(-4.5px);
&::before {
content: "";
display: block;
width: 9px;
height: 9px;
background: #AAA;
transform: rotate(45deg) {
origin: center center;
}
}
&.selected::before {
background: #0FF; // TODO
}
}
@@ -0,0 +1,372 @@
import * as studio from "@storyteller/studio";
import { type EntitySelectEvent } from "@storyteller/studio";
import { drag, on } from "@storyteller/framework";
import { LitElement, PropertyValues, html, nothing, unsafeCSS } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { repeat } from "lit/directives/repeat.js";
import { styleMap } from "lit/directives/style-map.js";
import styles from "./anim-timeline.element.scss?inline";
// TODO: This is basically a [signal](https://www.solidjs.com/tutorial/introduction_signals)
// that requires manual dependency tracking. Given that requirement, it's
// questionable whether this is even a valuable abstraction to have.
//
// We should either make it more obviously valuable (e.g. by coming up
// with a way to automatically handle dependencies) and move it to
// `@storyteller/framework`, or just get rid of it.
//
// We could maybe add a decorator that could automatically override the
// `update` method to invalidate the cache when dependencies are changed,
// which would look something like:
// ```
// @memo(["rangeStart", "rangeEnd", "_renderWidth"])
// msPerPx = new Computed(() => (this.rangeEnd - this.rangeStart) / this._renderWidth);
// ```
// But that feels a little obtuse and overengineered. In this particular
// case we're probably only saving on the order of nanoseconds, so I'm
// leaning toward just nuking the idea.
class Computed extends Number {
#value?: number;
#compute: () => number;
constructor (compute: () => number) {
const value = compute();
super(value);
this.#compute = compute;
}
override valueOf(): number {
return this.#value ??= this.#compute();
}
get(): number {
return this.valueOf();
}
invalidate(): void {
this.#value = undefined;
}
}
@customElement("sts-anim-timeline")
export class AnimTimelineElement extends LitElement {
static override styles = unsafeCSS(styles);
/** The time (in ms) at the left edge of the timeline view */
@property({ type: Number }) rangeStart = 0;
/** The time (in ms) at the right edge of the timeline view */
@property({ type: Number }) rangeEnd = 5_000;
/** The time (in ms) at the beginning of the active clip range */
@property({ type: Number }) clipStart = 0;
/** The time (in ms) at the end of the active clip range */
@property({ type: Number }) clipEnd = 15_000;
/** The time (in ms) of the current play-head position */
@property({ type: Number }) playHead = 0;
/** Keyframe positions (in ms) */
@property({ attribute: false }) keyframes: Record<string, number> = {};
@state() _selectedKeyframe: string | null = null;
/** The width (in px) of the timeline view */
@state() _renderWidth!: number;
/** The offset from 0ms of the left edge of the timeline view, in px */
@state() _offsetX = 0;
/** The vertical scroll offset (in px) */
@state() _offsetY = 0;
/**
* `true` when the timeline is being mouse-panned. Primarily useful for
* setting the cursor style to the grabby hand.
*/
@state() _panning = false;
/** How many ms are represented by each px of horizontal space. */
msPerPx = new Computed(() => (this.rangeEnd - this.rangeStart) / this._renderWidth);
#resizeObserver: ResizeObserver;
constructor () {
super();
this.#resizeObserver = new ResizeObserver(() => this.#updateRenderWidth());
this.#resizeObserver.observe(this);
}
override connectedCallback(): void {
super.connectedCallback();
this.#updateRenderWidth();
}
override disconnectedCallback(): void {
super.disconnectedCallback();
this.#resizeObserver.disconnect();
}
protected override update(changes: PropertyValues<this>): void {
if (
changes.has("rangeStart")
|| changes.has("rangeEnd")
|| changes.has("_renderWidth")
) {
this.msPerPx.invalidate();
}
super.update(changes);
}
@on("wheel")
onWheel(event: WheelEvent): void {
event.preventDefault();
event.stopImmediatePropagation();
if (event.ctrlKey) {
this.#zoomTimeline(event.offsetX, event.deltaY);
} else if (event.deltaX) {
this.#pan(-event.deltaX);
} else if (event.shiftKey && event.deltaY) {
this.#pan(-event.deltaY);
}
}
@on("window:entity-select")
onEntitySelected({ detail: id }: EntitySelectEvent): void {
if (!id) {
this._selectedKeyframe = null;
return;
}
if (Array.from(Object.keys(this.keyframes)).includes(id)) {
this._selectedKeyframe = id;
this.dispatchEvent(new CustomEvent("playHeadChange", { detail: this.keyframes[id]! }));
} else {
this._selectedKeyframe = null;
}
}
selectKeyframe(id: string): void {
studio.select(id);
}
#zoomTimeline(origin: number, delta: number): void {
const currentRange = this.rangeEnd - this.rangeStart;
const deltaRange = delta * 4;
// FIXME: When zooming the timeline, the transform origin should be
// centered on the mouse cursor. Currently this does not work as
// intended, always centering on the 00:00 timestamp instead.
const t = ((this._offsetX + origin) * this.msPerPx.get()) / currentRange;
console.log(`t: ${t}`);
console.log(`deltaStart: ${-deltaRange * t}`);
console.log(`deltaEnd: ${deltaRange * (1 - t)}`);
const rangeStart = this.rangeStart - deltaRange * t;
const rangeEnd = this.rangeEnd + deltaRange * (1 - t);
this.dispatchEvent(new CustomEvent("rangeStartChange", { detail: rangeStart }));
this.dispatchEvent(new CustomEvent("rangeEndChange", { detail: rangeEnd }));
}
#onDragStart(): void {
this._panning = true;
}
#onDragEnd(): void {
this._panning = false;
}
#onDrag(event: PointerEvent): void {
this.#pan(event.movementX);
}
#pan(deltaPx: number): void {
this._offsetX += deltaPx;
const msPerPx = this.msPerPx.get();
const rangeStart = this.rangeStart - deltaPx * msPerPx;
const rangeEnd = this.rangeEnd - deltaPx * msPerPx;
this.dispatchEvent(new CustomEvent("rangeStartChange", { detail: rangeStart }));
this.dispatchEvent(new CustomEvent("rangeEndChange", { detail: rangeEnd }));
}
#onPlayheadDrag(event: PointerEvent): void {
const deltaMs = event.movementX * this.msPerPx.get();
const updated = this.playHead + deltaMs;
this.dispatchEvent(new CustomEvent("playHeadChange", { detail: updated }));
}
#onKeyframeDrag(id: string, event: PointerEvent): void {
const deltaMs = event.movementX * this.msPerPx.get();
const updated = this.keyframes[id] + deltaMs;
studio.updateKeyframe(id, updated);
const updatedKeyframes = Object.fromEntries(
Object
.entries(this.keyframes)
.map(entry => entry[0] === id
? [id, updated]
: entry
)
);
this.dispatchEvent(new CustomEvent("keyframesChange", { detail: updatedKeyframes }));
}
#updateRenderWidth(): void {
this._renderWidth = this.getBoundingClientRect().width;
}
/**
* Format a time value (in ms) for displaying in the timeline view
*/
#timestamp(ms: number) {
const sign = ms < 0 ? "-" : "";
ms = Math.abs(ms);
let s = Math.floor(ms / 1000);
let frac = Math.round(ms % 1000);
if (frac === 1000) {
frac = 0;
s += 1;
}
let m = 0;
if (s >= 60) {
m = Math.floor(s / 60);
s %= 60;
}
return html`${
sign
}${
m.toString(10).padStart(2, "0")
}:${
s.toString(10).padStart(2, "0")
}<!--<span class="frac">.${
frac.toString(10).padEnd(3, "0")
}</span>-->`;
}
/**
* Convert a time value (in ms) to a px offset from the left edge of the view
*/
#msToPx(ms: number): number {
return this._offsetX + ms / this.msPerPx.get();
}
protected override render = () => {
const msPerPx = this.msPerPx.get();
const range = this.rangeEnd - this.rangeStart;
let subdivsPerSec = 32;
let subdivsCount = (range / 1000) * subdivsPerSec;
let pxPerSubdiv = this._renderWidth / subdivsCount;
while (pxPerSubdiv < 64) {
subdivsPerSec /= 2;
subdivsCount = (range / 1000) * subdivsPerSec;
pxPerSubdiv = this._renderWidth / subdivsCount;
}
const msPerSubdiv = msPerPx * pxPerSubdiv;
const subdivs = new Array(Math.ceil(subdivsCount) + 2).fill(null);
const fracPxOffset = this._offsetX % pxPerSubdiv;
let startingIdx = -Math.floor(this._offsetX / pxPerSubdiv);
if (fracPxOffset < 0)
startingIdx -= 2;
else
startingIdx -= 1;
return html`
<div
class=${classMap({
wrapper: true,
panning: this._panning,
})}
${drag({
button: 1,
start: this.#onDragStart,
move: this.#onDrag,
end: this.#onDragEnd,
})}
tabindex="-1"
>
<div class="clip-region"
style=${styleMap({
left: `${this.#msToPx(this.clipStart)}px`,
right: `${this._renderWidth - this.#msToPx(this.clipEnd)}px`,
})}
></div>
${subdivs.map((_, idx) => {
const pxOffset = (idx - 1) * pxPerSubdiv + fracPxOffset;
const ms = (startingIdx + idx) * msPerSubdiv;
const msFrac = Math.abs(Math.round(ms) % 1000);
const strong = msFrac === 0;
return html`
<div
class=${classMap({
hash: true,
strong,
})}
style=${styleMap({
left: `${pxOffset}px`
})}
></div>
${strong ? html`
<span class="timestamp"
style=${styleMap({
left: `${pxOffset + 8}px`
})}
>
${this.#timestamp(ms)}
</span>
` : nothing}
`;
})}
<div class="playhead"
${drag({
move: this.#onPlayheadDrag
})}
style=${styleMap({
left: `${this.#msToPx(this.playHead)}px`
})}
tabindex="-1"
>
<div class="head"></div>
<div class="bar"></div>
</div>
${repeat(
Object.entries(this.keyframes),
([id]) => id,
([id, keyframe]) => html`
<div id=${id}
class=${classMap({
keyframe: true,
selected: this._selectedKeyframe === id,
})}
style=${styleMap({
left: `${this.#msToPx(keyframe)}px`
})}
tabindex="-1"
${drag({
move: event => this.#onKeyframeDrag(id, event),
})}
@click=${() => this.selectKeyframe(id)}
></div>
`
)}
</div>
`;
}
}
+10 -25
View File
@@ -1,3 +1,5 @@
$hierarchy-width: 290px;
.app-root {
position: fixed;
inset: 0;
@@ -8,12 +10,19 @@
position: absolute;
top: 16px;
}
.v-end {
position: absolute;
bottom: 16px;
}
.v-after-toolbar {
position: absolute;
top: 60px;
}
.h-start { left: 16px }
.h-end { right: 16px }
.h-before-hierarchy {
right: $hierarchy-width + 32px;
}
.h-center {
left: 50%;
transform: translateX(-50%);
@@ -26,33 +35,9 @@ sts-button.active {
// Hierarchy (TODO: Probably move this)
.hierarchy {
overflow: auto;
box-sizing: border-box;
width: 290px;
width: $hierarchy-width;
max-height: calc(100vh - 32px);
padding: 10px 15px;
background: #0006;
backdrop-filter: blur(16px);
border: 8px solid transparent;
border-radius: 0.5rem;
pointer-events: auto;
&::-webkit-scrollbar {
width: 8px;
height: 8px;
&-thumb {
border-radius: 2px;
background: #FFF2;
&:hover {
background: #FFF3;
}
}
}
}
.loading {
+59 -188
View File
@@ -1,84 +1,41 @@
import { on } from "@storyteller/framework";
import * as studio from "@storyteller/studio";
import {
type EntityDespawnEvent,
type EntityMultiSpawnEvent,
type EntitySelectEvent,
type EntitySpawnEvent,
type SceneSavedEvent,
type SceneStateEvent,
TransformSpace,
TransformType,
SceneState,
StudioMode,
} from "@storyteller/studio";
import { StudioURLParams } from "@storyteller/studio-web";
import { CanvasProvider } from "@storyteller/studio-web/canvas-provider";
import { match } from "@storyteller/utility";
import { LitElement, html, nothing } from "lit";
import { customElement, state } from "lit/decorators.js";
import { classMap } from "lit-html/directives/class-map.js";
import { Ref, createRef, ref } from "lit-html/directives/ref.js";
import { v4 as uuid } from "uuid";
import "@storyteller/studio-ui/button";
import "@storyteller/studio-ui/icon";
import "@storyteller/studio-ui/toolbar";
import "@storyteller/studio-ui/tree";
import "@storyteller/studio-web/canvas-provider";
import "@storyteller/studio-web/viewer";
import "@storyteller/studio-web/bvh-viewer";
import "@storyteller/studio-web/mixamo-viewer";
import "./button.element";
import "@storyteller/studio-web/scene-hierarchy";
import "@storyteller/studio-web/studio";
import "@storyteller/studio-web/transform-toolbar";
import "./anim-editor.element";
import "./help.element";
import "./icon.element";
import "./toolbar.element";
import "./transform-toolbar.element";
import "./tree.element";
import "./app.element.scss";
interface SceneObject extends Omit<studio.SceneObject, "children"> {
children: SceneObject[];
}
@customElement("app-root")
export class AppElement extends LitElement {
@state() showHelp = false;
@state() loading = true;
@state() transformSpace = TransformSpace.Local;
@state() transformType = TransformType.Translate;
@state() sceneObjects: SceneObject[] = [];
@state() selection?: string | null;
@state() bvhPath?: string;
@state() mixamoPath?: string;
@state() mode?: StudioMode;
#sceneObjects: studio.SceneObject[] = [];
#entityMap = new Map<string, SceneObject>();
#params = new StudioURLParams();
#canvasProvider: Ref<CanvasProvider> = createRef();
get #studioCanvas(): HTMLCanvasElement | null {
const provider = this.#canvasProvider.value;
if (!provider) return null;
return document.getElementById(provider.canvasId) as HTMLCanvasElement | null;
}
override connectedCallback(): void {
super.connectedCallback();
const params = new URLSearchParams(document.location.search);
if (params.has("bvh"))
this.bvhPath = params.get("bvh")!;
if (params.has("mixamo"))
this.mixamoPath = params.get("mixamo")!;
this.mode = match (params.get("mode"), {
"studio": () => StudioMode.Editor,
"viewer": () => StudioMode.Viewer,
_: () => StudioMode.Viewer,
});
}
protected override createRenderRoot(): Element | ShadowRoot {
return this;
}
@@ -89,33 +46,30 @@ export class AppElement extends LitElement {
console.log("Inspector changed:", data);
}
@on("keydown")
@on("keyup")
pipeKeyboardEvents(event: KeyboardEvent): void {
// When the user interacts with one of the web UI elements, the Bevy
// canvas will lose focus, preventing it from receiving keyboard events.
// As a workaround, we can listen for any events that have bubbled up to
// the app root and repeat them for the canvas.
//
// This does unfortunately burden the web front-end with some additional
// responsibility: when we handle a keyboard event and _don't_ want the
// Bevy app to receive it, we'll need to call `event.stopPropagation()` in
// the handler to prevent it from reaching this listener.
const {
type,
code, charCode, key, keyCode, location, repeat,
altKey, ctrlKey, metaKey, shiftKey,
} = event;
this.#studioCanvas?.dispatchEvent(new KeyboardEvent(type, {
code, charCode, key, keyCode, location, repeat,
altKey, ctrlKey, metaKey, shiftKey,
bubbles: true,
cancelable: true,
composed: true,
}));
@on("scene-saved")
async onSceneSaved({ detail: scene }: SceneSavedEvent): Promise<void> {
const file = new File([scene], "scene.scn.ron", { type: "text/ron" });
const formData = new FormData();
formData.append("uuid_idempotency_token", uuid());
formData.append("file", file);
try {
const response = await fetch("https://api.fakeyou.com/v1/engine/create_scene", {
method: "POST",
credentials: "include",
headers: { "Accept": "text/plain" },
body: formData,
});
console.log(response);
}
catch (err) {
console.error(err);
}
}
@on("scene-state")
onSceneStateChange({ detail: state }: SceneStateEvent): void {
console.log("scene state change:", SceneState[state]);
@@ -125,107 +79,40 @@ export class AppElement extends LitElement {
});
}
@on("window:entity-select")
onEntitySelectionChange({ detail: entity }: EntitySelectEvent): void {
const sceneObject = this.#entityMap.get(entity!);
console.log("Entity selection changed:", sceneObject);
this.selection = entity;
}
@on("window:entity-spawn")
onEntitySpawned({ detail: value }: EntitySpawnEvent): void {
console.log("Entity spawned:", value);
this.#sceneObjects = this.#sceneObjects.concat(value).sort(sortByName);
this.#updateHierarchy();
}
@on("window:entity-multi-spawn")
onEntitiesSpawned({ detail: values }: EntityMultiSpawnEvent): void {
console.log("Entities spawned:", values);
this.#sceneObjects = this.#sceneObjects.concat(values).sort(sortByName);
this.#updateHierarchy();
}
@on("window:entity-despawn")
onEntityDespawned({ detail: entity }: EntityDespawnEvent): void {
console.log("Entity despawned:", entity);
this.#sceneObjects = this.#sceneObjects.filter(({ id }) => id !== entity);
this.#updateHierarchy();
}
toggleHelp(): void {
this.showHelp = !this.showHelp;
}
onTransformSpaceChange({ detail: space }: CustomEvent<TransformSpace>): void {
this.transformSpace = space;
studio.setTransformSpace(space);
}
onTransformTypeChange({ detail: type }: CustomEvent<TransformType>): void {
this.transformType = type;
studio.setTransformType(type);
}
// TODO: Get some help from someone who knows what an algorithm is
#updateHierarchy(): void {
this.#entityMap.clear();
for (let obj of this.#sceneObjects)
this.#entityMap.set(obj.id, { ...obj, children: [] });
for (let obj of this.#entityMap.values()) {
if (obj.parent != null) {
const parent = this.#entityMap.get(obj.parent);
if (!parent)
throw new Error(`Missing parent: ${obj.parent}`);
parent.children.push(obj);
}
}
for (let obj of this.#entityMap.values())
obj.children.sort(sortByName);
this.sceneObjects = Array
.from(this.#entityMap.values())
.filter(obj => obj.parent == null)
.sort(sortByName);
console.log("Hierarchy:", this.sceneObjects);
}
protected override render = () => html`
<sts-canvas-provider
class="app-root"
${ref(this.#canvasProvider as any)}
>
${this.bvhPath ? html`
<sts-bvh-viewer
bvhPath=${this.bvhPath}
skyboxId="test_scene"
@scene-state=${this.onSceneStateChange}
></sts-bvh-viewer>
` : this.mixamoPath ? html`
<sts-mixamo-viewer
mixamoPath=${this.mixamoPath}
skyboxId="test_scene"
@scene-state=${this.onSceneStateChange}
></sts-mixamo-viewer>
` : html`
<sts-viewer
objectId="sample-room.gltf"
skyboxId="test_scene"
.mode=${this.mode}
@scene-state=${this.onSceneStateChange}
></sts-viewer>
`}
<!-- Scene view -->
<sts-studio
.objectId=${this.#params.objectId}
.bvhPath=${this.#params.bvh}
.mixamoPath=${this.#params.mixamo}
.sceneImport=${this.#params.sceneImport}
.storytellerScene=${this.#params.scene}
.skyboxId=${this.#params.skybox ?? "test_scene"}
.mode=${this.#params.mode}
></sts-studio>
<!-- Actions toolbar -->
<sts-toolbar class="v-start h-start">
<sts-toolbar-group>
${this.#params.mode === StudioMode.Editor ? html`
<sts-button
icon="floppy-disk"
@click=${() => studio.saveScene()}
>
Save
</sts-button>
` : nothing}
<sts-button
class=${classMap({ active: this.showHelp })}
icon="circle-info"
icon="question"
@click=${this.toggleHelp}
>
Help
@@ -233,26 +120,14 @@ export class AppElement extends LitElement {
</sts-toolbar-group>
</sts-toolbar>
${this.mode === StudioMode.Editor ? html`
<sts-transform-toolbar
class="v-start h-center"
.type=${this.transformType}
@type-change=${this.onTransformTypeChange}
.space=${this.transformSpace}
@space-change=${this.onTransformSpaceChange}
></sts-transform-toolbar>
<div class="hierarchy v-start h-end">
${this.sceneObjects.map(obj => html`
<sts-tree class="scene-object"
id=${obj.id}
.name=${obj.name}
.treeChildren=${obj.children}
></sts-tree>
`)}
</div>
<!-- Studio-mode-only elements -->
${this.#params.mode === StudioMode.Editor ? html`
<sts-transform-toolbar class="v-start h-center"></sts-transform-toolbar>
<sts-scene-hierarchy class="hierarchy v-start h-end"></sts-scene-hierarchy>
<sts-anim-editor class="timeline v-end h-start h-before-hierarchy"></sts-anim-editor>
` : nothing}
<!-- Dialogs and modals -->
${this.showHelp ? html`
<sts-help class="help v-after-toolbar h-start"></sts-help>
` : nothing}
@@ -266,7 +141,3 @@ export class AppElement extends LitElement {
</sts-canvas-provider>
`;
}
function sortByName(a: { name: string }, b: { name: string }): number {
return a.name.localeCompare(b.name);
}
@@ -1,31 +0,0 @@
:host {
display: inline-flex;
align-items: center;
gap: 0.5rem;
padding: 10px 20px;
border-radius: 0.5rem;
font: {
family: "Fira Sans", sans-serif;
size: 15px;
weight: 500;
}
line-height: 1;
pointer-events: auto;
cursor: pointer;
user-select: none;
background: #0006;
color: #FFF;
backdrop-filter: blur(16px);
transition: background 0.1s linear;
}
:host(:hover) {
background: #0008;
}
.icon {
fill: currentColor;
}
-32
View File
@@ -1,32 +0,0 @@
import { on } from "@storyteller/framework";
import { LitElement, html, nothing, unsafeCSS } from "lit";
import { customElement, property } from "lit/decorators.js";
import styles from "./button.element.scss?inline";
import "./icon.element";
@customElement("sts-button")
export class ButtonElement extends LitElement {
static override styles = unsafeCSS(styles);
@property() icon?: string;
override role = "button";
override tabIndex = 0;
@on("keydown")
onKeyDown(event: KeyboardEvent) {
if (/^( |Enter)$/.test(event.key)) {
event.stopPropagation();
this.click();
}
}
protected override render = () => html`
${this.icon
? html`<sts-icon icon=${this.icon} aria-hidden="true"></sts-icon>`
: nothing}
<slot></slot>
`
}
+6 -1
View File
@@ -13,7 +13,7 @@ export class HelpElement extends LitElement {
${section("Navigation", [
{ name: "Pan View", controls: "MMB" },
{ name: "Orbit around point", controls: "Alt + LMB" },
{ name: "Zoom", controls: ["Alt + RMB", "Mouse Wheel"]},
{ name: "Zoom", controls: ["Alt + RMB", "Scroll"]},
{ name: "Free fly", controls: "W, A, S, D, E, Q" },
{ name: "Free look", controls: ["RMB", "Shift"] },
])}
@@ -21,6 +21,11 @@ export class HelpElement extends LitElement {
{ name: "Select", controls: "LMB" },
{ name: "Clear selection", controls: "Esc" },
{ name: "Focus selection", controls: "F" },
{ name: "Delete selection", controls: "Delete" },
])}
${section("Animation Timeline", [
{ name: "Pan", controls: ["MMB", "Scroll"] },
{ name: "Zoom", controls: "Ctrl + Scroll" },
])}
${isDevEnvironment()
? section("Developer", [
-28
View File
@@ -1,28 +0,0 @@
:host {
display: flex;
width: auto;
align-items: center;
justify-content: center;
height: 1em;
}
.a11y-label {
position: absolute;
overflow: hidden;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
clip: rect(1px, 1px, 1px, 1px);
border: 0;
white-space: nowrap;
}
.svg-wrapper {
display: flex;
}
svg {
height: 1em;
fill: currentColor;
}
-67
View File
@@ -1,67 +0,0 @@
import { spread } from "@storyteller/framework";
import { match } from "@storyteller/utility";
import { LitElement, TemplateResult, html, nothing, unsafeCSS } from "lit";
import { customElement, property } from "lit/decorators.js";
import { unsafeSVG } from "lit/directives/unsafe-svg.js";
import fasCircleInfo from "@fortawesome/fontawesome-pro/svgs/regular/circle-info.svg?raw";
import fasCube from "@fortawesome/fontawesome-pro/svgs/regular/cube.svg?raw";
import fasGlobe from "@fortawesome/fontawesome-pro/svgs/regular/globe.svg?raw";
import fasUpDownLeftRight from "@fortawesome/fontawesome-pro/svgs/regular/arrows-up-down-left-right.svg?raw";
import fasRotate from "@fortawesome/fontawesome-pro/svgs/regular/arrows-rotate.svg?raw";
import fasScale from "@fortawesome/fontawesome-pro/svgs/regular/arrow-up-right-and-arrow-down-left-from-center.svg?raw";
import fasCaretRight from "@fortawesome/fontawesome-pro/svgs/solid/caret-right.svg?raw";
import styles from "./icon.element.scss?inline";
@customElement("sts-icon")
export class IconElement extends LitElement {
static override styles = unsafeCSS(styles);
@property() icon = "";
protected override render = () => html`
<span class="a11y-label">
<slot></slot>
</span>
<div class="svg-wrapper" role="presentation">
${renderSvg(this.icon)}
</div>
`;
}
const RENDER_RESULTS = new Map<string, TemplateResult>();
function renderSvg(iconId: string) {
if (RENDER_RESULTS.has(iconId))
return RENDER_RESULTS.get(iconId)!;
const svgSrc = match (iconId, {
"circle-info": () => fasCircleInfo,
"caret-right": () => fasCaretRight,
"cube": () => fasCube,
"globe": () => fasGlobe,
"up-down-left-right": () => fasUpDownLeftRight,
"rotate": () => fasRotate,
"scale": () => fasScale,
_: () => null,
});
if (!svgSrc) return nothing;
const [, attrsString, body] = svgSrc.match(/<svg(.+?)>(.+?)<\/svg>/)!;
const attrs = Object.fromEntries(
Array.from(attrsString.matchAll(/([-:_a-zA-Z0-9]+)(?:=['"]?([^'"]*)['"]?)/g))
.map(([, attr, value]) => [attr, value])
);
const result = html`
<svg ${spread(attrs)}>
${unsafeSVG(body)}
</svg>
`;
RENDER_RESULTS.set(iconId, result);
return result;
}
@@ -1,22 +0,0 @@
:host {
display: flex;
gap: 2px;
}
::slotted(*) {
border-radius: 0;
}
::slotted(:first-child) {
border: {
top-left-radius: 8px;
bottom-left-radius: 8px;
}
}
::slotted(:last-child) {
border: {
top-right-radius: 8px;
bottom-right-radius: 8px;
}
}
@@ -1,4 +0,0 @@
:host {
display: flex;
gap: 16px;
}
@@ -1,21 +0,0 @@
import { LitElement, html, unsafeCSS } from "lit";
import { customElement } from "lit/decorators.js";
import toolbarStyles from "./toolbar.element.scss?inline";
import toolbarGroupStyles from "./toolbar-group.element.scss?inline";
// TODO: A11y - https://www.w3.org/WAI/ARIA/apg/patterns/toolbar/
@customElement("sts-toolbar")
export class ToolbarElement extends LitElement {
static override styles = unsafeCSS(toolbarStyles);
override role = "toolbar";
protected override render = () => html`<slot></slot>`;
}
@customElement("sts-toolbar-group")
export class ToolbarGroupElement extends LitElement {
static override styles = unsafeCSS(toolbarGroupStyles);
override role = "group";
protected override render = () => html`<slot></slot>`;
}
@@ -1,57 +0,0 @@
.btn-toggle {
display: inline-flex;
align-items: center;
justify-content: center;
position: relative;
font-size: 16px;
padding: 10px 12px;
pointer-events: auto;
// TODO: Copypasta
background: #0006;
color: #FFFC;
backdrop-filter: blur(16px);
transition: {
property: background, color;
duration: 0.1s;
timing-function: linear;
}
&:hover {
background: #0008;
color: #FFF;
}
&:has(:focus-visible) {
z-index: 1;
outline: 4px solid #2F83FFBF;
}
&:has(:checked) {
background: #7A2C34E3;
color: #FFF;
&:hover {
background: #90363F;
}
}
&:has(:disabled) {
background: #0003 !important;
color: #FFF6 !important;
}
input {
display: block;
appearance: none;
border: 0;
margin: 0;
padding: 0;
opacity: 0;
position: absolute;
inset: 0;
width: 100%;
height: 100%;
z-index: 1;
}
}
@@ -1,105 +0,0 @@
import { TransformSpace, TransformType } from "@storyteller/studio";
import { match } from "@storyteller/utility";
import { LitElement, html, unsafeCSS } from "lit";
import { customElement, property } from "lit/decorators.js";
import styles from "./transform-toolbar.element.scss?inline";
@customElement("sts-transform-toolbar")
export class TransformToolbarElement extends LitElement {
static override styles = unsafeCSS(styles);
@property({ type: Number }) space = TransformSpace.Local;
@property({ type: Number }) type = TransformType.Translate;
#onSpaceChange(event: InputEvent): void {
const { value } = event.target as HTMLInputElement;
const space = match (value, {
"local": () => TransformSpace.Local,
"world": () => TransformSpace.World,
_: () => {
throw new Error(`Unknown transform space: "${value}"`);
}
});
this.dispatchEvent(new CustomEvent<TransformSpace>("space-change", {
detail: space,
bubbles: true,
composed: true,
}));
}
#onTypeChange(event: InputEvent): void {
const { name, checked } = event.target as HTMLInputElement;
const flag = match (name, {
"translate": () => TransformType.Translate,
"rotate": () => TransformType.Rotate,
"scale": () => TransformType.Scale,
_: () => {
throw new Error(`Unknown transform type: "${name}"`);
}
});
const type = checked
? this.type | flag
: this.type & ~flag;
this.dispatchEvent(new CustomEvent<TransformType>("type-change", {
detail: type,
bubbles: true,
composed: true,
}));
}
protected override render = () => html`
<sts-toolbar>
<sts-toolbar-group>
<label class="btn-toggle">
<sts-icon icon="cube">Local</sts-icon>
<input type="radio"
name="transform-space"
value="local"
?checked=${this.space === TransformSpace.Local}
@input=${this.#onSpaceChange}
/>
</label>
<label class="btn-toggle">
<sts-icon icon="globe">Global</sts-icon>
<input type="radio"
name="transform-space"
value="world"
?checked=${this.space === TransformSpace.World}
@input=${this.#onSpaceChange}
/>
</label>
</sts-toolbar-group>
<sts-toolbar-group>
<label class="btn-toggle">
<sts-icon icon="up-down-left-right">Translate</sts-icon>
<input type="checkbox"
name="translate"
?checked=${Boolean(this.type & TransformType.Translate)}
@input=${this.#onTypeChange}
/>
</label>
<label class="btn-toggle">
<sts-icon icon="rotate">Rotate</sts-icon>
<input type="checkbox"
name="rotate"
?checked=${Boolean(this.type & TransformType.Rotate)}
@input=${this.#onTypeChange}
/>
</label>
<label class="btn-toggle">
<sts-icon icon="scale">Scale</sts-icon>
<input type="checkbox"
name="scale"
disabled
?checked=${Boolean(this.type & TransformType.Scale)}
@input=${this.#onTypeChange}
/>
</label>
</sts-toolbar-group>
</sts-toolbar>
`;
}
-87
View File
@@ -1,87 +0,0 @@
:host, .children {
display: flex;
flex-direction: column;
}
:host {
min-width: min-content;
}
header {
display: flex;
align-items: center;
border-radius: 4px;
font: {
family: "Fira Sans", sans-serif;
size: 14px;
weight: 400;
}
line-height: 1;
white-space: nowrap;
pointer-events: auto;
cursor: default;
user-select: none;
color: #FFFC;
transition: {
property: background, color;
duration: 0.1s;
timing-function: linear;
}
&:hover:not(:has(.toggle-expanded:hover)) {
background: #0000001F;
}
&.selected {
background: #7A2C34E3;
color: #FFF;
font-weight: 500;
&:hover {
background: #90363F !important;
}
}
}
.toggle-expanded {
display: flex;
align-items: center;
justify-content: center;
flex: 0 0 auto;
width: 16px;
height: 24px;
margin-block: -5px;
padding-inline-start: 4px;
appearance: none;
background: transparent;
border: none;
font: inherit;
color: #FFFC;
&:hover {
color: #FFF;
}
}
.icon.expanded {
transform: rotate(90deg);
}
.name {
flex: 1 0 0;
padding: {
block: 5px;
inline: 5px 10px;
}
}
.children {
margin-inline-start: 7px;
padding-inline-start: 9px;
border-inline-start: 1px solid #FFF2;
}
-77
View File
@@ -1,77 +0,0 @@
import { on } from "@storyteller/framework";
import * as studio from "@storyteller/studio";
import { type EntitySelectEvent } from "@storyteller/studio";
import { LitElement, html, nothing, unsafeCSS } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import "./icon.element";
import styles from "./tree.element.scss?inline";
export interface Tree {
id?: string;
name: string;
children?: Tree[];
}
@customElement("sts-tree")
export class TreeElement extends LitElement {
static override styles = unsafeCSS(styles);
@property() name = "";
@property() treeChildren?: Tree[];
@property() expanded = false;
@state() selected = false;
toggleExpanded(): void {
this.expanded = !this.expanded;
}
@on("window:entity-select")
onEntitySelect({ detail: entity }: EntitySelectEvent): void {
this.selected = this.id === entity;
}
selectEntity(): void {
studio.select(this.id);
}
// TODO: A11y
protected override render = () => html`
<header class=${classMap({ selected: this.selected })}>
${this.treeChildren?.length ? html`
<button class="toggle-expanded"
@click=${this.toggleExpanded}
>
<sts-icon
class=${classMap({
icon: true,
expanded: this.expanded,
})}
icon="caret-right"
aria-label=${this.expanded ? "Expand" : "Collapse"}
></sts-icon>
</button>
` : nothing}
<span class="name"
@click=${this.selectEntity}
>
${this.name}
</span>
</header>
${this.expanded && this.treeChildren?.length ? html`
<div class="children">
${this.treeChildren?.map(tree => html`
<sts-tree
id=${tree.id}
.name=${tree.name}
.treeChildren=${tree.children}
></sts-tree>
`)}
</div>
` : nothing}
`;
}
@@ -1,25 +0,0 @@
import fs from "fs";
import {NormalizedOutputOptions} from "rollup";
function copyRecursively(src: string, dst: string) {
if (fs.statSync(src).isDirectory()) {
// folder
if (!fs.existsSync(dst)) {
fs.mkdirSync(dst);
}
for (const file of fs.readdirSync(src)) {
copyRecursively(src + "/" + file, dst + "/" + file);
}
} else {
// file
fs.copyFileSync(src, dst);
}
}
export function buildMiddleWare(options: NormalizedOutputOptions, assets: string[] = []) {
const dst = options.dir;
if (!dst) throw new Error("options.dir is undefined");
for (const dir of assets) {
copyRecursively(dir, dst)
}
}
@@ -1,23 +0,0 @@
// NOTE: This is an inline fork of this npm package:
// https://github.com/nguyenbatranvan/vite-multiple-assets
//
// The published version at time of writing doesn't include a mime type for
// WGSL, which is fixed in this copy. This module can be removed and replaced
// with the npm dependency if/when this PR is accepted:
// https://github.com/nguyenbatranvan/vite-multiple-assets/pull/5
import {PluginOption, ViteDevServer} from "vite";
import {ServerMiddleWare} from "./server";
import {buildMiddleWare} from "./build";
export default function DynamicPublicDirectory(assets: string[]): PluginOption {
return {
configureServer(server: ViteDevServer) {
return ServerMiddleWare(server, assets)
},
async writeBundle(options) {
buildMiddleWare(options, assets)
},
name: "dynamic assets"
};
}
@@ -1,93 +0,0 @@
import fs from "fs";
import path from "path";
import { ViteDevServer } from "vite";
interface IPropsFile {
name: string;
files: string[];
}
function getFiles(dir, files_): string[] {
files_ = files_ || [];
const files = fs.readdirSync(dir);
for (const i in files) {
const name = dir + "/" + files[i];
if (fs.statSync(name).isDirectory()) {
getFiles(name, files_);
} else {
files_.push(name);
}
}
return files_;
}
const mimeTypes = {
".7z": "application/x-7z-compressed",
".bin": "application/octet-stream",
".bvh": "text/bvh",
".css": "text/css",
".eot": "font/eot",
".gif": "image/gif",
".glb": "model/gltf-binary",
".gltf": "model/gltf+json",
".gz": "application/gzip",
".hdr": "image/vnd.radiance",
".html": "text/html",
".ico": "image/x-icon",
".jpg": "image/jpeg",
".js": "text/javascript",
".json": "application/json",
".ktx": "image/ktx",
".ktx2": "image/ktx",
".meta": "text/plain",
".mjs": "text/javascript",
".mp3": "audio/mpeg",
".mp4": "video/mp4",
".otf": "font/otf",
".png": "image/png",
".rar": "application/x-rar-compressed",
".svg": "image/svg+xml",
".tiff": "image/tiff",
".ttf": "font/ttf",
".txt": "text/plain",
".wasm": "application/wasm",
".webm": "video/webm",
".webp": "image/webp",
".wgsl": "text/wgsl",
".woff": "font/woff",
".woff2": "font/woff2",
".xml": "text/xml",
".zip": "application/zip",
}
export function ServerMiddleWare(server: ViteDevServer, assets: string[] = []) {
if (!assets || !assets.length)
return;
const fileObject: IPropsFile[] = [];
for (let i = 0; i < assets.length; i++) {
const files = getFiles(path.join(process.cwd(), `/${assets[i]}`), []);
fileObject.push({
name: assets[i],
files
});
}
return () => {
server.middlewares.use(async (req, res, next) => {
for (let i = 0; i < fileObject.length; i++) {
const file = path.join(process.cwd(), `${fileObject[i].name}/${req.originalUrl}`);
if (fileObject[i].files.some(f => path.relative(f, file) === "")) {
const extension = file.substring(file.lastIndexOf("."));
res.setHeader("Cache-Control", "max-age=31536000, immutable");
res.setHeader("Content-Type", mimeTypes[extension]);
res.writeHead(200);
res.write(fs.readFileSync(path.join(process.cwd(), "/" + fileObject[i].name + req.originalUrl)));
res.end();
break;
}
}
next();
});
};
}
+1 -2
View File
@@ -5,8 +5,7 @@ import { defineConfig } from "vite";
import { nxViteTsPaths } from "@nx/vite/plugins/nx-tsconfig-paths.plugin";
import wasm from "vite-plugin-wasm";
import topLevelAwait from "vite-plugin-top-level-await";
import mergeAssetsDirs from "./vite-plugins/multiple-assets";
import mergeAssetsDirs from "../vite-multiple-assets";
export default defineConfig({
cacheDir: "../node_modules/.vite/studio-frontend",