Factor out TextureSpec

This commit is contained in:
Tony Garnock-Jones 2023-01-06 12:16:42 +01:00
parent 6a9d2424ce
commit 6e137692e4
3 changed files with 50 additions and 47 deletions

View File

@ -19,10 +19,11 @@ Scale = <scale @v Vector3 @shape Shape> .
Move = <move @v Vector3 @shape Shape> .
Rotate = @euler <rotate @v Vector3 @shape Shape> / @quaternion <rotate @q Quaternion @shape Shape> .
Texture =
/ @simple <texture @path string @shape Shape>
/ @uv <texture @path string @scale Vector2 @offset Vector2 @shape Shape>
/ @uvAlpha <texture @path string @scale Vector2 @offset Vector2 @alpha double @shape Shape>
Texture = <texture @spec TextureSpec @shape Shape> .
TextureSpec =
/ @simple [@path string]
/ @uv [@path string @scale Vector2 @offset Vector2]
/ @uvAlpha [@path string @scale Vector2 @offset Vector2 @alpha double]
.
Color =

View File

@ -1,19 +1,18 @@
<sprite "light" <hemispheric-light <v 0.0 1.0 0.0>>>
<sprite "ground"
<texture
"textures/grass-256x256.jpg"
<v 0.01 0.01>
<v 0.0 0.0>
<texture ["textures/grass-256x256.jpg"
<v 0.01 0.01>
<v 0.0 0.0>]
<floor <ground <v 300.0 300.0>>>>>
<sprite "box"
<move <v -6.0 1.0 8.0>
<scale <v 10.0 0.1 10.0>
<texture "textures/oak-herringbone-5e80fb40b00c9-1200.jpg"
<v 0.1 0.1>
<v 0.0 0.0>
0.75
<texture ["textures/oak-herringbone-5e80fb40b00c9-1200.jpg"
<v 0.1 0.1>
<v 0.0 0.0>
0.75]
<floor <box>>>>>>
<sprite "box2"
@ -26,7 +25,7 @@
<color 0.0 0.5 0.0
<move <v 0.0 0.0 3.5>
<scale <v 1.0 1.0 3.0>
<texture "textures/oak-herringbone-5e80fb40b00c9-1200.jpg"
<texture ["textures/oak-herringbone-5e80fb40b00c9-1200.jpg"]
[
<move <v 0.0 0.25 0.0> <box>>
<move <v 1.0 0.0 0.0> <box>>
@ -41,7 +40,7 @@
<sphere>>>>>>
<sprite "plans"
<texture "plans/signal-2022-12-27-125451_002.jpeg"
<texture ["plans/signal-2022-12-27-125451_002.jpeg"]
<move <v 0.0 1.0 -10.0>
<rotate <v 0.5 0.0 0.0>
<scale <v 2.0 2.0 0.1>

View File

@ -2,6 +2,7 @@ import {
Color3,
CSG,
HemisphericLight,
Material,
Mesh,
MeshBuilder,
Node,
@ -120,33 +121,53 @@ function applyCustomizer(m: ShapeTree<Mesh>, 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<Mesh>;
switch (mesh._variant) {
case "Sphere": {
const t = new ShapeTree<Mesh>(scene, shape, MeshBuilder.CreateSphere(name, {}, scene));
applyCustomizer(t, customize);
return t;
}
case "Box": {
const t = new ShapeTree<Mesh>(scene, shape, MeshBuilder.CreateBox(name, {}, scene));
applyCustomizer(t, customize);
return t;
}
case "Sphere":
t = new ShapeTree<Mesh>(scene, shape, MeshBuilder.CreateSphere(name, {}, scene));
break;
case "Box":
t = new ShapeTree<Mesh>(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,