Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Material names #100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions GLTF/include/GLTFMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace GLTF {
Values* values = NULL;

Material();
GLTF::Material * getInstancedEffect(std::string name);
bool hasTexture();
virtual void writeJSON(void* writer, GLTF::Options* options);
};
Expand Down
7 changes: 7 additions & 0 deletions GLTF/src/GLTFMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ bool GLTF::Material::hasTexture() {
return this->values->diffuseTexture != NULL;
}

GLTF::Material * GLTF::Material::getInstancedEffect(std::string name) {
// TODO - apply Material parameters to the effect, not just the name
GLTF::Material *instancedEffect = new GLTF::Material(*this);
instancedEffect -> name = name;
return instancedEffect;
}

void GLTF::Material::Values::writeJSON(void* writer, GLTF::Options* options) {
rapidjson::Writer<rapidjson::StringBuffer>* jsonWriter = (rapidjson::Writer<rapidjson::StringBuffer>*)writer;
if (ambient != NULL || ambientTexture != NULL) {
Expand Down
1 change: 1 addition & 0 deletions include/COLLADA2GLTFWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace COLLADA2GLTF {
COLLADA2GLTF::ExtrasHandler* _extrasHandler;
GLTF::Node* _rootNode = NULL;
std::map<COLLADAFW::UniqueId, COLLADAFW::UniqueId> _materialEffects;
std::map<COLLADAFW::UniqueId, std::string > _materialName;
std::map<COLLADAFW::UniqueId, GLTF::Material*> _effectInstances;
std::map<COLLADAFW::UniqueId, GLTF::Camera*> _cameraInstances;
std::map<COLLADAFW::UniqueId, GLTF::Mesh*> _meshInstances;
Expand Down
14 changes: 11 additions & 3 deletions src/COLLADA2GLTFWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ bool COLLADA2GLTF::Writer::writeNodeToGroup(std::vector<GLTF::Node*>* group, con
GLTF::Primitive* primitive = skinnedMesh->primitives[j];
COLLADAFW::UniqueId materialId = materialBinding.getReferencedMaterial();
COLLADAFW::UniqueId effectId = this->_materialEffects[materialId];
GLTF::Material* material = _effectInstances[effectId];

GLTF::Material* material = _effectInstances[effectId]->getInstancedEffect(this->_materialName[materialId]);

if (material->type == GLTF::Material::Type::MATERIAL_COMMON) {
GLTF::MaterialCommon* materialCommon = (GLTF::MaterialCommon*)material;
materialCommon->jointCount = _skinJointNodes[uniqueId].size();
Expand Down Expand Up @@ -319,7 +321,9 @@ bool COLLADA2GLTF::Writer::writeNodeToGroup(std::vector<GLTF::Node*>* group, con
COLLADAFW::MaterialBinding materialBinding = materialBindings[j];
COLLADAFW::UniqueId materialId = materialBinding.getReferencedMaterial();
COLLADAFW::UniqueId effectId = this->_materialEffects[materialId];
GLTF::Material* material = _effectInstances[effectId];

GLTF::Material* material = _effectInstances[effectId]->getInstancedEffect(this->_materialName[materialId]);

for (GLTF::Primitive* primitive : primitiveMaterialMapping[materialBinding.getMaterialId()]) {
if (primitive->material != NULL && primitive->material != material) {
// This mesh primitive has a different material from a previous instance, clone the mesh and primitives
Expand Down Expand Up @@ -816,7 +820,11 @@ bool COLLADA2GLTF::Writer::writeGeometry(const COLLADAFW::Geometry* geometry) {
}

bool COLLADA2GLTF::Writer::writeMaterial(const COLLADAFW::Material* material) {
this->_materialEffects[material->getUniqueId()] = material->getInstantiatedEffect();
auto id = material->getUniqueId();
this->_materialEffects[id] = material->getInstantiatedEffect();
this->_materialName[id] = material->getName();

// TODO - read Material setting and properly apply to effect Instance
return true;
}

Expand Down