add method for setting material color
This commit is contained in:
@@ -218,6 +218,9 @@ class _ExampleWidgetState extends State<ExampleWidget> {
|
|||||||
.build();
|
.build();
|
||||||
_filamentController.setMorphAnimationData(_cube!, animation);
|
_filamentController.setMorphAnimationData(_cube!, animation);
|
||||||
}, "animate morph weights #3 and #4"),
|
}, "animate morph weights #3 and #4"),
|
||||||
|
_item(() {
|
||||||
|
_filamentController.setMaterialColor(_cube!, "Cone", 0, Colors.purple);
|
||||||
|
}, "set cone material color to purple")
|
||||||
];
|
];
|
||||||
if (_animations != null) {
|
if (_animations != null) {
|
||||||
children.addAll(_animations!.map((a) => _item(() {
|
children.addAll(_animations!.map((a) => _item(() {
|
||||||
|
|||||||
@@ -615,7 +615,18 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
|
|||||||
case "setCameraFocusDistance":
|
case "setCameraFocusDistance":
|
||||||
set_camera_focus_distance(viewer, call.arguments as! Float)
|
set_camera_focus_distance(viewer, call.arguments as! Float)
|
||||||
result(true)
|
result(true)
|
||||||
|
case "setMaterialColor":
|
||||||
|
guard let args = call.arguments as? [Any], args.count == 5,
|
||||||
|
let assetManager = args[0] as? Int64,
|
||||||
|
let asset = args[1] as? EntityId,
|
||||||
|
let meshName = args[2] as? String,
|
||||||
|
let materialIndex = args[3] as? Int32,
|
||||||
|
let color = args[4] as? [Double] else {
|
||||||
|
result(FlutterError(code: "INVALID_ARGUMENTS", message: "Expected correct arguments for setMaterialColor", details: nil))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
set_material_color(unsafeBitCast(assetManager, to:UnsafeMutableRawPointer.self), asset, meshName, materialIndex, Float(color[0]), Float(color[1]), Float(color[2]), Float(color[3]))
|
||||||
|
result(true)
|
||||||
|
|
||||||
case "hideMesh":
|
case "hideMesh":
|
||||||
guard let args = call.arguments as? [Any], args.count == 3,
|
guard let args = call.arguments as? [Any], args.count == 3,
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ namespace polyvox {
|
|||||||
const utils::Entity* getLightEntities(EntityId e) const noexcept;
|
const utils::Entity* getLightEntities(EntityId e) const noexcept;
|
||||||
size_t getLightEntityCount(EntityId e) const noexcept;
|
size_t getLightEntityCount(EntityId e) const noexcept;
|
||||||
void updateAnimations();
|
void updateAnimations();
|
||||||
|
bool setMaterialColor(EntityId e, const char* meshName, int materialInstance, const float r, const float g, const float b, const float a);
|
||||||
|
|
||||||
bool setMorphAnimationBuffer(
|
bool setMorphAnimationBuffer(
|
||||||
EntityId entityId,
|
EntityId entityId,
|
||||||
|
|||||||
@@ -82,6 +82,7 @@ void remove_asset(const void* const viewer, EntityId asset);
|
|||||||
void clear_assets(const void* const viewer);
|
void clear_assets(const void* const viewer);
|
||||||
void load_texture(void* assetManager, EntityId asset, const char *assetPath, int renderableIndex);
|
void load_texture(void* assetManager, EntityId asset, const char *assetPath, int renderableIndex);
|
||||||
void set_texture(void* assetManager, EntityId asset);
|
void set_texture(void* assetManager, EntityId asset);
|
||||||
|
bool set_material_color(void* assetManager, EntityId asset, const char* meshName, int materialIndex, const float r, const float g, const float b, const float a);
|
||||||
void transform_to_unit_cube(void* assetManager, EntityId asset);
|
void transform_to_unit_cube(void* assetManager, EntityId asset);
|
||||||
void set_position(void* assetManager, EntityId asset, float x, float y, float z);
|
void set_position(void* assetManager, EntityId asset, float x, float y, float z);
|
||||||
void set_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z);
|
void set_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z);
|
||||||
|
|||||||
@@ -24,8 +24,8 @@
|
|||||||
#include "gltfio/materials/uberarchive.h"
|
#include "gltfio/materials/uberarchive.h"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "material/image.h"
|
#include "material/image.h"
|
||||||
#include "material/unlit_opaque.h"
|
#include "material/unlit_opaque.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace polyvox {
|
namespace polyvox {
|
||||||
@@ -41,10 +41,10 @@ AssetManager::AssetManager(const ResourceLoaderWrapper* const resourceLoaderWrap
|
|||||||
NameComponentManager *ncm,
|
NameComponentManager *ncm,
|
||||||
Engine *engine,
|
Engine *engine,
|
||||||
Scene *scene)
|
Scene *scene)
|
||||||
: _resourceLoaderWrapper(resourceLoaderWrapper),
|
: _resourceLoaderWrapper(resourceLoaderWrapper),
|
||||||
_ncm(ncm),
|
_ncm(ncm),
|
||||||
_engine(engine),
|
_engine(engine),
|
||||||
_scene(scene) {
|
_scene(scene) {
|
||||||
|
|
||||||
_stbDecoder = createStbProvider(_engine);
|
_stbDecoder = createStbProvider(_engine);
|
||||||
_ktxDecoder = createKtx2Provider(_engine);
|
_ktxDecoder = createKtx2Provider(_engine);
|
||||||
@@ -64,7 +64,7 @@ AssetManager::AssetManager(const ResourceLoaderWrapper* const resourceLoaderWrap
|
|||||||
_gltfResourceLoader->addTextureProvider("image/ktx2", _ktxDecoder);
|
_gltfResourceLoader->addTextureProvider("image/ktx2", _ktxDecoder);
|
||||||
_gltfResourceLoader->addTextureProvider("image/png", _stbDecoder);
|
_gltfResourceLoader->addTextureProvider("image/png", _stbDecoder);
|
||||||
_gltfResourceLoader->addTextureProvider("image/jpeg", _stbDecoder);
|
_gltfResourceLoader->addTextureProvider("image/jpeg", _stbDecoder);
|
||||||
}
|
}
|
||||||
|
|
||||||
AssetManager::~AssetManager() {
|
AssetManager::~AssetManager() {
|
||||||
_gltfResourceLoader->asyncCancelLoad();
|
_gltfResourceLoader->asyncCancelLoad();
|
||||||
@@ -273,11 +273,12 @@ void AssetManager::updateAnimations() {
|
|||||||
frameNumber = lengthInFrames - frameNumber;
|
frameNumber = lengthInFrames - frameNumber;
|
||||||
}
|
}
|
||||||
auto baseOffset = frameNumber * asset.mMorphAnimationBuffer.mMorphIndices.size();
|
auto baseOffset = frameNumber * asset.mMorphAnimationBuffer.mMorphIndices.size();
|
||||||
for(auto morphIndex : asset.mMorphAnimationBuffer.mMorphIndices) {
|
for(int i = 0; i < asset.mMorphAnimationBuffer.mMorphIndices.size(); i++) {
|
||||||
|
auto morphIndex = asset.mMorphAnimationBuffer.mMorphIndices[i];
|
||||||
// set the weights appropriately
|
// set the weights appropriately
|
||||||
rm.setMorphWeights(
|
rm.setMorphWeights(
|
||||||
rm.getInstance(asset.mMorphAnimationBuffer.mMeshTarget),
|
rm.getInstance(asset.mMorphAnimationBuffer.mMeshTarget),
|
||||||
asset.mMorphAnimationBuffer.mFrameData.data() + baseOffset + morphIndex,
|
asset.mMorphAnimationBuffer.mFrameData.data() + baseOffset + i,
|
||||||
1,
|
1,
|
||||||
morphIndex
|
morphIndex
|
||||||
);
|
);
|
||||||
@@ -312,7 +313,7 @@ void AssetManager::updateAnimations() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssetManager::setBoneTransform(SceneAsset& asset, int frameNumber) {
|
void AssetManager::setBoneTransform(SceneAsset& asset, int frameNumber) {
|
||||||
|
|
||||||
RenderableManager& rm = _engine->getRenderableManager();
|
RenderableManager& rm = _engine->getRenderableManager();
|
||||||
|
|
||||||
@@ -459,6 +460,35 @@ bool AssetManager::setMorphAnimationBuffer(
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool AssetManager::setMaterialColor(EntityId entityId, const char* meshName, int materialIndex, const float r, const float g, const float b, const float a) {
|
||||||
|
|
||||||
|
const auto& pos = _entityIdLookup.find(entityId);
|
||||||
|
if(pos == _entityIdLookup.end()) {
|
||||||
|
Log("ERROR: asset not found for entity.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
auto& asset = _assets[pos->second];
|
||||||
|
auto entity = findEntityByName(asset, meshName);
|
||||||
|
|
||||||
|
RenderableManager& rm = _engine->getRenderableManager();
|
||||||
|
|
||||||
|
auto renderable = rm.getInstance(entity);
|
||||||
|
|
||||||
|
if(!renderable.isValid()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
MaterialInstance* mi = rm.getMaterialInstanceAt(renderable, materialIndex);
|
||||||
|
|
||||||
|
if(!mi) {
|
||||||
|
Log("ERROR: material index must be less than number of material instances");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
mi->setParameter("baseColorFactor", RgbaType::sRGB, math::float4(r, g, b, a));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool AssetManager::setBoneAnimationBuffer(
|
bool AssetManager::setBoneAnimationBuffer(
|
||||||
EntityId entityId,
|
EntityId entityId,
|
||||||
const float* const frameData,
|
const float* const frameData,
|
||||||
@@ -557,6 +587,10 @@ bool AssetManager::setBoneAnimationBuffer(
|
|||||||
|
|
||||||
|
|
||||||
void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse, bool replaceActive, float crossfade) {
|
void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse, bool replaceActive, float crossfade) {
|
||||||
|
if(index < 0) {
|
||||||
|
Log("ERROR: glTF animation index must be greater than zero.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
const auto& pos = _entityIdLookup.find(e);
|
const auto& pos = _entityIdLookup.find(e);
|
||||||
if(pos == _entityIdLookup.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
@@ -875,36 +909,36 @@ size_t AssetManager::getLightEntityCount(EntityId entity) const noexcept {
|
|||||||
|
|
||||||
// auto& inverseBindMatrix = filamentInstance->getInverseBindMatricesAt(skinIndex)[mBoneIndex];
|
// auto& inverseBindMatrix = filamentInstance->getInverseBindMatricesAt(skinIndex)[mBoneIndex];
|
||||||
|
|
||||||
// auto globalJointTransform = transformManager.getWorldTransform(jointInstance);
|
// auto globalJointTransform = transformManager.getWorldTransform(jointInstance);
|
||||||
|
|
||||||
// for(auto& target : asset.mBoneAnimationBuffer.mMeshTargets) {
|
// for(auto& target : asset.mBoneAnimationBuffer.mMeshTargets) {
|
||||||
|
|
||||||
// auto inverseGlobalTransform = inverse(
|
// auto inverseGlobalTransform = inverse(
|
||||||
// transformManager.getWorldTransform(
|
// transformManager.getWorldTransform(
|
||||||
// transformManager.getInstance(target)
|
// transformManager.getInstance(target)
|
||||||
// )
|
// )
|
||||||
// );
|
// );
|
||||||
|
|
||||||
// auto boneTransform = inverseGlobalTransform * globalJointTransform * localTransform * inverseBindMatrix;
|
// auto boneTransform = inverseGlobalTransform * globalJointTransform * localTransform * inverseBindMatrix;
|
||||||
// auto renderable = rm.getInstance(target);
|
// auto renderable = rm.getInstance(target);
|
||||||
// rm.setBones(
|
// rm.setBones(
|
||||||
// renderable,
|
// renderable,
|
||||||
// &boneTransform,
|
// &boneTransform,
|
||||||
// 1,
|
// 1,
|
||||||
// mBoneIndex
|
// mBoneIndex
|
||||||
// );
|
// );
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 1.0f, 0.0f, 0.0f, 0.0f,
|
// 1.0f, 0.0f, 0.0f, 0.0f,
|
||||||
// 0.0f, 0.0f, 1.0f, 0.0f,
|
// 0.0f, 0.0f, 1.0f, 0.0f,
|
||||||
// 0.0f, -1.0f, 0.0f, 0.0f,
|
// 0.0f, -1.0f, 0.0f, 0.0f,
|
||||||
// 0.0f, 0.0f, 0.0f, 1.0f
|
// 0.0f, 0.0f, 0.0f, 1.0f
|
||||||
// };
|
// };
|
||||||
// Log("TRANSFORM");
|
// Log("TRANSFORM");
|
||||||
// Log("%f %f %f %f", localTransform[0][0], localTransform[1][0], localTransform[2][0], localTransform[3][0] ) ;
|
// Log("%f %f %f %f", localTransform[0][0], localTransform[1][0], localTransform[2][0], localTransform[3][0] ) ;
|
||||||
// Log("%f %f %f %f", localTransform[0][1], localTransform[1][1], localTransform[2][1], localTransform[3][1] ) ;
|
// Log("%f %f %f %f", localTransform[0][1], localTransform[1][1], localTransform[2][1], localTransform[3][1] ) ;
|
||||||
// Log("%f %f %f %f", localTransform[0][2], localTransform[1][2], localTransform[2][2], localTransform[3][2] ) ;
|
// Log("%f %f %f %f", localTransform[0][2], localTransform[1][2], localTransform[2][2], localTransform[3][2] ) ;
|
||||||
// Log("%f %f %f %f", localTransform[0][3], localTransform[1][3], localTransform[2][3], localTransform[3][3] ) ;
|
// Log("%f %f %f %f", localTransform[0][3], localTransform[1][3], localTransform[2][3], localTransform[3][3] ) ;
|
||||||
// transformManager.getTransform(jointInstance);
|
// transformManager.getTransform(jointInstance);
|
||||||
|
|||||||
@@ -337,6 +337,10 @@ extern "C" {
|
|||||||
// ((AssetManager*)assetManager)->setTexture();
|
// ((AssetManager*)assetManager)->setTexture();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool set_material_color(void* assetManager, EntityId asset, const char* meshName, int materialIndex, const float r, const float g, const float b, const float a) {
|
||||||
|
return ((AssetManager*)assetManager)->setMaterialColor(asset, meshName, materialIndex, r, g, b, a);
|
||||||
|
}
|
||||||
|
|
||||||
FLUTTER_PLUGIN_EXPORT void transform_to_unit_cube(void* assetManager, EntityId asset) {
|
FLUTTER_PLUGIN_EXPORT void transform_to_unit_cube(void* assetManager, EntityId asset) {
|
||||||
((AssetManager*)assetManager)->transformToUnitCube(asset);
|
((AssetManager*)assetManager)->transformToUnitCube(asset);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -260,13 +260,12 @@ class FilamentController {
|
|||||||
///
|
///
|
||||||
void setMorphAnimationData(
|
void setMorphAnimationData(
|
||||||
FilamentEntity asset, MorphAnimationData animation) async {
|
FilamentEntity asset, MorphAnimationData animation) async {
|
||||||
print("SETTING animation with ${animation.data}");
|
|
||||||
await _channel.invokeMethod("setMorphAnimation", [
|
await _channel.invokeMethod("setMorphAnimation", [
|
||||||
_assetManager,
|
_assetManager,
|
||||||
asset,
|
asset,
|
||||||
animation.meshName,
|
animation.meshName,
|
||||||
animation.data,
|
animation.data,
|
||||||
animation.morphIndices,
|
animation.animatedMorphIndices,
|
||||||
animation.numMorphTargets,
|
animation.numMorphTargets,
|
||||||
animation.numFrames,
|
animation.numFrames,
|
||||||
animation.frameLengthInMs
|
animation.frameLengthInMs
|
||||||
@@ -402,6 +401,22 @@ class FilamentController {
|
|||||||
await _channel.invokeMethod("setTexture", [_assetManager, asset]);
|
await _channel.invokeMethod("setTexture", [_assetManager, asset]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setMaterialColor(FilamentEntity asset, String meshName,
|
||||||
|
int materialIndex, Color color) async {
|
||||||
|
await _channel.invokeMethod("setMaterialColor", [
|
||||||
|
_assetManager,
|
||||||
|
asset,
|
||||||
|
meshName,
|
||||||
|
materialIndex,
|
||||||
|
[
|
||||||
|
color.red.toDouble() / 255.0,
|
||||||
|
color.green.toDouble() / 255.0,
|
||||||
|
color.blue.toDouble() / 255.0,
|
||||||
|
color.alpha.toDouble() / 255.0
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
void transformToUnitCube(FilamentEntity asset) async {
|
void transformToUnitCube(FilamentEntity asset) async {
|
||||||
await _channel.invokeMethod("transformToUnitCube", [_assetManager, asset]);
|
await _channel.invokeMethod("transformToUnitCube", [_assetManager, asset]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user