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     public Vector3 offset = Vector3.zero;
17 
18     override void setup() {
19         gen_model();
20     }
21 
22     @property BoundingBox bounds() {
23         return Bounds.calculate(raylib.GetMeshBoundingBox(_mesh), entity.transform);
24     }
25 
26     /// gets the effect
27     @property ref Effect effect() {
28         return _effect;
29     }
30 
31     /// sets the effect
32     @property Effect effect(Effect value) {
33         _effect = value;
34         _model.materials[0].shader = _effect.shader;
35         return value;
36     }
37 
38     /// gets the model
39     @property ref Model model() {
40         return _model;
41     }
42 
43     /// create the mesh
44     protected abstract Mesh gen_mesh();
45 
46     /// generate the model (from the mesh)
47     protected void gen_model() {
48         _mesh = gen_mesh();
49         _model = raylib.LoadModelFromMesh(_mesh);
50     }
51 
52     public void render() {
53         raylib.DrawModelEx(_model, transform.position + offset, transform.axis_angle.axis,
54                 transform.axis_angle.angle * C_RAD2DEG, transform.scale, effect.color);
55     }
56 
57     public void debug_render() {
58         DebugRender.default_debug_render(this, _model);
59     }
60 
61     override void destroy() {
62         // freeing the model also frees the mesh
63         raylib.UnloadModel(_model);
64     }
65 }