diff --git a/source/MaterialXGraphEditor/Graph.cpp b/source/MaterialXGraphEditor/Graph.cpp index 714c4d54ea..39a514de05 100644 --- a/source/MaterialXGraphEditor/Graph.cpp +++ b/source/MaterialXGraphEditor/Graph.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include diff --git a/source/MaterialXRender/GltfMaterialHandler.cpp b/source/MaterialXRender/External/glTF_MaterialX/GltfMaterialHandler.cpp similarity index 89% rename from source/MaterialXRender/GltfMaterialHandler.cpp rename to source/MaterialXRender/External/glTF_MaterialX/GltfMaterialHandler.cpp index d9b72e6739..ad8fcc8c64 100644 --- a/source/MaterialXRender/GltfMaterialHandler.cpp +++ b/source/MaterialXRender/External/glTF_MaterialX/GltfMaterialHandler.cpp @@ -1,3 +1,20 @@ +/* + +Copyright 2022 - 2023 Bernard Kwok + +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 #include @@ -7,7 +24,7 @@ #include #include #include -#include +#include #include @@ -26,7 +43,8 @@ #pragma warning(disable : 4996) #endif -#undef CGLTF_IMPLEMENTATION //-- don't set to avoid duplicate symbols +#undef CGLTF_IMPLEMENTATION +//#define CGLTF_IMPLEMENTATION //-- don't set to avoid duplicate symbols #include #define CGLTF_WRITE_IMPLEMENTATION #include @@ -45,14 +63,12 @@ #include #include -#include - MATERIALX_NAMESPACE_BEGIN namespace { const float TO_DEGREE = 180.0f / 3.1415926535f; -//const float TO_RADIAN = 3.1415926535f / 180.0f; +const float TO_RADIAN = 3.1415926535f / 180.0f; const std::string SPACE_STRING = " "; const std::string IN_STRING = "in"; const std::string FLOAT_STRING = "float"; @@ -96,9 +112,10 @@ void initialize_cgtlf_texture(cgltf_texture& texture, const string& name, const texture.image->uri = const_cast((new string(uriPath.asString(FilePath::FormatPosix)))->c_str()); } -void writeTexcoordIndex(cgltf_texture_view& texture_view, NodePtr imageNode) +void writeImageProperties(cgltf_texture_view& textureView, NodePtr imageNode, + std::vector& samplerList, size_t& samplerIndex) { - // Check for any upstream `texcoord` node and grab it's uv index if not 0. + // Handle uvset index int uvindex = 0; InputPtr texcoordInput = imageNode->getInput("texcoord"); if (texcoordInput) @@ -117,7 +134,114 @@ void writeTexcoordIndex(cgltf_texture_view& texture_view, NodePtr imageNode) } } } - texture_view.texcoord = static_cast(uvindex); + textureView.texcoord = static_cast(uvindex); + + // Handle transform + // See: https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_texture_transform/README.md + cgltf_texture_transform& transform = textureView.transform; + + InputPtr offsetInput = imageNode->getInput("offset"); + if (offsetInput) + { + ValuePtr offsetInputValue = offsetInput->getValue(); + if (offsetInputValue) + { + Vector2 val = offsetInputValue->asA(); + transform.offset[0] = val[0]; + transform.offset[1] = val[1]; + textureView.has_transform = true; + } + } + InputPtr rotationInput = imageNode->getInput("rotate"); + if (rotationInput) + { + ValuePtr rotationInputValue = rotationInput->getValue(); + if (rotationInputValue) + { + float val = rotationInputValue->asA(); + // Note: Rotation in glTF is in radians and degrees in MaterialX + transform.rotation = val * TO_RADIAN; + textureView.has_transform = true; + } + } + InputPtr scaleInput = imageNode->getInput("scale"); + if (scaleInput) + { + ValuePtr scaleInputValue = scaleInput->getValue(); + if (scaleInputValue) + { + Vector2 val = scaleInputValue->asA(); + transform.scale[0] = val[0]; + transform.scale[1] = val[1]; + textureView.has_transform = true; + } + } + + if (textureView.texcoord != 0) + { + transform.has_texcoord = true; + transform.texcoord = textureView.texcoord; + } + + // Handle sampler. + // Based on: https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/sampler.schema.json + cgltf_texture* texture = textureView.texture; + if (texture) + { + InputPtr uaddressInput = imageNode->addInputFromNodeDef("uaddressmode"); + ValuePtr uaddressInputValue = uaddressInput ? uaddressInput->getValue() : nullptr; + InputPtr vaddressInput = imageNode->addInputFromNodeDef("vaddressmode"); + ValuePtr vaddressInputValue = vaddressInput ? vaddressInput->getValue() : nullptr; + InputPtr filterInput = imageNode->addInputFromNodeDef("filtertype"); + ValuePtr filterInputValue = filterInput ? filterInput->getValue() : nullptr; + + if (uaddressInputValue || vaddressInputValue || filterInputValue) + { + texture->sampler = &(samplerList[samplerIndex]); + samplerIndex++; + + textureView.texture = texture; + std::unordered_map wrapMap; + wrapMap["clamp"] = 33071; + wrapMap["mirror"] = 33648; + wrapMap["periodic"] = 10497; + + if (uaddressInputValue && wrapMap.count(uaddressInputValue->asA())) + { + texture->sampler->wrap_s = wrapMap[uaddressInputValue->asA()]; + } + else + { + texture->sampler->wrap_s = 10497; + } + if (vaddressInputValue && wrapMap.count(vaddressInputValue->asA())) + { + texture->sampler->wrap_t = wrapMap[vaddressInputValue->asA()]; + } + else + { + texture->sampler->wrap_t = 10497; + } + + std::unordered_map filterMap; + filterMap["closest"] = 9728; + filterMap["linear"] = 9729; + filterMap["cubic"] = 9984; + filterMap["closest"] = 9985; + filterMap["linear"] = 9986; + filterMap["cubic"] = 9987; + if (filterInputValue && filterMap.count(filterInputValue->asA())) + { + texture->sampler->mag_filter = filterMap[filterInputValue->asA()]; + texture->sampler->min_filter = filterMap[filterInputValue->asA()]; + } + else + { + texture->sampler->mag_filter = 9729; + texture->sampler->min_filter = 9986; + } + } + } } void writeColor3Input(const NodePtr pbrNode, const string& inputName, @@ -125,7 +249,8 @@ void writeColor3Input(const NodePtr pbrNode, const string& inputName, cgltf_float* write_value, cgltf_bool& hasFlag, std::vector& textureList, - std::vector& imageList, size_t& imageIndex) + std::vector& imageList, size_t& imageIndex, + std::vector& samplerList, size_t& samplerIndex) { string filename; @@ -146,7 +271,7 @@ void writeColor3Input(const NodePtr pbrNode, const string& inputName, texture_view.texture = texture; initialize_cgtlf_texture(*texture, imageNode->getNamePath(), filename, &(imageList[imageIndex])); - writeTexcoordIndex(texture_view, imageNode); + writeImageProperties(texture_view, imageNode, samplerList, samplerIndex); write_value[0] = 1.0f; write_value[1] = 1.0f; @@ -175,7 +300,8 @@ void writeFloatInput(const NodePtr pbrNode, const string& inputName, float* write_value, cgltf_bool& hasFlag, std::vector& textureList, - std::vector& imageList, size_t& imageIndex) + std::vector& imageList, size_t& imageIndex, + std::vector& samplerList, size_t& samplerIndex) { string filename; @@ -194,10 +320,9 @@ void writeFloatInput(const NodePtr pbrNode, const string& inputName, { cgltf_texture* texture = &(textureList[imageIndex]); texture_view.texture = texture; - // Fix this to create a valid name... initialize_cgtlf_texture(*texture, imageNode->getNamePath(), filename, &(imageList[imageIndex])); - writeTexcoordIndex(texture_view, imageNode); + writeImageProperties(texture_view, imageNode, samplerList, samplerIndex); *write_value = 1.0f; imageIndex++; @@ -488,9 +613,12 @@ bool GltfMaterialHandler::save(const FilePath& filePath, StringVec& logger) textureList.resize(64); std::vector imageList; imageList.resize(64); + std::vector samplerList; + samplerList.resize(64); size_t material_idx = 0; size_t imageIndex = 0; + size_t samplerIndex = 0; // Handle unlit nodes for (const NodePtr& unlitNode : unlitNodes) @@ -584,7 +712,7 @@ bool GltfMaterialHandler::save(const FilePath& filePath, StringVec& logger) roughness.base_color_texture.texture = texture; initialize_cgtlf_texture(*texture, imageNode->getNamePath(), filename, &(imageList[imageIndex])); - writeTexcoordIndex(roughness.base_color_texture, imageNode); + writeImageProperties(roughness.base_color_texture, imageNode, samplerList, samplerIndex); roughness.base_color_factor[0] = 1.0; roughness.base_color_factor[1] = 1.0; @@ -862,7 +990,7 @@ bool GltfMaterialHandler::save(const FilePath& filePath, StringVec& logger) material->normal_texture.texture = texture; initialize_cgtlf_texture(*texture, imageNode->getNamePath(), filename, &(imageList[imageIndex])); - writeTexcoordIndex(material->normal_texture, imageNode); + writeImageProperties(material->normal_texture, imageNode, samplerList, samplerIndex); imageIndex++; } @@ -871,17 +999,17 @@ bool GltfMaterialHandler::save(const FilePath& filePath, StringVec& logger) cgltf_transmission& transmission = material->transmission; writeFloatInput(pbrNode, "transmission", transmission.transmission_texture, &(transmission.transmission_factor), - material->has_transmission, textureList, imageList, imageIndex); + material->has_transmission, textureList, imageList, imageIndex, samplerList, samplerIndex); // Handle specular color cgltf_specular& specular = material->specular; writeColor3Input(pbrNode, "specular_color", specular.specular_color_texture, &(specular.specular_color_factor[0]), material->has_specular, - textureList, imageList, imageIndex); + textureList, imageList, imageIndex, samplerList, samplerIndex); // - Handle specular writeFloatInput(pbrNode, "specular", specular.specular_texture, &(specular.specular_factor), material->has_specular, - textureList, imageList, imageIndex); + textureList, imageList, imageIndex, samplerList, samplerIndex); // Handle ior value = pbrNode->getInputValue("ior"); @@ -910,7 +1038,7 @@ bool GltfMaterialHandler::save(const FilePath& filePath, StringVec& logger) iridescence.iridescence_factor = 0.0f; writeFloatInput(pbrNode, "iridescence", iridescence.iridescence_texture, &(iridescence.iridescence_factor), - material->has_iridescence, textureList, imageList, imageIndex); + material->has_iridescence, textureList, imageList, imageIndex, samplerList, samplerIndex); // Scan for upstream node. // Note: This is the agreed upon upstream node to create to map @@ -936,7 +1064,7 @@ bool GltfMaterialHandler::save(const FilePath& filePath, StringVec& logger) iridescence.iridescence_thickness_texture.texture = texture; initialize_cgtlf_texture(*texture, thicknessNode->getNamePath(), thicknessFileName, &(imageList[imageIndex])); - writeTexcoordIndex(iridescence.iridescence_thickness_texture, thicknessNode); + writeImageProperties(iridescence.iridescence_thickness_texture, thicknessNode, samplerList, samplerIndex); imageIndex++; InputPtr thickessInput = thicknessNode->getInput("thicknessMin"); @@ -958,20 +1086,20 @@ bool GltfMaterialHandler::save(const FilePath& filePath, StringVec& logger) cgltf_sheen& sheen = material->sheen; writeColor3Input(pbrNode, "sheen_color", sheen.sheen_color_texture, &(sheen.sheen_color_factor[0]), - material->has_sheen, textureList, imageList, imageIndex); + material->has_sheen, textureList, imageList, imageIndex, samplerList, samplerIndex); // - Handle sheen roughness writeFloatInput(pbrNode, "sheen_roughness", sheen.sheen_roughness_texture, &(sheen.sheen_roughness_factor), - material->has_sheen, textureList, imageList, imageIndex); + material->has_sheen, textureList, imageList, imageIndex, samplerList, samplerIndex); // Handle clearcloat cgltf_clearcoat& clearcoat = material->clearcoat; writeFloatInput(pbrNode, "clearcoat", clearcoat.clearcoat_texture, &(clearcoat.clearcoat_factor), - material->has_clearcoat, textureList, imageList, imageIndex); + material->has_clearcoat, textureList, imageList, imageIndex, samplerList, samplerIndex); writeFloatInput(pbrNode, "clearcoat_roughness", clearcoat.clearcoat_roughness_texture, &(clearcoat.clearcoat_roughness_factor), - material->has_clearcoat, textureList, imageList, imageIndex); + material->has_clearcoat, textureList, imageList, imageIndex, samplerList, samplerIndex); // Handle clearcoat normal filename = EMPTY_STRING; @@ -999,7 +1127,7 @@ bool GltfMaterialHandler::save(const FilePath& filePath, StringVec& logger) clearcoat.clearcoat_normal_texture.texture = texture; initialize_cgtlf_texture(*texture, imageNode->getNamePath(), filename, &(imageList[imageIndex])); - writeTexcoordIndex(material->normal_texture, imageNode); + writeImageProperties(material->normal_texture, imageNode, samplerList, samplerIndex); imageIndex++; material->has_clearcoat = true; @@ -1009,7 +1137,7 @@ bool GltfMaterialHandler::save(const FilePath& filePath, StringVec& logger) cgltf_bool dummy = false; writeColor3Input(pbrNode, "emissive", material->emissive_texture, &(material->emissive_factor[0]), - dummy, textureList, imageList, imageIndex); + dummy, textureList, imageList, imageIndex, samplerList, samplerIndex); // - Handle emissive strength value = pbrNode->getInputValue("emissive_strength"); if (value) @@ -1026,6 +1154,8 @@ bool GltfMaterialHandler::save(const FilePath& filePath, StringVec& logger) data->images = &imageList[0]; data->textures_count = imageIndex; data->textures = &textureList[0]; + data->samplers_count = samplerIndex; + data->samplers = &samplerList[0]; // Write to disk cgltf_result result = cgltf_write_file(&options, filePath.asString().c_str(), data); @@ -1163,7 +1293,7 @@ void addTexCoordNode(NodePtr image, int uvindex) } } -void setImageProperties(NodePtr image, const cgltf_texture_view* textureView) +void readImageProperties(NodePtr image, const cgltf_texture_view* textureView) { cgltf_texture* texture = textureView ? textureView->texture : nullptr; if (!texture) @@ -1196,7 +1326,7 @@ void setImageProperties(NodePtr image, const cgltf_texture_view* textureView) if (rotationInput) { // Note: Rotation in glTF and MaterialX are opposite directions - // This is handled in the MaterialX implementation + // Direction is handled in the MaterialX implementation rotationInput->setValue(TO_DEGREE * transform.rotation); } InputPtr scaleInput = image->addInputFromNodeDef("scale"); @@ -1270,7 +1400,7 @@ void setImageProperties(NodePtr image, const cgltf_texture_view* textureView) } } -void GltfMaterialHandler::setNormalMapInput(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, +void GltfMaterialHandler::readNormalMapInput(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, const void* textureViewIn, const std::string& inputImageNodeName) { const cgltf_texture_view* textureView = (const cgltf_texture_view*)(textureViewIn); @@ -1287,7 +1417,7 @@ void GltfMaterialHandler::setNormalMapInput(DocumentPtr materials, NodePtr shade VEC3_STRING, EMPTY_STRING, "gltf_normalmap"); if (newTexture) { - setImageProperties(newTexture, textureView); + readImageProperties(newTexture, textureView); InputPtr normalInput = shaderNode->addInputFromNodeDef(inputName); if (normalInput) @@ -1299,7 +1429,7 @@ void GltfMaterialHandler::setNormalMapInput(DocumentPtr materials, NodePtr shade } } -void GltfMaterialHandler::setColorInput(DocumentPtr materials, NodePtr shaderNode, const std::string& colorInputName, +void GltfMaterialHandler::readColorInput(DocumentPtr materials, NodePtr shaderNode, const std::string& colorInputName, const Color3& color, float alpha, const std::string& alphaInputName, const void* textureViewIn, const std::string& inputImageNodeName) @@ -1323,7 +1453,7 @@ void GltfMaterialHandler::setColorInput(DocumentPtr materials, NodePtr shaderNod NodePtr newTexture = createTexture(materials, imageNodeName, uri, "color3", "srgb_texture"); if (newTexture) { - setImageProperties(newTexture, textureView); + readImageProperties(newTexture, textureView); } if (!colorInput) { @@ -1344,7 +1474,7 @@ void GltfMaterialHandler::setColorInput(DocumentPtr materials, NodePtr shaderNod NodePtr newTexture = createColoredTexture(materials, imageNodeName, uri, color4, "srgb_texture"); if (newTexture) { - setImageProperties(newTexture, textureView); + readImageProperties(newTexture, textureView); const string& newTextureName = newTexture->getName(); if (!colorInput) @@ -1390,7 +1520,7 @@ void GltfMaterialHandler::setColorInput(DocumentPtr materials, NodePtr shaderNod } } -void GltfMaterialHandler::setFloatInput(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, +void GltfMaterialHandler::readFloatInput(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, float floatFactor, const void* textureViewIn, const std::string& inputImageNodeName) { @@ -1408,7 +1538,7 @@ void GltfMaterialHandler::setFloatInput(DocumentPtr materials, NodePtr shaderNod FLOAT_STRING, EMPTY_STRING); if (newTexture) { - setImageProperties(newTexture, textureView); + readImageProperties(newTexture, textureView); } floatInput->setAttribute(PortElement::NODE_NAME_ATTRIBUTE, newTexture->getName()); floatInput->removeAttribute(AttributeDef::VALUE_ATTRIBUTE); @@ -1426,7 +1556,7 @@ void GltfMaterialHandler::setFloatInput(DocumentPtr materials, NodePtr shaderNod } } -void GltfMaterialHandler::setVector3Input(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, +void GltfMaterialHandler::readVector3Input(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, const Vector3& vecFactor, const void* textureViewIn, const std::string& inputImageNodeName) { @@ -1447,7 +1577,7 @@ void GltfMaterialHandler::setVector3Input(DocumentPtr materials, NodePtr shaderN VEC3_STRING, EMPTY_STRING); if (newTexture) { - setImageProperties(newTexture, textureView); + readImageProperties(newTexture, textureView); } vecInput->setAttribute(PortElement::NODE_NAME_ATTRIBUTE, newTexture->getName()); vecInput->removeAttribute(AttributeDef::VALUE_ATTRIBUTE); @@ -1590,7 +1720,7 @@ void GltfMaterialHandler::loadMaterials(void *vdata) { haveSeparateOcclusion = true; InputPtr occlusionInput = shaderNode->addInputFromNodeDef("occlusion"); - setFloatInput(_materials, shaderNode, "occlusion", 1.0, &material->occlusion_texture, "image_occlusion"); + readFloatInput(_materials, shaderNode, "occlusion", 1.0, &material->occlusion_texture, "image_occlusion"); } } } @@ -1607,7 +1737,7 @@ void GltfMaterialHandler::loadMaterials(void *vdata) roughness.base_color_factor[1], roughness.base_color_factor[2]); float alpha = roughness.base_color_factor[3]; - setColorInput(_materials, shaderNode, colorAlphaInputs[colorAlphaInputOffset], + readColorInput(_materials, shaderNode, colorAlphaInputs[colorAlphaInputOffset], colorFactor, alpha, colorAlphaInputs[colorAlphaInputOffset + 1], &roughness.base_color_texture, "image_basecolor"); @@ -1635,7 +1765,7 @@ void GltfMaterialHandler::loadMaterials(void *vdata) VEC3_STRING, EMPTY_STRING); if (textureNode) { - setImageProperties(textureNode, &textureView); + readImageProperties(textureNode, &textureView); } // Map image channesl to inputs @@ -1690,7 +1820,7 @@ void GltfMaterialHandler::loadMaterials(void *vdata) } // Normal texture - setNormalMapInput(_materials, shaderNode, "normal", &(material->normal_texture), "image_normal"); + readNormalMapInput(_materials, shaderNode, "normal", &(material->normal_texture), "image_normal"); // Handle sheen if (material->has_sheen) @@ -1700,10 +1830,10 @@ void GltfMaterialHandler::loadMaterials(void *vdata) Color3 colorFactor(sheen.sheen_color_factor[0], sheen.sheen_color_factor[1], sheen.sheen_color_factor[2]); - setColorInput(_materials, shaderNode, "sheen_color", + readColorInput(_materials, shaderNode, "sheen_color", colorFactor, 1.0f, EMPTY_STRING, &sheen.sheen_color_texture, "image_sheen"); - setFloatInput(_materials, shaderNode, "sheen_roughness", + readFloatInput(_materials, shaderNode, "sheen_roughness", sheen.sheen_roughness_factor, &sheen.sheen_roughness_texture, "image_sheen_roughness"); } @@ -1727,11 +1857,11 @@ void GltfMaterialHandler::loadMaterials(void *vdata) { const cgltf_iridescence& iridescence = material->iridescence; - setFloatInput(_materials, shaderNode, "iridescence", + readFloatInput(_materials, shaderNode, "iridescence", iridescence.iridescence_factor, &iridescence.iridescence_texture, "image_iridescence"); - setFloatInput(_materials, shaderNode, "iridescence_ior", + readFloatInput(_materials, shaderNode, "iridescence_ior", iridescence.iridescence_ior, nullptr, "image_iridescence_ior"); @@ -1759,7 +1889,7 @@ void GltfMaterialHandler::loadMaterials(void *vdata) { maxInput->setValue(iridescence.iridescence_thickness_max); } - setImageProperties(newTexture, &textureView); + readImageProperties(newTexture, &textureView); } floatInput->setAttribute(PortElement::NODE_NAME_ATTRIBUTE, newTexture->getName()); floatInput->removeAttribute(AttributeDef::VALUE_ATTRIBUTE); @@ -1783,19 +1913,19 @@ void GltfMaterialHandler::loadMaterials(void *vdata) cgltf_clearcoat& clearcoat = material->clearcoat; // Mapped or unmapped clearcoat - setFloatInput(_materials, shaderNode, "clearcoat", + readFloatInput(_materials, shaderNode, "clearcoat", clearcoat.clearcoat_factor, &clearcoat.clearcoat_texture, "image_clearcoat"); // Mapped or unmapped clearcoat roughness - setFloatInput(_materials, shaderNode, "clearcoat_roughness", + readFloatInput(_materials, shaderNode, "clearcoat_roughness", clearcoat.clearcoat_roughness_factor, &clearcoat.clearcoat_roughness_texture, "image_clearcoat_roughness"); // Normal map clearcoat_normal - setNormalMapInput(_materials, shaderNode, "clearcoat_normal", &material->normal_texture, + readNormalMapInput(_materials, shaderNode, "clearcoat_normal", &material->normal_texture, "image_clearcoat_normal"); } @@ -1809,7 +1939,7 @@ void GltfMaterialHandler::loadMaterials(void *vdata) { cgltf_transmission& transmission = material->transmission; - setFloatInput(_materials, shaderNode, "transmission", + readFloatInput(_materials, shaderNode, "transmission", transmission.transmission_factor, &transmission.transmission_texture, "image_transmission"); @@ -1831,13 +1961,13 @@ void GltfMaterialHandler::loadMaterials(void *vdata) Color3 colorFactor(specular.specular_color_factor[0], specular.specular_color_factor[1], specular.specular_color_factor[2]); - setColorInput(_materials, shaderNode, "specular_color", + readColorInput(_materials, shaderNode, "specular_color", colorFactor, 1.0f, EMPTY_STRING, &specular.specular_color_texture, "image_specularcolor"); // Mapped or unmapped specular color - setFloatInput(_materials, shaderNode, "specular", + readFloatInput(_materials, shaderNode, "specular", specular.specular_factor, &specular.specular_texture, "image_specular"); @@ -1862,7 +1992,7 @@ void GltfMaterialHandler::loadMaterials(void *vdata) Color3 colorFactor(material->emissive_factor[0], material->emissive_factor[1], material->emissive_factor[2]); - setColorInput(_materials, shaderNode, "emissive", + readColorInput(_materials, shaderNode, "emissive", colorFactor, 1.0f, EMPTY_STRING, &material->emissive_texture, "image_emission"); if (material->has_emissive_strength) @@ -1890,7 +2020,7 @@ void GltfMaterialHandler::loadMaterials(void *vdata) cgltf_volume& volume = material->volume; // Textured or untexture thickness - setFloatInput(_materials, shaderNode, "thickness", + readFloatInput(_materials, shaderNode, "thickness", volume.thickness_factor, &volume.thickness_texture, "thickness"); @@ -1899,11 +2029,11 @@ void GltfMaterialHandler::loadMaterials(void *vdata) Color3 attenFactor(volume.attenuation_color[0], volume.attenuation_color[1], volume.attenuation_color[2]); - setColorInput(_materials, shaderNode, "attenuation_color", + readColorInput(_materials, shaderNode, "attenuation_color", attenFactor, 1.0f, EMPTY_STRING, nullptr, EMPTY_STRING); // Untextured attenuation distance - setFloatInput(_materials, shaderNode, "attenuation_distance", + readFloatInput(_materials, shaderNode, "attenuation_distance", volume.attenuation_distance, nullptr, EMPTY_STRING); } } diff --git a/source/MaterialXRender/GltfMaterialHandler.h b/source/MaterialXRender/External/glTF_MaterialX/GltfMaterialHandler.h similarity index 84% rename from source/MaterialXRender/GltfMaterialHandler.h rename to source/MaterialXRender/External/glTF_MaterialX/GltfMaterialHandler.h index b0042c72a9..5216491be4 100644 --- a/source/MaterialXRender/GltfMaterialHandler.h +++ b/source/MaterialXRender/External/glTF_MaterialX/GltfMaterialHandler.h @@ -1,7 +1,27 @@ +/* + +Copyright 2022 - 2023 Bernard Kwok + +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. + +*/ #ifndef MATERIALX_CGLTF_MaterialHandler_H #define MATERIALX_CGLTF_MaterialHandler_H +// This line added to original source code to place this into the MaterialX Render module. +#define MX_GLTF_API MX_RENDER_API + /// @file /// GLTF material loader using the Cgltf library @@ -20,7 +40,7 @@ using GltfMaterialHandlerPtr = std::shared_ptr; /// @class MaterialHandler /// Wrapper for handler to convert materials to / from MaterialX -class MX_RENDER_API MaterialHandler +class MX_GLTF_API MaterialHandler { public: MaterialHandler() @@ -135,7 +155,7 @@ class MX_RENDER_API MaterialHandler /// @class GltfMaterialHandler /// Wrapper for handling import / export of materials to / from GLTF files -class MX_RENDER_API GltfMaterialHandler : public MaterialHandler +class MX_GLTF_API GltfMaterialHandler : public MaterialHandler { public: GltfMaterialHandler() @@ -185,16 +205,16 @@ class MX_RENDER_API GltfMaterialHandler : public MaterialHandler NodePtr createTexture(DocumentPtr& doc, const std::string & nodeName, const std::string& fileName, const std::string & textureType, const std::string & colorspace, const std::string& nodeType = "gltf_image"); - void setColorInput(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, + void readColorInput(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, const Color3& color, float alpha, const std::string& alphaInputName, const void* textureView, const std::string& inputImageNodeName); - void setFloatInput(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, + void readFloatInput(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, float floatFactor, const void* textureView, const std::string& inputImageNodeName); - void setVector3Input(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, + void readVector3Input(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, const Vector3& vecFactor, const void* textureViewIn, const std::string& inputImageNodeName); - void setNormalMapInput(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, + void readNormalMapInput(DocumentPtr materials, NodePtr shaderNode, const std::string& inputName, const void* textureViewIn, const std::string& inputImageNodeName); void loadMaterials(void *); diff --git a/source/MaterialXRender/External/glTF_MaterialX/LICENSE b/source/MaterialXRender/External/glTF_MaterialX/LICENSE new file mode 100644 index 0000000000..73827d120e --- /dev/null +++ b/source/MaterialXRender/External/glTF_MaterialX/LICENSE @@ -0,0 +1,13 @@ +Copyright 2022-2023 Bernard Kwok + +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. diff --git a/source/MaterialXView/Viewer.cpp b/source/MaterialXView/Viewer.cpp index cc969dbfd3..df36b24ebe 100644 --- a/source/MaterialXView/Viewer.cpp +++ b/source/MaterialXView/Viewer.cpp @@ -34,7 +34,7 @@ #endif #include -#include +#include #include #include diff --git a/source/PyMaterialX/PyMaterialXRender/PyGltfMaterialHandler.cpp b/source/PyMaterialX/PyMaterialXRender/PyGltfMaterialHandler.cpp index 3bd8a5e00e..3187d567d1 100644 --- a/source/PyMaterialX/PyMaterialXRender/PyGltfMaterialHandler.cpp +++ b/source/PyMaterialX/PyMaterialXRender/PyGltfMaterialHandler.cpp @@ -1,10 +1,23 @@ -// -// Copyright Bernard Kwok -// SPDX-License-Identifier: Apache-2.0 -// +/* + +Copyright 2022 - 2023 Bernard Kwok + +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 -#include +#include namespace py = pybind11; namespace mx = MaterialX;