props/prop_instance.cpp

367 lines
9.9 KiB
C++
Raw Normal View History

#include "prop_instance.h"
#include "../mesh_data_resource/nodes/mesh_data_instance.h"
//#include "../thread_pool/thread_pool.h"
2021-02-06 11:54:57 +01:00
#include "core/version.h"
#if VERSION_MAJOR > 3
#include "core/config/engine.h"
2021-04-26 18:04:52 +02:00
#define VARIANT_ARRAY_GET(arr) \
Vector<Variant> r; \
for (int i = 0; i < arr.size(); i++) { \
r.push_back(arr[i]); \
} \
return r;
#include "servers/rendering_server.h"
typedef class RenderingServer VS;
2021-02-06 11:54:57 +01:00
#else
#include "core/engine.h"
2021-04-26 18:04:52 +02:00
#define VARIANT_ARRAY_GET(arr) \
Vector<Variant> r; \
for (int i = 0; i < arr.size(); i++) { \
r.push_back(arr[i].get_ref_ptr()); \
} \
return r;
#include "servers/visual_server.h"
2021-02-06 11:54:57 +01:00
#endif
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-04-26 18:04:52 +02:00
queue_build();
}
2021-04-26 18:04:52 +02:00
Ref<PropInstanceJob> PropInstance::get_job() {
return _job;
}
2021-04-26 18:04:52 +02:00
void PropInstance::set_job(const Ref<PropInstanceJob> &job) {
_job = job;
}
2021-04-26 18:04:52 +02:00
#ifdef TEXTURE_PACKER_PRESENT
bool PropInstance::get_merge_textures() const {
return _merge_textures;
}
2021-04-26 18:04:52 +02:00
void PropInstance::set_merge_textures(const bool value) {
_merge_textures = value;
}
2021-04-26 18:04:52 +02:00
#endif
//Materials
Ref<Material> PropInstance::material_get(const int index) {
ERR_FAIL_INDEX_V(index, _materials.size(), Ref<Material>(NULL));
2021-04-26 18:04:52 +02:00
return _materials[index];
}
2021-04-26 18:04:52 +02:00
void PropInstance::material_add(const Ref<Material> &value) {
ERR_FAIL_COND(!value.is_valid());
_materials.push_back(value);
}
2021-04-26 18:04:52 +02:00
int PropInstance::material_get_num() const {
return _materials.size();
}
2021-04-26 18:04:52 +02:00
void PropInstance::materials_clear() {
_materials.clear();
}
Vector<Variant> PropInstance::materials_get() {
VARIANT_ARRAY_GET(_materials);
}
void PropInstance::materials_set(const Vector<Variant> &materials) {
_materials.clear();
for (int i = 0; i < materials.size(); i++) {
Ref<Material> material = Ref<Material>(materials[i]);
_materials.push_back(material);
}
2021-04-26 18:04:52 +02:00
}
2021-04-26 18:04:52 +02:00
//Meshes
RID PropInstance::mesh_get(const int index) {
ERR_FAIL_INDEX_V(index, _meshes.size(), RID());
return _meshes[index];
}
2021-04-26 18:04:52 +02:00
void PropInstance::mesh_add(const RID value) {
_meshes.push_back(value);
}
2021-04-26 18:04:52 +02:00
int PropInstance::mesh_get_num() const {
return _meshes.size();
}
void PropInstance::meshs_clear() {
_meshes.clear();
}
2021-04-26 18:04:52 +02:00
Vector<Variant> PropInstance::meshes_get() {
Vector<Variant> r;
for (int i = 0; i < _meshes.size(); i++) {
r.push_back(_meshes[i]);
}
2021-04-26 18:04:52 +02:00
return r;
}
2021-04-26 18:04:52 +02:00
void PropInstance::meshes_set(const Vector<Variant> &meshs) {
_meshes.clear();
2021-04-26 18:04:52 +02:00
for (int i = 0; i < _meshes.size(); i++) {
RID mesh = RID(meshs[i]);
_meshes.push_back(mesh);
}
}
2021-04-26 18:04:52 +02:00
//Collider
RID PropInstance::collider_get(const int index) {
ERR_FAIL_INDEX_V(index, _colliders.size(), RID());
return _colliders[index];
}
void PropInstance::collider_add(const RID value) {
_colliders.push_back(value);
}
int PropInstance::collider_get_num() const {
return _colliders.size();
}
void PropInstance::colliders_clear() {
_colliders.clear();
}
Vector<Variant> PropInstance::colliders_get() {
Vector<Variant> r;
for (int i = 0; i < _colliders.size(); i++) {
r.push_back(_colliders[i]);
}
return r;
}
void PropInstance::colliders_set(const Vector<Variant> &colliders) {
_colliders.clear();
for (int i = 0; i < colliders.size(); i++) {
RID collider = (colliders[i]);
_colliders.push_back(collider);
}
}
float PropInstance::get_first_lod_distance_squared() {
return _first_lod_distance_squared;
}
void PropInstance::set_first_lod_distance_squared(const float dist) {
_first_lod_distance_squared = dist;
}
float PropInstance::get_lod_reduction_distance_squared() {
return _lod_reduction_distance_squared;
}
void PropInstance::set_lod_reduction_distance_squared(const float dist) {
_lod_reduction_distance_squared = dist;
}
void PropInstance::free_meshes() {
}
void PropInstance::free_colliders() {
}
void PropInstance::init_materials() {
call("_init_materials");
}
void PropInstance::_init_materials() {
}
void PropInstance::build() {
2021-04-26 18:04:52 +02:00
_building = true;
_build_queued = false;
if (!_job.is_valid()) {
_job = Ref<PropInstanceJob>(memnew(PropInstancePropJob()));
}
_job->set_prop_instace(this);
if (!is_inside_tree()) {
return;
}
for (int i = 0; i < get_child_count(); ++i) {
Node *n = get_child(i);
//this way we won't delete the user's nodes
if (n->get_owner() == NULL) {
n->queue_delete();
}
}
if (!_prop_data.is_valid())
return;
for (int i = 0; i < _prop_data->get_prop_count(); ++i) {
Ref<PropDataEntry> e = _prop_data->get_prop(i);
if (!e.is_valid())
continue;
Node *n = e->processor_get_node_for(get_transform());
if (n) {
add_child(n);
//if (Engine::get_singleton()->is_editor_hint())
// n->set_owner(get_tree()->get_edited_scene_root());
}
}
2021-04-26 18:04:52 +02:00
}
void PropInstance::queue_build() {
}
void PropInstance::build_finished() {
_building = false;
if (_build_queued) {
call_deferred("build");
}
}
2021-04-26 18:04:52 +02:00
void PropInstance::_build_finished() {
}
PropInstance::PropInstance() {
2021-04-26 18:04:52 +02:00
_build_queued = false;
_building = false;
#ifdef TEXTURE_PACKER_PRESENT
_merge_textures = true;
#endif
_first_lod_distance_squared = 20;
_lod_reduction_distance_squared = 10;
}
2021-04-26 18:04:52 +02:00
PropInstance::~PropInstance() {
2021-04-26 18:04:52 +02:00
_job.unref();
_prop_data.unref();
2021-04-26 18:04:52 +02:00
_materials.clear();
}
void PropInstance::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
if (_prop_data.is_valid()) {
build();
}
}
2021-04-26 18:04:52 +02:00
case NOTIFICATION_EXIT_TREE: {
if (_job.is_valid()) {
_job->set_cancelled(true);
}
free_meshes();
free_colliders();
}
}
}
void PropInstance::_bind_methods() {
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-26 18:04:52 +02:00
ClassDB::bind_method(D_METHOD("get_job"), &PropInstance::get_job);
ClassDB::bind_method(D_METHOD("set_job", "value"), &PropInstance::set_job);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "job", PROPERTY_HINT_RESOURCE_TYPE, "PropInstanceJob", 0), "set_job", "get_job");
#ifdef TEXTURE_PACKER_PRESENT
ClassDB::bind_method(D_METHOD("get_merge_textures"), &PropInstance::get_merge_textures);
ClassDB::bind_method(D_METHOD("set_merge_textures", "value"), &PropInstance::set_merge_textures);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "merge_textures"), "set_merge_textures", "get_merge_textures");
#endif
///Materials
ClassDB::bind_method(D_METHOD("material_get", "index"), &PropInstance::material_get);
ClassDB::bind_method(D_METHOD("material_add", "value"), &PropInstance::material_add);
ClassDB::bind_method(D_METHOD("material_get_num"), &PropInstance::material_get_num);
ClassDB::bind_method(D_METHOD("materials_clear"), &PropInstance::materials_clear);
ClassDB::bind_method(D_METHOD("materials_get"), &PropInstance::materials_get);
ClassDB::bind_method(D_METHOD("materials_set"), &PropInstance::materials_set);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "materials", PROPERTY_HINT_NONE, "17/17:Material", PROPERTY_USAGE_DEFAULT, "Material"), "materials_set", "materials_get");
//Meshes
ClassDB::bind_method(D_METHOD("mesh_get", "index"), &PropInstance::mesh_get);
ClassDB::bind_method(D_METHOD("mesh_add", "value"), &PropInstance::mesh_add);
ClassDB::bind_method(D_METHOD("mesh_get_num"), &PropInstance::mesh_get_num);
ClassDB::bind_method(D_METHOD("meshs_clear"), &PropInstance::meshs_clear);
ClassDB::bind_method(D_METHOD("meshes_get"), &PropInstance::meshes_get);
ClassDB::bind_method(D_METHOD("meshes_set"), &PropInstance::meshes_set);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "meshes", PROPERTY_HINT_NONE, "", 0), "meshes_set", "meshes_get");
//Colliders
ClassDB::bind_method(D_METHOD("collider_get", "index"), &PropInstance::collider_get);
ClassDB::bind_method(D_METHOD("collider_add", "value"), &PropInstance::collider_add);
ClassDB::bind_method(D_METHOD("collider_get_num"), &PropInstance::collider_get_num);
ClassDB::bind_method(D_METHOD("colliders_clear"), &PropInstance::colliders_clear);
ClassDB::bind_method(D_METHOD("colliders_get"), &PropInstance::colliders_get);
ClassDB::bind_method(D_METHOD("colliders_set"), &PropInstance::colliders_set);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "colliders", PROPERTY_HINT_NONE, "", 0), "colliders_set", "colliders_get");
//---
ClassDB::bind_method(D_METHOD("get_first_lod_distance_squared"), &PropInstance::get_first_lod_distance_squared);
ClassDB::bind_method(D_METHOD("set_first_lod_distance_squared", "value"), &PropInstance::set_first_lod_distance_squared);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "first_lod_distance_squared"), "set_first_lod_distance_squared", "get_first_lod_distance_squared");
ClassDB::bind_method(D_METHOD("get_lod_reduction_distance_squared"), &PropInstance::get_lod_reduction_distance_squared);
ClassDB::bind_method(D_METHOD("set_lod_reduction_distance_squared", "value"), &PropInstance::set_lod_reduction_distance_squared);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "lod_reduction_distance_squared"), "set_lod_reduction_distance_squared", "get_lod_reduction_distance_squared");
//---
ClassDB::bind_method(D_METHOD("free_meshes"), &PropInstance::free_meshes);
ClassDB::bind_method(D_METHOD("free_colliders"), &PropInstance::free_colliders);
//---
BIND_VMETHOD(MethodInfo("_init_materials"));
ClassDB::bind_method(D_METHOD("init_materials"), &PropInstance::init_materials);
ClassDB::bind_method(D_METHOD("_init_materials"), &PropInstance::_init_materials);
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);
2021-04-26 18:04:52 +02:00
BIND_VMETHOD(MethodInfo("_build_finished"));
2021-04-26 18:04:52 +02:00
ClassDB::bind_method(D_METHOD("_build_finished"), &PropInstance::_build_finished);
}