From 6e137692e4a1f77a50ce25820674277a7a21ce28 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Fri, 6 Jan 2023 12:16:42 +0100 Subject: [PATCH] Factor out TextureSpec --- protocols/schemas/shapes.prs | 9 ++--- scene/example.pr | 19 +++++----- src/shapes.ts | 69 +++++++++++++++++++----------------- 3 files changed, 50 insertions(+), 47 deletions(-) diff --git a/protocols/schemas/shapes.prs b/protocols/schemas/shapes.prs index 800fbad..67b575e 100644 --- a/protocols/schemas/shapes.prs +++ b/protocols/schemas/shapes.prs @@ -19,10 +19,11 @@ Scale = . Move = . Rotate = @euler / @quaternion . -Texture = -/ @simple -/ @uv -/ @uvAlpha +Texture = . +TextureSpec = +/ @simple [@path string] +/ @uv [@path string @scale Vector2 @offset Vector2] +/ @uvAlpha [@path string @scale Vector2 @offset Vector2 @alpha double] . Color = diff --git a/scene/example.pr b/scene/example.pr index 5c235b6..81a3f19 100644 --- a/scene/example.pr +++ b/scene/example.pr @@ -1,19 +1,18 @@ >> - + + ] >>>> - - - 0.75 + + + 0.75] >>>>> - > > @@ -41,7 +40,7 @@ >>>>> diff --git a/src/shapes.ts b/src/shapes.ts index 371e1a7..48c6399 100644 --- a/src/shapes.ts +++ b/src/shapes.ts @@ -2,6 +2,7 @@ import { Color3, CSG, HemisphericLight, + Material, Mesh, MeshBuilder, Node, @@ -120,33 +121,53 @@ function applyCustomizer(m: ShapeTree, c: MeshCustomizer) { Object.values(c).forEach(f => f(m)); } +function buildTexture(name: string, scene: Scene, spec: Shapes.TextureSpec): Material { + const mat = new StandardMaterial(name, scene); + const tex = new Texture(spec.path, scene); + mat.diffuseTexture = tex; + switch (spec._variant) { + case "simple": + break; + case "uvAlpha": + mat.alpha = spec.alpha; + /* FALL THROUGH */ + case "uv": { + const scale = v2(spec.scale); + const offset = v2(spec.offset); + tex.uScale = 1 / scale.x; + tex.vScale = 1 / scale.y; + tex.uOffset = offset.x; + tex.vOffset = offset.y; + break; + } + } + return mat; +} + export function build(name: string, scene: Scene, shape: Shapes.Shape, customize: MeshCustomizer): ShapeTree { switch (shape._variant) { case "Mesh": { const mesh = shape.value; + let t: ShapeTree; switch (mesh._variant) { - case "Sphere": { - const t = new ShapeTree(scene, shape, MeshBuilder.CreateSphere(name, {}, scene)); - applyCustomizer(t, customize); - return t; - } - - case "Box": { - const t = new ShapeTree(scene, shape, MeshBuilder.CreateBox(name, {}, scene)); - applyCustomizer(t, customize); - return t; - } + case "Sphere": + t = new ShapeTree(scene, shape, MeshBuilder.CreateSphere(name, {}, scene)); + break; + case "Box": + t = new ShapeTree(scene, shape, MeshBuilder.CreateBox(name, {}, scene)); + break; case "Ground": { const v = v2(mesh.value.size); - const t = new ShapeTree( + t = new ShapeTree( scene, shape, MeshBuilder.CreateGround(name, { width: v.x, height: v.y }, scene)); - applyCustomizer(t, customize); - return t; + break; } } + applyCustomizer(t, customize); + return t; } case "Light": @@ -192,25 +213,7 @@ export function build(name: string, scene: Scene, shape: Shapes.Shape, customize } case "Texture": { - const mat = new StandardMaterial(name + '.texture', scene); - const tex = new Texture(shape.value.path, scene); - mat.diffuseTexture = tex; - switch (shape.value._variant) { - case "simple": - break; - case "uvAlpha": - mat.alpha = shape.value.alpha; - /* FALL THROUGH */ - case "uv": { - const scale = v2(shape.value.scale); - const offset = v2(shape.value.offset); - tex.uScale = 1 / scale.x; - tex.vScale = 1 / scale.y; - tex.uOffset = offset.x; - tex.vOffset = offset.y; - break; - } - } + const mat = buildTexture(name + '.texture', scene, shape.value.spec); return build(name + '.inner', scene, shape.value.shape, { ... customize, material: m => m.node.material = mat,