Finished up MeshDataInstance.

This commit is contained in:
Relintai 2020-07-05 12:18:00 +02:00
parent d5c95a6c5d
commit eb3c654998
3 changed files with 123 additions and 43 deletions

6
SCsub
View File

@ -1,7 +1,13 @@
import os
Import('env')
module_env = env.Clone()
if os.path.isdir('../texture_packer'):
module_env.Append(CPPDEFINES=['TEXTURE_PACKER_PRESENT'])
module_env.add_source_files(env.modules_sources,"register_types.cpp")
module_env.add_source_files(env.modules_sources,"mesh_data_resource.cpp")

View File

@ -1,5 +1,11 @@
#include "mesh_data_instance.h"
#if TEXTURE_PACKER_PRESENT
#include "../../texture_packer/texture_resource/packer_image_resource.h"
#endif
#include "core/image.h"
bool MeshDataInstance::get_snap_to_mesh() const {
return _snap_to_mesh;
}
@ -14,11 +20,19 @@ void MeshDataInstance::set_snap_axis(const Vector3 &value) {
_snap_axis = value;
}
Ref<MeshDataResource> MeshDataInstance::get_mesh() {
Ref<MeshDataResource> MeshDataInstance::get_mesh_data() {
return _mesh;
}
void MeshDataInstance::set_mesh(const Ref<MeshDataResource> &mesh) {
void MeshDataInstance::set_mesh_data(const Ref<MeshDataResource> &mesh) {
if (_mesh == mesh) {
return;
}
_mesh = mesh;
if (_mesh.is_valid() && is_inside_tree()) {
setup_mesh();
}
}
Ref<Texture> MeshDataInstance::get_texture() {
@ -26,6 +40,81 @@ Ref<Texture> MeshDataInstance::get_texture() {
}
void MeshDataInstance::set_texture(const Ref<Texture> &texture) {
_texture = texture;
setup_material_texture();
}
Ref<Material> MeshDataInstance::get_material() {
return _material;
}
void MeshDataInstance::set_material(const Ref<Material> &mat) {
_material = mat;
setup_material_texture();
}
void MeshDataInstance::setup_mesh() {
if (!_mesh.is_valid()) {
return;
}
Array arr = _mesh->get_array();
Ref<ArrayMesh> mesh = get_mesh();
if (!mesh.is_valid()) {
mesh.instance();
}
for (int i = 0; i < mesh->get_surface_count(); ++i) {
mesh->surface_remove(i);
}
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
if (_material.is_valid() && mesh->get_surface_count() > 0) {
mesh->surface_set_material(0, _material);
}
if (get_mesh() == mesh) {
return;
}
set_mesh(mesh);
}
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();
tex->create_from_image(i, 0);
if (sm.is_valid()) {
sm->set_texture(SpatialMaterial::TEXTURE_ALBEDO, tex);
}
return;
}
#endif
sm->set_texture(SpatialMaterial::TEXTURE_ALBEDO, _texture);
}
}
MeshDataInstance::MeshDataInstance() {
@ -36,42 +125,16 @@ MeshDataInstance::~MeshDataInstance() {
_mesh.unref();
_texture.unref();
}
/*
void MeshDataInstance::notification(int p_what) {
/*
switch (p_what) {
case NOTIFICATION_READY: {
case NOTIFICATION_ENTER_TREE: {
if (get_parent() == NULL)
return;
}
//fallthrough
case NOTIFICATION_PARENTED: {
Node *n = this;
while (n) {
PropInstance *pi = Object::cast_to<PropInstance>(n);
if (pi) {
_owner = pi;
pi->register_prop_mesh_data_instance(this);
return;
}
n = n->get_parent();
}
} break;
case NOTIFICATION_EXIT_TREE:
case NOTIFICATION_UNPARENTED: {
if (_owner) {
_owner->unregister_prop_mesh_data_instance(this);
}
break;
}
}*/
}
}
*/
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);
@ -81,11 +144,15 @@ void MeshDataInstance::_bind_methods() {
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");
ClassDB::bind_method(D_METHOD("get_mesh"), &MeshDataInstance::get_mesh);
ClassDB::bind_method(D_METHOD("set_mesh", "value"), &MeshDataInstance::set_mesh);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "MeshDataResource"), "set_mesh", "get_mesh");
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");
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");
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");
}

View File

@ -27,11 +27,11 @@ SOFTWARE.
#include "scene/resources/texture.h"
#if VERSION_MAJOR < 4
#include "scene/3d/spatial.h"
#include "scene/3d/mesh_instance.h"
#else
#include "scene/3d/node_3d.h"
#include "scene/3d/mesh_instance_3d.h"
#define Spatial Node3D
#define MeshInstance MeshInstance3D
#define Texture Texture2D
#endif
@ -41,8 +41,8 @@ SOFTWARE.
class PropInstance;
class MeshDataInstance : public Spatial {
GDCLASS(MeshDataInstance, Spatial);
class MeshDataInstance : public MeshInstance {
GDCLASS(MeshDataInstance, MeshInstance);
public:
bool get_snap_to_mesh() const;
@ -51,17 +51,23 @@ public:
Vector3 get_snap_axis() const;
void set_snap_axis(const Vector3 &value);
Ref<MeshDataResource> get_mesh();
void set_mesh(const Ref<MeshDataResource> &mesh);
Ref<MeshDataResource> get_mesh_data();
void set_mesh_data(const Ref<MeshDataResource> &mesh);
Ref<Texture> get_texture();
void set_texture(const Ref<Texture> &texture);
Ref<Material> get_material();
void set_material(const Ref<Material> &mat);
void setup_mesh();
void setup_material_texture();
MeshDataInstance();
~MeshDataInstance();
protected:
void notification(int p_what);
//void notification(int p_what);
static void _bind_methods();
private:
@ -69,6 +75,7 @@ private:
Vector3 _snap_axis;
Ref<MeshDataResource> _mesh;
Ref<Texture> _texture;
Ref<Material> _material;
};
#endif