#if 0 #include "behavior.h" #include #include #include #include #include #include #include #include #include #include #include #include #include UF_BEHAVIOR_REGISTER_CPP(ext::NoiseBehavior) #define this ((uf::Scene*) &self) void ext::NoiseBehavior::initialize( uf::Object& self ) { // auto& image = this->getComponent(); auto& noiseGenerator = this->getComponent(); auto& graphic = this->getComponent(); auto mesh = this->getComponent(); auto& texture = graphic.material.textures.emplace_back(); auto& metadata = this->getComponent(); noiseGenerator.seed(rand()); float high = std::numeric_limits::min(); float low = std::numeric_limits::max(); float amplitude = metadata["amplitude"].is() ? metadata["amplitude"].as() : 1.5; pod::Vector3ui size = uf::vector::decode(metadata["size"], pod::Vector3ui{512, 512, 1}); pod::Vector3d coefficients = uf::vector::decode(metadata["coefficients"], pod::Vector3d{3.0, 3.0, 3.0}); std::vector pixels(size.x * size.y * size.z); std::vector perlins(size.x * size.y * size.z); #pragma omp parallel for for ( size_t z = 0; z < size.z; ++z ) { for ( size_t y = 0; y < size.y; ++y ) { for ( size_t x = 0; x < size.x; ++x ) { float nx = (float) x / (float) size.x; float ny = (float) y / (float) size.y; float nz = (float) z / (float) size.z; float n = amplitude * noiseGenerator.noise(coefficients.x * nx, coefficients.y * ny, coefficients.z * nz); high = std::max( high, n ); low = std::min( low, n ); perlins[x + y * size.x + z * size.x * size.y] = n; } } } for ( size_t i = 0; i < perlins.size(); ++i ) { float n = perlins[i]; n = n - floor(n); float normalized = (n - low) / (high - low); pixels[i] = static_cast(floor(normalized * 255)); } texture.fromBuffers( (void*) pixels.data(), pixels.size(), uf::renderer::enums::Format::R8_UNORM, size.x, size.y, size.z, 1 ); mesh.vertices = { {{-1*-0.5f, 0.0f, 0.0f}, {1.0f, 0.0f}, { 0.0f, 0.0f, -1.0f } }, {{-1*0.5f, 0.0f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, -1.0f } }, {{-1*0.5f, 1.0f, 0.0f}, {0.0f, 1.0f}, { 0.0f, 0.0f, -1.0f } }, {{-1*0.5f, 1.0f, 0.0f}, {0.0f, 1.0f}, { 0.0f, 0.0f, -1.0f } }, {{-1*-0.5f, 1.0f, 0.0f}, {1.0f, 1.0f}, { 0.0f, 0.0f, -1.0f } }, {{-1*-0.5f, 0.0f, 0.0f}, {1.0f, 0.0f}, { 0.0f, 0.0f, -1.0f } }, {{-1*0.5f, 1.0f, 0.0f}, {0.0f, 1.0f}, { 0.0f, 0.0f, 1.0f } }, {{-1*0.5f, 0.0f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f } }, {{-1*-0.5f, 0.0f, 0.0f}, {1.0f, 0.0f}, { 0.0f, 0.0f, 1.0f } }, {{-1*-0.5f, 0.0f, 0.0f}, {1.0f, 0.0f}, { 0.0f, 0.0f, 1.0f } }, {{-1*-0.5f, 1.0f, 0.0f}, {1.0f, 1.0f}, { 0.0f, 0.0f, 1.0f } }, {{-1*0.5f, 1.0f, 0.0f}, {0.0f, 1.0f}, { 0.0f, 0.0f, 1.0f } }, }; graphic.initialize(); graphic.initializeMesh( mesh ); this->callHook("entity:TextureUpdate.%UID%"); graphic.material.attachShader(uf::io::root+"/shaders/base/vert.spv", uf::renderer::enums::Shader::VERTEX); graphic.material.attachShader(uf::io::root+"/shaders/noise/frag.spv", uf::renderer::enums::Shader::FRAGMENT); } void ext::NoiseBehavior::tick( uf::Object& self ) { auto& metadata = this->getComponent(); float slice = metadata["slice"].as(); slice += uf::physics::time::delta * metadata["speed"].as(); // if ( slice >= metadata["size"][2].as() ) slice = 0; metadata["slice"] = slice; if ( !this->hasComponent() ) return; auto& scene = uf::scene::getCurrentScene(); auto& graphic = this->getComponent(); auto& transform = this->getComponent>(); auto& controller = scene.getController(); auto& camera = controller.getComponent(); if ( !graphic.initialized ) return; #if UF_USE_VULKAN if ( !graphic.material.hasShader("fragment") ) return; auto& shader = graphic.material.getShader("fragment"); auto& uniform = shader.getUniform("UBO"); #if UF_UNIFORMS_UPDATE_WITH_JSON // auto uniforms = shader.getUniformJson("UBO"); ext::json::Value uniforms; uniforms["slice"] = slice; shader.updateUniform("UBO", uniforms ); #else struct UniformDescriptor { alignas(4) float slice = 0; }; auto& uniforms = uniform.get(); uniforms.slice = slice; shader.updateUniform( "UBO", uniform ); #endif #endif } void ext::NoiseBehavior::render( uf::Object& self ) {} void ext::NoiseBehavior::destroy( uf::Object& self ) {} #undef this #endif