Lagrange
Loading...
Searching...
No Matches
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/Entity.h>
15#include <lagrange/ui/api.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
86set_colormap(Registry& r, Entity meshrender_entity, std::shared_ptr<Texture> texture);
87
88LA_UI_API void set_colormap_range(
89 Registry& r,
90 Entity meshrender_entity,
91 const Eigen::Vector4f& range_min,
92 const Eigen::Vector4f& range_max);
93
94
95LA_UI_API void set_colormap_range(
96 Registry& r,
97 Entity meshrender_entity,
98 const std::pair<Eigen::VectorXf, Eigen::VectorXf>& range);
99
100
101LA_UI_API std::shared_ptr<Material> get_material(Registry& r, Entity meshrender_entity);
102
103inline Transform& get_transform(Registry& r, Entity e)
104{
105 return r.get<Transform>(e);
106}
107
108
109template <typename Derived>
110inline Eigen::Affine3f& set_transform(Registry& r, Entity e, const Derived& local_transform)
111{
112 auto& t = r.get<Transform>(e).local;
113 t = local_transform;
114 return t;
115}
116
117template <typename Derived>
118inline Eigen::Affine3f& apply_transform(Registry& r, Entity e, const Derived& local_transform)
119{
120 Eigen::Affine3f t;
121 t = local_transform; // Convert to affine
122 return set_transform(r, e, t * get_transform(r, e).local);
123}
124
125/*
126 Mesh
127*/
128template <typename MeshType>
129Entity add_mesh(
130 Registry& r,
131 std::shared_ptr<MeshType> mesh,
132 const std::string& name = "Unnamed mesh",
133 StringID shader = DefaultShaders::PBR)
134{
135 auto mesh_geometry = register_mesh<MeshType>(r, std::move(mesh));
136 auto mesh_view = show_mesh(r, mesh_geometry, shader);
137 ui::set_name(r, mesh_geometry, name);
138 ui::set_name(r, mesh_view, name);
139 return mesh_view;
140}
141
142template <typename MeshType>
143Entity add_mesh(
144 Registry& r,
145 std::unique_ptr<MeshType> mesh,
146 const std::string& name = "Unnamed mesh",
147 StringID shader = DefaultShaders::PBR)
148{
149 return add_mesh(r, to_shared_ptr(std::move(mesh)), name, shader);
150}
151
152template <typename MeshType>
153Entity load_mesh(
154 Registry& r,
155 const lagrange::fs::path& path_to_obj,
156 bool load_materials = true,
157 const std::string& name = "Unnamed mesh",
158 StringID shader = DefaultShaders::PBR)
159{
160 if (!load_materials) {
161 auto me = load_obj<MeshType>(r, path_to_obj);
162 if (r.valid(me)) {
163 auto e = show_mesh(r, me, shader);
164 ui::set_name(r, e, name);
165 return e;
166 }
167 } else {
168 auto [me, mats] = load_obj_with_materials<MeshType>(r, path_to_obj);
169
170 if (!r.valid(me)) return NullEntity;
171
172 if (mats.size() <= 1) {
173 auto e = show_mesh(r, me, shader);
174 ui::set_name(r, e, name);
175 if (mats.size() == 1) ui::set_material(r, e, mats.front());
176 return e;
177 } else {
178 auto e = create_scene_node(r, name);
179 for (auto mat_index = 0; mat_index < mats.size(); mat_index++) {
180 auto sub = ui::show_submesh(r, me, mats[mat_index], mat_index);
181 ui::set_name(r, sub, lagrange::string_format("{} submesh {}", name, mat_index));
182 ui::set_parent(r, sub, e);
183 }
184 return e;
185 }
186 }
187
188 return NullEntity;
189}
190
191/*
192 * Mesh update
193 */
194LA_UI_API void set_mesh_vertices_dirty(Registry& r, Entity mesh_entity);
195LA_UI_API void set_mesh_normals_dirty(Registry& r, Entity mesh_entity);
196LA_UI_API void set_mesh_dirty(Registry& r, Entity mesh_entity);
197
198LA_UI_API void set_show_attribute_dirty(Registry& r, Entity scene_entity);
199LA_UI_API void set_mesh_attribute_dirty(
200 Registry& r,
201 Entity mesh_entity,
202 IndexingMode mode,
203 const std::string& name);
204
205LA_UI_API Entity get_meshdata_entity(Registry& r, Entity scene_entity);
206LA_UI_API MeshData& get_meshdata(Registry& r, Entity scene_or_mesh_entity);
207
208
209/*
210 Material
211*/
212std::shared_ptr<Material> LA_UI_API
213create_material(Registry& r, entt::id_type shader_id, const ShaderDefines& shader_defines = {});
214
215
216LA_UI_API Entity add_camera(Registry& r, const Camera& camera = Camera::default_camera(1, 1));
217
218
219// Clears all user added entities
220LA_UI_API void clear_scene(Registry& r);
221
225LA_UI_API std::optional<std::pair<Entity, RayFacetHit>> intersect_ray(
226 Registry& r,
227 const Eigen::Vector3f& origin,
228 const Eigen::Vector3f& dir,
229 ui::Entity root = ui::NullEntity,
230 Layer visible_layers = Layer(true),
231 Layer hidden_layers = Layer(false));
232
233
234} // namespace ui
235} // namespace lagrange
Camera class.
Definition Camera.h:38
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:23
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
std::pair< Entity, std::vector< std::shared_ptr< Material > > > load_obj_with_materials(Registry &r, const fs::path &path, const io::MeshLoaderParams &params=io::MeshLoaderParams())
Loads obj as a single mesh and materials.
Definition io.h:73
Entity load_obj(Registry &r, const fs::path &path, const io::MeshLoaderParams &params=io::MeshLoaderParams())
Loads obj as a single mesh.
Definition io.h:43
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.
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:88
Definition Layer.h:29
Definition MeshData.h:21
Affine transformation.
Definition Transform.h:23