From a8347cac6e82e9dc0d8e83e881e4f8c5ee3275df Mon Sep 17 00:00:00 2001 From: samcake Date: Fri, 6 May 2016 18:36:55 -0700 Subject: [PATCH] Getting the unlit to work from the pipeline, and found the isLinear for texture bug! --- libraries/fbx/src/FBXReader_Material.cpp | 23 +++++++++++++++++-- libraries/model/src/model/TextureMap.cpp | 4 ++-- .../render-utils/src/MeshPartPayload.cpp | 5 ++++ libraries/render-utils/src/model_unlit.slf | 20 +++++++++++----- .../utilities/render/framebuffer.qml | 1 + 5 files changed, 43 insertions(+), 10 deletions(-) diff --git a/libraries/fbx/src/FBXReader_Material.cpp b/libraries/fbx/src/FBXReader_Material.cpp index 9861fe1d68..2e9f5550b9 100644 --- a/libraries/fbx/src/FBXReader_Material.cpp +++ b/libraries/fbx/src/FBXReader_Material.cpp @@ -72,6 +72,10 @@ void FBXReader::consolidateFBXMaterials() { // foreach (const QString& materialID, materials) { for (QHash::iterator it = _fbxMaterials.begin(); it != _fbxMaterials.end(); it++) { FBXMaterial& material = (*it); + + // Maya is the exporting the shading model and we aretrying to use it + bool isMaterialLambert = (material.shadingModel.toLower() == "lambert"); + // the pure material associated with this part bool detectDifferentUVs = false; FBXTexture diffuseTexture; @@ -171,6 +175,13 @@ void FBXReader::consolidateFBXMaterials() { emissiveTexture = getTexture(emissiveTextureID); detectDifferentUVs |= (emissiveTexture.texcoordSet != 0) || (!emissiveTexture.transform.isIdentity()); material.emissiveTexture = emissiveTexture; + + if (isMaterialLambert) { + // If the emissiveTextureID comes from the Texture bound to Emissive when material is lambert, we know it s exported from maya + // And the EMissiveColor is forced to 0.5 by Maya which is bad + // So we need to force it to 1.0 + material.emissiveColor = vec3(1.0); + } } FBXTexture occlusionTexture; @@ -198,7 +209,7 @@ void FBXReader::consolidateFBXMaterials() { material._material = std::make_shared(); // Emissive color is the mix of emissiveColor with emissiveFactor - auto emissive = material.emissiveColor * material.emissiveFactor; + auto emissive = material.emissiveColor * (isMaterialLambert ? 1.0f : material.emissiveFactor); // In lambert there is not emissiveFactor material._material->setEmissive(emissive); // Final diffuse color is the mix of diffuseColor with diffuseFactor @@ -213,9 +224,17 @@ void FBXReader::consolidateFBXMaterials() { float metallic = std::max(material.specularColor.x, std::max(material.specularColor.y, material.specularColor.z)); material._material->setMetallic(metallic); - if (material.shadingModel == "lambert") { + if (isMaterialLambert) { if (!material._material->getKey().isAlbedo()) { + // switch emissive to material albedo as we tag the material to unlit material._material->setUnlit(true); + material._material->setEmissive(diffuse); + material._material->setAlbedo(emissive); + + if (!material.emissiveTexture.isNull()) { + material.albedoTexture = material.emissiveTexture; + } + std::cout << emissive; } } diff --git a/libraries/model/src/model/TextureMap.cpp b/libraries/model/src/model/TextureMap.cpp index 349980b702..9ac9626876 100755 --- a/libraries/model/src/model/TextureMap.cpp +++ b/libraries/model/src/model/TextureMap.cpp @@ -104,7 +104,7 @@ const QImage& image, bool isLinear, bool doCompress) { if (image.hasAlphaChannel()) { gpu::Semantic gpuSemantic; gpu::Semantic mipSemantic; - if (isLinear) { + if (!isLinear) { mipSemantic = gpu::SBGRA; if (doCompress) { gpuSemantic = gpu::COMPRESSED_SRGBA; @@ -124,7 +124,7 @@ const QImage& image, bool isLinear, bool doCompress) { } else { gpu::Semantic gpuSemantic; gpu::Semantic mipSemantic; - if (isLinear) { + if (!isLinear) { mipSemantic = gpu::SRGB; if (doCompress) { gpuSemantic = gpu::COMPRESSED_SRGB; diff --git a/libraries/render-utils/src/MeshPartPayload.cpp b/libraries/render-utils/src/MeshPartPayload.cpp index 6146666157..08c8dc23b4 100644 --- a/libraries/render-utils/src/MeshPartPayload.cpp +++ b/libraries/render-utils/src/MeshPartPayload.cpp @@ -144,6 +144,11 @@ void MeshPartPayload::bindMaterial(gpu::Batch& batch, const ShapePipeline::Locat auto materialKey = _drawMaterial->getKey(); auto textureMaps = _drawMaterial->getTextureMaps(); + int numUnlit = 0; + if (materialKey.isUnlit()) { + numUnlit++; + } + // Albedo if (materialKey.isAlbedoMap()) { auto albedoMap = textureMaps[model::MaterialKey::ALBEDO_MAP]; diff --git a/libraries/render-utils/src/model_unlit.slf b/libraries/render-utils/src/model_unlit.slf index 78ddae77ce..5a081911b7 100644 --- a/libraries/render-utils/src/model_unlit.slf +++ b/libraries/render-utils/src/model_unlit.slf @@ -15,7 +15,8 @@ <@include DeferredBufferWrite.slh@> <@include model/Material.slh@> -uniform sampler2D albedoMap; +<@include MaterialTextures.slh@> +<$declareMaterialTextures(ALBEDO, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL)$> in vec2 _texCoord0; in vec3 _normal; @@ -23,14 +24,21 @@ in vec3 _color; in float _alpha; void main(void) { - vec4 texel = texture(albedoMap, _texCoord0); Material mat = getMaterial(); - //vec3 fragColor = getMaterialEmissive(mat) * texel.rgb * _color; - vec3 fragColor = vec3(1.0, 0.5, 0.0); + int matKey = getMaterialKey(mat); + <$fetchMaterialTextures(matKey, _texCoord0, albedoTex, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL)$> + + float opacity = 1.0; + <$evalMaterialOpacity(albedoTex.a, opacity, matKey, opacity)&>; + <$discardTransparent(opacity)$>; + + vec3 albedo = getMaterialAlbedo(mat); + <$evalMaterialAlbedo(albedoTex, albedo, matKey, albedo)$>; + albedo *= _color; packDeferredFragmentUnlit( normalize(_normal), - 1.0, - fragColor); + opacity, + albedo); } diff --git a/scripts/developer/utilities/render/framebuffer.qml b/scripts/developer/utilities/render/framebuffer.qml index f13018142e..0d8d85cc32 100644 --- a/scripts/developer/utilities/render/framebuffer.qml +++ b/scripts/developer/utilities/render/framebuffer.qml @@ -33,6 +33,7 @@ Column { "Roughness", "Metallic", "Emissive", + "Shaded/Lightmapped/Unlit", "Occlusion", "Lightmap", "Lighting",