Repository navigation
Expand file tree
/
Copy pathmaterial.d
More file actions
45 lines (40 loc) · 1.57 KB
/
Copy pathmaterial.d
File metadata and controls
45 lines (40 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Authors: Danny Arends
* License: GPL-v3 (See accompanying file LICENSE.txt or copy at https://www.gnu.org/licenses/gpl-3.0.en.html)
*/
import engine;
struct Material {
int tid = -1;
int nid = -1;
int oid = -1;
int pad = 0;
}
/** Bake a mesh's global material id into normal.w across its vertex range (replaces the meshSSBO lookup) */
void stampMid(ref Geometry object, const ref Mesh mesh) {
foreach(ref v; object.vertices[mesh.vertices[0] .. mesh.vertices[1]]) {
v.definition[0] = cast(float)mesh.mid;
}
object.vertices.invalidate();
}
/** Register a global material slot for every mesh with a bindable material (call once at load) */
void registerMaterials(ref App app, ref Geometry object) {
foreach(ref mesh; object.meshes) {
if(object.materials.length == 0) continue; // no material to bind
if(mesh.mid >= object.materials.length) continue; // invalid local index
if(mesh.mid >= 0) mesh.mat = mesh.mid; // assimp: save local index (procedural keeps its mat)
mesh.mid = cast(int)(app.materials.length);
app.materials ~= Material();
stampMid(object, mesh);
}
app.buffers["MaterialBuffer"].invalidate();
}
/** Idempotent top-up: give a global material slot to any mesh still lacking one (safe to repeat) */
void ensureMaterial(ref App app, ref Geometry object) {
foreach(ref mesh; object.meshes) {
if(mesh.mid >= 0) continue;
mesh.mid = cast(int)(app.materials.length);
app.materials ~= Material();
stampMid(object, mesh);
}
app.buffers["MaterialBuffer"].invalidate();
}