fix camera manipulator/gesture detector, add explicit functions to set camera pos/rot, add unlitshader material provider

This commit is contained in:
Nick Fisher
2022-09-01 13:03:15 +10:00
parent 6280ea5f57
commit b0ff3457f0
13 changed files with 2085 additions and 118 deletions

View File

@@ -69,6 +69,7 @@
#include "image/imagematerials_ios.h"
#else
#include "image/imagematerial.h"
#include "shaders/unlitopaque.h"
#endif
#include "FilamentViewer.hpp"
#include "StreamBufferAdapter.hpp"
@@ -86,6 +87,48 @@ class LightManager;
namespace polyvox {
class UnlitMaterialProvider : public MaterialProvider {
const Material* _m;
const Material* _ms[1];
public:
UnlitMaterialProvider(Engine* engine) {
_m = Material::Builder()
.package(UNLITOPAQUE_UNLIT_OPAQUE_DATA, UNLITOPAQUE_UNLIT_OPAQUE_SIZE)
.build(*engine);
_ms[0] = _m;
}
filament::MaterialInstance* createMaterialInstance(MaterialKey* config, UvMap* uvmap,
const char* label = "material", const char* extras = nullptr) {
MaterialInstance* d = (MaterialInstance*)_m->getDefaultInstance();
return d;
}
/**
* Gets a weak reference to the array of cached materials.
*/
const filament::Material* const* getMaterials() const noexcept {
return _ms;
}
/**
* Gets the number of cached materials.
*/
size_t getMaterialsCount() const noexcept {
return (size_t)1;
}
void destroyMaterials() {
}
bool needsDummyData(filament::VertexAttribute attrib) const noexcept {
return true;
}
};
const double kNearPlane = 0.05; // 5 cm
const double kFarPlane = 1000.0; // 1 km
const float kScaleMultiplier = 100.0f;
@@ -160,8 +203,10 @@ FilamentViewer::FilamentViewer(void *layer, LoadResource loadResource,
_view->setMultiSampleAntiAliasingOptions(multiSampleAntiAliasingOptions);
_materialProvider = gltfio::createUbershaderProvider(
_engine, UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE);
_materialProvider =
// new UnlitMaterialProvider(_engine);
gltfio::createUbershaderProvider(
_engine, UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE);
EntityManager &em = EntityManager::get();
_ncm = new NameComponentManager(em);
@@ -172,10 +217,6 @@ FilamentViewer::FilamentViewer(void *layer, LoadResource loadResource,
_stbDecoder = createStbProvider(_engine);
_resourceLoader->addTextureProvider("image/png", _stbDecoder);
_resourceLoader->addTextureProvider("image/jpeg", _stbDecoder);
manipulator = Manipulator<float>::Builder()
.orbitHomePosition(0.0f, 0.0f, 1.0f)
.targetPosition(0.0f, 0.0f, 0.0f)
.build(Mode::ORBIT);
// Always add a direct light source since it is required for shadowing.
_sun = EntityManager::get().create();
@@ -406,6 +447,11 @@ void FilamentViewer::clearAssets() {
if(_mainCamera) {
_view->setCamera(_mainCamera);
}
if(_manipulator) {
delete _manipulator;
_manipulator = nullptr;
}
int i = 0;
for (auto asset : _assets) {
@@ -579,21 +625,23 @@ void FilamentViewer::render() {
return;
}
// mtx.lock();
for (auto &asset : _assets) {
asset->updateAnimations();
}
math::float3 eye, target, upward;
manipulator->getLookAt(&eye, &target, &upward);
_mainCamera->lookAt(eye, target, upward);
if(_manipulator) {
math::float3 eye, target, upward;
Camera& cam =_view->getCamera();
_manipulator->getLookAt(&eye, &target, &upward);
cam.lookAt(eye, target, upward);
}
// Render the scene, unless the renderer wants to skip the frame.
if (_renderer->beginFrame(_swapChain)) {
_renderer->render(_view);
_renderer->endFrame();
}
// mtx.unlock();
}
void FilamentViewer::updateViewportAndCameraProjection(
@@ -622,11 +670,57 @@ void FilamentViewer::updateViewportAndCameraProjection(
void FilamentViewer::setCameraPosition(float x, float y, float z) {
Camera& cam =_view->getCamera();
auto &tm = _engine->getTransformManager();
// tm.setTransform(tm.getInstance(_asset->getRoot()), math::mat4f::translation(math::float3(x,y,z)) * _cameraRotation);
_cameraPosition = math::mat4f::translation(math::float3(x,y,z));
cam.setModelMatrix(_cameraPosition * _cameraRotation);
}
void FilamentViewer::setCameraRotation(float rads, float x, float y, float z) {
Camera& cam =_view->getCamera();
auto &tm = _engine->getTransformManager();
_cameraRotation = math::mat4f::rotation(rads, math::float3(x,y,z));
cam.setModelMatrix(_cameraPosition * _cameraRotation);
}
void FilamentViewer::grabBegin(float x, float y, bool pan) {
if(!_manipulator) {
Camera& cam =_view->getCamera();
math::float3 home = cam.getPosition();
math::float3 fv = cam.getForwardVector();
Viewport const& vp = _view->getViewport();
_manipulator = Manipulator<float>::Builder()
.viewport(vp.width, vp.height)
.orbitHomePosition(home[0], home[1], home[2])
.targetPosition(fv[0], fv[1], fv[2])
.build(Mode::ORBIT);
Log("Created manipualtor for vp width %d height %d ", vp.width, vp.height);
} else {
// Log("Error - calling grabBegin while another grab session is active. This will probably cause weirdness");
}
_manipulator->grabBegin(x, y, pan);
}
void FilamentViewer::grabUpdate(float x, float y) {
if(_manipulator) {
Log("grab update %f %f", x, y);
_manipulator->grabUpdate(x, y);
} else {
Log("Error - trying to use a manipulator when one is not available. Ensure you call grabBegin before grabUpdate/grabEnd");
}
}
void FilamentViewer::grabEnd() {
if(_manipulator) {
_manipulator->grabEnd();
// delete _manipulator;
} else {
Log("Error - trying to use a manipulator when one is not available. Ensure you call grabBegin before grabUpdate/grabEnd");
}
}
void FilamentViewer::scroll(float x, float y, float delta) {
if(_manipulator) {
_manipulator->scroll(x, y, delta);
}
}
} // namespace polyvox

View File

@@ -62,8 +62,6 @@ namespace polyvox {
void updateViewportAndCameraProjection(int height, int width, float scaleFactor);
void render();
Manipulator<float>* manipulator;
bool setFirstCamera(SceneAsset* asset);
bool setCamera(SceneAsset* asset, const char* nodeName);
@@ -79,6 +77,11 @@ namespace polyvox {
void setCameraFocalLength(float fl);
void setCameraFocusDistance(float focusDistance);
void grabBegin(float x, float y, bool pan);
void grabUpdate(float x, float y);
void grabEnd();
void scroll(float x, float y, float delta);
private:
void createImageRenderable();
void loadResources(std::string relativeResourcePath);
@@ -87,6 +90,10 @@ namespace polyvox {
void* _layer;
Manipulator<float>* _manipulator;
math::mat4f _cameraPosition;
math::mat4f _cameraRotation;
LoadResource _loadResource;
FreeResource _freeResource;

View File

@@ -87,21 +87,21 @@ extern "C" {
}
void scroll(void* viewer, float x, float y , float z) {
return ((FilamentViewer*)viewer)->manipulator->scroll(x, y, z);
void scroll(void* viewer, float x, float y, float delta) {
return ((FilamentViewer*)viewer)->scroll(x, y, delta);
}
void grab_begin(void* viewer, int x, int y, bool pan) {
((FilamentViewer*)viewer)->manipulator->grabBegin(x, y, pan);
void grab_begin(void* viewer, float x, float y, bool pan) {
((FilamentViewer*)viewer)->grabBegin(x, y, pan);
}
void grab_update(void* viewer, int x, int y) {
((FilamentViewer*)viewer)->manipulator->grabUpdate(x, y);
void grab_update(void* viewer, float x, float y) {
((FilamentViewer*)viewer)->grabUpdate(x, y);
}
void grab_end(void* viewer) {
((FilamentViewer*)viewer)->manipulator->grabEnd();
((FilamentViewer*)viewer)->grabEnd();
}
void apply_weights(void* asset, float* const weights, int count) {
@@ -158,14 +158,18 @@ extern "C" {
((SceneAsset*)asset)->transformToUnitCube();
}
void set_position(void* asset, float x, float y, float z) {
void set_position(void* asset, float x, float y, float z) {
((SceneAsset*)asset)->setPosition(x, y, z);
}
}
void set_rotation(void* asset, float rads, float x, float y, float z) {
((SceneAsset*)asset)->setRotation(rads, x, y, z);
}
void set_scale(void* asset, float scale) {
((SceneAsset*)asset)->setScale(scale);
}
void stop_animation(void* asset, int index) {
((SceneAsset*)asset)->stopAnimation(index);
}

View File

@@ -128,7 +128,7 @@ void SceneAsset::loadTexture(const char* resourcePath, int renderableIndex) {
Log("Loading texture at %s for renderableIndex %d", resourcePath, renderableIndex);
string rp("flutter_assets/assets/background.png");
string rp(resourcePath);
if(_texture) {
_engine->destroy(_texture);
@@ -161,8 +161,6 @@ void SceneAsset::loadTexture(const char* resourcePath, int renderableIndex) {
.sampler(Texture::Sampler::SAMPLER_2D)
.build(*_engine);
Log("build texture");
Texture::PixelBufferDescriptor::Callback freeCallback = [](void *buf, size_t,
void *data) {
delete reinterpret_cast<LinearImage *>(data);
@@ -174,7 +172,6 @@ void SceneAsset::loadTexture(const char* resourcePath, int renderableIndex) {
Texture::Type::FLOAT, freeCallback);
_texture->setImage(*_engine, 0, std::move(buffer));
Log("set image");
setTexture();
delete inputStream;
@@ -290,32 +287,28 @@ void SceneAsset::transformToUnitCube() {
tm.setTransform(tm.getInstance(_asset->getRoot()), transform);
}
void SceneAsset::setPosition(float x, float y, float z) {
Log("Setting position to %f %f %f", x, y, z);
void SceneAsset::updateTransform() {
auto &tm = _engine->getTransformManager();
_position = math::mat4f::translation(math::float3(x,y,z));
auto aabb = _asset->getBoundingBox();
auto center = aabb.center();
auto halfExtent = aabb.extent();
auto maxExtent = max(halfExtent) * 2;
auto scaleFactor = 2.0f / maxExtent;
auto transform =
math::mat4f::scaling(scaleFactor) * math::mat4f::translation(-center) * _position * _rotation;
auto transform =
math::mat4f::scaling(_scale) * _position * _rotation;
tm.setTransform(tm.getInstance(_asset->getRoot()), transform);
}
void SceneAsset::setScale(float scale) {
_scale = scale;
updateTransform();
}
void SceneAsset::setPosition(float x, float y, float z) {
Log("Setting position to %f %f %f", x, y, z);
_position = math::mat4f::translation(math::float3(x,y,z));
updateTransform();
}
void SceneAsset::setRotation(float rads, float x, float y, float z) {
Log("Rotating %f radians around axis %f %f %f", x, y, z);
auto &tm = _engine->getTransformManager();
Log("Rotating %f radians around axis %f %f %f", rads, x, y, z);
_rotation = math::mat4f::rotation(rads, math::float3(x,y,z));
auto aabb = _asset->getBoundingBox();
auto center = aabb.center();
auto halfExtent = aabb.extent();
auto maxExtent = max(halfExtent) * 2;
auto scaleFactor = 2.0f / maxExtent;
auto transform =
math::mat4f::scaling(scaleFactor) * math::mat4f::translation(-center) * _position * _rotation;
tm.setTransform(tm.getInstance(_asset->getRoot()), transform);
updateTransform();
}

View File

@@ -71,6 +71,8 @@ namespace polyvox {
void transformToUnitCube();
void setScale(float scale);
void setPosition(float x, float y, float z);
void setRotation(float rads, float x, float y, float z);
@@ -108,6 +110,9 @@ namespace polyvox {
math::mat4f _position;
math::mat4f _rotation;
float _scale = 1;
void updateTransform();
};
}

View File

@@ -0,0 +1,12 @@
.global UNLITOPAQUE_UNLIT_OPAQUE_OFFSET;
.global UNLITOPAQUE_UNLIT_OPAQUE_SIZE;
.global UNLITOPAQUE_PACKAGE
.section .rodata
UNLITOPAQUE_PACKAGE:
.incbin "unlitopaque.bin"
UNLITOPAQUE_UNLIT_OPAQUE_OFFSET:
.int 0
UNLITOPAQUE_UNLIT_OPAQUE_SIZE:
.int 35615

View File

@@ -0,0 +1,12 @@
.global _UNLITOPAQUE_UNLIT_OPAQUE_OFFSET;
.global _UNLITOPAQUE_UNLIT_OPAQUE_SIZE;
.global _UNLITOPAQUE_PACKAGE
.section __TEXT,__const
_UNLITOPAQUE_PACKAGE:
.incbin "unlitopaque.bin"
_UNLITOPAQUE_UNLIT_OPAQUE_OFFSET:
.int 0
_UNLITOPAQUE_UNLIT_OPAQUE_SIZE:
.int 35615

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,13 @@
#ifndef UNLITOPAQUE_H_
#define UNLITOPAQUE_H_
#include <stdint.h>
extern "C" {
extern const uint8_t UNLITOPAQUE_PACKAGE[];
extern int UNLITOPAQUE_UNLIT_OPAQUE_OFFSET;
extern int UNLITOPAQUE_UNLIT_OPAQUE_SIZE;
}
#define UNLITOPAQUE_UNLIT_OPAQUE_DATA (UNLITOPAQUE_PACKAGE + UNLITOPAQUE_UNLIT_OPAQUE_OFFSET)
#endif