-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmerge.h
239 lines (208 loc) · 9.44 KB
/
merge.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*************************************************************************/
/* merge.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
/*
xatlas
https://github.com/jpcy/xatlas
Copyright (c) 2018 Jonathan Young
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
thekla_atlas
https://github.com/Thekla/thekla_atlas
MIT License
Copyright (c) 2013 Thekla, Inc
Copyright NVIDIA Corporation 2006 -- Ignacio Castano <[email protected]>
*/
#ifndef MESH_MERGE_MATERIAL_REPACK_H
#define MESH_MERGE_MATERIAL_REPACK_H
#include "core/object/ref_counted.h"
#ifdef TOOLS_ENABLED
#include "editor/editor_node.h"
#include "editor/editor_plugin.h"
#endif
#include "modules/csg/csg_shape.h"
#include "modules/gridmap/grid_map.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/gui/check_box.h"
#include "scene/main/node.h"
class SceneMerge : public RefCounted {
private:
GDCLASS(SceneMerge, RefCounted);
void _dialog_action(String p_file);
public:
void merge(const String p_file, Node *p_root_node);
};
#ifdef TOOLS_ENABLED
#include "editor/editor_file_dialog.h"
class SceneMergePlugin : public EditorPlugin {
GDCLASS(SceneMergePlugin, EditorPlugin);
CheckBox *file_export_lib_merge = memnew(CheckBox);
EditorFileDialog *file_export_lib = memnew(EditorFileDialog);
Ref<SceneMerge> scene_optimize;
void _dialog_action(String p_file);
void merge();
protected:
static void _bind_methods();
public:
SceneMergePlugin();
~SceneMergePlugin() {
EditorNode::get_singleton()->remove_tool_menu_item("Merge Scene");
}
void _notification(int notification);
};
#endif
#endif
#include "core/math/vector2.h"
#include "core/object/ref_counted.h"
#include "scene/3d/mesh_instance_3d.h"
#include "thirdparty/xatlas/xatlas.h"
class MeshMergeMaterialRepack : public RefCounted {
private:
struct TextureData {
uint16_t width;
uint16_t height;
int numComponents;
Ref<Image> image;
};
/// A callback to sample the environment. Return false to terminate rasterization.
typedef bool (*SamplingCallback)(void *param, int x, int y, const Vector3 &bar, const Vector3 &dx, const Vector3 &dy, float coverage);
struct Triangle {
Triangle(const Vector2 &v0, const Vector2 &v1, const Vector2 &v2, const Vector3 &t0, const Vector3 &t1, const Vector3 &t2);
/// Compute texture space deltas.
/// This method takes two edge vectors that form a basis, determines the
/// coordinates of the canonic vectors in that basis, and computes the
/// texture gradient that corresponds to those vectors.
bool computeDeltas();
void flipBackface();
// compute unit inward normals for each edge.
void computeUnitInwardNormals();
bool drawAA(SamplingCallback cb, void *param);
Vector2 v1, v2, v3;
Vector2 n1, n2, n3; // unit inward normals
Vector3 t1, t2, t3;
Vector3 dx, dy;
};
class ClippedTriangle {
public:
ClippedTriangle(const Vector2 &a, const Vector2 &b, const Vector2 &c);
void clipHorizontalPlane(float offset, float clipdirection);
void clipVerticalPlane(float offset, float clipdirection);
void computeAreaCentroid();
void clipAABox(float x0, float y0, float x1, float y1);
Vector2 centroid();
float area();
private:
Vector2 m_verticesA[7 + 1];
Vector2 m_verticesB[7 + 1];
Vector2 *m_vertexBuffers[2];
uint32_t m_numVertices;
uint32_t m_activeVertexBuffer;
float m_area;
Vector2 m_centroid;
};
struct AtlasLookupTexel {
uint16_t material_index;
uint16_t x, y;
};
struct SetAtlasTexelArgs {
Ref<Image> atlasData;
Ref<Image> sourceTexture;
AtlasLookupTexel *atlas_lookup = nullptr;
uint16_t material_index = 0;
Vector2 source_uvs[3];
uint32_t atlas_width = 0;
};
const int32_t default_texture_length = 512;
struct ModelVertex {
Vector3 pos;
Vector3 normal;
Vector2 uv;
};
struct MeshState {
Ref<Mesh> mesh;
NodePath path;
MeshInstance3D *mesh_instance;
bool operator==(const MeshState &rhs) const;
};
struct MaterialImageCache {
Ref<Image> albedo_img;
Ref<Image> normal_img;
Ref<Image> orm_img;
Ref<Image> emission_img;
};
struct MergeState {
Node *p_root;
xatlas::Atlas *atlas;
Vector<MeshState> &r_mesh_items;
Array &vertex_to_material;
const Vector<Vector<Vector2> > uvs;
const Vector<Vector<ModelVertex> > &model_vertices;
String p_name;
String output_path;
const xatlas::PackOptions &pack_options;
Vector<AtlasLookupTexel> &atlas_lookup;
Vector<Ref<Material> > &material_cache;
HashMap<String, Ref<Image> > texture_atlas;
HashMap<int32_t, MaterialImageCache> material_image_cache;
};
struct MeshMerge {
Vector<MeshState> meshes;
int vertex_count = 0;
};
static bool setAtlasTexel(void *param, int x, int y, const Vector3 &bar, const Vector3 &, const Vector3 &, float);
Ref<Image> dilate(Ref<Image> source_image);
void _find_all_animated_meshes(Vector<MeshMerge> &r_items, Node *p_current_node, const Node *p_owner);
void _find_all_mesh_instances(Vector<MeshMerge> &r_items, Node *p_current_node, const Node *p_owner);
void _generate_texture_atlas(MergeState &state, String texture_type);
Ref<Image> _get_source_texture(MergeState &state, Ref<BaseMaterial3D> material, String texture_type);
void _generate_atlas(const int32_t p_num_meshes, Vector<Vector<Vector2> > &r_uvs, xatlas::Atlas *atlas, const Vector<MeshState> &r_meshes, const Vector<Ref<Material> > material_cache,
xatlas::PackOptions &pack_options);
void scale_uvs_by_texture_dimension(const Vector<MeshState> &original_mesh_items, Vector<MeshState> &mesh_items, Vector<Vector<Vector2> > &uv_groups, Array &r_vertex_to_material, Vector<Vector<ModelVertex> > &r_model_vertices);
void map_mesh_to_index_to_material(const Vector<MeshState> mesh_items, Array &vertex_to_material, Vector<Ref<Material> > &material_cache);
Node *_output(MergeState &state, int p_count);
struct MeshMergeState {
Vector<MeshMerge> mesh_items;
Vector<MeshMerge> original_mesh_items;
Node *root = nullptr;
Node *original_root = nullptr;
String output_path;
};
Node *_merge_list(MeshMergeState p_mesh_merge_state, int p_index);
void _mark_nodes(Node *p_current, Node *p_owner, Vector<Node *> &r_nodes);
void _remove_empty_Node3Ds(Node *scene);
void _clean_animation_player(Node *scene);
void _remove_nodes(Node *scene, Vector<Node *> &r_nodes);
protected:
static void _bind_methods();
public:
Node *merge(Node *p_root, Node *p_original_root, String p_output_path);
};