Clean up normalmapping, make sure tangents are imported correctly.

This commit is contained in:
Juan Linietsky 2017-07-03 10:44:45 -03:00
parent afbb5261e6
commit e63c64e256
9 changed files with 133 additions and 99 deletions

View File

@ -908,12 +908,20 @@ Error ColladaImport::_create_mesh_surfaces(bool p_optimize, Ref<ArrayMesh> &p_me
#ifndef NO_UP_AXIS_SWAP
if (collada.state.up_axis == Vector3::AXIS_Z) {
Vector3 bn = vertex.normal.cross(vertex.tangent.normal) * vertex.tangent.d;
SWAP(vertex.vertex.z, vertex.vertex.y);
vertex.vertex.z = -vertex.vertex.z;
SWAP(vertex.normal.z, vertex.normal.y);
vertex.normal.z = -vertex.normal.z;
SWAP(vertex.tangent.normal.z, vertex.tangent.normal.y);
vertex.tangent.normal.z = -vertex.tangent.normal.z;
SWAP(bn.z, bn.y);
bn.z = -bn.z;
vertex.tangent.d = vertex.normal.cross(vertex.tangent.normal).dot(bn) > 0 ? 1 : -1;
print_line("Tangent " + itos(p_i) + ": " + vertex.tangent);
}
#endif

View File

@ -32,6 +32,7 @@
#include "body_shape.h"
#include "core_string_names.h"
#include "physics_body.h"
#include "scene/resources/material.h"
#include "scene/scene_string_names.h"
#include "skeleton.h"
bool MeshInstance::_set(const StringName &p_name, const Variant &p_value) {
@ -274,6 +275,80 @@ void MeshInstance::_mesh_changed() {
materials.resize(mesh->get_surface_count());
}
void MeshInstance::create_debug_tagents() {
Vector<Vector3> lines;
Vector<Color> colors;
Ref<Mesh> mesh = get_mesh();
if (!mesh.is_valid())
return;
for (int i = 0; i < mesh->get_surface_count(); i++) {
Array arrays = mesh->surface_get_arrays(i);
Vector<Vector3> verts = arrays[Mesh::ARRAY_VERTEX];
Vector<Vector3> norms = arrays[Mesh::ARRAY_NORMAL];
if (norms.size() == 0)
continue;
Vector<float> tangents = arrays[Mesh::ARRAY_TANGENT];
if (tangents.size() == 0)
continue;
for (int j = 0; j < verts.size(); j++) {
Vector3 v = verts[j];
Vector3 n = norms[j];
Vector3 t = Vector3(tangents[j * 4 + 0], tangents[j * 4 + 1], tangents[j * 4 + 2]);
Vector3 b = (n.cross(t)).normalized() * tangents[j * 4 + 3];
lines.push_back(v); //normal
colors.push_back(Color(0, 0, 1)); //color
lines.push_back(v + n * 0.04); //normal
colors.push_back(Color(0, 0, 1)); //color
lines.push_back(v); //tangent
colors.push_back(Color(1, 0, 0)); //color
lines.push_back(v + t * 0.04); //tangent
colors.push_back(Color(1, 0, 0)); //color
lines.push_back(v); //binormal
colors.push_back(Color(0, 1, 0)); //color
lines.push_back(v + b * 0.04); //binormal
colors.push_back(Color(0, 1, 0)); //color
}
}
if (lines.size()) {
Ref<SpatialMaterial> sm;
sm.instance();
sm->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
sm->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
sm->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
Ref<ArrayMesh> am;
am.instance();
Array a;
a.resize(Mesh::ARRAY_MAX);
a[Mesh::ARRAY_VERTEX] = lines;
a[Mesh::ARRAY_COLOR] = colors;
am->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a);
am->surface_set_material(0, sm);
MeshInstance *mi = memnew(MeshInstance);
mi->set_mesh(am);
mi->set_name("DebugTangents");
add_child(mi);
if (get_parent()) {
if (get_parent() == get_tree()->get_edited_scene_root())
mi->set_owner(get_parent());
else
mi->set_owner(get_parent()->get_owner());
}
}
}
void MeshInstance::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_mesh", "mesh:Mesh"), &MeshInstance::set_mesh);
@ -290,6 +365,9 @@ void MeshInstance::_bind_methods() {
ClassDB::set_method_flags("MeshInstance", "create_convex_collision", METHOD_FLAGS_DEFAULT);
ClassDB::bind_method(D_METHOD("_mesh_changed"), &MeshInstance::_mesh_changed);
ClassDB::bind_method(D_METHOD("create_debug_tagents"), &MeshInstance::create_debug_tagents);
ClassDB::set_method_flags("MeshInstance", "create_debug_tagents", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton"), "set_skeleton_path", "get_skeleton_path");
}

View File

@ -83,6 +83,8 @@ public:
Node *create_convex_collision_node();
void create_convex_collision();
void create_debug_tagents();
virtual Rect3 get_aabb() const;
virtual PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;

View File

@ -37,7 +37,6 @@
#include "viewport.h"
VARIANT_ENUM_CAST(Node::PauseMode);
VARIANT_ENUM_CAST(Node::NetworkMode);
VARIANT_ENUM_CAST(Node::RPCMode);
void Node::_notification(int p_notification) {
@ -77,16 +76,6 @@ void Node::_notification(int p_notification) {
data.pause_owner = this;
}
if (data.network_mode == NETWORK_MODE_INHERIT) {
if (data.parent)
data.network_owner = data.parent->data.network_owner;
else
data.network_owner = NULL;
} else {
data.network_owner = this;
}
if (data.input)
add_to_group("_vp_input" + itos(get_viewport()->get_instance_ID()));
if (data.unhandled_input)
@ -108,7 +97,6 @@ void Node::_notification(int p_notification) {
remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_ID()));
data.pause_owner = NULL;
data.network_owner = NULL;
if (data.path_cache) {
memdelete(data.path_cache);
data.path_cache = NULL;
@ -472,69 +460,28 @@ void Node::_propagate_pause_owner(Node *p_owner) {
}
}
void Node::set_network_mode(NetworkMode p_mode) {
void Node::set_network_master(int p_peer_id, bool p_recursive) {
if (data.network_mode == p_mode)
return;
data.network_master = p_peer_id;
bool prev_inherits = data.network_mode == NETWORK_MODE_INHERIT;
data.network_mode = p_mode;
if (!is_inside_tree())
return; //pointless
if ((data.network_mode == NETWORK_MODE_INHERIT) == prev_inherits)
return; ///nothing changed
if (p_recursive) {
for (int i = 0; i < data.children.size(); i++) {
Node *owner = NULL;
if (data.network_mode == NETWORK_MODE_INHERIT) {
if (data.parent)
owner = data.parent->data.network_owner;
} else {
owner = this;
data.children[i]->set_network_master(p_peer_id, true);
}
}
}
_propagate_network_owner(owner);
}
int Node::get_network_master() const {
Node::NetworkMode Node::get_network_mode() const {
return data.network_mode;
return data.network_master;
}
bool Node::is_network_master() const {
ERR_FAIL_COND_V(!is_inside_tree(), false);
switch (data.network_mode) {
case NETWORK_MODE_INHERIT: {
if (data.network_owner)
return data.network_owner->is_network_master();
else
return get_tree()->is_network_server();
} break;
case NETWORK_MODE_MASTER: {
return true;
} break;
case NETWORK_MODE_SLAVE: {
return false;
} break;
}
return false;
}
void Node::_propagate_network_owner(Node *p_owner) {
if (data.network_mode != NETWORK_MODE_INHERIT)
return;
data.network_owner = p_owner;
for (int i = 0; i < data.children.size(); i++) {
data.children[i]->_propagate_network_owner(p_owner);
}
return get_tree()->get_network_unique_id() == data.network_master;
}
/***** RPC CONFIG ********/
@ -962,7 +909,7 @@ void Node::rset_unreliable_id(int p_peer_id, const StringName &p_property, const
//////////// end of rpc
bool Node::can_call_rpc(const StringName &p_method) const {
bool Node::can_call_rpc(const StringName &p_method, int p_from) const {
const Map<StringName, RPCMode>::Element *E = data.rpc_methods.find(p_method);
if (E) {
@ -982,7 +929,7 @@ bool Node::can_call_rpc(const StringName &p_method) const {
return is_network_master();
} break;
case RPC_MODE_SLAVE: {
return !is_network_master();
return !is_network_master() && p_from == get_network_master();
} break;
}
}
@ -1006,16 +953,16 @@ bool Node::can_call_rpc(const StringName &p_method) const {
return is_network_master();
} break;
case ScriptInstance::RPC_MODE_SLAVE: {
return !is_network_master();
return !is_network_master() && p_from == get_network_master();
} break;
}
}
ERR_PRINTS("RPC on unauthorized method attempted: " + String(p_method) + " on base: " + String(Variant(this)));
ERR_PRINTS("RPC from " + itos(p_from) + " on unauthorized method attempted: " + String(p_method) + " on base: " + String(Variant(this)));
return false;
}
bool Node::can_call_rset(const StringName &p_property) const {
bool Node::can_call_rset(const StringName &p_property, int p_from) const {
const Map<StringName, RPCMode>::Element *E = data.rpc_properties.find(p_property);
if (E) {
@ -1035,7 +982,7 @@ bool Node::can_call_rset(const StringName &p_property) const {
return is_network_master();
} break;
case RPC_MODE_SLAVE: {
return !is_network_master();
return !is_network_master() && p_from == get_network_master();
} break;
}
}
@ -1059,12 +1006,12 @@ bool Node::can_call_rset(const StringName &p_property) const {
return is_network_master();
} break;
case ScriptInstance::RPC_MODE_SLAVE: {
return !is_network_master();
return !is_network_master() && p_from == get_network_master();
} break;
}
}
ERR_PRINTS("RSET on unauthorized property attempted: " + String(p_property) + " on base: " + String(Variant(this)));
ERR_PRINTS("RSET from " + itos(p_from) + " on unauthorized property attempted: " + String(p_property) + " on base: " + String(Variant(this)));
return false;
}
@ -2845,8 +2792,8 @@ void Node::_bind_methods() {
ClassDB::bind_method(D_METHOD("request_ready"), &Node::request_ready);
ClassDB::bind_method(D_METHOD("set_network_mode", "mode"), &Node::set_network_mode);
ClassDB::bind_method(D_METHOD("get_network_mode"), &Node::get_network_mode);
ClassDB::bind_method(D_METHOD("set_network_master", "id", "recursive"), &Node::set_network_master, DEFVAL(true));
ClassDB::bind_method(D_METHOD("get_network_master"), &Node::get_network_master);
ClassDB::bind_method(D_METHOD("is_network_master"), &Node::is_network_master);
@ -2902,10 +2849,6 @@ void Node::_bind_methods() {
BIND_CONSTANT(NOTIFICATION_INTERNAL_PROCESS);
BIND_CONSTANT(NOTIFICATION_INTERNAL_FIXED_PROCESS);
BIND_CONSTANT(NETWORK_MODE_INHERIT);
BIND_CONSTANT(NETWORK_MODE_MASTER);
BIND_CONSTANT(NETWORK_MODE_SLAVE);
BIND_CONSTANT(RPC_MODE_DISABLED);
BIND_CONSTANT(RPC_MODE_REMOTE);
BIND_CONSTANT(RPC_MODE_SYNC);
@ -2977,8 +2920,7 @@ Node::Node() {
data.unhandled_key_input = false;
data.pause_mode = PAUSE_MODE_INHERIT;
data.pause_owner = NULL;
data.network_mode = NETWORK_MODE_INHERIT;
data.network_owner = NULL;
data.network_master = 1; //server by default
data.path_cache = NULL;
data.parent_owned = false;
data.in_constructor = true;

View File

@ -61,13 +61,6 @@ public:
DUPLICATE_USE_INSTANCING = 8
};
enum NetworkMode {
NETWORK_MODE_INHERIT,
NETWORK_MODE_MASTER,
NETWORK_MODE_SLAVE
};
enum RPCMode {
RPC_MODE_DISABLED, //no rpc for this method, calls to this will be blocked (default)
@ -122,8 +115,7 @@ private:
PauseMode pause_mode;
Node *pause_owner;
NetworkMode network_mode;
Node *network_owner;
int network_master;
Map<StringName, RPCMode> rpc_methods;
Map<StringName, RPCMode> rpc_properties;
@ -173,7 +165,6 @@ private:
void _propagate_validate_owner();
void _print_stray_nodes();
void _propagate_pause_owner(Node *p_owner);
void _propagate_network_owner(Node *p_owner);
Array _get_node_and_resource(const NodePath &p_path);
void _duplicate_signals(const Node *p_original, Node *p_copy) const;
@ -393,8 +384,8 @@ public:
bool is_displayed_folded() const;
/* NETWORK */
void set_network_mode(NetworkMode p_mode);
NetworkMode get_network_mode() const;
void set_network_master(int p_peer_id, bool p_recursive = true);
int get_network_master() const;
bool is_network_master() const;
void rpc_config(const StringName &p_method, RPCMode p_mode); // config a local method for RPC
@ -414,8 +405,8 @@ public:
void rsetp(int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value);
bool can_call_rpc(const StringName &p_method) const;
bool can_call_rset(const StringName &p_property) const;
bool can_call_rpc(const StringName &p_method, int p_from) const;
bool can_call_rset(const StringName &p_property, int p_from) const;
Node();
~Node();

View File

@ -1763,6 +1763,16 @@ int SceneTree::get_network_unique_id() const {
return network_peer->get_unique_id();
}
Vector<int> SceneTree::get_network_connected_peers() const {
ERR_FAIL_COND_V(!network_peer.is_valid(), Vector<int>());
Vector<int> ret;
for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) {
ret.push_back(E->get());
}
return ret;
}
void SceneTree::set_refuse_new_network_connections(bool p_refuse) {
ERR_FAIL_COND(!network_peer.is_valid());
network_peer->set_refuse_new_connections(p_refuse);
@ -1973,6 +1983,7 @@ void SceneTree::_network_process_packet(int p_from, const uint8_t *p_packet, int
Node *node = NULL;
if (target & 0x80000000) {
//use full path (not cached yet)
int ofs = target & 0x7FFFFFFF;
ERR_FAIL_COND(ofs >= p_packet_len);
@ -1988,7 +1999,7 @@ void SceneTree::_network_process_packet(int p_from, const uint8_t *p_packet, int
ERR_FAIL_COND(node == NULL);
}
} else {
//use cached path
int id = target;
Map<int, PathGetCache>::Element *E = path_get_cache.find(p_from);
@ -2023,7 +2034,7 @@ void SceneTree::_network_process_packet(int p_from, const uint8_t *p_packet, int
if (packet_type == NETWORK_COMMAND_REMOTE_CALL) {
if (!node->can_call_rpc(name))
if (!node->can_call_rpc(name, p_from))
return;
int ofs = len_end + 1;
@ -2060,7 +2071,7 @@ void SceneTree::_network_process_packet(int p_from, const uint8_t *p_packet, int
} else {
if (!node->can_call_rset(name))
if (!node->can_call_rset(name, p_from))
return;
int ofs = len_end + 1;
@ -2236,6 +2247,7 @@ void SceneTree::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_network_peer", "peer:NetworkedMultiplayerPeer"), &SceneTree::set_network_peer);
ClassDB::bind_method(D_METHOD("is_network_server"), &SceneTree::is_network_server);
ClassDB::bind_method(D_METHOD("has_network_peer"), &SceneTree::has_network_peer);
ClassDB::bind_method(D_METHOD("get_network_connected_peers"), &SceneTree::get_network_connected_peers);
ClassDB::bind_method(D_METHOD("get_network_unique_id"), &SceneTree::get_network_unique_id);
ClassDB::bind_method(D_METHOD("set_refuse_new_network_connections", "refuse"), &SceneTree::set_refuse_new_network_connections);
ClassDB::bind_method(D_METHOD("is_refusing_new_network_connections"), &SceneTree::is_refusing_new_network_connections);

View File

@ -451,6 +451,7 @@ public:
bool is_network_server() const;
bool has_network_peer() const;
int get_network_unique_id() const;
Vector<int> get_network_connected_peers() const;
void set_refuse_new_network_connections(bool p_refuse);
bool is_refusing_new_network_connections() const;

View File

@ -452,7 +452,7 @@ void SpatialMaterial::_update_shader() {
if (features[FEATURE_DEPTH_MAPPING]) {
code += "\t{\n";
code += "\t\tvec3 view_dir = normalize(normalize(-VERTEX)*mat3(TANGENT,BINORMAL,NORMAL));\n";
code += "\t\tvec3 view_dir = normalize(normalize(-VERTEX)*mat3(TANGENT,-BINORMAL,NORMAL));\n"; //binormal is negative due to mikktpsace
if (deep_parallax) {
code += "\t\tfloat num_layers = mix(float(depth_max_layers),float(depth_min_layers), abs(dot(vec3(0.0, 0.0, 1.0), view_dir)));\n";
@ -1265,7 +1265,7 @@ void SpatialMaterial::_bind_methods() {
ADD_GROUP("Anisotropy", "anisotropy_");
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "anisotropy_enabled"), "set_feature", "get_feature", FEATURE_ANISOTROPY);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "anisotropy", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_anisotropy", "get_anisotropy");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "anisotropy", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_anisotropy", "get_anisotropy");
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anisotropy_flowmap", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_FLOWMAP);
ADD_GROUP("Ambient Occlusion", "ao_");

View File

@ -1262,9 +1262,9 @@ Array VisualServer::_get_array_from_surface(uint32_t p_format, PoolVector<uint8_
for (int j = 0; j < p_vertex_len; j++) {
const uint8_t *v = (const uint8_t *)&r[j * total_elem_size + offsets[i]];
const int8_t *v = (const int8_t *)&r[j * total_elem_size + offsets[i]];
for (int k = 0; k < 4; k++) {
w[j * 4 + k] = float(v[k] / 255.0) * 2.0 - 1.0;
w[j * 4 + k] = float(v[k] / 127.0);
}
}
} else {