1 module re.gfx.shapes.mesh;
2 
3 import re.ecs;
4 import re.gfx;
5 import re.math;
6 import re.ng.diag;
7 static import raylib;
8 
9 /// renders a model given a mesh. should only be used for procedural meshes; use Model3D for models instead
10 abstract class RenderableMesh : Component, Renderable3D {
11     mixin Reflect;
12     /// effect
13     private Effect _effect;
14     private Mesh _mesh;
15     private Model _model;
16 
17     override void setup() {
18         gen_model();
19     }
20 
21     @property BoundingBox bounds() {
22         return Bounds.calculate(raylib.MeshBoundingBox(_mesh), entity.transform);
23     }
24 
25     /// gets the effect
26     @property ref Effect effect() {
27         return _effect;
28     }
29 
30     /// sets the effect
31     @property Effect effect(Effect value) {
32         _effect = value;
33         _model.materials[0].shader = _effect.shader;
34         return value;
35     }
36 
37     /// gets the model
38     @property ref Model model() {
39         return _model;
40     }
41 
42     /// create the mesh
43     protected abstract Mesh gen_mesh();
44 
45     /// generate the model (from the mesh)
46     protected void gen_model() {
47         _mesh = gen_mesh();
48         _model = raylib.LoadModelFromMesh(_mesh);
49     }
50 
51     public void render() {
52         raylib.DrawModelEx(_model, transform.position, transform.axis_angle.axis,
53                 transform.axis_angle.angle * C_RAD2DEG, transform.scale, effect.color);
54     }
55 
56     public void debug_render() {
57         DebugRender.default_debug_render(this, _model);
58     }
59 
60     override void destroy() {
61         // freeing the model also frees the mesh
62         raylib.UnloadModel(_model);
63     }
64 }