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