diff --git a/bench/bevy-parity/examples.list b/bench/bevy-parity/examples.list
index 5a8cf2d5..93a84c68 100644
--- a/bench/bevy-parity/examples.list
+++ b/bench/bevy-parity/examples.list
@@ -141,3 +141,4 @@ shader/pipeline_constants
shader_advanced/compute_mesh
shader_advanced/custom_phase_item
shader_advanced/custom_post_processing
+shader_advanced/custom_vertex_attribute
diff --git a/bench/bevy-parity/runs.tsv b/bench/bevy-parity/runs.tsv
index 8f78fdec..dbdc939b 100644
--- a/bench/bevy-parity/runs.tsv
+++ b/bench/bevy-parity/runs.tsv
@@ -140,3 +140,4 @@ shader/gpu_component_array_buffer - 60,180,240,400
shader_advanced/compute_mesh - 5,10
shader_advanced/custom_phase_item - 5,10
shader_advanced/custom_post_processing - 10,30,60
+shader_advanced/custom_vertex_attribute - 5,10
diff --git a/docs/bevy-parity.md b/docs/bevy-parity.md
index 5dcbcb51..0f65ae38 100644
--- a/docs/bevy-parity.md
+++ b/docs/bevy-parity.md
@@ -4,7 +4,7 @@ Bevy v0.20.0's examples against their raea TSX ports (docs/control.feature FC0a)
don't edit. An example passes when every probed frame passes pixels (SSIM 0.98 and no 32 px tile off by more than 24 of 255) and cost (batch keys and
frame time at most 1.25x the example's). Wall time is one machine's, measured in the same run.
-Examples: **438**, ported **141**, passing **141**, excluded **17** (docs/bevy-parity/excluded.tsv), left **280**.
+Examples: **438**, ported **142**, passing **142**, excluded **17** (docs/bevy-parity/excluded.tsv), left **279**.
raea's frame ms is its update's plus the time React takes to step a frame for its 3D (step), which the example
doesn't pay.
@@ -227,6 +227,8 @@ doesn't pay.
| shader_advanced/custom_post_processing | 10 | 0.999958 | 0 | 1 / 1 | 443 / 522 | 6 / 8 | 2 / 2 | 57.331 / 2.521 = 2.521 + 0 | yes | |
| shader_advanced/custom_post_processing | 30 | 0.999954 | 0 | 1 / 1 | 443 / 522 | 6 / 8 | 2 / 2 | 3.654 / 3.275 = 3.275 + 0 | yes | |
| shader_advanced/custom_post_processing | 60 | 0.999954 | 0 | 1 / 1 | 443 / 522 | 6 / 8 | 2 / 2 | 3.697 / 2.305 = 2.305 + 0 | yes | |
+| shader_advanced/custom_vertex_attribute | 10 | 1.000000 | 0 | 0 / 0 | 449 / 521 | 6 / 8 | 1 / 2 | 2.875 / 1.367 = 1.367 + 0 | yes | |
+| shader_advanced/custom_vertex_attribute | 5 | 1.000000 | 0 | 0 / 0 | 447 / 521 | 6 / 8 | 1 / 2 | 41.257 / 2.953 = 2.953 + 0 | yes | |
## Excluded
diff --git a/docs/bevy-parity/results.jsonl b/docs/bevy-parity/results.jsonl
index 9bf31f6b..60c9f94a 100644
--- a/docs/bevy-parity/results.jsonl
+++ b/docs/bevy-parity/results.jsonl
@@ -214,3 +214,5 @@
{"example":"shader_advanced/custom_post_processing","frame":10,"ssim":0.999958,"tile":0,"bevy":{"batch_keys":1,"entities":443,"extents":["0.5000 0.5000 0.5000"],"frame":10,"frame_ms":57.331,"materials":2,"mesh_entities":1,"meshes":6,"other_material":0,"visible":1},"raea":{"frame":17,"entities":522,"mesh_entities":1,"visible":1,"meshes":8,"materials":2,"batch_keys":1,"other_material":0,"extents":["0.5000 0.5000 0.5000"],"frame_ms":2.521,"step_ms":null,"cost_ms":2.521},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
{"example":"shader_advanced/custom_post_processing","frame":30,"ssim":0.999954,"tile":0,"bevy":{"batch_keys":1,"entities":443,"extents":["0.5000 0.5000 0.5000"],"frame":30,"frame_ms":3.654,"materials":2,"mesh_entities":1,"meshes":6,"other_material":0,"visible":1},"raea":{"frame":37,"entities":522,"mesh_entities":1,"visible":1,"meshes":8,"materials":2,"batch_keys":1,"other_material":0,"extents":["0.5000 0.5000 0.5000"],"frame_ms":3.275,"step_ms":null,"cost_ms":3.275},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
{"example":"shader_advanced/custom_post_processing","frame":60,"ssim":0.999954,"tile":0,"bevy":{"batch_keys":1,"entities":443,"extents":["0.5000 0.5000 0.5000"],"frame":60,"frame_ms":3.697,"materials":2,"mesh_entities":1,"meshes":6,"other_material":0,"visible":1},"raea":{"frame":67,"entities":522,"mesh_entities":1,"visible":1,"meshes":8,"materials":2,"batch_keys":1,"other_material":0,"extents":["0.5000 0.5000 0.5000"],"frame_ms":2.305,"step_ms":null,"cost_ms":2.305},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
+{"example":"shader_advanced/custom_vertex_attribute","frame":10,"ssim":1.000000,"tile":0,"bevy":{"batch_keys":0,"entities":449,"extents":["0.5000 0.5000 0.5000"],"frame":10,"frame_ms":2.875,"materials":1,"mesh_entities":1,"meshes":6,"other_material":1,"visible":1},"raea":{"frame":17,"entities":521,"mesh_entities":1,"visible":1,"meshes":8,"materials":2,"batch_keys":0,"other_material":1,"extents":["0.5000 0.5000 0.5000"],"frame_ms":1.367,"step_ms":null,"cost_ms":1.367},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
+{"example":"shader_advanced/custom_vertex_attribute","frame":5,"ssim":1.000000,"tile":0,"bevy":{"batch_keys":0,"entities":447,"extents":["0.5000 0.5000 0.5000"],"frame":5,"frame_ms":41.257,"materials":1,"mesh_entities":1,"meshes":6,"other_material":1,"visible":1},"raea":{"frame":12,"entities":521,"mesh_entities":1,"visible":1,"meshes":8,"materials":2,"batch_keys":0,"other_material":1,"extents":["0.5000 0.5000 0.5000"],"frame_ms":2.953,"step_ms":null,"cost_ms":2.953},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
diff --git a/engine/render/src/engine/spawn.rs b/engine/render/src/engine/spawn.rs
index ce10ff70..cc97188c 100644
--- a/engine/render/src/engine/spawn.rs
+++ b/engine/render/src/engine/spawn.rs
@@ -237,13 +237,13 @@ pub(crate) fn spawn_3d(world: &mut World, e: Entity, t: &Tup, inserted: &[Tup],
// a shape as a value (docs/media.feature M2): its `shape` column, a CSG expression, built by Manifold
// once per expression (shape::cached), flat-shaded
let expr = match vals.get("shape") { Some(V::S(s)) => s.clone(), _ => return Err(format!("mesh {}: needs shape, as box(1, 1, 1) or minus(rounded_box(2, 1, 0.2, 0.1), text3d(\"SALE\", 0.45, 0.3))", t.id)) };
- // a picture map needs texture coordinates, which Manifold doesn't make (scene3d); a normal map needs
- // tangents too, made from them (mikktspace); so does a grid's outline (grid.rs)
+ // a picture map needs UVs, which Manifold doesn't make (scene3d); a normal map tangents (mikktspace), as a grid's outline
let grid = grid::grid_of(vals);
let uvs = scene3d::has_maps(vals) || grid.is_some();
let tangents = raea_sys_material::maps::needs_tangents(vals);
+ let attrs = match vals.get("attributes") { Some(V::S(a)) => a.as_str(), _ => "" };
// made once per shape and what it's given (share.rs): equal parts hold one Mesh, so Bevy batches them
- let mesh = scene3d::shared_mesh(world, &format!("{expr}\0{uvs}\0{tangents}"), || raea_sys_material::mesh::shape_mesh(&t.id, &expr, uvs, tangents))?;
+ let mesh = scene3d::shared_mesh(world, &format!("{expr}\0{uvs}\0{tangents}\0{attrs}"), || raea_sys_material::mesh::shape_mesh(&t.id, &expr, uvs, tangents).and_then(|m| raea_sys_material::mesh::with_attributes(m, attrs).map_err(|e| format!("mesh {}: {e}", t.id))))?;
// a three.js shader (raea-sys-three-shader, docs/r3f.feature R3F11): its own material, and the mesh given
// what it reads; its pictures' files found as a model's are (the edit's folder, its media)
let three = match vals.get("three") {
diff --git a/runtime/scene/src/parts.rs b/runtime/scene/src/parts.rs
index ad0d3deb..edeb33ea 100644
--- a/runtime/scene/src/parts.rs
+++ b/runtime/scene/src/parts.rs
@@ -267,6 +267,10 @@ pub(crate) fn meshes_rows(tr: &Tracks, names: &[String], p: &mut Program) {
}
layers_column(&mut row, &its[0].props);
instances_column(&mut row, &its);
+ // its geometry's own vertex attributes (JSON, Mesh3d `attributes`), from the first frame: raea-sys-mesh with_attributes
+ if let Some(Value::String(a)) = its[0].props.get("attributes") {
+ row.insert("attributes".into(), V::S(a.clone()));
+ }
name_column(&mut row, &its[0].props);
p.spawns.push(("mesh".into(), vec![row]));
components(p, &id, &its);
diff --git a/sdk/raea/bench/bevy-parity/shader_advanced/custom_vertex_attribute.tsx b/sdk/raea/bench/bevy-parity/shader_advanced/custom_vertex_attribute.tsx
new file mode 100644
index 00000000..582d1e3d
--- /dev/null
+++ b/sdk/raea/bench/bevy-parity/shader_advanced/custom_vertex_attribute.tsx
@@ -0,0 +1,28 @@
+// Bevy v0.20.0 examples/shader_advanced/custom_vertex_attribute.rs in raea's TSX (docs/control.feature FC2d): a
+// default Cuboid given its own vertex attribute, ATTRIBUTE_BLEND_COLOR ("BlendColor", id 988540917, Float32x4), red
+// at all 24 vertices, drawn by a Material whose vertex and fragment shaders are assets/shaders/custom_vertex_attribute.wesl
+// as written; its specialize lays the vertex buffer out as POSITION at location 0 and BlendColor at 1, and its
+// uniform `material` is { color: LinearRgba::WHITE }. The cube is at (0, 0.5, 0), the camera at (-2, 2.5, 5) looking
+// at the origin, with no light, in Bevy's default 1280 x 720 window.
+import { Composition, registerRoot, staticFile } from "raea";
+import { Camera3d } from "raea/camera";
+import { ATTRIBUTE_POSITION, Cuboid, Mesh3d, MeshVertexAttribute } from "raea/mesh";
+import { Material } from "@raea/render/bevy3d"; // raea/pbr once scene-loop lands (its pbr.ts commit)
+import { Transform } from "raea/transform";
+
+const ATTRIBUTE_BLEND_COLOR = MeshVertexAttribute.new("BlendColor", 988540917, "Float32x4");
+const SHADER = staticFile("shaders/custom_vertex_attribute.wesl");
+
+function Shot() {
+ return (
+ <>
+ [1, 0, 0, 1])]]}>
+
+
+
+
+ >
+ );
+}
+
+registerRoot(() => );
diff --git a/sdk/raea/src/mesh.ts b/sdk/raea/src/mesh.ts
index 4feed682..d744c942 100644
--- a/sdk/raea/src/mesh.ts
+++ b/sdk/raea/src/mesh.ts
@@ -5,6 +5,8 @@ export { Sphere, Cylinder, Torus, Cone, Capsule, BufferMesh, type SphereProps, t
export * from "@raea/render/solid";
export { Mesh3d, ConicalFrustum, type Mesh3dProps, type ConicalFrustumProps } from "@raea/render/bevy3d";
export { Cuboid, Capsule3d, Plane3d, type CuboidProps, type Capsule3dProps, type Plane3dProps } from "./3d";
+// a mesh's own vertex attributes (Mesh3d attributes) and the ones a Material's vertex buffer reads (vertex_attributes)
+export { MeshVertexAttribute, ATTRIBUTE_POSITION, ATTRIBUTE_NORMAL, ATTRIBUTE_UV_0, ATTRIBUTE_UV_1, ATTRIBUTE_TANGENT, ATTRIBUTE_COLOR, type VertexFormat, type MeshAttribute } from "./3d";
export { MeshSequence, type MeshSequenceProps } from "@raea/physics/physics";
export { Mesh2d, type Mesh2dProps } from "@raea/render/bevy3d";
// one mesh or glTF drawn at many placements, batched by the GPU, and the seeded placements for it
diff --git a/sdk/render/src/3d.tsx b/sdk/render/src/3d.tsx
index 2aa58af4..5b28a403 100644
--- a/sdk/render/src/3d.tsx
+++ b/sdk/render/src/3d.tsx
@@ -2,8 +2,8 @@
// carries its name (data-3d) and props (data-props, JSON) for Bevy to draw; a Mesh also carries its Manifold
// expression (data-shape). Engine-safe: this module imports nothing beyond React.
import { toHex } from "./colour";
-export { Canvas3D, Cuboid, Sphere, Plane3d, Cylinder, Torus, Cone, Capsule, Capsule3d, BufferMesh, Text3D, SurfaceShader, ThreeShaderMaterial, surfaceShader, samplerSpec, FramePass, Compute, PhaseItem, Atmosphere, eulerOf, Entity, Refinish, clipEvents, Model, ImagePlane, surfaceTurn, Decal, Plate, Bloom, Fog, ToneMapping, Grade, Environment, EnvironmentMapLight, Skybox, AmbientOcclusion, AntiAliasing, Shadows, VolumetricFog, FogVolume, Fire, dofOf, DepthOfField, Outline, Fisheye, LensDistortion, MotionBlur, checkPass, Look, Scene, Entities, BevyComponent, Gizmo, ShaderMesh, BevyResource, Span, RichText, Instances, scatter, Grass } from "./3d/all";
-export type { Vec3, Canvas3DProps, CuboidProps, SphereProps, CylinderProps, TorusProps, Plane3dProps, ConeProps, CapsuleProps, Capsule3dProps, BufferMeshProps, Text3DProps, Animate3d, SurfaceDrawProps, ToonBand, ToonRim, ToonHighlight, ToonOutline, SurfaceShaderSpec, ThreeShaderMaterialProps, ThreeUniform, SamplerSpec, SamplerWrap, FramePassProps, ComputeProps, MeshSlab, PhaseItemProps, PhaseAttribute, AtmosphereProps, BevyParts, EntityProps, ModelProps, AnimGraphNode, ImagePlaneProps, DecalProps, PlateProps, BloomProps, FogProps, FogColor, FogFalloff, ToneMappingProps, GradeSection, GradeProps, EnvironmentProps, EnvironmentMapLightProps, SkyboxProps, AmbientOcclusionProps, AntiAliasingProps, ShadowsProps, VolumetricFogProps, FogVolumeProps, FireProps, DepthOfFieldProps, OutlineProps, LensCommon, FisheyeProps, LensDistortionProps, MotionBlurProps, LookProps, LookPass, SceneProps, BevyComponentProps, GizmoProps, ShaderMeshProps, SpanProps, GlyphInfo, GlyphStyle, RichTextProps, Instance, InstancesProps, PlanBox, ScatterOptions, GrassProps, GrassLod } from "./3d/all";
+export { Canvas3D, Cuboid, Sphere, Plane3d, Cylinder, Torus, Cone, Capsule, Capsule3d, BufferMesh, Text3D, SurfaceShader, ThreeShaderMaterial, surfaceShader, samplerSpec, FramePass, Compute, PhaseItem, MeshVertexAttribute, ATTRIBUTE_POSITION, ATTRIBUTE_NORMAL, ATTRIBUTE_UV_0, ATTRIBUTE_UV_1, ATTRIBUTE_TANGENT, ATTRIBUTE_COLOR, Atmosphere, eulerOf, Entity, Refinish, clipEvents, Model, ImagePlane, surfaceTurn, Decal, Plate, Bloom, Fog, ToneMapping, Grade, Environment, EnvironmentMapLight, Skybox, AmbientOcclusion, AntiAliasing, Shadows, VolumetricFog, FogVolume, Fire, dofOf, DepthOfField, Outline, Fisheye, LensDistortion, MotionBlur, checkPass, Look, Scene, Entities, BevyComponent, Gizmo, ShaderMesh, BevyResource, Span, RichText, Instances, scatter, Grass } from "./3d/all";
+export type { Vec3, Canvas3DProps, CuboidProps, SphereProps, CylinderProps, TorusProps, Plane3dProps, ConeProps, CapsuleProps, Capsule3dProps, BufferMeshProps, Text3DProps, Animate3d, SurfaceDrawProps, ToonBand, ToonRim, ToonHighlight, ToonOutline, SurfaceShaderSpec, ThreeShaderMaterialProps, ThreeUniform, SamplerSpec, SamplerWrap, FramePassProps, ComputeProps, MeshSlab, PhaseItemProps, PhaseAttribute, VertexFormat, MeshAttribute, AtmosphereProps, BevyParts, EntityProps, ModelProps, AnimGraphNode, ImagePlaneProps, DecalProps, PlateProps, BloomProps, FogProps, FogColor, FogFalloff, ToneMappingProps, GradeSection, GradeProps, EnvironmentProps, EnvironmentMapLightProps, SkyboxProps, AmbientOcclusionProps, AntiAliasingProps, ShadowsProps, VolumetricFogProps, FogVolumeProps, FireProps, DepthOfFieldProps, OutlineProps, LensCommon, FisheyeProps, LensDistortionProps, MotionBlurProps, LookProps, LookPass, SceneProps, BevyComponentProps, GizmoProps, ShaderMeshProps, SpanProps, GlyphInfo, GlyphStyle, RichTextProps, Instance, InstancesProps, PlanBox, ScatterOptions, GrassProps, GrassLod } from "./3d/all";
export { CrossSection, crossSection, manifold, outline, Solid, type Bounds, type Polygons, type Section } from "./solid";
// blended distance fields and generated growth (trees, lofts, terrain, boulders, rosettes), as Solids (grow.ts)
diff --git a/sdk/render/src/3d/all.ts b/sdk/render/src/3d/all.ts
index 8b856eac..b70d3e7e 100644
--- a/sdk/render/src/3d/all.ts
+++ b/sdk/render/src/3d/all.ts
@@ -11,6 +11,7 @@ export * from "./bevy";
export * from "./compute";
export * from "./framepass";
export * from "./phase";
+export * from "./attribute";
export * from "./text";
export * from "./shapes";
export * from "./sprite";
diff --git a/sdk/render/src/3d/attribute.ts b/sdk/render/src/3d/attribute.ts
new file mode 100644
index 00000000..9d89c922
--- /dev/null
+++ b/sdk/render/src/3d/attribute.ts
@@ -0,0 +1,32 @@
+// A mesh's vertex attributes by Bevy's name, id and format (docs/control.feature FC2d): a mesh's own (Mesh3d
+// `attributes`) and the ones a Material's vertex buffer reads (Material `vertex_attributes`).
+
+/** A vertex attribute's format, as Bevy's VertexFormat names it. */
+export type VertexFormat = "Float32" | "Float32x2" | "Float32x3" | "Float32x4" | "Uint32" | "Uint32x2" | "Uint32x3" | "Uint32x4" | "Sint32" | "Sint32x4";
+
+/** A vertex attribute: its name, its unique id (a high random one for your own, as Bevy asks, so it sorts after the
+ * built-in ones) and its format. MeshVertexAttribute.new("BlendColor", 988540917, "Float32x4") is Bevy's
+ * MeshVertexAttribute::new; Mesh's own are ATTRIBUTE_POSITION and the rest.
+ * @bevy bevy_mesh::MeshVertexAttribute */
+export interface MeshVertexAttribute { name: string; id: number; format: VertexFormat }
+
+const FORMATS: VertexFormat[] = ["Float32", "Float32x2", "Float32x3", "Float32x4", "Uint32", "Uint32x2", "Uint32x3", "Uint32x4", "Sint32", "Sint32x4"];
+
+export const MeshVertexAttribute = {
+ new(name: string, id: number, format: VertexFormat): MeshVertexAttribute {
+ if (!name || !Number.isSafeInteger(id) || id < 0) throw new TypeError(`MeshVertexAttribute: a name and a whole-number id, got ${JSON.stringify([name, id])}`);
+ if (!FORMATS.includes(format)) throw new TypeError(`MeshVertexAttribute ${name}: format is one of ${FORMATS.join(", ")}, got ${JSON.stringify(format)}`);
+ return { name, id, format };
+ },
+};
+
+/** Bevy's Mesh::ATTRIBUTE_* (ids 0 to 5), as a Material's vertex buffer names them. */
+export const ATTRIBUTE_POSITION = MeshVertexAttribute.new("Vertex_Position", 0, "Float32x3");
+export const ATTRIBUTE_NORMAL = MeshVertexAttribute.new("Vertex_Normal", 1, "Float32x3");
+export const ATTRIBUTE_UV_0 = MeshVertexAttribute.new("Vertex_Uv", 2, "Float32x2");
+export const ATTRIBUTE_UV_1 = MeshVertexAttribute.new("Vertex_Uv_1", 3, "Float32x2");
+export const ATTRIBUTE_TANGENT = MeshVertexAttribute.new("Vertex_Tangent", 4, "Float32x4");
+export const ATTRIBUTE_COLOR = MeshVertexAttribute.new("Vertex_Color", 5, "Float32x4");
+
+/** One attribute's values on a mesh: one a vertex, each a number or a list of the format's size. */
+export type MeshAttribute = [MeshVertexAttribute, (number | number[])[]];
diff --git a/sdk/render/src/3d/shapes.tsx b/sdk/render/src/3d/shapes.tsx
index 38ec624b..cb1101ba 100644
--- a/sdk/render/src/3d/shapes.tsx
+++ b/sdk/render/src/3d/shapes.tsx
@@ -5,6 +5,7 @@ import { useAnimation3d, type Keys, type Move, type TrackSpec } from "@raea/math
import { Place3d, usePlace3d, usePointer3d, type LocalBounds, type PointerHandlers3d } from "../events3d";
import { Canvas3DProps, Vec3, checkArgs, checkPass, checkVec, node3d, serialisable } from "./all";
import { InCanvas3D } from "./model";
+import type { MeshAttribute } from "./attribute";
const FILL = { position: "absolute", left: 0, top: 0, width: "100%", height: "100%" } as const;
@@ -329,6 +330,9 @@ export interface MeshProps extends PointerHandlers3d {
/** false: no shadow falls on it (Bevy's NotShadowReceiver: a backdrop that stays clean under a floating product, a
* glowing sign). True by default */
receiveShadow?: boolean;
+ /** its geometry's own vertex attributes, as Bevy's Mesh::with_inserted_attribute adds them: [[attribute, values]],
+ * a value a vertex (a Cuboid has 24), for a Material whose vertex_attributes read them */
+ attributes?: MeshAttribute[];
/** the render layers it's on ([0] by default; Bevy's RenderLayers): a camera draws it only when they share a layer,
* and only lights sharing a layer light it */
layers?: number[];
@@ -405,5 +409,5 @@ export function Mesh(props: MeshProps) {
const id = useId();
const m = usePlace3d(position, rotation, scale, props.quaternion);
usePointer3d(props.name ?? id, m, () => (shape === undefined ? boundsFromChildren(props.children) : undefined), props as Record);
- return node3d("Mesh", { ...props, animate: undefined, shape: undefined, position, rotation, scale }, {props.children}, Object.keys(extra).length ? extra : undefined);
+ return node3d("Mesh", { ...props, animate: undefined, shape: undefined, position, rotation, scale, attributes: props.attributes && JSON.stringify(props.attributes) }, {props.children}, Object.keys(extra).length ? extra : undefined);
}
diff --git a/sdk/render/src/bevy3d/material.tsx b/sdk/render/src/bevy3d/material.tsx
index 2bce520a..f0523c4c 100644
--- a/sdk/render/src/bevy3d/material.tsx
+++ b/sdk/render/src/bevy3d/material.tsx
@@ -2,6 +2,7 @@
import { ExtendedMaterial, ShaderExtension, StandardMaterial, type AlphaMode } from "./materials";
import { surfaceShader, type SurfaceShaderSpec } from "../3d";
import type { StorageValue } from "../3d/materials";
+import type { MeshVertexAttribute } from "../3d/attribute";
/** Your own Material (docs/control.feature FC2a): Bevy's `impl Material`, not an extension of StandardMaterial. Its
* fragment_shader() (and vertex_shader(), when `vertex` names one) is the file compiled as written: its imports (a
@@ -18,6 +19,9 @@ export interface MaterialProps {
fragment: string;
/** a vertex shader file, as Material::vertex_shader names one (Bevy's mesh vertex shader when not given) */
vertex?: string;
+ /** the mesh attributes its vertex shader reads, each at its @location: [[ATTRIBUTE_POSITION, 0], [BLEND_COLOR, 1]], the
+ * vertex buffer its specialize lays out (layout.0.get_layout); the mesh's whole layout when not given */
+ vertex_attributes?: [MeshVertexAttribute, number][];
uniforms?: Record>;
/** its read-only storage buffers (`var NAME: TYPE`, up to 2), each in TYPE's storage layout: an
* array a list of its elements, a number or [x, y(, z(, w))] each, as Bevy's ShaderBuffer::from(Vec<[f32; 4]>)
@@ -41,7 +45,7 @@ const storageValue = (x: unknown): boolean => Number.isFinite(x) || (Array.isArr
/** A Material's props checked, as the shader spec it is drawn by (in a Mesh3d an OwnMaterial, in a Mesh2d its
* Material2d). */
-function ownSpec({ fragment, vertex, uniforms, buffers, textures, defs, alpha_mode, depth_bias, cull_mode, constants }: MaterialProps): SurfaceShaderSpec {
+function ownSpec({ fragment, vertex, vertex_attributes, uniforms, buffers, textures, defs, alpha_mode, depth_bias, cull_mode, constants }: MaterialProps): SurfaceShaderSpec {
for (const [k, b] of Object.entries(buffers ?? {})) if (typeof b !== "object" || b === null || !storageValue(b)) throw new TypeError(`Material: buffer ${k} is its elements' list or a struct's { field: value }, of numbers and lists, got ${JSON.stringify(b)}`);
if (typeof fragment !== "string" || !fragment) throw new TypeError(`Material: fragment is the shader file, staticFile("shaders/x.wesl"), got ${JSON.stringify(fragment)}`);
if (vertex !== undefined && (typeof vertex !== "string" || !vertex)) throw new TypeError(`Material: vertex is a shader file, got ${JSON.stringify(vertex)}`);
@@ -50,7 +54,8 @@ function ownSpec({ fragment, vertex, uniforms, buffers, textures, defs, alpha_mo
if (depth_bias !== undefined && !Number.isFinite(depth_bias)) throw new TypeError(`Material: depth_bias is a number, got ${depth_bias}`);
if (cull_mode !== undefined && cull_mode !== null && cull_mode !== "Front" && cull_mode !== "Back") throw new TypeError(`Material: cull_mode is "Front", "Back" or null, got ${JSON.stringify(cull_mode)}`);
for (const [k, n] of Object.entries(constants ?? {})) if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(k) || !Number.isFinite(n)) throw new TypeError(`Material: constant ${k} is a WGSL name with a number, got ${JSON.stringify(n)}`);
- const own = { ...(vertex !== undefined ? { vertex } : {}), ...(alpha_mode !== undefined ? { alpha_mode } : {}), ...(depth_bias !== undefined ? { depth_bias } : {}), ...(cull_mode !== undefined ? { cull_mode } : {}), ...(constants !== undefined ? { constants } : {}) };
+ for (const e of vertex_attributes ?? []) if (!Array.isArray(e) || typeof e[0] !== "object" || !Number.isInteger(e[1]) || e[1] < 0) throw new TypeError(`Material: vertex_attributes are [attribute, location] pairs, got ${JSON.stringify(e)}`);
+ const own = { ...(vertex !== undefined ? { vertex } : {}), ...(vertex_attributes !== undefined ? { vertex_attributes } : {}), ...(alpha_mode !== undefined ? { alpha_mode } : {}), ...(depth_bias !== undefined ? { depth_bias } : {}), ...(cull_mode !== undefined ? { cull_mode } : {}), ...(constants !== undefined ? { constants } : {}) };
return { fragment, uniforms: uniforms as SurfaceShaderSpec["uniforms"], textures, defs, own, ...(buffers ? { buffers } : {}) };
}
diff --git a/skills b/skills
index 8fa445cf..52186449 160000
--- a/skills
+++ b/skills
@@ -1 +1 @@
-Subproject commit 8fa445cf35370b89cccbe9fa12259b16e35b544c
+Subproject commit 521864498d43655b9b5fe4b673c95df99ff9d5ee
diff --git a/systems/mesh/src/attribute.rs b/systems/mesh/src/attribute.rs
new file mode 100644
index 00000000..6bf77050
--- /dev/null
+++ b/systems/mesh/src/attribute.rs
@@ -0,0 +1,119 @@
+//! A mesh's own vertex attributes (docs/control.feature FC2d): Bevy's MeshVertexAttribute by its name, id and format,
+//! as shader_advanced/custom_vertex_attribute's ATTRIBUTE_BLEND_COLOR is one, inserted into a mesh with its values as
+//! Mesh::with_inserted_attribute inserts them, and named in a material's vertex buffer layout.
+
+use std::collections::BTreeSet;
+use std::sync::Mutex;
+
+use bevy::mesh::{MeshVertexAttribute, VertexAttributeValues};
+use bevy::prelude::*;
+use bevy::render::render_resource::VertexFormat;
+use serde_json::Value;
+
+/// Each name given once for all time: MeshVertexAttribute holds a `&'static str`, as Bevy's are constants.
+static NAMES: Mutex> = Mutex::new(BTreeSet::new());
+
+/// The vertex formats an attribute may have, by Bevy's VertexFormat name.
+fn format(f: &str) -> Option {
+ Some(match f {
+ "Float32" => VertexFormat::Float32,
+ "Float32x2" => VertexFormat::Float32x2,
+ "Float32x3" => VertexFormat::Float32x3,
+ "Float32x4" => VertexFormat::Float32x4,
+ "Uint32" => VertexFormat::Uint32,
+ "Uint32x2" => VertexFormat::Uint32x2,
+ "Uint32x3" => VertexFormat::Uint32x3,
+ "Uint32x4" => VertexFormat::Uint32x4,
+ "Sint32" => VertexFormat::Sint32,
+ "Sint32x4" => VertexFormat::Sint32x4,
+ _ => return None,
+ })
+}
+
+/// The attribute { name, id, format } names.
+pub fn attribute(v: &Value) -> Result {
+ let name = v.get("name").and_then(Value::as_str).ok_or_else(|| format!("a mesh attribute is {{ name, id, format }}, got {v}"))?;
+ let id = v.get("id").and_then(Value::as_u64).ok_or_else(|| format!("mesh attribute {name}: its id is a whole number"))?;
+ let f = v.get("format").and_then(Value::as_str).unwrap_or_default();
+ let format = format(f).ok_or_else(|| format!("mesh attribute {name}: format {f:?} (Float32, Float32x2/3/4, Uint32, Uint32x2/3/4, Sint32, Sint32x4)"))?;
+ let mut names = NAMES.lock().map_err(|_| "mesh attribute names: poisoned")?;
+ let name: &'static str = match names.get(name) {
+ Some(n) => n,
+ None => {
+ let n: &'static str = Box::leak(name.into());
+ names.insert(n);
+ n
+ }
+ };
+ Ok(MeshVertexAttribute::new(name, id, format))
+}
+
+/// One vertex's value as `n` numbers (a number alone when n is 1).
+fn row(v: &Value, n: usize) -> Option> {
+ match v {
+ Value::Number(x) if n == 1 => x.as_f64().map(|x| vec![x]),
+ Value::Array(a) if a.len() == n => a.iter().map(Value::as_f64).collect(),
+ _ => None,
+ }
+}
+
+/// An attribute's values, one a vertex, in its format.
+fn values(a: &MeshVertexAttribute, vals: &[Value]) -> Result {
+ let n = match a.format { VertexFormat::Float32 | VertexFormat::Uint32 | VertexFormat::Sint32 => 1, VertexFormat::Float32x2 | VertexFormat::Uint32x2 => 2, VertexFormat::Float32x3 | VertexFormat::Uint32x3 => 3, _ => 4 };
+ let rows: Vec> = vals.iter().map(|v| row(v, n)).collect::