Debian Codegraph @

scene/resources/immediate_mesh.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* immediate_mesh.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "immediate_mesh.h"
32
33 void ImmediateMesh::surface_begin(PrimitiveType p_primitive, const Ref<Material> &p_material) {
34 ERR_FAIL_COND_MSG(surface_active, "Already creating a new surface.");
35 active_surface_data.primitive = p_primitive;
36 active_surface_data.material = p_material;
37 surface_active = true;
38 }
39 void ImmediateMesh::surface_set_color(const Color &p_color) {
40 ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
41
42 if (!uses_colors) {
43 colors.resize(vertices.size());
44 for (Color &color : colors) {
45 color = p_color;
46 }
47 uses_colors = true;
48 }
49
50 current_color = p_color;
51 }
52 void ImmediateMesh::surface_set_normal(const Vector3 &p_normal) {
53 ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
54
55 if (!uses_normals) {
56 normals.resize(vertices.size());
57 for (Vector3 &normal : normals) {
58 normal = p_normal;
59 }
60 uses_normals = true;
61 }
62
63 current_normal = p_normal;
64 }
65 void ImmediateMesh::surface_set_tangent(const Plane &p_tangent) {
66 ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
67 if (!uses_tangents) {
68 tangents.resize(vertices.size());
69 for (Plane &tangent : tangents) {
70 tangent = p_tangent;
71 }
72 uses_tangents = true;
73 }
74
75 current_tangent = p_tangent;
76 }
77 void ImmediateMesh::surface_set_uv(const Vector2 &p_uv) {
78 ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
79 if (!uses_uvs) {
80 uvs.resize(vertices.size());
81 for (Vector2 &uv : uvs) {
82 uv = p_uv;
83 }
84 uses_uvs = true;
85 }
86
87 current_uv = p_uv;
88 }
89 void ImmediateMesh::surface_set_uv2(const Vector2 &p_uv2) {
90 ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
91 if (!uses_uv2s) {
92 uv2s.resize(vertices.size());
93 for (Vector2 &uv : uv2s) {
94 uv = p_uv2;
95 }
96 uses_uv2s = true;
97 }
98
99 current_uv2 = p_uv2;
100 }
101 void ImmediateMesh::surface_add_vertex(const Vector3 &p_vertex) {
102 ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
103 ERR_FAIL_COND_MSG(vertices.size() && active_surface_data.vertex_2d, "Can't mix 2D and 3D vertices in a surface.");
104
105 if (uses_colors) {
106 colors.push_back(current_color);
107 }
108 if (uses_normals) {
109 normals.push_back(current_normal);
110 }
111 if (uses_tangents) {
112 tangents.push_back(current_tangent);
113 }
114 if (uses_uvs) {
115 uvs.push_back(current_uv);
116 }
117 if (uses_uv2s) {
118 uv2s.push_back(current_uv2);
119 }
120 vertices.push_back(p_vertex);
121 }
122
123 void ImmediateMesh::surface_add_vertex_2d(const Vector2 &p_vertex) {
124 ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
125 ERR_FAIL_COND_MSG(vertices.size() && !active_surface_data.vertex_2d, "Can't mix 2D and 3D vertices in a surface.");
126
127 if (uses_colors) {
128 colors.push_back(current_color);
129 }
130 if (uses_normals) {
131 normals.push_back(current_normal);
132 }
133 if (uses_tangents) {
134 tangents.push_back(current_tangent);
135 }
136 if (uses_uvs) {
137 uvs.push_back(current_uv);
138 }
139 if (uses_uv2s) {
140 uv2s.push_back(current_uv2);
141 }
142 Vector3 v(p_vertex.x, p_vertex.y, 0);
143 vertices.push_back(v);
144
145 active_surface_data.vertex_2d = true;
146 }
147
148 void ImmediateMesh::surface_end() {
149 ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
150 ERR_FAIL_COND_MSG(vertices.is_empty(), "No vertices were added, surface can't be created.");
151
152 uint64_t format = ARRAY_FORMAT_VERTEX | ARRAY_FLAG_FORMAT_CURRENT_VERSION;
153
154 uint32_t vertex_stride = 0;
155 if (active_surface_data.vertex_2d) {
156 format |= ARRAY_FLAG_USE_2D_VERTICES;
157 vertex_stride = sizeof(float) * 2;
158 } else {
159 vertex_stride = sizeof(float) * 3;
160 }
161 uint32_t normal_tangent_stride = 0;
162 uint32_t normal_offset = 0;
163 if (uses_normals) {
164 format |= ARRAY_FORMAT_NORMAL;
165 normal_offset = vertex_stride * vertices.size();
166 normal_tangent_stride += sizeof(uint32_t);
167 }
168 uint32_t tangent_offset = 0;
169 if (uses_tangents || uses_normals) {
170 format |= ARRAY_FORMAT_TANGENT;
171 tangent_offset = vertex_stride * vertices.size() + normal_tangent_stride;
172 normal_tangent_stride += sizeof(uint32_t);
173 }
174
175 AABB aabb;
176
177 {
178 surface_vertex_create_cache.resize((vertex_stride + normal_tangent_stride) * vertices.size());
179 uint8_t *surface_vertex_ptr = surface_vertex_create_cache.ptrw();
180 for (uint32_t i = 0; i < vertices.size(); i++) {
181 {
182 float *vtx = (float *)&surface_vertex_ptr[i * vertex_stride];
183 vtx[0] = vertices[i].x;
184 vtx[1] = vertices[i].y;
185 if (!active_surface_data.vertex_2d) {
186 vtx[2] = vertices[i].z;
187 }
188 if (i == 0) {
189 aabb = AABB(vertices[i], SMALL_VEC3); // Must have a bit of size.
190 } else {
191 aabb.expand_to(vertices[i]);
192 }
193 }
194 if (uses_normals) {
195 uint32_t *normal = (uint32_t *)&surface_vertex_ptr[i * normal_tangent_stride + normal_offset];
196
197 Vector2 n = normals[i].octahedron_encode();
198
199 uint32_t value = 0;
200 value |= (uint16_t)CLAMP(n.x * 65535, 0, 65535);
201 value |= (uint16_t)CLAMP(n.y * 65535, 0, 65535) << 16;
202
203 *normal = value;
204 }
205 if (uses_tangents || uses_normals) {
206 uint32_t *tangent = (uint32_t *)&surface_vertex_ptr[i * normal_tangent_stride + tangent_offset];
207 Vector2 t;
208 if (uses_tangents) {
209 t = tangents[i].normal.octahedron_tangent_encode(tangents[i].d);
210 } else {
211 Vector3 tan = Vector3(normals[i].z, -normals[i].x, normals[i].y).cross(normals[i].normalized()).normalized();
212 t = tan.octahedron_tangent_encode(1.0);
213 }
214
215 uint32_t value = 0;
216 value |= (uint16_t)CLAMP(t.x * 65535, 0, 65535);
217 value |= (uint16_t)CLAMP(t.y * 65535, 0, 65535) << 16;
218 if (value == 4294901760) {
219 // (1, 1) and (0, 1) decode to the same value, but (0, 1) messes with our compression detection.
220 // So we sanitize here.
221 value = 4294967295;
222 }
223
224 *tangent = value;
225 }
226 }
227 }
228
229 if (uses_colors || uses_uvs || uses_uv2s) {
230 uint32_t attribute_stride = 0;
231
232 if (uses_colors) {
233 format |= ARRAY_FORMAT_COLOR;
234 attribute_stride += sizeof(uint8_t) * 4;
235 }
236 uint32_t uv_offset = 0;
237 if (uses_uvs) {
238 format |= ARRAY_FORMAT_TEX_UV;
239 uv_offset = attribute_stride;
240 attribute_stride += sizeof(float) * 2;
241 }
242 uint32_t uv2_offset = 0;
243 if (uses_uv2s) {
244 format |= ARRAY_FORMAT_TEX_UV2;
245 uv2_offset = attribute_stride;
246 attribute_stride += sizeof(float) * 2;
247 }
248
249 surface_attribute_create_cache.resize(vertices.size() * attribute_stride);
250
251 uint8_t *surface_attribute_ptr = surface_attribute_create_cache.ptrw();
252
253 for (uint32_t i = 0; i < vertices.size(); i++) {
254 if (uses_colors) {
255 uint8_t *color8 = (uint8_t *)&surface_attribute_ptr[i * attribute_stride];
256
257 color8[0] = uint8_t(CLAMP(colors[i].r * 255.0, 0.0, 255.0));
258 color8[1] = uint8_t(CLAMP(colors[i].g * 255.0, 0.0, 255.0));
259 color8[2] = uint8_t(CLAMP(colors[i].b * 255.0, 0.0, 255.0));
260 color8[3] = uint8_t(CLAMP(colors[i].a * 255.0, 0.0, 255.0));
261 }
262 if (uses_uvs) {
263 float *uv = (float *)&surface_attribute_ptr[i * attribute_stride + uv_offset];
264
265 uv[0] = uvs[i].x;
266 uv[1] = uvs[i].y;
267 }
268
269 if (uses_uv2s) {
270 float *uv2 = (float *)&surface_attribute_ptr[i * attribute_stride + uv2_offset];
271
272 uv2[0] = uv2s[i].x;
273 uv2[1] = uv2s[i].y;
274 }
275 }
276 }
277
278 RS::SurfaceData sd;
279
280 sd.primitive = RS::PrimitiveType(active_surface_data.primitive);
281 sd.format = format;
282 sd.vertex_data = surface_vertex_create_cache;
283 if (uses_colors || uses_uvs || uses_uv2s) {
284 sd.attribute_data = surface_attribute_create_cache;
285 }
286 sd.vertex_count = vertices.size();
287 sd.aabb = aabb;
288 if (active_surface_data.material.is_valid()) {
289 sd.material = active_surface_data.material->get_rid();
290 }
291
292 RS::get_singleton()->mesh_add_surface(mesh, sd);
293
294 active_surface_data.aabb = aabb;
295
296 active_surface_data.format = format;
297 active_surface_data.array_len = vertices.size();
298
299 surfaces.push_back(active_surface_data);
300
301 colors.clear();
302 normals.clear();
303 tangents.clear();
304 uvs.clear();
305 uv2s.clear();
306 vertices.clear();
307
308 uses_colors = false;
309 uses_normals = false;
310 uses_tangents = false;
311 uses_uvs = false;
312 uses_uv2s = false;
313
314 surface_active = false;
315
316 emit_changed();
317 }
318
319 void ImmediateMesh::clear_surfaces() {
320 RS::get_singleton()->mesh_clear(mesh);
321 surfaces.clear();
322 surface_active = false;
323
324 colors.clear();
325 normals.clear();
326 tangents.clear();
327 uvs.clear();
328 uv2s.clear();
329 vertices.clear();
330
331 uses_colors = false;
332 uses_normals = false;
333 uses_tangents = false;
334 uses_uvs = false;
335 uses_uv2s = false;
336 }
337
338 int ImmediateMesh::get_surface_count() const {
339 return surfaces.size();
340 }
341 int ImmediateMesh::surface_get_array_len(int p_idx) const {
342 ERR_FAIL_INDEX_V(p_idx, int(surfaces.size()), -1);
343 return surfaces[p_idx].array_len;
344 }
345 int ImmediateMesh::surface_get_array_index_len(int p_idx) const {
346 return 0;
347 }
348 Array ImmediateMesh::surface_get_arrays(int p_surface) const {
349 ERR_FAIL_INDEX_V(p_surface, int(surfaces.size()), Array());
350 return RS::get_singleton()->mesh_surface_get_arrays(mesh, p_surface);
351 }
352 TypedArray<Array> ImmediateMesh::surface_get_blend_shape_arrays(int p_surface) const {
353 return TypedArray<Array>();
354 }
355 Dictionary ImmediateMesh::surface_get_lods(int p_surface) const {
356 return Dictionary();
357 }
358 BitField<Mesh::ArrayFormat> ImmediateMesh::surface_get_format(int p_idx) const {
359 ERR_FAIL_INDEX_V(p_idx, int(surfaces.size()), 0);
360 return surfaces[p_idx].format;
361 }
362 Mesh::PrimitiveType ImmediateMesh::surface_get_primitive_type(int p_idx) const {
363 ERR_FAIL_INDEX_V(p_idx, int(surfaces.size()), PRIMITIVE_MAX);
364 return surfaces[p_idx].primitive;
365 }
366 void ImmediateMesh::surface_set_material(int p_idx, const Ref<Material> &p_material) {
367 ERR_FAIL_INDEX(p_idx, int(surfaces.size()));
368 surfaces[p_idx].material = p_material;
369 RID mat;
370 if (p_material.is_valid()) {
371 mat = p_material->get_rid();
372 }
373 RS::get_singleton()->mesh_surface_set_material(mesh, p_idx, mat);
374 }
375 Ref<Material> ImmediateMesh::surface_get_material(int p_idx) const {
376 ERR_FAIL_INDEX_V(p_idx, int(surfaces.size()), Ref<Material>());
377 return surfaces[p_idx].material;
378 }
379 int ImmediateMesh::get_blend_shape_count() const {
380 return 0;
381 }
382 StringName ImmediateMesh::get_blend_shape_name(int p_index) const {
383 return StringName();
384 }
385 void ImmediateMesh::set_blend_shape_name(int p_index, const StringName &p_name) {
386 }
387
388 AABB ImmediateMesh::get_aabb() const {
389 AABB aabb;
390 for (uint32_t i = 0; i < surfaces.size(); i++) {
391 if (i == 0) {
392 aabb = surfaces[i].aabb;
393 } else {
394 aabb = aabb.merge(surfaces[i].aabb);
395 }
396 }
397 return aabb;
398 }
399
400 void ImmediateMesh::_bind_methods() {
401 ClassDB::bind_method(D_METHOD("surface_begin", "primitive", "material"), &ImmediateMesh::surface_begin, DEFVAL(Ref<Material>()));
402 ClassDB::bind_method(D_METHOD("surface_set_color", "color"), &ImmediateMesh::surface_set_color);
403 ClassDB::bind_method(D_METHOD("surface_set_normal", "normal"), &ImmediateMesh::surface_set_normal);
404 ClassDB::bind_method(D_METHOD("surface_set_tangent", "tangent"), &ImmediateMesh::surface_set_tangent);
405 ClassDB::bind_method(D_METHOD("surface_set_uv", "uv"), &ImmediateMesh::surface_set_uv);
406 ClassDB::bind_method(D_METHOD("surface_set_uv2", "uv2"), &ImmediateMesh::surface_set_uv2);
407 ClassDB::bind_method(D_METHOD("surface_add_vertex", "vertex"), &ImmediateMesh::surface_add_vertex);
408 ClassDB::bind_method(D_METHOD("surface_add_vertex_2d", "vertex"), &ImmediateMesh::surface_add_vertex_2d);
409 ClassDB::bind_method(D_METHOD("surface_end"), &ImmediateMesh::surface_end);
410
411 ClassDB::bind_method(D_METHOD("clear_surfaces"), &ImmediateMesh::clear_surfaces);
412 }
413
414 RID ImmediateMesh::get_rid() const {
415 return mesh;
416 }
417
418 ImmediateMesh::ImmediateMesh() {
419 mesh = RS::get_singleton()->mesh_create();
420 }
421 ImmediateMesh::~ImmediateMesh() {
422 ERR_FAIL_NULL(RenderingServer::get_singleton());
423 RS::get_singleton()->free_rid(mesh);
424 }