From 7a04a3c77e6d7ca351f5dab51da5ae9e02c0aca1 Mon Sep 17 00:00:00 2001 From: wjl1118 <62125828+wjl1118@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:53:25 +0800 Subject: [PATCH] Make the gltf plugin support color --- readerwriter/LoadSceneGLTF.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/readerwriter/LoadSceneGLTF.cpp b/readerwriter/LoadSceneGLTF.cpp index 02d9082..0a1d16a 100644 --- a/readerwriter/LoadSceneGLTF.cpp +++ b/readerwriter/LoadSceneGLTF.cpp @@ -630,6 +630,23 @@ namespace osgVerse withNames ? mesh.extras.Get("targetNames") : tinygltf::Value()); return true; } + + osg::Texture2D* createDefaultTexture(const osg::Vec4& color) + { + osg::ref_ptr image = new osg::Image; + image->allocateImage(1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE); + image->setInternalTextureFormat(GL_RGBA); + + osg::Vec4ub* ptr = (osg::Vec4ub*)image->data(); + *ptr = osg::Vec4ub(color[0] * 255, color[1] * 255, color[2] * 255, color[3] * 255); + + osg::ref_ptr tex2D = new osg::Texture2D; + tex2D->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST); + tex2D->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST); + tex2D->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT); + tex2D->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT); + tex2D->setImage(image.get()); return tex2D.release(); + } void LoaderGLTF::createMaterial(osg::StateSet* ss, tinygltf::Material material) { @@ -641,6 +658,15 @@ namespace osgVerse int occlusionID = material.occlusionTexture.index; if (baseID >= 0) createTexture(ss, 0, uniformNames[0], _modelDef.textures[baseID]); + else + { + osg::Texture2D* tex2D = createDefaultTexture(osg::Vec4(material.pbrMetallicRoughness.baseColorFactor[0], material.pbrMetallicRoughness.baseColorFactor[1], + material.pbrMetallicRoughness.baseColorFactor[2], material.pbrMetallicRoughness.baseColorFactor[3])); + if (tex2D) + { + ss->setTextureAttributeAndModes(0, tex2D); + } + } if (normalID >= 0) createTexture(ss, 1, uniformNames[1], _modelDef.textures[normalID]); if (roughnessID >= 0) createTexture(ss, 3, uniformNames[3], _modelDef.textures[roughnessID]); if (occlusionID >= 0) createTexture(ss, 4, uniformNames[4], _modelDef.textures[occlusionID]); -- Gitee