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> . Move = <move @v Vector3 @shape Shape> .
Rotate = @euler <rotate @v Vector3 @shape Shape> / @quaternion <rotate @q Quaternion @shape Shape> . Rotate = @euler <rotate @v Vector3 @shape Shape> / @quaternion <rotate @q Quaternion @shape Shape> .
Texture = Texture = <texture @spec TextureSpec @shape Shape> .
/ @simple <texture @path string @shape Shape> TextureSpec =
/ @uv <texture @path string @scale Vector2 @offset Vector2 @shape Shape> / @simple [@path string]
/ @uvAlpha <texture @path string @scale Vector2 @offset Vector2 @alpha double @shape Shape> / @uv [@path string @scale Vector2 @offset Vector2]
/ @uvAlpha [@path string @scale Vector2 @offset Vector2 @alpha double]
. .
Color = Color =

View File

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

View File

@ -2,6 +2,7 @@ import {
Color3, Color3,
CSG, CSG,
HemisphericLight, HemisphericLight,
Material,
Mesh, Mesh,
MeshBuilder, MeshBuilder,
Node, Node,
@ -120,33 +121,53 @@ function applyCustomizer(m: ShapeTree<Mesh>, c: MeshCustomizer) {
Object.values(c).forEach(f => f(m)); 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 { export function build(name: string, scene: Scene, shape: Shapes.Shape, customize: MeshCustomizer): ShapeTree {
switch (shape._variant) { switch (shape._variant) {
case "Mesh": { case "Mesh": {
const mesh = shape.value; const mesh = shape.value;
let t: ShapeTree<Mesh>;
switch (mesh._variant) { switch (mesh._variant) {
case "Sphere": { case "Sphere":
const t = new ShapeTree<Mesh>(scene, shape, MeshBuilder.CreateSphere(name, {}, scene)); t = new ShapeTree<Mesh>(scene, shape, MeshBuilder.CreateSphere(name, {}, scene));
applyCustomizer(t, customize); break;
return t; case "Box":
} t = new ShapeTree<Mesh>(scene, shape, MeshBuilder.CreateBox(name, {}, scene));
break;
case "Box": {
const t = new ShapeTree<Mesh>(scene, shape, MeshBuilder.CreateBox(name, {}, scene));
applyCustomizer(t, customize);
return t;
}
case "Ground": { case "Ground": {
const v = v2(mesh.value.size); const v = v2(mesh.value.size);
const t = new ShapeTree( t = new ShapeTree(
scene, scene,
shape, shape,
MeshBuilder.CreateGround(name, { width: v.x, height: v.y }, scene)); MeshBuilder.CreateGround(name, { width: v.x, height: v.y }, scene));
applyCustomizer(t, customize); break;
return t;
} }
} }
applyCustomizer(t, customize);
return t;
} }
case "Light": case "Light":
@ -192,25 +213,7 @@ export function build(name: string, scene: Scene, shape: Shapes.Shape, customize
} }
case "Texture": { case "Texture": {
const mat = new StandardMaterial(name + '.texture', scene); const mat = buildTexture(name + '.texture', scene, shape.value.spec);
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;
}
}
return build(name + '.inner', scene, shape.value.shape, { return build(name + '.inner', scene, shape.value.shape, {
... customize, ... customize,
material: m => m.node.material = mat, material: m => m.node.material = mat,