From: seungho baek Date: Thu, 17 Nov 2022 14:25:47 +0000 (+0900) Subject: [Tizen] Support KHR_materials_specular and KHR_materials_ior extension of glTF X-Git-Tag: accepted/tizen/7.0/unified/20221128.015442~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d63237ffbc775aa0328b802aed35d9da80874533;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git [Tizen] Support KHR_materials_specular and KHR_materials_ior extension of glTF Change-Id: Ieccdc7046bcc0e0a231d4d39526d610a9b7cf036 Signed-off-by: seungho baek --- diff --git a/dali-scene3d/internal/graphics/shaders/default-physically-based-shader.frag b/dali-scene3d/internal/graphics/shaders/default-physically-based-shader.frag index a55e9c3420..6ef02b2803 100644 --- a/dali-scene3d/internal/graphics/shaders/default-physically-based-shader.frag +++ b/dali-scene3d/internal/graphics/shaders/default-physically-based-shader.frag @@ -36,6 +36,7 @@ precision mediump float; uniform lowp vec4 uColorFactor; uniform lowp float uMetallicFactor; uniform lowp float uRoughnessFactor; +uniform lowp float uDielectricSpecular; #ifdef THREE_TEX #ifdef BASECOLOR_TEX @@ -53,6 +54,15 @@ uniform sampler2D sAlbedoMetal; uniform sampler2D sNormalRoughness; #endif +uniform float uSpecularFactor; +uniform vec3 uSpecularColorFactor; +#ifdef MATERIAL_SPECULAR_TEXTURE +uniform sampler2D sSpecular; +#endif +#ifdef MATERIAL_SPECULAR_COLOR_TEXTURE +uniform sampler2D sSpecularColor; +#endif + #ifdef OCCLUSION uniform sampler2D sOcclusion; uniform float uOcclusionStrength; @@ -83,43 +93,8 @@ in highp vec3 vPositionToCamera; out vec4 FragColor; -struct PBRInfo -{ - mediump float NdotL; // cos angle between normal and light direction - mediump float NdotV; // cos angle between normal and view direction - mediump float NdotH; // cos angle between normal and half vector - mediump float VdotH; // cos angle between view direction and half vector - mediump vec3 reflectance0; // full reflectance color (normal incidence angle) - mediump vec3 reflectance90; // reflectance color at grazing angle - lowp float alphaRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2]) -}; - -const float M_PI = 3.141592653589793; const float c_MinRoughness = 0.04; -vec3 specularReflection(PBRInfo pbrInputs) -{ - return pbrInputs.reflectance0 + (pbrInputs.reflectance90 - pbrInputs.reflectance0) * pow(clamp(1.0 - pbrInputs.VdotH, 0.0, 1.0), 5.0); -} - -float geometricOcclusion(PBRInfo pbrInputs) -{ - mediump float NdotL = pbrInputs.NdotL; - mediump float NdotV = pbrInputs.NdotV; - lowp float r = pbrInputs.alphaRoughness; - - lowp float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL))); - lowp float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV))); - return attenuationL * attenuationV; -} - -float microfacetDistribution(PBRInfo pbrInputs) -{ - mediump float roughnessSq = pbrInputs.alphaRoughness * pbrInputs.alphaRoughness; - lowp float f = (pbrInputs.NdotH * roughnessSq - pbrInputs.NdotH) * pbrInputs.NdotH + 1.0; - return roughnessSq / (M_PI * f * f); -} - vec3 linear(vec3 color) { return pow(color, vec3(2.2)); @@ -181,35 +156,45 @@ void main() // Roughness is authored as perceptual roughness; as is convention, // convert to material roughness by squaring the perceptual roughness [2]. perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0); - lowp float alphaRoughness = perceptualRoughness * perceptualRoughness; - - lowp vec3 f0 = vec3(0.04); - lowp vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0); - diffuseColor *= (1.0 - metallic); - lowp vec3 specularColor = mix(f0, baseColor.rgb, metallic); - // Compute reflectance. - lowp float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b); + // Material ior + lowp vec3 f0 = vec3(uDielectricSpecular); - // For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect. - // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflecance to 0%. - lowp float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0); - lowp vec3 specularEnvironmentR0 = specularColor.rgb; - lowp vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90; + // Material Specular + float specularWeight = 1.0; + vec4 materialSpecularTexture = vec4(1.0); +#ifdef MATERIAL_SPECULAR_TEXTURE + materialSpecularTexture.a = texture(sSpecular, vUV).a; +#endif +#ifdef MATERIAL_SPECULAR_COLOR_TEXTURE + materialSpecularTexture.rgb = texture(sSpecularColor, vUV).rgb; +#endif + specularWeight = uSpecularFactor * materialSpecularTexture.a; + f0 = min(f0 * uSpecularColorFactor * materialSpecularTexture.rgb, vec3(1.0)); + f0 = mix(f0, baseColor.rgb, metallic); mediump vec3 v = normalize(vPositionToCamera); // Vector from surface point to camera mediump float NdotV = clamp(abs(dot(n, v)), 0.001, 1.0); mediump vec3 reflection = -normalize(reflect(v, n)); - - lowp vec3 color = vec3(0.0); - lowp vec3 diffuseLight = linear(texture(sDiffuseEnvSampler, n * uYDirection).rgb); - lowp vec3 specularLight = linear(texture(sSpecularEnvSampler, reflection * uYDirection).rgb); - // retrieve a scale and bias to F0. See [1], Figure 3 lowp vec3 brdf = linear(texture(sbrdfLUT, vec2(NdotV, 1.0 - perceptualRoughness)).rgb); + vec3 Fr = max(vec3(1.0 - perceptualRoughness), f0) - f0; + vec3 k_S = f0 + Fr * pow(1.0 - NdotV, 5.0); + vec3 FssEss = specularWeight * (k_S * brdf.x + brdf.y); - lowp vec3 diffuse = diffuseLight * diffuseColor; - lowp vec3 specular = specularLight * (specularColor * brdf.x + brdf.y); - color += (diffuse + specular) * uIblIntensity; + // Specular Light + lowp vec3 specularLight = linear(texture(sSpecularEnvSampler, reflection * uYDirection).rgb); + lowp vec3 specular = specularLight * FssEss; + + // Diffuse Light + lowp vec3 diffuseColor = mix(baseColor.rgb, vec3(0), metallic); + lowp vec3 irradiance = linear(texture(sDiffuseEnvSampler, n * uYDirection).rgb); + float Ems = (1.0 - (brdf.x + brdf.y)); + vec3 F_avg = specularWeight * (f0 + (1.0 - f0) / 21.0); + vec3 FmsEms = Ems * FssEss * F_avg / (1.0 - F_avg * Ems); + vec3 k_D = diffuseColor * (1.0 - FssEss + FmsEms); + lowp vec3 diffuse = (FmsEms + k_D) * irradiance; + + lowp vec3 color = (diffuse + specular) * uIblIntensity; #ifdef OCCLUSION lowp float ao = texture(sOcclusion, vUV).r; diff --git a/dali-scene3d/internal/loader/gltf2-asset.h b/dali-scene3d/internal/loader/gltf2-asset.h index 7769751bbc..67744eb65e 100644 --- a/dali-scene3d/internal/loader/gltf2-asset.h +++ b/dali-scene3d/internal/loader/gltf2-asset.h @@ -325,9 +325,28 @@ struct TextureInfo } }; +struct MaterialIor +{ + float mIor = MAXFLOAT; +}; + +struct MaterialSpecular +{ + float mSpecularFactor = 1.0f; + TextureInfo mSpecularTexture; + Dali::Vector3 mSpecularColorFactor = Dali::Vector3::ONE; + TextureInfo mSpecularColorTexture; +}; + +struct MaterialExtensions +{ + MaterialSpecular mMaterialSpecular; + MaterialIor mMaterialIor; +}; + struct Material : Named { - struct Pbr //MetallicRoughness + struct Pbr // MetallicRoughness { Dali::Vector4 mBaseColorFactor = Dali::Vector4::ONE; TextureInfo mBaseColorTexture; @@ -338,15 +357,17 @@ struct Material : Named //TODO: extras }; - Pbr mPbrMetallicRoughness; - TextureInfo mNormalTexture; - TextureInfo mOcclusionTexture; - TextureInfo mEmissiveTexture; - Dali::Vector3 mEmissiveFactor; - AlphaMode::Type mAlphaMode = AlphaMode::OPAQUE; - float mAlphaCutoff = .5f; - bool mDoubleSided = false; - //TODO: extensions + Pbr mPbrMetallicRoughness; + TextureInfo mNormalTexture; + TextureInfo mOcclusionTexture; + TextureInfo mEmissiveTexture; + Dali::Vector3 mEmissiveFactor; + AlphaMode::Type mAlphaMode = AlphaMode::OPAQUE; + float mAlphaCutoff = .5f; + bool mDoubleSided = false; + + //extensions + MaterialExtensions mMaterialExtensions; //TODO: extras }; diff --git a/dali-scene3d/public-api/loader/gltf2-loader.cpp b/dali-scene3d/public-api/loader/gltf2-loader.cpp index ceb95d66ad..09ce609a9b 100644 --- a/dali-scene3d/public-api/loader/gltf2-loader.cpp +++ b/dali-scene3d/public-api/loader/gltf2-loader.cpp @@ -168,6 +168,20 @@ const auto MATERIAL_PBR_READER = std::move(js::Reader() .Register(*js::MakeProperty("roughnessFactor", js::Read::Number, >::Material::Pbr::mRoughnessFactor)) .Register(*js::MakeProperty("metallicRoughnessTexture", js::ObjectReader::Read, >::Material::Pbr::mMetallicRoughnessTexture))); +const auto MATERIAL_SPECULAR_READER = std::move(js::Reader() + .Register(*js::MakeProperty("specularFactor", js::Read::Number, >::MaterialSpecular::mSpecularFactor)) + .Register(*js::MakeProperty("specularTexture", js::ObjectReader::Read, >::MaterialSpecular::mSpecularTexture)) + .Register(*js::MakeProperty("specularColorFactor", gt::ReadDaliVector, >::MaterialSpecular::mSpecularColorFactor)) + .Register(*js::MakeProperty("specularColorTexture", js::ObjectReader::Read, >::MaterialSpecular::mSpecularColorTexture))); + +const auto MATERIAL_IOR_READER = std::move(js::Reader() + .Register(*js::MakeProperty("ior", js::Read::Number, >::MaterialIor::mIor))); + + +const auto MATERIAL_EXTENSION_READER = std::move(js::Reader() + .Register(*js::MakeProperty("KHR_materials_ior", js::ObjectReader::Read, >::MaterialExtensions::mMaterialIor)) + .Register(*js::MakeProperty("KHR_materials_specular", js::ObjectReader::Read, >::MaterialExtensions::mMaterialSpecular))); + const auto MATERIAL_READER = std::move(js::Reader() .Register(*new js::Property("name", js::Read::StringView, >::Material::mName)) .Register(*js::MakeProperty("pbrMetallicRoughness", js::ObjectReader::Read, >::Material::mPbrMetallicRoughness)) @@ -177,7 +191,8 @@ const auto MATERIAL_READER = std::move(js::Reader() .Register(*js::MakeProperty("emissiveFactor", gt::ReadDaliVector, >::Material::mEmissiveFactor)) .Register(*js::MakeProperty("alphaMode", gt::ReadStringEnum, >::Material::mAlphaMode)) .Register(*js::MakeProperty("alphaCutoff", js::Read::Number, >::Material::mAlphaCutoff)) - .Register(*js::MakeProperty("doubleSided", js::Read::Boolean, >::Material::mDoubleSided))); + .Register(*js::MakeProperty("doubleSided", js::Read::Boolean, >::Material::mDoubleSided)) + .Register(*js::MakeProperty("extensions", js::ObjectReader::Read, >::Material::mMaterialExtensions))); std::map> ReadMeshPrimitiveAttributes(const json_value_s& j) { @@ -503,6 +518,28 @@ void ConvertMaterial(const gt::Material& m, decltype(ResourceBundle::mMaterials) matDef.mEmissiveFactor = m.mEmissiveFactor; } + if(m.mMaterialExtensions.mMaterialIor.mIor < MAXFLOAT) + { + float ior = m.mMaterialExtensions.mMaterialIor.mIor; + matDef.mDielectricSpecular = powf((ior-1.0f)/(ior+1.0f), 2.0f); + } + matDef.mSpecularFactor = m.mMaterialExtensions.mMaterialSpecular.mSpecularFactor; + matDef.mSpecularColorFactor = m.mMaterialExtensions.mMaterialSpecular.mSpecularColorFactor; + + if(m.mMaterialExtensions.mMaterialSpecular.mSpecularTexture) + { + const auto semantic = MaterialDefinition::SPECULAR; + matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(m.mMaterialExtensions.mMaterialSpecular.mSpecularTexture)}); + matDef.mFlags |= semantic; + } + + if(m.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture) + { + const auto semantic = MaterialDefinition::SPECULAR_COLOR; + matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(m.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture)}); + matDef.mFlags |= semantic; + } + matDef.mDoubleSided = m.mDoubleSided; outMaterials.emplace_back(std::move(matDef), TextureSet()); @@ -1153,6 +1190,9 @@ void SetObjectReaders() js::SetObjectReader(TEXURE_READER); js::SetObjectReader(TEXURE_INFO_READER); js::SetObjectReader(MATERIAL_PBR_READER); + js::SetObjectReader(MATERIAL_SPECULAR_READER); + js::SetObjectReader(MATERIAL_IOR_READER); + js::SetObjectReader(MATERIAL_EXTENSION_READER); js::SetObjectReader(MATERIAL_READER); js::SetObjectReader(MESH_PRIMITIVE_READER); js::SetObjectReader(MESH_READER); diff --git a/dali-scene3d/public-api/loader/material-definition.cpp b/dali-scene3d/public-api/loader/material-definition.cpp index 480da953ad..b346e49f4e 100644 --- a/dali-scene3d/public-api/loader/material-definition.cpp +++ b/dali-scene3d/public-api/loader/material-definition.cpp @@ -224,6 +224,18 @@ MaterialDefinition::LoadRaw(const std::string& imagesPath) const ++iTexture; } + if(checkStage(SPECULAR)) + { + raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags}); + ++iTexture; + } + + if(checkStage(SPECULAR_COLOR)) + { + raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags}); + ++iTexture; + } + if(checkStage(OCCLUSION)) { raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags}); diff --git a/dali-scene3d/public-api/loader/material-definition.h b/dali-scene3d/public-api/loader/material-definition.h index e45b69b95b..119f7c4de8 100644 --- a/dali-scene3d/public-api/loader/material-definition.h +++ b/dali-scene3d/public-api/loader/material-definition.h @@ -130,13 +130,15 @@ struct DALI_SCENE3D_API MaterialDefinition enum Flags : uint32_t { // Texture semantics - ALBEDO = NthBit(0), - METALLIC = NthBit(1), - ROUGHNESS = NthBit(2), - NORMAL = NthBit(3), - EMISSIVE = NthBit(4), // TODO: support - OCCLUSION = NthBit(5), // TODO: support - SUBSURFACE = NthBit(6), // Note: dli-only + ALBEDO = NthBit(0), + METALLIC = NthBit(1), + ROUGHNESS = NthBit(2), + NORMAL = NthBit(3), + EMISSIVE = NthBit(4), + OCCLUSION = NthBit(5), + SPECULAR = NthBit(6), + SPECULAR_COLOR = NthBit(7), + SUBSURFACE = NthBit(8), // Note: dli-only // Other binary options TRANSPARENCY = NthBit(20), @@ -221,14 +223,17 @@ struct DALI_SCENE3D_API MaterialDefinition public: // DATA uint32_t mFlags = 0x0; - Index mEnvironmentIdx = 0; - Vector4 mColor = Color::WHITE; - float mMetallic = 1.f; - float mRoughness = 1.f; - Vector4 mBaseColorFactor = Vector4::ONE; - float mNormalScale = 1.f; - float mOcclusionStrength = 1.f; - Vector3 mEmissiveFactor = Vector3::ZERO; + Index mEnvironmentIdx = 0; + Vector4 mColor = Color::WHITE; + float mMetallic = 1.f; + float mRoughness = 1.f; + Vector4 mBaseColorFactor = Vector4::ONE; + float mNormalScale = 1.f; + float mOcclusionStrength = 1.f; + Vector3 mEmissiveFactor = Vector3::ZERO; + float mDielectricSpecular = 0.04f; + float mSpecularFactor = 1.0f; + Vector3 mSpecularColorFactor = Vector3::ONE; // For the glTF, each of albedo, metallicRoughness, normal textures are not essential. bool mNeedAlbedoTexture = true; diff --git a/dali-scene3d/public-api/loader/node-definition.cpp b/dali-scene3d/public-api/loader/node-definition.cpp index 24ad69146f..3a8e20533a 100644 --- a/dali-scene3d/public-api/loader/node-definition.cpp +++ b/dali-scene3d/public-api/loader/node-definition.cpp @@ -218,6 +218,9 @@ void ModelNode::OnCreate(const NodeDefinition& node, NodeDefinition::CreateParam actor.RegisterProperty("uColorFactor", matDef.mBaseColorFactor); actor.RegisterProperty("uMetallicFactor", matDef.mMetallic); actor.RegisterProperty("uRoughnessFactor", matDef.mRoughness); + actor.RegisterProperty("uDielectricSpecular", matDef.mDielectricSpecular); + actor.RegisterProperty("uSpecularFactor", matDef.mSpecularFactor); + actor.RegisterProperty("uSpecularColorFactor", matDef.mSpecularColorFactor); actor.RegisterProperty("uNormalScale", matDef.mNormalScale); if(matDef.mFlags & MaterialDefinition::OCCLUSION) { diff --git a/dali-scene3d/public-api/loader/shader-definition-factory.cpp b/dali-scene3d/public-api/loader/shader-definition-factory.cpp index f76745fab6..92ed552cac 100644 --- a/dali-scene3d/public-api/loader/shader-definition-factory.cpp +++ b/dali-scene3d/public-api/loader/shader-definition-factory.cpp @@ -244,6 +244,16 @@ Index ShaderDefinitionFactory::ProduceShader(const NodeDefinition& nodeDef) shaderDef.mDefines.push_back("SSS"); } + if(MaskMatch(materialDef.mFlags, MaterialDefinition::SPECULAR)) + { + shaderDef.mDefines.push_back("MATERIAL_SPECULAR_TEXTURE"); + } + + if(MaskMatch(materialDef.mFlags, MaterialDefinition::SPECULAR_COLOR)) + { + shaderDef.mDefines.push_back("MATERIAL_SPECULAR_COLOR_TEXTURE"); + } + if(MaskMatch(materialDef.mFlags, MaterialDefinition::OCCLUSION)) { shaderDef.mDefines.push_back("OCCLUSION");