Skip to content

@web-engine-dev/resources


@web-engine-dev/resources / defineSceneResource

Function: defineSceneResource()

defineSceneResource<T>(schema): ResourceDescriptor<T>

Define a resource that is automatically serialized into scene files.

Scene resources are:

  • Persisted in the scene file's resources section (delta-encoded against defaults)
  • Restored by loadScene() automatically
  • Editable in the editor's Scene panel (UI auto-generated from schema)
  • Versioned with per-field migration support

Uses a string key (not Symbol) so the resource is addressable by typeId across editor and player sessions.

Type Parameters

T

T

Parameters

schema

ResourceSchema

The resource schema defining fields, defaults, and serialization metadata

Returns

ResourceDescriptor<T>

A ResourceDescriptor with a string type matching schema.typeId

Throws

If a resource with the same typeId is already registered

Example

ts
interface FogSettings {
  enabled: boolean;
  density: number;
  color: [number, number, number];
}

const FogSettingsSchema: ResourceSchema = {
  typeId: 'FogSettings',
  version: 1,
  fields: {
    enabled: { type: 'boolean', default: true },
    density: { type: 'number', default: 0.02, min: 0, max: 1, step: 0.01 },
    color: { type: 'vec3', default: [0.8, 0.8, 0.9] },
  },
};

const FogSettingsResource = defineSceneResource<FogSettings>(FogSettingsSchema);

// Use with a Resources container
resources.insert(FogSettingsResource, { enabled: true, density: 0.05, color: [1, 1, 1] });
const fog = resources.get(FogSettingsResource);

Proprietary software. All rights reserved.