Lagrange
default_entities.h
1/*
2 * Copyright 2021 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/ui/api.h>
15#include <lagrange/ui/Entity.h>
16#include <lagrange/ui/default_components.h>
17#include <lagrange/ui/default_shaders.h>
18#include <lagrange/ui/utils/io.h>
19#include <lagrange/ui/utils/treenode.h>
20
21namespace lagrange {
22namespace ui {
23
24LA_UI_API void set_material(Registry& r, Entity meshrender_entity, std::shared_ptr<Material> mat);
25
26LA_UI_API Entity show_mesh(
27 Registry& r,
28 const Entity& mesh_entity,
29 StringID shader = DefaultShaders::PBR,
30 const ShaderDefines& shader_defines = {});
31LA_UI_API Entity show_submesh(
32 Registry& r,
33 const Entity& mesh_entity,
34 std::shared_ptr<Material> material,
35 entt::id_type submesh_id);
36
37LA_UI_API Entity show_mesh(
38 Registry& r,
39 Entity mesh_entity,
40 Entity scene_node_entity,
41 StringID shader = DefaultShaders::PBR,
42 const ShaderDefines& shader_defines = {});
43
44LA_UI_API Entity show_mesh(
45 Registry& r,
46 Entity mesh_entity,
47 Entity scene_node_entity,
48 std::shared_ptr<Material> material);
49
50
51/*
52 Attribute visualization
53*/
54LA_UI_API Entity show_vertex_attribute(
55 Registry& r,
56 const Entity& mesh_entity,
57 const std::string& attribute,
58 Glyph glyph);
59
60LA_UI_API Entity show_facet_attribute(
61 Registry& r,
62 const Entity& mesh_entity,
63 const std::string& attribute,
64 Glyph glyph);
65
66LA_UI_API Entity show_edge_attribute(
67 Registry& r,
68 const Entity& mesh_entity,
69 const std::string& attribute,
70 Glyph glyph);
71
72LA_UI_API Entity show_corner_attribute(
73 Registry& r,
74 const Entity& mesh_entity,
75 const std::string& attribute,
76 Glyph glyph);
77
78LA_UI_API Entity show_indexed_attribute(
79 Registry& r,
80 const Entity& mesh_entity,
81 const std::string& attribute,
82 Glyph glyph);
83
84
85LA_UI_API void set_colormap(Registry& r, Entity meshrender_entity, std::shared_ptr<Texture> texture);
86
87LA_UI_API void set_colormap_range(
88 Registry& r,
89 Entity meshrender_entity,
90 const Eigen::Vector4f& range_min,
91 const Eigen::Vector4f& range_max);
92
93
94LA_UI_API void set_colormap_range(
95 Registry& r,
96 Entity meshrender_entity,
97 const std::pair<Eigen::VectorXf, Eigen::VectorXf>& range);
98
99
100LA_UI_API std::shared_ptr<Material> get_material(Registry& r, Entity meshrender_entity);
101
102inline Transform& get_transform(Registry& r, Entity e)
103{
104 return r.get<Transform>(e);
105}
106
107
108template <typename Derived>
109inline Eigen::Affine3f& set_transform(Registry& r, Entity e, const Derived& local_transform)
110{
111 auto& t = r.get<Transform>(e).local;
112 t = local_transform;
113 return t;
114}
115
116template <typename Derived>
117inline Eigen::Affine3f& apply_transform(Registry& r, Entity e, const Derived& local_transform)
118{
119 Eigen::Affine3f t;
120 t = local_transform; // Convert to affine
121 return set_transform(r, e, t * get_transform(r, e).local);
122}
123
124/*
125 Mesh
126*/
127template <typename MeshType>
128Entity add_mesh(
129 Registry& r,
130 std::shared_ptr<MeshType> mesh,
131 const std::string& name = "Unnamed mesh",
132 StringID shader = DefaultShaders::PBR)
133{
134 auto mesh_geometry = register_mesh<MeshType>(r, std::move(mesh));
135 auto mesh_view = show_mesh(r, mesh_geometry, shader);
136 ui::set_name(r, mesh_geometry, name);
137 ui::set_name(r, mesh_view, name);
138 return mesh_view;
139}
140
141template <typename MeshType>
142Entity add_mesh(
143 Registry& r,
144 std::unique_ptr<MeshType> mesh,
145 const std::string& name = "Unnamed mesh",
146 StringID shader = DefaultShaders::PBR)
147{
148 return add_mesh(r, to_shared_ptr(std::move(mesh)), name, shader);
149}
150
151template <typename MeshType>
152Entity load_mesh(
153 Registry& r,
154 const lagrange::fs::path& path_to_obj,
155 bool load_materials = true,
156 const std::string& name = "Unnamed mesh",
157 StringID shader = DefaultShaders::PBR)
158{
159 if (!load_materials) {
160 auto me = load_obj<MeshType>(r, path_to_obj);
161 if (r.valid(me)) {
162 auto e = show_mesh(r, me, shader);
163 ui::set_name(r, e, name);
164 return e;
165 }
166 } else {
167 auto [me, mats] = load_obj_with_materials<MeshType>(r, path_to_obj);
168
169 if (!r.valid(me)) return NullEntity;
170
171 if (mats.size() <= 1) {
172 auto e = show_mesh(r, me, shader);
173 ui::set_name(r, e, name);
174 if (mats.size() == 1) ui::set_material(r, e, mats.front());
175 return e;
176 } else {
177 auto e = create_scene_node(r, name);
178 for (auto mat_index = 0; mat_index < mats.size(); mat_index++) {
179 auto sub = ui::show_submesh(r, me, mats[mat_index], mat_index);
180 ui::set_name(r, sub, lagrange::string_format("{} submesh {}", name, mat_index));
181 ui::set_parent(r, sub, e);
182 }
183 return e;
184 }
185 }
186
187 return NullEntity;
188}
189
190/*
191 * Mesh update
192 */
193LA_UI_API void set_mesh_vertices_dirty(Registry& r, Entity mesh_entity);
194LA_UI_API void set_mesh_normals_dirty(Registry& r, Entity mesh_entity);
195LA_UI_API void set_mesh_dirty(Registry& r, Entity mesh_entity);
196
197LA_UI_API void set_show_attribute_dirty(Registry& r, Entity scene_entity);
198LA_UI_API void set_mesh_attribute_dirty(
199 Registry& r,
200 Entity mesh_entity,
201 IndexingMode mode,
202 const std::string& name);
203
204LA_UI_API Entity get_meshdata_entity(Registry& r, Entity scene_entity);
205LA_UI_API MeshData& get_meshdata(Registry& r, Entity scene_or_mesh_entity);
206
207
208/*
209 Material
210*/
211std::shared_ptr<Material>
212LA_UI_API create_material(Registry& r, entt::id_type shader_id, const ShaderDefines& shader_defines = {});
213
214
215LA_UI_API Entity add_camera(Registry& r, const Camera& camera = Camera::default_camera(1, 1));
216
217
218// Clears all user added entities
219LA_UI_API void clear_scene(Registry& r);
220
224LA_UI_API std::optional<std::pair<Entity, RayFacetHit>> intersect_ray(
225 Registry& r,
226 const Eigen::Vector3f& origin,
227 const Eigen::Vector3f& dir,
228 ui::Entity root = ui::NullEntity,
229 Layer visible_layers = Layer(true),
230 Layer hidden_layers = Layer(false));
231
232
233} // namespace ui
234} // namespace lagrange
static Camera default_camera(float width, float height, Type type=Type::PERSPECTIVE)
Initializes default view.
Definition: Camera.cpp:29
internal::Range< Index > range(Index end)
Returns an iterable object representing the range [0, end).
Definition: range.h:176
std::string string_format(fmt::format_string< Args... > format, Args &&... args)
Format args according to the format string fmt, and return the result as a string.
Definition: strings.h:103
Lagrange UI Viewer and mini 3D engine.
Definition: AcceleratedPicking.h:22
LA_UI_API void set_parent(Registry &registry, Entity child, Entity new_parent)
Sets new_parent as as child's new parent.
Definition: treenode.cpp:155
LA_UI_API std::optional< std::pair< Entity, RayFacetHit > > intersect_ray(Registry &r, const Eigen::Vector3f &origin, const Eigen::Vector3f &dir, ui::Entity root=ui::NullEntity, Layer visible_layers=Layer(true), Layer hidden_layers=Layer(false))
Intersect ray with meshes in root's hierarchy Returns a pair of intersected entity and the correspond...
Definition: default_entities.cpp:502
Main namespace for Lagrange.
Definition: AABBIGL.h:30
std::shared_ptr< T > to_shared_ptr(std::unique_ptr< T > &&ptr)
Helper for automatic type deduction for unique_ptr to shared_ptr conversion.
Definition: common.h:84