2020-04-03 12:00:02 +02:00
|
|
|
#include "prop_instance.h"
|
|
|
|
|
2020-06-27 21:44:10 +02:00
|
|
|
#include "../mesh_data_resource/nodes/mesh_data_instance.h"
|
2020-06-22 02:32:53 +02:00
|
|
|
|
2021-02-06 11:54:57 +01:00
|
|
|
#include "core/version.h"
|
|
|
|
|
2021-04-27 17:17:31 +02:00
|
|
|
#if VERSION_MAJOR < 4
|
|
|
|
#include "scene/3d/light.h"
|
|
|
|
#else
|
|
|
|
#include "scene/3d/light_3d.h"
|
|
|
|
#define OmniLight OmniLight3D
|
|
|
|
#define Light Light3D
|
|
|
|
#endif
|
|
|
|
|
2021-04-27 13:50:51 +02:00
|
|
|
#if MESH_DATA_RESOURCE_PRESENT
|
2021-04-27 17:06:46 +02:00
|
|
|
//define PROPS_PRESENT, so things compile. That module's scsub will define this too while compiling,
|
2021-04-27 13:50:51 +02:00
|
|
|
//but not when included from here.
|
|
|
|
#define PROPS_PRESENT 1
|
|
|
|
#include "../mesh_data_resource/props/prop_data_mesh_data.h"
|
2021-04-27 17:06:46 +02:00
|
|
|
#endif
|
2021-04-27 13:50:51 +02:00
|
|
|
|
|
|
|
#include "./props/prop_data_entry.h"
|
|
|
|
#include "./props/prop_data_light.h"
|
|
|
|
#include "./props/prop_data_prop.h"
|
|
|
|
#include "./props/prop_data_scene.h"
|
2021-08-18 18:48:13 +02:00
|
|
|
#include "./props/prop_data_tiled_wall.h"
|
|
|
|
|
|
|
|
#include "tiled_wall/tiled_wall.h"
|
|
|
|
#include "tiled_wall/tiled_wall_data.h"
|
2021-04-27 13:50:51 +02:00
|
|
|
|
2020-07-05 21:19:32 +02:00
|
|
|
Ref<PropData> PropInstance::get_prop_data() {
|
|
|
|
return _prop_data;
|
|
|
|
}
|
|
|
|
void PropInstance::set_prop_data(const Ref<PropData> &data) {
|
|
|
|
if (_prop_data == data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_prop_data = data;
|
|
|
|
|
2021-08-10 17:10:05 +02:00
|
|
|
if (_building) {
|
|
|
|
queue_build();
|
|
|
|
} else {
|
|
|
|
call_deferred("build");
|
|
|
|
}
|
2020-07-05 21:19:32 +02:00
|
|
|
}
|
|
|
|
|
2021-04-27 17:32:33 +02:00
|
|
|
Ref<Material> PropInstance::get_material() {
|
|
|
|
return _material;
|
|
|
|
}
|
|
|
|
void PropInstance::set_material(const Ref<Material> &material) {
|
|
|
|
_material = material;
|
|
|
|
}
|
|
|
|
|
2021-08-25 22:29:00 +02:00
|
|
|
uint32_t PropInstance::get_collision_layer() const {
|
|
|
|
return _collision_layer;
|
|
|
|
}
|
|
|
|
|
2021-08-25 22:21:34 +02:00
|
|
|
void PropInstance::set_collision_layer(uint32_t p_layer) {
|
|
|
|
_collision_layer = p_layer;
|
|
|
|
|
|
|
|
collision_layer_changed();
|
|
|
|
}
|
|
|
|
|
2021-08-25 22:29:00 +02:00
|
|
|
uint32_t PropInstance::get_collision_mask() const {
|
|
|
|
return _collision_mask;
|
2021-08-25 22:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropInstance::set_collision_mask(uint32_t p_mask) {
|
|
|
|
_collision_mask = p_mask;
|
|
|
|
|
|
|
|
collision_mask_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropInstance::collision_layer_changed() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropInstance::collision_mask_changed() {
|
|
|
|
}
|
|
|
|
|
2021-04-26 18:04:52 +02:00
|
|
|
void PropInstance::init_materials() {
|
|
|
|
call("_init_materials");
|
|
|
|
}
|
|
|
|
void PropInstance::_init_materials() {
|
|
|
|
}
|
|
|
|
|
2020-07-05 21:19:32 +02:00
|
|
|
void PropInstance::build() {
|
2021-04-27 16:29:09 +02:00
|
|
|
call("_build");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropInstance::queue_build() {
|
2021-08-10 10:29:52 +02:00
|
|
|
_build_queued = true;
|
2021-04-27 16:29:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropInstance::build_finished() {
|
2021-08-10 17:10:05 +02:00
|
|
|
call("_build_finished");
|
2021-04-27 16:29:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropInstance::_build() {
|
2021-04-26 18:04:52 +02:00
|
|
|
_building = true;
|
|
|
|
_build_queued = false;
|
|
|
|
|
2020-07-05 21:19:32 +02:00
|
|
|
if (!is_inside_tree()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < get_child_count(); ++i) {
|
2020-07-05 23:08:24 +02:00
|
|
|
Node *n = get_child(i);
|
|
|
|
|
|
|
|
//this way we won't delete the user's nodes
|
|
|
|
if (n->get_owner() == NULL) {
|
2023-01-09 14:10:10 +01:00
|
|
|
n->queue_free();
|
2020-07-05 23:08:24 +02:00
|
|
|
}
|
2020-07-05 21:19:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_prop_data.is_valid())
|
|
|
|
return;
|
|
|
|
|
2021-04-27 13:50:51 +02:00
|
|
|
prop_preprocess(Transform(), _prop_data);
|
2021-04-26 18:04:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropInstance::_build_finished() {
|
2021-08-10 17:10:05 +02:00
|
|
|
_building = false;
|
|
|
|
|
|
|
|
if (_build_queued) {
|
|
|
|
call_deferred("build");
|
|
|
|
}
|
2020-07-05 21:19:32 +02:00
|
|
|
}
|
|
|
|
|
2021-04-27 13:50:51 +02:00
|
|
|
void PropInstance::prop_preprocess(Transform transform, const Ref<PropData> &prop) {
|
2021-04-27 16:52:36 +02:00
|
|
|
call("_prop_preprocess", transform, prop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropInstance::_prop_preprocess(Transform transform, const Ref<PropData> &prop) {
|
2021-04-27 17:17:31 +02:00
|
|
|
//don't set owners, to help working with the editor
|
|
|
|
|
2021-04-27 13:50:51 +02:00
|
|
|
ERR_FAIL_COND(!prop.is_valid());
|
|
|
|
|
|
|
|
int count = prop->get_prop_count();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
Ref<PropDataEntry> e = prop->get_prop(i);
|
|
|
|
|
|
|
|
if (!e.is_valid())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Transform t = transform * e->get_transform();
|
|
|
|
|
|
|
|
Ref<PropDataProp> prop_entry_data = e;
|
|
|
|
|
|
|
|
if (prop_entry_data.is_valid()) {
|
|
|
|
Ref<PropData> p = prop_entry_data->get_prop();
|
|
|
|
|
|
|
|
if (!p.is_valid())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
prop_preprocess(t, p);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-08-18 18:48:13 +02:00
|
|
|
Ref<PropDataTiledWall> tiled_wall_data = e;
|
|
|
|
|
|
|
|
if (tiled_wall_data.is_valid()) {
|
|
|
|
TiledWall *twn = memnew(TiledWall);
|
|
|
|
|
|
|
|
twn->set_width(tiled_wall_data->get_width());
|
|
|
|
twn->set_heigth(tiled_wall_data->get_heigth());
|
|
|
|
twn->set_data(tiled_wall_data->get_data());
|
|
|
|
twn->set_collision(tiled_wall_data->get_collision());
|
|
|
|
|
|
|
|
twn->set_transform(t);
|
|
|
|
|
|
|
|
add_child(twn);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-04-27 13:50:51 +02:00
|
|
|
Ref<PropDataScene> scene_data = e;
|
|
|
|
|
|
|
|
if (scene_data.is_valid()) {
|
|
|
|
Ref<PackedScene> sc = scene_data->get_scene();
|
|
|
|
|
|
|
|
if (!sc.is_valid())
|
|
|
|
continue;
|
|
|
|
|
2023-01-09 14:10:10 +01:00
|
|
|
Node *n = sc->instantiate();
|
2021-04-27 13:50:51 +02:00
|
|
|
add_child(n);
|
|
|
|
|
|
|
|
Spatial *sp = Object::cast_to<Spatial>(n);
|
|
|
|
|
|
|
|
if (sp) {
|
|
|
|
sp->set_transform(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-04-27 17:17:31 +02:00
|
|
|
Ref<PropDataLight> light_data = e;
|
2021-04-27 13:50:51 +02:00
|
|
|
|
|
|
|
if (light_data.is_valid()) {
|
2021-04-27 17:17:31 +02:00
|
|
|
OmniLight *light = memnew(OmniLight);
|
|
|
|
add_child(light);
|
2021-04-27 13:50:51 +02:00
|
|
|
light->set_color(light_data->get_light_color());
|
2021-04-27 17:17:31 +02:00
|
|
|
light->set_param(Light::PARAM_RANGE, light_data->get_light_size());
|
|
|
|
light->set_transform(t);
|
2021-04-27 13:50:51 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MESH_DATA_RESOURCE_PRESENT
|
|
|
|
Ref<PropDataMeshData> mesh_data = e;
|
|
|
|
|
|
|
|
if (mesh_data.is_valid()) {
|
|
|
|
Ref<MeshDataResource> mdr = mesh_data->get_mesh();
|
|
|
|
|
|
|
|
if (!mdr.is_valid())
|
|
|
|
continue;
|
|
|
|
|
2021-04-27 17:06:46 +02:00
|
|
|
MeshDataInstance *mdi = memnew(MeshDataInstance);
|
|
|
|
add_child(mdi);
|
|
|
|
mdi->set_transform(t);
|
2021-04-27 17:32:33 +02:00
|
|
|
|
|
|
|
if (_material.is_valid()) {
|
|
|
|
//duplicate the material, so that textures will work
|
|
|
|
Ref<Material> mat = _material->duplicate();
|
|
|
|
|
|
|
|
Ref<Texture> texture = mdi->get_texture();
|
|
|
|
|
|
|
|
if (texture.is_valid()) {
|
|
|
|
//texture is valid, try to set it into the material
|
|
|
|
Ref<SpatialMaterial> spmat = mat;
|
|
|
|
|
|
|
|
if (spmat.is_valid()) {
|
|
|
|
spmat->set_texture(SpatialMaterial::TEXTURE_ALBEDO, texture);
|
|
|
|
} else {
|
|
|
|
Ref<ShaderMaterial> shmat = mat;
|
|
|
|
|
|
|
|
if (shmat.is_valid()) {
|
2023-01-09 14:10:10 +01:00
|
|
|
shmat->set_shader_parameter("texture_albedo", texture);
|
2021-04-27 17:32:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mdi->set_material(mat);
|
|
|
|
}
|
|
|
|
|
2021-04-27 17:06:46 +02:00
|
|
|
mdi->set_mesh_data(mdr);
|
2021-04-27 13:50:51 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 12:00:02 +02:00
|
|
|
PropInstance::PropInstance() {
|
2021-04-26 18:04:52 +02:00
|
|
|
_build_queued = false;
|
|
|
|
_building = false;
|
2021-08-25 22:21:34 +02:00
|
|
|
|
|
|
|
_collision_layer = 1;
|
|
|
|
_collision_mask = 1;
|
2020-04-03 12:00:02 +02:00
|
|
|
}
|
2021-04-26 18:04:52 +02:00
|
|
|
|
2020-04-03 12:00:02 +02:00
|
|
|
PropInstance::~PropInstance() {
|
2020-07-05 21:19:32 +02:00
|
|
|
_prop_data.unref();
|
2020-04-03 12:00:02 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 23:08:24 +02:00
|
|
|
void PropInstance::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2021-04-26 16:29:53 +02:00
|
|
|
if (_prop_data.is_valid()) {
|
|
|
|
build();
|
|
|
|
}
|
2020-07-05 23:08:24 +02:00
|
|
|
}
|
2021-04-26 18:04:52 +02:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
}
|
2020-07-05 23:08:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 12:00:02 +02:00
|
|
|
void PropInstance::_bind_methods() {
|
2020-07-05 23:08:24 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_prop_data"), &PropInstance::get_prop_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_prop_data", "value"), &PropInstance::set_prop_data);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "prop_data", PROPERTY_HINT_RESOURCE_TYPE, "PropData"), "set_prop_data", "get_prop_data");
|
|
|
|
|
2021-04-27 17:32:33 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_material"), &PropInstance::get_material);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_material", "material"), &PropInstance::set_material);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_material", "get_material");
|
|
|
|
|
2021-08-25 22:21:34 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_collision_layer"), &PropInstance::get_collision_layer);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &PropInstance::set_collision_layer);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_collision_mask"), &PropInstance::get_collision_mask);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_collision_mask", "layer"), &PropInstance::set_collision_mask);
|
|
|
|
|
|
|
|
ADD_GROUP("Collision", "collision_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
|
|
|
|
|
2023-01-09 14:10:10 +01:00
|
|
|
//BIND_VMETHOD(MethodInfo("_prop_preprocess",
|
|
|
|
// PropertyInfo(Variant::TRANSFORM, "tarnsform"),
|
|
|
|
// PropertyInfo(Variant::OBJECT, "prop_data", PROPERTY_HINT_RESOURCE_TYPE, "PropData")));
|
2021-04-27 16:52:36 +02:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("prop_preprocess", "tarnsform", "prop"), &PropInstance::prop_preprocess);
|
|
|
|
ClassDB::bind_method(D_METHOD("_prop_preprocess", "tarnsform", "prop"), &PropInstance::_prop_preprocess);
|
|
|
|
|
2021-04-26 18:04:52 +02:00
|
|
|
//---
|
2023-01-09 14:10:10 +01:00
|
|
|
//BIND_VMETHOD(MethodInfo("_init_materials"));
|
2021-04-26 18:04:52 +02:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("init_materials"), &PropInstance::init_materials);
|
|
|
|
ClassDB::bind_method(D_METHOD("_init_materials"), &PropInstance::_init_materials);
|
2020-06-22 02:32:53 +02:00
|
|
|
|
2021-04-26 18:04:52 +02:00
|
|
|
//---
|
|
|
|
ClassDB::bind_method(D_METHOD("build"), &PropInstance::build);
|
|
|
|
ClassDB::bind_method(D_METHOD("queue_build"), &PropInstance::queue_build);
|
|
|
|
ClassDB::bind_method(D_METHOD("build_finished"), &PropInstance::build_finished);
|
2020-04-03 12:00:02 +02:00
|
|
|
|
2023-01-09 14:10:10 +01:00
|
|
|
//BIND_VMETHOD(MethodInfo("_build"));
|
|
|
|
//BIND_VMETHOD(MethodInfo("_build_finished"));
|
2020-06-22 02:32:53 +02:00
|
|
|
|
2021-04-27 16:29:09 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_build"), &PropInstance::_build);
|
2021-04-26 18:04:52 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_build_finished"), &PropInstance::_build_finished);
|
2020-04-03 12:00:02 +02:00
|
|
|
}
|