mesh_data_resource/nodes/mesh_data_instance.cpp

191 lines
4.6 KiB
C++
Raw Normal View History

2020-06-27 21:44:45 +02:00
#include "mesh_data_instance.h"
2020-07-29 00:02:05 +02:00
#include "core/version.h"
2021-02-06 11:51:02 +01:00
#include "core/version.h"
#include "scene/resources/texture.h"
#if VERSION_MAJOR < 4
#include "core/image.h"
#else
#include "core/io/image.h"
#endif
2020-07-05 12:18:00 +02:00
#if TEXTURE_PACKER_PRESENT
#include "../../texture_packer/texture_resource/packer_image_resource.h"
#endif
2020-06-27 21:44:45 +02:00
bool MeshDataInstance::get_snap_to_mesh() const {
return _snap_to_mesh;
}
void MeshDataInstance::set_snap_to_mesh(const bool value) {
_snap_to_mesh = value;
}
Vector3 MeshDataInstance::get_snap_axis() const {
return _snap_axis;
}
void MeshDataInstance::set_snap_axis(const Vector3 &value) {
_snap_axis = value;
}
2020-07-05 12:18:00 +02:00
Ref<MeshDataResource> MeshDataInstance::get_mesh_data() {
2020-06-27 21:44:45 +02:00
return _mesh;
}
2020-07-05 12:18:00 +02:00
void MeshDataInstance::set_mesh_data(const Ref<MeshDataResource> &mesh) {
2020-06-27 21:44:45 +02:00
_mesh = mesh;
2020-07-05 12:18:00 +02:00
if (is_inside_tree()) {
refresh();
} else {
_dirty = true;
2020-07-05 12:18:00 +02:00
}
2020-06-27 21:44:45 +02:00
}
Ref<Texture> MeshDataInstance::get_texture() {
return _texture;
}
void MeshDataInstance::set_texture(const Ref<Texture> &texture) {
_texture = texture;
2020-07-05 12:18:00 +02:00
setup_material_texture();
refresh();
2020-07-05 12:18:00 +02:00
}
Ref<Material> MeshDataInstance::get_material() {
return _material;
}
void MeshDataInstance::set_material(const Ref<Material> &mat) {
_material = mat;
setup_material_texture();
refresh();
2020-07-05 12:18:00 +02:00
}
void MeshDataInstance::refresh() {
2020-07-05 12:18:00 +02:00
Ref<ArrayMesh> mesh = get_mesh();
if (!mesh.is_valid()) {
mesh.instance();
}
2020-07-29 00:02:05 +02:00
#if VERSION_MAJOR < 4
2020-07-05 12:18:00 +02:00
for (int i = 0; i < mesh->get_surface_count(); ++i) {
mesh->surface_remove(i);
}
2020-07-29 00:02:05 +02:00
#else
mesh->clear_surfaces();
#endif
2020-07-05 12:18:00 +02:00
//Always check/set mesh in MeshInstance in case it got set to something else. For example got cleared in the editor.
if (get_mesh() != mesh) {
set_mesh(mesh);
2020-07-05 12:18:00 +02:00
}
if (!_mesh.is_valid()) {
2020-07-05 12:18:00 +02:00
return;
}
Array arr = _mesh->get_array();
if (arr.size() != Mesh::ARRAY_MAX) {
return;
}
PoolVector<Vector3> vertices = arr[Mesh::ARRAY_VERTEX];
if (vertices.size() == 0) {
return;
}
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
if (_material.is_valid() && mesh->get_surface_count() > 0) {
mesh->surface_set_material(0, _material);
}
2020-07-05 12:18:00 +02:00
}
void MeshDataInstance::setup_material_texture() {
if (!_texture.is_valid()) {
return;
}
if (_material.is_valid()) {
Ref<SpatialMaterial> sm = _material;
if (!sm.is_valid()) {
return;
}
#if TEXTURE_PACKER_PRESENT
Ref<PackerImageResource> r = _texture;
if (r.is_valid()) {
Ref<Image> i = r->get_data();
Ref<ImageTexture> tex;
tex.instance();
2020-07-29 00:02:05 +02:00
#if VERSION_MAJOR < 4
2020-07-05 12:18:00 +02:00
tex->create_from_image(i, 0);
2020-07-29 00:02:05 +02:00
#else
tex->create_from_image(i);
#endif
2020-07-05 12:18:00 +02:00
if (sm.is_valid()) {
sm->set_texture(SpatialMaterial::TEXTURE_ALBEDO, tex);
}
return;
}
#endif
sm->set_texture(SpatialMaterial::TEXTURE_ALBEDO, _texture);
}
2020-06-27 21:44:45 +02:00
}
MeshDataInstance::MeshDataInstance() {
_dirty = false;
2020-06-27 21:44:45 +02:00
_snap_to_mesh = false;
_snap_axis = Vector3(0, -1, 0);
}
MeshDataInstance::~MeshDataInstance() {
_mesh.unref();
_texture.unref();
}
void MeshDataInstance::_notification(int p_what) {
2020-06-27 21:44:45 +02:00
switch (p_what) {
2020-07-05 12:18:00 +02:00
case NOTIFICATION_ENTER_TREE: {
if (_dirty) {
_dirty = false;
refresh();
}
2020-06-27 21:44:45 +02:00
}
2020-07-05 12:18:00 +02:00
}
2020-06-27 21:44:45 +02:00
}
2020-06-27 21:44:45 +02:00
void MeshDataInstance::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_snap_to_mesh"), &MeshDataInstance::get_snap_to_mesh);
ClassDB::bind_method(D_METHOD("set_snap_to_mesh", "value"), &MeshDataInstance::set_snap_to_mesh);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snap_to_mesh"), "set_snap_to_mesh", "get_snap_to_mesh");
ClassDB::bind_method(D_METHOD("get_snap_axis"), &MeshDataInstance::get_snap_axis);
ClassDB::bind_method(D_METHOD("set_snap_axis", "value"), &MeshDataInstance::set_snap_axis);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "snap_axis"), "set_snap_axis", "get_snap_axis");
2020-07-05 12:18:00 +02:00
ClassDB::bind_method(D_METHOD("get_mesh_data"), &MeshDataInstance::get_mesh_data);
ClassDB::bind_method(D_METHOD("set_mesh_data", "value"), &MeshDataInstance::set_mesh_data);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_data", PROPERTY_HINT_RESOURCE_TYPE, "MeshDataResource"), "set_mesh_data", "get_mesh_data");
2020-06-27 21:44:45 +02:00
ClassDB::bind_method(D_METHOD("get_texture"), &MeshDataInstance::get_texture);
ClassDB::bind_method(D_METHOD("set_texture", "value"), &MeshDataInstance::set_texture);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
2020-07-05 12:18:00 +02:00
ClassDB::bind_method(D_METHOD("get_material"), &MeshDataInstance::get_material);
ClassDB::bind_method(D_METHOD("set_material", "value"), &MeshDataInstance::set_material);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_material", "get_material");
ClassDB::bind_method(D_METHOD("refresh"), &MeshDataInstance::refresh);
2020-06-27 21:44:45 +02:00
}