1 module unecht.core.components.editor.mousePicking;
2 
3 version(UEIncludeEditor):
4 
5 import unecht.ue;
6 import unecht.core.entity;
7 import unecht.core.components.renderer;
8 import unecht.core.math.ray;
9 
10 import unecht.core.stdex;
11 
12 import gl3n.linalg;
13 import gl3n.aabb;
14 
15 ///
16 struct MousePicking
17 {
18     ///
19     public static void onPick(ray _r)
20     {
21         auto renderers = ue.scene.gatherAllComponents!UERenderer;
22 
23         UEEntity target;
24         float minDistance = float.infinity;
25 
26         foreach(r; renderers)
27         {
28             if(r.enabled && r.sceneNode.enabled && r.sceneNode.parent.enabled && r.entity.layer != UELayer.editor)
29             {
30                 auto box = r.mesh.aabb.scale(r.sceneNode.scaling);
31                 box = box.translate(r.sceneNode.position);
32 
33                 float distance;
34                 if(_r.intersects(box,distance))
35                 {
36                     if(!target || minDistance > distance)
37                     {
38                         target = r.entity;
39                         minDistance = distance;
40                     }
41                 }
42             }
43         }
44 
45         import unecht.core.components._editor;
46         EditorRootComponent.selectEntity(target);
47     }
48 }
49 
50 static AABB scale(AABB box, vec3 scaling)
51 {
52     return AABB(box.min.scale(scaling), box.max.scale(scaling));
53 }
54 
55 static vec3 scale(vec3 a, vec3 b)
56 {
57     return vec3(a.x * b.x, a.y * b.y, a.z * b.z);
58 }
59 
60 static AABB translate(AABB box, vec3 translate)
61 {
62     return AABB(box.min + translate, box.max + translate);
63 }