Skip to content
0.2.0-dev Download ZIP
243 lines ·8.8 KB Raw
1/**************************************************************************/
2/* register_types.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 "register_types.h"
32
33#include "core/crypto/crypto_core.h"
34#include "core/error/error_macros.h"
35#include "core/os/memory.h"
36
37#include <xatlas.h>
38
39#include <cstring>
40
41extern bool (*array_mesh_lightmap_unwrap_callback)(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, int p_index_count, const uint8_t *p_cache_data, bool *r_use_cache, uint8_t **r_mesh_cache, int *r_mesh_cache_size, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y);
42
43bool xatlas_mesh_lightmap_unwrap_callback(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, int p_index_count, const uint8_t *p_cache_data, bool *r_use_cache, uint8_t **r_mesh_cache, int *r_mesh_cache_size, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y) {
44 CryptoCore::MD5Context ctx;
45 ctx.start();
46
47 ctx.update((unsigned char *)&p_texel_size, sizeof(float));
48 ctx.update((unsigned char *)p_indices, sizeof(int) * p_index_count);
49 ctx.update((unsigned char *)p_vertices, sizeof(float) * p_vertex_count * 3);
50 ctx.update((unsigned char *)p_normals, sizeof(float) * p_vertex_count * 3);
51
52 unsigned char hash[16];
53 ctx.finish(hash);
54
55 bool cached = false;
56 unsigned int cache_idx = 0;
57
58 *r_mesh_cache = nullptr;
59 *r_mesh_cache_size = 0;
60
61 if (p_cache_data) {
62 //Check if hash is in cache data
63 int *cache_data = (int *)p_cache_data;
64 int n_entries = cache_data[0];
65 unsigned int read_idx = 1;
66 for (int i = 0; i < n_entries; ++i) {
67 if (memcmp(&cache_data[read_idx], hash, 16) == 0) {
68 cached = true;
69 cache_idx = read_idx;
70 break;
71 }
72
73 read_idx += 4; // hash
74 read_idx += 2; // size hint
75
76 int vertex_count = cache_data[read_idx];
77 read_idx += 1; // vertex count
78 read_idx += vertex_count; // vertex
79 read_idx += vertex_count * 2; // uvs
80
81 int index_count = cache_data[read_idx];
82 read_idx += 1; // index count
83 read_idx += index_count; // indices
84 }
85 }
86
87 if (cached) {
88 int *cache_data = (int *)p_cache_data;
89
90 cache_idx += 4;
91
92 // Load size
93 *r_size_hint_x = cache_data[cache_idx];
94 *r_size_hint_y = cache_data[cache_idx + 1];
95 cache_idx += 2;
96
97 // Load vertices
98 *r_vertex_count = cache_data[cache_idx];
99 cache_idx++;
100 *r_vertex = &cache_data[cache_idx];
101 cache_idx += *r_vertex_count;
102
103 // Load UVs
104 *r_uv = (float *)&cache_data[cache_idx];
105 cache_idx += *r_vertex_count * 2;
106
107 // Load indices
108 *r_index_count = cache_data[cache_idx];
109 cache_idx++;
110 *r_index = &cache_data[cache_idx];
111 } else {
112 // set up input mesh
113 xatlas::MeshDecl input_mesh;
114 input_mesh.indexData = p_indices;
115 input_mesh.indexCount = p_index_count;
116 input_mesh.indexFormat = xatlas::IndexFormat::UInt32;
117
118 input_mesh.vertexCount = p_vertex_count;
119 input_mesh.vertexPositionData = p_vertices;
120 input_mesh.vertexPositionStride = sizeof(float) * 3;
121 input_mesh.vertexNormalData = p_normals;
122 input_mesh.vertexNormalStride = sizeof(uint32_t) * 3;
123 input_mesh.vertexUvData = nullptr;
124 input_mesh.vertexUvStride = 0;
125
126 xatlas::ChartOptions chart_options;
127 chart_options.fixWinding = true;
128
129 ERR_FAIL_COND_V_MSG(p_texel_size <= 0.0f, false, "Texel size must be greater than 0.");
130
131 xatlas::PackOptions pack_options;
132 pack_options.padding = 1;
133 pack_options.maxChartSize = 4094; // Lightmap atlassing needs 2 for padding between meshes, so 4096-2
134 pack_options.blockAlign = true;
135 pack_options.texelsPerUnit = 1.0 / p_texel_size;
136
137 xatlas::Atlas *atlas = xatlas::Create();
138
139 xatlas::AddMeshError err = xatlas::AddMesh(atlas, input_mesh, 1);
140 ERR_FAIL_COND_V_MSG(err != xatlas::AddMeshError::Success, false, xatlas::StringForEnum(err));
141
142 xatlas::Generate(atlas, chart_options, pack_options);
143
144 *r_size_hint_x = atlas->width;
145 *r_size_hint_y = atlas->height;
146
147 float w = *r_size_hint_x;
148 float h = *r_size_hint_y;
149
150 if (w == 0 || h == 0) {
151 xatlas::Destroy(atlas);
152 return false; //could not bake because there is no area
153 }
154
155 const xatlas::Mesh &output = atlas->meshes[0];
156
157 *r_vertex = (int *)memalloc(sizeof(int) * output.vertexCount);
158 ERR_FAIL_NULL_V_MSG(*r_vertex, false, "Out of memory.");
159 *r_uv = (float *)memalloc(sizeof(float) * output.vertexCount * 2);
160 ERR_FAIL_NULL_V_MSG(*r_uv, false, "Out of memory.");
161 *r_index = (int *)memalloc(sizeof(int) * output.indexCount);
162 ERR_FAIL_NULL_V_MSG(*r_index, false, "Out of memory.");
163
164 float max_x = 0;
165 float max_y = 0;
166 for (uint32_t i = 0; i < output.vertexCount; i++) {
167 (*r_vertex)[i] = output.vertexArray[i].xref;
168 (*r_uv)[i * 2 + 0] = output.vertexArray[i].uv[0] / w;
169 (*r_uv)[i * 2 + 1] = output.vertexArray[i].uv[1] / h;
170 max_x = MAX(max_x, output.vertexArray[i].uv[0]);
171 max_y = MAX(max_y, output.vertexArray[i].uv[1]);
172 }
173
174 *r_vertex_count = output.vertexCount;
175
176 for (uint32_t i = 0; i < output.indexCount; i++) {
177 (*r_index)[i] = output.indexArray[i];
178 }
179
180 *r_index_count = output.indexCount;
181
182 xatlas::Destroy(atlas);
183 }
184
185 if (*r_use_cache) {
186 // Build cache data for current mesh
187
188 unsigned int new_cache_size = 4 + 2 + 1 + *r_vertex_count + (*r_vertex_count * 2) + 1 + *r_index_count; // hash + size hint + vertex_count + vertices + uvs + index_count + indices
189 new_cache_size *= sizeof(int);
190 int *new_cache_data = (int *)memalloc(new_cache_size);
191 unsigned int new_cache_idx = 0;
192
193 // hash
194 memcpy(&new_cache_data[new_cache_idx], hash, 16);
195 new_cache_idx += 4;
196
197 // size hint
198 new_cache_data[new_cache_idx] = *r_size_hint_x;
199 new_cache_data[new_cache_idx + 1] = *r_size_hint_y;
200 new_cache_idx += 2;
201
202 // vertex count
203 new_cache_data[new_cache_idx] = *r_vertex_count;
204 new_cache_idx++;
205
206 // vertices
207 memcpy(&new_cache_data[new_cache_idx], *r_vertex, sizeof(int) * (*r_vertex_count));
208 new_cache_idx += *r_vertex_count;
209
210 // uvs
211 memcpy(&new_cache_data[new_cache_idx], *r_uv, sizeof(float) * (*r_vertex_count) * 2);
212 new_cache_idx += *r_vertex_count * 2;
213
214 // index count
215 new_cache_data[new_cache_idx] = *r_index_count;
216 new_cache_idx++;
217
218 // indices
219 memcpy(&new_cache_data[new_cache_idx], *r_index, sizeof(int) * (*r_index_count));
220
221 // Return cache data to the caller
222 *r_mesh_cache = (uint8_t *)new_cache_data;
223 *r_mesh_cache_size = new_cache_size;
224 }
225
226 *r_use_cache = cached; // Return whether cache was used.
227
228 return true;
229}
230
231void initialize_xatlas_unwrap_module(ModuleInitializationLevel p_level) {
232 if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
233 return;
234 }
235
236 array_mesh_lightmap_unwrap_callback = xatlas_mesh_lightmap_unwrap_callback;
237}
238
239void uninitialize_xatlas_unwrap_module(ModuleInitializationLevel p_level) {
240 if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
241 return;
242 }
243}