Namespace procgen

Procedural Generation

Helpers for building procedural scene data.

Submodules:

  • geometry - geometry builders for SceneModel.createGeometry.
  • materials - PBR material painters for SceneModel.createTexture and SceneModel.createMaterial.
  • environments - environment images for IBL.
import { Scene } from "@xeokit/sdk/model/scene";
import { sRGBEncoding, LinearEncoding, TrianglesPrimitive } from "@xeokit/sdk/base/constants";
import { buildSphere } from "@xeokit/sdk/model/procgen/geometry";
import { paintCopper } from "@xeokit/sdk/model/procgen/materials";

const scene = new Scene();
const sceneModelResult = scene.createModel({ id: "demo" });
if (!sceneModelResult.ok) throw new Error(sceneModelResult.error);

const sceneModel = sceneModelResult.value;
const sphereResult = buildSphere({ radius: 1 });
if (!sphereResult.ok) throw new Error(sphereResult.error);

const sphere = sphereResult.value;

sceneModel.createGeometry({
id: "sphere",
primitive: TrianglesPrimitive,
positions: sphere.positions,
normals: sphere.normals,
uvs: sphere.uv,
indices: sphere.indices
});

const maps = paintCopper(256);
sceneModel.createTexture({ id: "copperColor", imageData: maps.color, encoding: sRGBEncoding });
sceneModel.createTexture({ id: "copperNormal", imageData: maps.normal, encoding: LinearEncoding });
sceneModel.createTexture({ id: "copperMR", imageData: maps.mr, encoding: LinearEncoding });

sceneModel.createMaterial({
id: "copper",
colorTextureId: "copperColor",
normalsTextureId: "copperNormal",
metallicRoughnessTextureId: "copperMR"
});

sceneModel.createMesh({
id: "sphereMesh",
geometryId: "sphere",
materialId: "copper"
});

sceneModel.createObject({ id: "sphereObject", meshIds: ["sphereMesh"] });
sceneModel.build();

Namespaces

environments
geometry
materials