#include "behavior.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include UF_BEHAVIOR_REGISTER_CPP(ext::CraetureBehavior) UF_BEHAVIOR_TRAITS_CPP(ext::CraetureBehavior, ticks = true, renders = false, multithread = false) #define this (&self) namespace { void load( uf::Object& self, const uf::Image& image ) { auto& graphic = self.getComponent(); auto& texture = graphic.material.textures.emplace_back(); texture.loadFromImage( image ); auto& mesh = self.getComponent(); mesh.bindIndirect(); mesh.bind(); float scaleX = 1; float scaleY = 1; mesh.insertVertices({ { {-1.0f * scaleX, 1.0f * scaleY, 0.0f}, {0.0f, 1.0f}, {}, {0.0f, 1.0f}, {0.0f, 0.0f, -1.0f} }, { {-1.0f * scaleX, -1.0f * scaleY, 0.0f}, {0.0f, 0.0f}, {}, {0.0f, 0.0f}, {0.0f, 0.0f, -1.0f} }, { {1.0f * scaleX, -1.0f * scaleY, 0.0f}, {1.0f, 0.0f}, {}, {1.0f, 0.0f}, {0.0f, 0.0f, -1.0f} }, { {1.0f * scaleX, 1.0f * scaleY, 0.0f}, {1.0f, 1.0f}, {}, {1.0f, 1.0f}, {0.0f, 0.0f, -1.0f}, } }); mesh.insertIndices({ 0, 1, 2, 2, 3, 0 }); uf::graph::convert( self ); } void load_migoto( uf::Object& self, const uf::Serializer& json ) { #if !UF_ENV_DREAMCAST && UF_USE_FLOAT16 struct Position { pod::Vector3f position; pod::Vector3f normal; pod::Vector4f tangent; }; struct TexCoord { pod::ColorRgba color; pod::Vector2f16 uv; pod::Vector2f st; pod::Vector2f16 wx; }; struct Weight { pod::Vector4f alpha; pod::Vector4f indices; }; auto& metadataJson = this->getComponent(); //auto root = metadataJson["system"]["root"].as(); auto root = json["root"].as(metadataJson["system"]["root"].as()); auto ib_buffer = uf::io::readAsBuffer( root + "/" + json["ib_buffer"].as() ); auto ib_buffer_start = (uint32_t*) ib_buffer.data(); auto pos_buffer = uf::io::readAsBuffer( root + "/" + json["pos_buffer"].as() ); auto uv_buffer = uf::io::readAsBuffer( root + "/" + json["texcoord_buffer"].as() ); auto weight_buffer = uf::io::readAsBuffer( root + "/" + json["weight_buffer"].as() ); auto pos_buffer_start = (Position*) pos_buffer.data(); auto uv_buffer_start = (TexCoord*) uv_buffer.data(); auto weight_buffer_start = (Weight*) weight_buffer.data(); auto pos_stride = sizeof(float) * 10; auto uv_stride = sizeof(uint8_t) * 4 + sizeof(float) * 4; auto weight_stride = sizeof(float) * 8; auto vb_stride = pos_stride + uv_stride + weight_stride; auto ib_stride = sizeof(uint32_t); auto ib_count = ib_buffer.size() / ib_stride; auto vb_count = pos_buffer.size() / pos_stride; uf::stl::vector indices( ib_buffer_start, ib_buffer_start + ib_count ); uf::stl::vector vertices( vb_count ); for ( auto i = 0; i < vb_count; ++i ) { auto& vertex = vertices[i]; auto& pos = pos_buffer_start[i]; auto& uv = uv_buffer_start[i]; auto& weight = weight_buffer_start[i]; vertex.position = pos.position; vertex.normal = pos.normal; vertex.tangent = pos.tangent; vertex.color = uv.color; vertex.uv[0] = uv.uv[0]; vertex.uv[1] = uv.uv[1]; //vertex.st = uv.st; vertex.joints = weight.indices; vertex.weights = weight.alpha; } uf::Image albedoImage; albedoImage.open( root + "/" + json["albedo_path"].as() ); auto& graphic = self.getComponent(); auto& albedoTexture = graphic.material.textures.emplace_back(); auto& mesh = self.getComponent(); albedoTexture.loadFromImage( albedoImage ); mesh.bindIndirect(); mesh.bind(); mesh.insertVertices( vertices ); mesh.insertIndices( indices ); uf::graph::convert( self ); #endif } } void ext::CraetureBehavior::initialize( uf::Object& self ) { /* this->addHook( "asset:Load.%UID%", [&](pod::payloads::assetLoad& payload){ if ( !uf::asset::isExpected( payload, uf::asset::Type::IMAGE ) ) return; if ( !uf::asset::has( payload ) ) uf::asset::load( payload ); const auto& image = uf::asset::get( payload ); ::load( self, image ); }); */ auto& metadata = this->getComponent(); auto& metadataJson = this->getComponent(); UF_BEHAVIOR_METADATA_BIND_SERIALIZER_HOOKS(metadata, metadataJson); if ( !ext::json::isNull( metadataJson["migoto"] ) ) { ::load_migoto( self, metadataJson["migoto"] ); } } void ext::CraetureBehavior::tick( uf::Object& self ) { auto& metadata = this->getComponent(); if ( this->hasComponent() ) { auto& graph = this->getComponent(); pod::payloads::QueueAnimationPayload payload; payload.name = metadata.animation; this->queueHook("animation:Set.%UID%", payload); metadata.animation = ""; } } void ext::CraetureBehavior::render( uf::Object& self ){} void ext::CraetureBehavior::destroy( uf::Object& self ){} void ext::CraetureBehavior::Metadata::serialize( uf::Object& self, uf::Serializer& serializer ){} void ext::CraetureBehavior::Metadata::deserialize( uf::Object& self, uf::Serializer& serializer ){} #undef this