separate IBL from skybox and add setBackgroundImage method
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
#include "FilamentViewer.hpp"
|
||||
|
||||
#include "streambuf.hpp"
|
||||
|
||||
#include <filament/Camera.h>
|
||||
#include <filament/ColorGrading.h>
|
||||
@@ -46,6 +47,8 @@
|
||||
|
||||
#include <utils/NameComponentManager.h>
|
||||
|
||||
#include <imageio/ImageDecoder.h>
|
||||
|
||||
#include "math.h"
|
||||
|
||||
#include <math/mat4.h>
|
||||
@@ -63,11 +66,14 @@
|
||||
|
||||
#include "Log.h"
|
||||
|
||||
#include "imagematerial.h"
|
||||
|
||||
using namespace filament;
|
||||
using namespace filament::math;
|
||||
using namespace gltfio;
|
||||
using namespace utils;
|
||||
using namespace std::chrono;
|
||||
using namespace image;
|
||||
|
||||
namespace filament
|
||||
{
|
||||
@@ -170,15 +176,161 @@ namespace polyvox
|
||||
|
||||
// Always add a direct light source since it is required for shadowing.
|
||||
_sun = EntityManager::get().create();
|
||||
LightManager::Builder(LightManager::Type::DIRECTIONAL)
|
||||
LightManager::Builder(LightManager::Type::SUN)
|
||||
.color(Color::cct(6500.0f))
|
||||
.intensity(100000.0f)
|
||||
.direction(math::float3(0.0f, 1.0f, 0.0f))
|
||||
.intensity(150000.0f)
|
||||
.direction(math::float3(0.0f, 0.0f, -1.0f))
|
||||
.castShadows(false)
|
||||
// .castShadows(true)
|
||||
.build(*_engine, _sun);
|
||||
_scene->addEntity(_sun);
|
||||
}
|
||||
|
||||
static constexpr float4 sFullScreenTriangleVertices[3] = {
|
||||
{-1.0f, -1.0f, 1.0f, 1.0f},
|
||||
{3.0f, -1.0f, 1.0f, 1.0f},
|
||||
{-1.0f, 3.0f, 1.0f, 1.0f}};
|
||||
|
||||
static const uint16_t sFullScreenTriangleIndices[3] = {0, 1, 2};
|
||||
|
||||
void FilamentViewer::createImageRenderable()
|
||||
{
|
||||
|
||||
if (_imageEntity)
|
||||
return;
|
||||
|
||||
auto &em = EntityManager::get();
|
||||
_imageMaterial = Material::Builder()
|
||||
.package(IMAGEMATERIAL_IMAGE_DATA, IMAGEMATERIAL_IMAGE_SIZE)
|
||||
.build(*_engine);
|
||||
|
||||
_imageVb = VertexBuffer::Builder()
|
||||
.vertexCount(3)
|
||||
.bufferCount(1)
|
||||
.attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT4, 0)
|
||||
.build(*_engine);
|
||||
|
||||
_imageVb->setBufferAt(
|
||||
*_engine, 0, {sFullScreenTriangleVertices, sizeof(sFullScreenTriangleVertices)});
|
||||
|
||||
_imageIb = IndexBuffer::Builder()
|
||||
.indexCount(3)
|
||||
.bufferType(IndexBuffer::IndexType::USHORT)
|
||||
.build(*_engine);
|
||||
|
||||
_imageIb->setBuffer(*_engine,
|
||||
{sFullScreenTriangleIndices, sizeof(sFullScreenTriangleIndices)});
|
||||
|
||||
Entity imageEntity = em.create();
|
||||
RenderableManager::Builder(1)
|
||||
.boundingBox({{}, {1.0f, 1.0f, 1.0f}})
|
||||
.material(0, _imageMaterial->getDefaultInstance())
|
||||
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES, _imageVb, _imageIb, 0, 3)
|
||||
.culling(false)
|
||||
.build(*_engine, imageEntity);
|
||||
|
||||
_scene->addEntity(imageEntity);
|
||||
|
||||
_imageEntity = &imageEntity;
|
||||
|
||||
Texture *texture = Texture::Builder()
|
||||
.width(1)
|
||||
.height(1)
|
||||
.levels(1)
|
||||
.format(Texture::InternalFormat::RGBA8)
|
||||
.sampler(Texture::Sampler::SAMPLER_2D)
|
||||
.build(*_engine);
|
||||
static uint32_t pixel = 0;
|
||||
Texture::PixelBufferDescriptor buffer(&pixel, 4, Texture::Format::RGBA, Texture::Type::UBYTE);
|
||||
texture->setImage(*_engine, 0, std::move(buffer));
|
||||
}
|
||||
|
||||
void FilamentViewer::setBackgroundImage(const char *resourcePath)
|
||||
{
|
||||
|
||||
if (colorGrading)
|
||||
{
|
||||
_engine->destroy(colorGrading);
|
||||
}
|
||||
ToneMapper *tm = new LinearToneMapper();
|
||||
colorGrading = ColorGrading::Builder()
|
||||
.toneMapper(tm)
|
||||
.build(*_engine);
|
||||
delete tm;
|
||||
|
||||
_view->setColorGrading(colorGrading);
|
||||
|
||||
createImageRenderable();
|
||||
|
||||
if (_imageTexture)
|
||||
{
|
||||
_engine->destroy(_imageTexture);
|
||||
_imageTexture = nullptr;
|
||||
}
|
||||
|
||||
ResourceBuffer bg = _loadResource(resourcePath);
|
||||
|
||||
polyvox::streambuf sb((char *)bg.data, (char *)bg.data + bg.size);
|
||||
|
||||
std::istream *inputStream = new std::istream(&sb);
|
||||
|
||||
LinearImage *image = new LinearImage(ImageDecoder::decode(
|
||||
*inputStream, resourcePath, ImageDecoder::ColorSpace::SRGB));
|
||||
|
||||
if (!image->isValid())
|
||||
{
|
||||
Log("Invalid image : %s", resourcePath);
|
||||
return;
|
||||
}
|
||||
|
||||
delete inputStream;
|
||||
|
||||
_freeResource(bg);
|
||||
|
||||
uint32_t channels = image->getChannels();
|
||||
uint32_t w = image->getWidth();
|
||||
uint32_t h = image->getHeight();
|
||||
_imageTexture = Texture::Builder()
|
||||
.width(w)
|
||||
.height(h)
|
||||
.levels(0xff)
|
||||
.format(channels == 3 ? Texture::InternalFormat::RGB16F : Texture::InternalFormat::RGBA16F)
|
||||
.sampler(Texture::Sampler::SAMPLER_2D)
|
||||
.build(*_engine);
|
||||
|
||||
Texture::PixelBufferDescriptor::Callback freeCallback = [](void *buf, size_t, void *data)
|
||||
{
|
||||
delete reinterpret_cast<LinearImage *>(data);
|
||||
};
|
||||
|
||||
Texture::PixelBufferDescriptor buffer(
|
||||
image->getPixelRef(),
|
||||
size_t(w * h * channels * sizeof(float)),
|
||||
channels == 3 ? Texture::Format::RGB : Texture::Format::RGBA,
|
||||
Texture::Type::FLOAT,
|
||||
freeCallback);
|
||||
|
||||
_imageTexture->setImage(*_engine, 0, std::move(buffer));
|
||||
// _imageTexture->generateMipmaps(*_engine);
|
||||
|
||||
float srcWidth = _imageTexture->getWidth();
|
||||
float srcHeight = _imageTexture->getHeight();
|
||||
float dstWidth = _view->getViewport().width;
|
||||
float dstHeight = _view->getViewport().height;
|
||||
|
||||
mat3f transform(
|
||||
1.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f);
|
||||
|
||||
_imageMaterial->setDefaultParameter("transform", transform);
|
||||
_imageMaterial->setDefaultParameter(
|
||||
"image", _imageTexture, _imageSampler);
|
||||
|
||||
_imageMaterial->setDefaultParameter("showImage", 1);
|
||||
|
||||
_imageMaterial->setDefaultParameter(
|
||||
"backgroundColor", RgbType::sRGB, float3(1.0f));
|
||||
}
|
||||
|
||||
FilamentViewer::~FilamentViewer()
|
||||
@@ -231,7 +383,7 @@ namespace polyvox
|
||||
for (size_t i = 0; i < resourceUriCount; i++)
|
||||
{
|
||||
string uri = relativeResourcePath + string(resourceUris[i]);
|
||||
Log("Creating resource buffer for resource at %s",uri.c_str());
|
||||
Log("Creating resource buffer for resource at %s", uri.c_str());
|
||||
ResourceBuffer buf = _loadResource(uri.c_str());
|
||||
|
||||
// using FunctionCallback = std::function<void(void*, unsigned int, void *)>;
|
||||
@@ -313,7 +465,7 @@ namespace polyvox
|
||||
_asset = nullptr;
|
||||
_animator = nullptr;
|
||||
|
||||
ResourceBuffer rbuf = _loadResource(uri);
|
||||
ResourceBuffer rbuf = _loadResource(uri);
|
||||
|
||||
// Parse the glTF file and create Filament entities.
|
||||
Log("Creating asset from JSON");
|
||||
@@ -335,14 +487,16 @@ namespace polyvox
|
||||
// transformToUnitCube();
|
||||
}
|
||||
|
||||
void FilamentViewer::removeAsset() {
|
||||
if (!_asset) {
|
||||
void FilamentViewer::removeAsset()
|
||||
{
|
||||
if (!_asset)
|
||||
{
|
||||
Log("No asset loaded, ignoring call.");
|
||||
return;
|
||||
}
|
||||
|
||||
mtx.lock();
|
||||
|
||||
|
||||
_resourceLoader->evictResourceData();
|
||||
_scene->removeEntities(_asset->getEntities(), _asset->getEntityCount());
|
||||
_assetLoader->destroyAsset(_asset);
|
||||
@@ -354,11 +508,6 @@ namespace polyvox
|
||||
mtx.unlock();
|
||||
}
|
||||
|
||||
void FilamentViewer::removeSkybox() {
|
||||
_scene->setSkybox(nullptr);
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Sets the active camera to the GLTF camera node specified by [name].
|
||||
/// N.B. Blender will generally export a three-node hierarchy - Camera1->Camera_Orientation->Camera2.
|
||||
@@ -368,23 +517,26 @@ namespace polyvox
|
||||
{
|
||||
Log("Attempting to set camera to %s.", cameraName);
|
||||
size_t count = _asset->getCameraEntityCount();
|
||||
if(count == 0) {
|
||||
Log("Failed, no cameras found in current asset.");
|
||||
if (count == 0)
|
||||
{
|
||||
Log("Failed, no cameras found in current asset.");
|
||||
return false;
|
||||
}
|
||||
|
||||
const utils::Entity* cameras = _asset->getCameraEntities();
|
||||
const utils::Entity *cameras = _asset->getCameraEntities();
|
||||
Log("%zu cameras found in current asset", count);
|
||||
for(int i=0; i < count; i++) {
|
||||
|
||||
auto inst = _ncm->getInstance(cameras[i]);
|
||||
const char* name = _ncm->getName(inst);
|
||||
Log("Camera %d : %s", i, name);
|
||||
if (strcmp(name, cameraName) == 0) {
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
|
||||
Camera* camera = _engine->getCameraComponent(cameras[i]);
|
||||
auto inst = _ncm->getInstance(cameras[i]);
|
||||
const char *name = _ncm->getName(inst);
|
||||
Log("Camera %d : %s", i, name);
|
||||
if (strcmp(name, cameraName) == 0)
|
||||
{
|
||||
|
||||
Camera *camera = _engine->getCameraComponent(cameras[i]);
|
||||
_view->setCamera(camera);
|
||||
|
||||
|
||||
const Viewport &vp = _view->getViewport();
|
||||
const double aspect = (double)vp.width / vp.height;
|
||||
|
||||
@@ -400,7 +552,8 @@ namespace polyvox
|
||||
|
||||
unique_ptr<vector<string>> FilamentViewer::getAnimationNames()
|
||||
{
|
||||
if(!_asset) {
|
||||
if (!_asset)
|
||||
{
|
||||
Log("No asset, ignoring call.");
|
||||
return nullptr;
|
||||
}
|
||||
@@ -420,7 +573,8 @@ namespace polyvox
|
||||
|
||||
unique_ptr<vector<string>> FilamentViewer::getTargetNames(const char *meshName)
|
||||
{
|
||||
if(!_asset) {
|
||||
if (!_asset)
|
||||
{
|
||||
Log("No asset, ignoring call.");
|
||||
return nullptr;
|
||||
}
|
||||
@@ -432,12 +586,14 @@ namespace polyvox
|
||||
{
|
||||
Entity e = entities[i];
|
||||
auto inst = _ncm->getInstance(e);
|
||||
const char* name = _ncm->getName(inst);
|
||||
const char *name = _ncm->getName(inst);
|
||||
Log("Got entity instance name %s", name);
|
||||
if(strcmp(name, meshName) == 0) {
|
||||
if (strcmp(name, meshName) == 0)
|
||||
{
|
||||
size_t count = _asset->getMorphTargetCountAt(e);
|
||||
for(int j=0; j< count; j++) {
|
||||
const char* morphName = _asset->getMorphTargetNameAt(e, j);
|
||||
for (int j = 0; j < count; j++)
|
||||
{
|
||||
const char *morphName = _asset->getMorphTargetNameAt(e, j);
|
||||
names->push_back(morphName);
|
||||
}
|
||||
break;
|
||||
@@ -446,39 +602,66 @@ namespace polyvox
|
||||
return names;
|
||||
}
|
||||
|
||||
void FilamentViewer::loadSkybox(const char *const skyboxPath, const char *const iblPath)
|
||||
void FilamentViewer::loadSkybox(const char *const skyboxPath)
|
||||
{
|
||||
if (!skyboxPath)
|
||||
{
|
||||
_scene->setSkybox(nullptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
ResourceBuffer skyboxBuffer = _loadResource(skyboxPath);
|
||||
|
||||
ResourceBuffer skyboxBuffer = _loadResource(skyboxPath);
|
||||
image::Ktx1Bundle *skyboxBundle =
|
||||
new image::Ktx1Bundle(static_cast<const uint8_t *>(skyboxBuffer.data), static_cast<uint32_t>(skyboxBuffer.size));
|
||||
_skyboxTexture = ktxreader::Ktx1Reader::createTexture(_engine, skyboxBundle, false);
|
||||
_skybox = filament::Skybox::Builder().environment(_skyboxTexture).build(*_engine);
|
||||
|
||||
image::Ktx1Bundle *skyboxBundle =
|
||||
new image::Ktx1Bundle(static_cast<const uint8_t *>(skyboxBuffer.data), static_cast<uint32_t>(skyboxBuffer.size));
|
||||
_skyboxTexture = ktxreader::Ktx1Reader::createTexture(_engine, skyboxBundle, false);
|
||||
_skybox = filament::Skybox::Builder().environment(_skyboxTexture).build(*_engine);
|
||||
_scene->setSkybox(_skybox);
|
||||
_freeResource(skyboxBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
_scene->setSkybox(_skybox);
|
||||
_freeResource(skyboxBuffer);
|
||||
void FilamentViewer::removeSkybox()
|
||||
{
|
||||
_scene->setSkybox(nullptr);
|
||||
}
|
||||
|
||||
Log("Loading IBL from %s", iblPath);
|
||||
void FilamentViewer::removeIbl()
|
||||
{
|
||||
_scene->setIndirectLight(nullptr);
|
||||
}
|
||||
|
||||
// Load IBL.
|
||||
ResourceBuffer iblBuffer = _loadResource(iblPath);
|
||||
void FilamentViewer::loadIbl(const char *const iblPath)
|
||||
{
|
||||
if (!iblPath)
|
||||
{
|
||||
_scene->setIndirectLight(nullptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
image::Ktx1Bundle *iblBundle = new image::Ktx1Bundle(
|
||||
static_cast<const uint8_t *>(iblBuffer.data), static_cast<uint32_t>(iblBuffer.size));
|
||||
math::float3 harmonics[9];
|
||||
iblBundle->getSphericalHarmonics(harmonics);
|
||||
_iblTexture = ktxreader::Ktx1Reader::createTexture(_engine, iblBundle, false);
|
||||
_indirectLight = IndirectLight::Builder()
|
||||
.reflections(_iblTexture)
|
||||
.irradiance(3, harmonics)
|
||||
.intensity(30000.0f)
|
||||
.build(*_engine);
|
||||
_scene->setIndirectLight(_indirectLight);
|
||||
Log("Loading IBL from %s", iblPath);
|
||||
|
||||
_freeResource(iblBuffer);
|
||||
// Load IBL.
|
||||
ResourceBuffer iblBuffer = _loadResource(iblPath);
|
||||
|
||||
Log("Skybox/IBL load complete.");
|
||||
image::Ktx1Bundle *iblBundle = new image::Ktx1Bundle(
|
||||
static_cast<const uint8_t *>(iblBuffer.data), static_cast<uint32_t>(iblBuffer.size));
|
||||
math::float3 harmonics[9];
|
||||
iblBundle->getSphericalHarmonics(harmonics);
|
||||
_iblTexture = ktxreader::Ktx1Reader::createTexture(_engine, iblBundle, false);
|
||||
_indirectLight = IndirectLight::Builder()
|
||||
.reflections(_iblTexture)
|
||||
.irradiance(3, harmonics)
|
||||
.intensity(30000.0f)
|
||||
.build(*_engine);
|
||||
_scene->setIndirectLight(_indirectLight);
|
||||
|
||||
_freeResource(iblBuffer);
|
||||
|
||||
Log("Skybox/IBL load complete.");
|
||||
}
|
||||
}
|
||||
|
||||
void FilamentViewer::transformToUnitCube()
|
||||
@@ -514,13 +697,13 @@ namespace polyvox
|
||||
Log("Not ready for rendering");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
mtx.lock();
|
||||
if(_asset) {
|
||||
if (_asset)
|
||||
{
|
||||
updateMorphAnimation();
|
||||
updateEmbeddedAnimation();
|
||||
}
|
||||
|
||||
|
||||
math::float3 eye, target, upward;
|
||||
manipulator->getLookAt(&eye, &target, &upward);
|
||||
@@ -532,7 +715,7 @@ namespace polyvox
|
||||
_renderer->render(_view);
|
||||
_renderer->endFrame();
|
||||
}
|
||||
mtx.unlock();
|
||||
mtx.unlock();
|
||||
}
|
||||
|
||||
void FilamentViewer::updateViewportAndCameraProjection(int width, int height, float contentScaleFactor)
|
||||
@@ -561,11 +744,13 @@ namespace polyvox
|
||||
|
||||
void FilamentViewer::updateMorphAnimation()
|
||||
{
|
||||
if(!_morphAnimationBuffer) {
|
||||
if (!_morphAnimationBuffer)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_morphAnimationBuffer->frameIndex == -1) {
|
||||
if (_morphAnimationBuffer->frameIndex == -1)
|
||||
{
|
||||
_morphAnimationBuffer->frameIndex++;
|
||||
_morphAnimationBuffer->startTime = high_resolution_clock::now();
|
||||
applyWeights(_morphAnimationBuffer->frameData, _morphAnimationBuffer->numWeights);
|
||||
@@ -580,7 +765,9 @@ namespace polyvox
|
||||
duration<double, std::milli> dur = high_resolution_clock::now() - _morphAnimationBuffer->startTime;
|
||||
Log("Morph animation completed in %f ms (%d frames at framerate %f), final frame was %d", dur.count(), _morphAnimationBuffer->numFrames, 1000 / _morphAnimationBuffer->frameLengthInMs, _morphAnimationBuffer->frameIndex);
|
||||
_morphAnimationBuffer = nullptr;
|
||||
} else if (frameIndex != _morphAnimationBuffer->frameIndex) {
|
||||
}
|
||||
else if (frameIndex != _morphAnimationBuffer->frameIndex)
|
||||
{
|
||||
Log("Rendering frame %d (of a total %d)", frameIndex, _morphAnimationBuffer->numFrames);
|
||||
_morphAnimationBuffer->frameIndex = frameIndex;
|
||||
auto framePtrOffset = frameIndex * _morphAnimationBuffer->numWeights;
|
||||
@@ -589,43 +776,56 @@ namespace polyvox
|
||||
}
|
||||
}
|
||||
|
||||
void FilamentViewer::playAnimation(int index, bool loop) {
|
||||
if(index > _animator->getAnimationCount() - 1) {
|
||||
void FilamentViewer::playAnimation(int index, bool loop)
|
||||
{
|
||||
if (index > _animator->getAnimationCount() - 1)
|
||||
{
|
||||
Log("Asset does not contain an animation at index %d", index);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
_embeddedAnimationBuffer = make_unique<EmbeddedAnimationBuffer>(index, _animator->getAnimationDuration(index), loop);
|
||||
}
|
||||
}
|
||||
|
||||
void FilamentViewer::stopAnimation() {
|
||||
void FilamentViewer::stopAnimation()
|
||||
{
|
||||
// TODO - does this need to be threadsafe?
|
||||
_embeddedAnimationBuffer = nullptr;
|
||||
}
|
||||
|
||||
void FilamentViewer::updateEmbeddedAnimation() {
|
||||
if(!_embeddedAnimationBuffer) {
|
||||
void FilamentViewer::updateEmbeddedAnimation()
|
||||
{
|
||||
if (!_embeddedAnimationBuffer)
|
||||
{
|
||||
return;
|
||||
}
|
||||
duration<double> dur = duration_cast<duration<double>>(high_resolution_clock::now() - _embeddedAnimationBuffer->lastTime);
|
||||
float startTime = 0;
|
||||
if(!_embeddedAnimationBuffer->hasStarted) {
|
||||
if (!_embeddedAnimationBuffer->hasStarted)
|
||||
{
|
||||
_embeddedAnimationBuffer->hasStarted = true;
|
||||
_embeddedAnimationBuffer->lastTime = high_resolution_clock::now();
|
||||
} else if(dur.count() >= _embeddedAnimationBuffer->duration) {
|
||||
if(_embeddedAnimationBuffer->loop) {
|
||||
}
|
||||
else if (dur.count() >= _embeddedAnimationBuffer->duration)
|
||||
{
|
||||
if (_embeddedAnimationBuffer->loop)
|
||||
{
|
||||
_embeddedAnimationBuffer->lastTime = high_resolution_clock::now();
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
_embeddedAnimationBuffer = nullptr;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
startTime = dur.count();
|
||||
}
|
||||
|
||||
_animator->applyAnimation(_embeddedAnimationBuffer->animationIndex, startTime);
|
||||
_animator->updateBoneMatrices();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -93,8 +93,11 @@ namespace polyvox {
|
||||
FilamentViewer(void* layer, LoadResource loadResource, FreeResource freeResource);
|
||||
~FilamentViewer();
|
||||
|
||||
void loadSkybox(const char* const skyboxUri, const char* const iblUri);
|
||||
void loadSkybox(const char* const skyboxUri);
|
||||
void removeSkybox();
|
||||
|
||||
void loadIbl(const char* const iblUri);
|
||||
void removeIbl();
|
||||
|
||||
void loadGlb(const char* const uri);
|
||||
void loadGltf(const char* const uri, const char* relativeResourcePath);
|
||||
@@ -137,8 +140,10 @@ namespace polyvox {
|
||||
|
||||
Renderer* getRenderer();
|
||||
|
||||
void setBackgroundImage(const char* resourcePath);
|
||||
|
||||
private:
|
||||
|
||||
void createImageRenderable();
|
||||
void loadResources(std::string relativeResourcePath);
|
||||
void transformToUnitCube();
|
||||
void cleanup();
|
||||
@@ -187,6 +192,16 @@ namespace polyvox {
|
||||
unique_ptr<MorphAnimationBuffer> _morphAnimationBuffer;
|
||||
unique_ptr<EmbeddedAnimationBuffer> _embeddedAnimationBuffer;
|
||||
|
||||
Texture* _imageTexture = nullptr;
|
||||
Entity* _imageEntity = nullptr;
|
||||
VertexBuffer* _imageVb = nullptr;
|
||||
IndexBuffer* _imageIb = nullptr;
|
||||
Material* _imageMaterial = nullptr;
|
||||
TextureSampler _imageSampler;
|
||||
|
||||
ColorGrading *colorGrading = nullptr;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
91
ios/src/HDRLoader.cpp
Normal file
91
ios/src/HDRLoader.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (C) 2021 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include <filament/Engine.h>
|
||||
#include <filament/Texture.h>
|
||||
|
||||
#include <imageio/HDRDecoder.h>
|
||||
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "common/NioUtils.h"
|
||||
|
||||
using namespace filament;
|
||||
using namespace image;
|
||||
using namespace utils;
|
||||
|
||||
using PixelBufferDescriptor = Texture::PixelBufferDescriptor;
|
||||
|
||||
jlong nCreateHDRTexture(JNIEnv* env, jclass,
|
||||
jlong nativeEngine, jobject javaBuffer, jint remaining, jint internalFormat) {
|
||||
slog.e << "Creating HDR texture." << io::endl;
|
||||
Engine* engine = (Engine*) nativeEngine;
|
||||
AutoBuffer buffer(env, javaBuffer, remaining);
|
||||
Texture::InternalFormat textureFormat = (Texture::InternalFormat) internalFormat;
|
||||
|
||||
auto dataPtr = (char const*) buffer.getData();
|
||||
const size_t byteCount = buffer.getSize();
|
||||
|
||||
// This creates a copy but it's the easest way to create a memory stream.
|
||||
std::string ins(dataPtr, byteCount);
|
||||
std::istringstream in(ins);
|
||||
|
||||
LinearImage* image = new LinearImage(ImageDecoder::decode(in, "memory.hdr"));
|
||||
|
||||
// This can happen if a decoding error occurs.
|
||||
if (image->getChannels() != 3) {
|
||||
delete image;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Texture* texture = Texture::Builder()
|
||||
.width(image->getWidth())
|
||||
.height(image->getHeight())
|
||||
.levels(0xff)
|
||||
.sampler(Texture::Sampler::SAMPLER_2D)
|
||||
.format(textureFormat)
|
||||
.build(*engine);
|
||||
|
||||
if (texture == nullptr) {
|
||||
slog.e << "Unable to create Filament Texture from HDR image." << io::endl;
|
||||
delete image;
|
||||
return 0;
|
||||
}
|
||||
|
||||
PixelBufferDescriptor::Callback freeCallback = [](void* buf, size_t, void* userdata) {
|
||||
delete (LinearImage*) userdata;
|
||||
};
|
||||
|
||||
PixelBufferDescriptor pbd(
|
||||
(void const* ) image->getPixelRef(),
|
||||
image->getWidth() * image->getHeight() * 3 * sizeof(float),
|
||||
PixelBufferDescriptor::PixelDataFormat::RGB,
|
||||
PixelBufferDescriptor::PixelDataType::FLOAT,
|
||||
freeCallback,
|
||||
image);
|
||||
|
||||
// Note that the setImage call could fail (e.g. due to an invalid combination of internal format
|
||||
// and PixelDataFormat) but there is no way of detecting such a failure.
|
||||
texture->setImage(*engine, 0, std::move(pbd));
|
||||
|
||||
texture->generateMipmaps(*engine);
|
||||
|
||||
return (jlong) texture;
|
||||
}
|
||||
160
ios/src/Utils.cpp
Normal file
160
ios/src/Utils.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include <filament/Engine.h>
|
||||
#include <filament/IndirectLight.h>
|
||||
#include <filament/Skybox.h>
|
||||
|
||||
#include <image/KtxUtility.h>
|
||||
|
||||
#include <android/asset_manager.h>
|
||||
#include <android/asset_manager_jni.h>
|
||||
|
||||
#include "common/NioUtils.h"
|
||||
|
||||
#include <android/log.h>
|
||||
|
||||
using namespace filament;
|
||||
using namespace filament::math;
|
||||
using namespace image;
|
||||
|
||||
jlong nCreateHDRTexture(JNIEnv* env, jclass,
|
||||
jlong nativeEngine, jobject javaBuffer, jint remaining, jint internalFormat);
|
||||
|
||||
static jlong nCreateKTXTexture(JNIEnv* env, jclass,
|
||||
jlong nativeEngine, jobject javaBuffer, jint remaining, jboolean srgb) {
|
||||
Engine* engine = (Engine*) nativeEngine;
|
||||
AutoBuffer buffer(env, javaBuffer, remaining);
|
||||
KtxBundle* bundle = new KtxBundle((const uint8_t*) buffer.getData(), buffer.getSize());
|
||||
return (jlong) ktx::createTexture(engine, *bundle, srgb, [](void* userdata) {
|
||||
KtxBundle* bundle = (KtxBundle*) userdata;
|
||||
delete bundle;
|
||||
}, bundle);
|
||||
}
|
||||
|
||||
static jlong nCreateIndirectLight(JNIEnv* env, jclass,
|
||||
jlong nativeEngine, jobject javaBuffer, jint remaining, jboolean srgb) {
|
||||
Engine* engine = (Engine*) nativeEngine;
|
||||
AutoBuffer buffer(env, javaBuffer, remaining);
|
||||
KtxBundle* bundle = new KtxBundle((const uint8_t*) buffer.getData(), buffer.getSize());
|
||||
Texture* cubemap = ktx::createTexture(engine, *bundle, srgb, [](void* userdata) {
|
||||
KtxBundle* bundle = (KtxBundle*) userdata;
|
||||
delete bundle;
|
||||
}, bundle);
|
||||
|
||||
float3 harmonics[9];
|
||||
bundle->getSphericalHarmonics(harmonics);
|
||||
|
||||
IndirectLight* indirectLight = IndirectLight::Builder()
|
||||
.reflections(cubemap)
|
||||
.irradiance(3, harmonics)
|
||||
.intensity(30000)
|
||||
.build(*engine);
|
||||
|
||||
return (jlong) indirectLight;
|
||||
}
|
||||
|
||||
static jlong nCreateSkybox(JNIEnv* env, jclass,
|
||||
jlong nativeEngine, jobject javaBuffer, jint remaining, jboolean srgb, jobject assetManager, jstring outpath) {
|
||||
|
||||
AAssetManager *mgr = AAssetManager_fromJava(env, assetManager);
|
||||
|
||||
AAsset *asset = AAssetManager_open(mgr, "envs/default_env/default_env_skybox.ktx", AASSET_MODE_BUFFER);
|
||||
if(asset == nullptr) {
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Couldn't open asset");
|
||||
return 0;
|
||||
}
|
||||
|
||||
off_t length = AAsset_getLength(asset);
|
||||
const void * buffer = AAsset_getBuffer(asset);
|
||||
jboolean isCopy = (jboolean)false;
|
||||
const char* out_cstr = env->GetStringUTFChars(outpath, &isCopy);
|
||||
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Opening outfile %s for writing", out_cstr);
|
||||
|
||||
FILE* outfile = fopen(out_cstr, "w");
|
||||
|
||||
fwrite(buffer, 1, length, outfile);
|
||||
|
||||
fclose(outfile);
|
||||
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Closed outfile %s", out_cstr);
|
||||
|
||||
Engine* engine = (Engine*) nativeEngine;
|
||||
// __android_log_print(ANDROID_LOG_VERBOSE, "UTILS", "CREATing autobuffer");
|
||||
// AutoBuffer buffer(env, javaBuffer, remaining);
|
||||
// __android_log_print(ANDROID_LOG_VERBOSE, "UTILS", "CREATied autobuffer");
|
||||
|
||||
KtxBundle* bundle = new KtxBundle((const uint8_t*) buffer, length);
|
||||
// KtxBundle* bundle = new KtxBundle((const uint8_t*) buffer.getData(), buffer.getSize());
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "UTILS", "CREATED BUNDLE FROM API");
|
||||
|
||||
|
||||
Texture* cubemap = ktx::createTexture(engine, *bundle, srgb, [](void* userdata) {
|
||||
KtxBundle* bundle = (KtxBundle*) userdata;
|
||||
delete bundle;
|
||||
}, bundle);
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "UTILS", "CREATED TEXTURE");
|
||||
return (jlong) Skybox::Builder().environment(cubemap).showSun(true).build(*engine);
|
||||
}
|
||||
|
||||
static jboolean nGetSphericalHarmonics(JNIEnv* env, jclass, jobject javaBuffer, jint remaining,
|
||||
jfloatArray outSphericalHarmonics_) {
|
||||
AutoBuffer buffer(env, javaBuffer, remaining);
|
||||
KtxBundle bundle((const uint8_t*) buffer.getData(), buffer.getSize());
|
||||
|
||||
jfloat* outSphericalHarmonics = env->GetFloatArrayElements(outSphericalHarmonics_, nullptr);
|
||||
const auto success = bundle.getSphericalHarmonics(
|
||||
reinterpret_cast<filament::math::float3*>(outSphericalHarmonics)
|
||||
);
|
||||
env->ReleaseFloatArrayElements(outSphericalHarmonics_, outSphericalHarmonics, JNI_ABORT);
|
||||
|
||||
return success ? JNI_TRUE : JNI_FALSE;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
|
||||
JNIEnv* env;
|
||||
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int rc;
|
||||
|
||||
// KTXLoader
|
||||
jclass ktxloaderClass = env->FindClass("app/polyvox/filament/KTXLoader2");
|
||||
if (ktxloaderClass == nullptr) return JNI_ERR;
|
||||
static const JNINativeMethod ktxMethods[] = {
|
||||
{(char*)"nCreateKTXTexture", (char*)"(JLjava/nio/Buffer;IZ)J", reinterpret_cast<void*>(nCreateKTXTexture)},
|
||||
{(char*)"nCreateIndirectLight", (char*)"(JLjava/nio/Buffer;IZ)J", reinterpret_cast<void*>(nCreateIndirectLight)},
|
||||
{(char*)"nCreateSkybox", (char*)"(JLjava/nio/Buffer;IZLandroid/content/res/AssetManager;Ljava/lang/String;)J", reinterpret_cast<void*>(nCreateSkybox)},
|
||||
{(char*)"nGetSphericalHarmonics", (char*)"(Ljava/nio/Buffer;I[F)Z", reinterpret_cast<void*>(nGetSphericalHarmonics)},
|
||||
};
|
||||
rc = env->RegisterNatives(ktxloaderClass, ktxMethods, sizeof(ktxMethods) / sizeof(JNINativeMethod));
|
||||
if (rc != JNI_OK) return rc;
|
||||
|
||||
// HDRLoader
|
||||
jclass hdrloaderClass = env->FindClass("com/google/android/filament/utils/HDRLoader");
|
||||
if (hdrloaderClass == nullptr) return JNI_ERR;
|
||||
static const JNINativeMethod hdrMethods[] = {
|
||||
{(char*)"nCreateHDRTexture", (char*)"(JLjava/nio/Buffer;II)J", reinterpret_cast<void*>(nCreateHDRTexture)},
|
||||
};
|
||||
rc = env->RegisterNatives(hdrloaderClass, hdrMethods, sizeof(hdrMethods) / sizeof(JNINativeMethod));
|
||||
if (rc != JNI_OK) return rc;
|
||||
|
||||
return JNI_VERSION_1_6;
|
||||
}
|
||||
BIN
ios/src/materials/image.filamat
Normal file
BIN
ios/src/materials/image.filamat
Normal file
Binary file not shown.
54
ios/src/materials/image.mat
Normal file
54
ios/src/materials/image.mat
Normal file
@@ -0,0 +1,54 @@
|
||||
material {
|
||||
name : Image,
|
||||
parameters : [
|
||||
{
|
||||
type : sampler2d,
|
||||
name : image
|
||||
},
|
||||
{
|
||||
type : mat3,
|
||||
name : transform,
|
||||
precision : high
|
||||
},
|
||||
{
|
||||
type : float3,
|
||||
name : backgroundColor
|
||||
},
|
||||
{
|
||||
type : int,
|
||||
name : showImage
|
||||
}
|
||||
],
|
||||
variables : [
|
||||
imageUV
|
||||
],
|
||||
vertexDomain : device,
|
||||
depthWrite : false,
|
||||
shadingModel : unlit,
|
||||
variantFilter : [ skinning, shadowReceiver, vsm ],
|
||||
culling: none
|
||||
}
|
||||
|
||||
vertex {
|
||||
void materialVertex(inout MaterialVertexInputs material) {
|
||||
material.imageUV.st = getPosition().st * 0.5 + 0.5;
|
||||
}
|
||||
}
|
||||
|
||||
fragment {
|
||||
void material(inout MaterialInputs material) {
|
||||
prepareMaterial(material);
|
||||
|
||||
vec4 bg = vec4(materialParams.backgroundColor, 1.0);
|
||||
highp vec2 uv = (materialParams.transform * vec3(saturate(variable_imageUV.st), 1.0)).st;
|
||||
if (materialParams.showImage == 0 || uv.s > 1.0 || uv.s < 0.0 || uv.t < 0.0 || uv.t > 1.0) {
|
||||
material.baseColor = bg;
|
||||
} else {
|
||||
uv.t = 1.0 - uv.t;
|
||||
vec4 color = max(texture(materialParams_image, uv.st), 0.0);
|
||||
color.rgb *= color.a;
|
||||
// Manual, pre-multiplied srcOver with opaque destination optimization
|
||||
material.baseColor.rgb = color.rgb + bg.rgb * (1.0 - color.a);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user