From 86d2d5c33fbe22db4e0498e758c27f81bee990cc Mon Sep 17 00:00:00 2001 From: seungho Date: Mon, 18 Apr 2022 18:02:30 +0900 Subject: [PATCH] Fix Scene3d-View light issue Change-Id: I61bd1ed2ec4dacec5952e82666ad476480fed774 Signed-off-by: seungho --- .../devel-api/controls/scene3d-view/scene3d-view.h | 10 ++----- .../internal/controls/scene3d-view/gltf-loader.cpp | 15 ++++++---- .../controls/scene3d-view/scene3d-view-impl.cpp | 25 ++++++++-------- .../controls/scene3d-view/scene3d-view-impl.h | 6 ++++ .../shaders/gltf-physically-based-shader.frag | 24 +++++++-------- .../shaders/gltf-physically-based-shader.vert | 35 ++++++---------------- 6 files changed, 51 insertions(+), 64 deletions(-) diff --git a/dali-toolkit/devel-api/controls/scene3d-view/scene3d-view.h b/dali-toolkit/devel-api/controls/scene3d-view/scene3d-view.h index 1f0effb..756a7a5 100644 --- a/dali-toolkit/devel-api/controls/scene3d-view/scene3d-view.h +++ b/dali-toolkit/devel-api/controls/scene3d-view/scene3d-view.h @@ -80,15 +80,9 @@ public: // Scene doesn't use both of point and directional light NONE = 0, // Scene use point light - POINT_LIGHT, + POINT_LIGHT = 1, // Scene use directional light - DIRECTIONAL_LIGHT, - // Scene use Image Based Lighting - IMAGE_BASED_LIGHT, - // Scene use Image Based Lighting and point light - IMAGE_BASED_LIGHT_AND_POINT_LIGHT, - // Scene use Image Based Lighting and directional light - IMAGE_BASED_LIGHT_AND_DIRECTIONAL_LIGHT + DIRECTIONAL_LIGHT = 2 }; /** diff --git a/dali-toolkit/internal/controls/scene3d-view/gltf-loader.cpp b/dali-toolkit/internal/controls/scene3d-view/gltf-loader.cpp index 2c8de12..55dfd8d 100644 --- a/dali-toolkit/internal/controls/scene3d-view/gltf-loader.cpp +++ b/dali-toolkit/internal/controls/scene3d-view/gltf-loader.cpp @@ -1509,7 +1509,7 @@ Actor Loader::AddNode(Scene3dView& scene3dView, uint32_t index) VERTEX_SHADER += SHADER_GLTF_PHYSICALLY_BASED_SHADER_VERT.data(); FRAGMENT_SHADER = SHADER_GLTF_GLES_VERSION_300_DEF.data(); - bool useIBL = (scene3dView.GetLightType() >= Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT); + bool useIBL = scene3dView.HasImageBasedLighting(); if(isMaterial) { MaterialInfo materialInfo = mMaterialArray[meshInfo.materialsIdx]; @@ -1592,11 +1592,14 @@ Actor Loader::AddNode(Scene3dView& scene3dView, uint32_t index) actor.RotateBy(orientation); actor.SetProperty(Actor::Property::POSITION, translation); - shader.RegisterProperty("uLightType", (scene3dView.GetLightType() & ~Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT)); + float hasLightSource = static_cast(!!(scene3dView.GetLightType() & (Toolkit::Scene3dView::LightType::POINT_LIGHT | Toolkit::Scene3dView::LightType::DIRECTIONAL_LIGHT))); + float isPointLight = static_cast(!!(scene3dView.GetLightType() & Toolkit::Scene3dView::LightType::POINT_LIGHT)); + shader.RegisterProperty("uHasLightSource", hasLightSource); + shader.RegisterProperty("uIsPointLight", isPointLight); shader.RegisterProperty("uLightVector", scene3dView.GetLightVector()); shader.RegisterProperty("uLightColor", scene3dView.GetLightColor()); - actor.RegisterProperty("uIsColor", meshInfo.attribute.COLOR.size() > 0); + actor.RegisterProperty("uHasVertexColor", meshInfo.attribute.COLOR.size() > 0 ? 1.0f : 0.0f); if(isMaterial) { MaterialInfo materialInfo = mMaterialArray[meshInfo.materialsIdx]; @@ -1605,15 +1608,15 @@ Actor Loader::AddNode(Scene3dView& scene3dView, uint32_t index) if(materialInfo.alphaMode == "OPAQUE") { - actor.RegisterProperty("alphaMode", 0); + actor.RegisterProperty("uAlphaMode", 0.0f); } else if(materialInfo.alphaMode == "MASK") { - actor.RegisterProperty("alphaMode", 1); + actor.RegisterProperty("uAlphaMode", 1.0f); } else { - actor.RegisterProperty("alphaMode", 2); + actor.RegisterProperty("uAlphaMode", 2.0f); } actor.RegisterProperty("alphaCutoff", materialInfo.alphaCutoff); diff --git a/dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.cpp b/dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.cpp index aa8438a..6231b64 100644 --- a/dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.cpp +++ b/dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.cpp @@ -54,7 +54,8 @@ Scene3dView::Scene3dView() mAnimationArray(), mLightType(Toolkit::Scene3dView::LightType::NONE), mLightVector(Vector3::ONE), - mLightColor(Vector3::ONE) + mLightColor(Vector3::ONE), + mUseIBL(false) { } @@ -130,20 +131,16 @@ bool Scene3dView::PlayAnimations() bool Scene3dView::SetLight(Toolkit::Scene3dView::LightType type, Vector3 lightVector, Vector3 lightColor) { - if(type > Toolkit::Scene3dView::LightType::DIRECTIONAL_LIGHT) - { - return false; - } - - mLightType = static_cast( - (mLightType >= Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT) ? Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT + type : type); - + mLightType = type; mLightVector = lightVector; mLightColor = lightColor; for(auto&& shader : mShaderArray) { - shader.RegisterProperty("uLightType", (GetLightType() & ~Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT)); + float hasLightSource = static_cast(!!(GetLightType() & (Toolkit::Scene3dView::LightType::POINT_LIGHT | Toolkit::Scene3dView::LightType::DIRECTIONAL_LIGHT))); + float isPointLight = static_cast(!!(GetLightType() & Toolkit::Scene3dView::LightType::POINT_LIGHT)); + shader.RegisterProperty("uHasLightSource", hasLightSource); + shader.RegisterProperty("uIsPointLight", isPointLight); shader.RegisterProperty("uLightVector", lightVector); shader.RegisterProperty("uLightColor", lightColor); } @@ -203,8 +200,6 @@ void Scene3dView::UploadTextureFace(Texture& texture, Devel::PixelBuffer pixelBu void Scene3dView::SetCubeMap(const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 scaleFactor) { - mLightType = Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT; - // BRDF texture const std::string imageDirPath = AssetManager::GetDaliImagePath(); const std::string imageBrdfUrl = imageDirPath + IMAGE_BRDF_FILE_NAME; @@ -235,6 +230,7 @@ void Scene3dView::SetCubeMap(const std::string& diffuseTexturePath, const std::s mSpecularTexture.GenerateMipmaps(); mIBLScaleFactor = scaleFactor; + mUseIBL = true; } bool Scene3dView::SetDefaultCamera(const Dali::Camera::Type type, const float nearPlane, const Vector3 cameraPosition) @@ -318,6 +314,11 @@ Texture Scene3dView::GetSpecularTexture() return mSpecularTexture; } +bool Scene3dView::HasImageBasedLighting() +{ + return mUseIBL; +} + Texture Scene3dView::GetDiffuseTexture() { return mDiffuseTexture; diff --git a/dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.h b/dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.h index 6dbe859..bc731d3 100644 --- a/dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.h +++ b/dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.h @@ -185,6 +185,11 @@ public: */ Texture GetSpecularTexture(); + /** + * @brief Get whether the scene has image based rendering or not. + */ + bool HasImageBasedLighting(); + private: /** * @brief Get Cropped image buffer. @@ -232,6 +237,7 @@ private: Texture mBRDFTexture; // BRDF texture for the PBR rendering Texture mSpecularTexture; // Specular cube map texture Texture mDiffuseTexture; // Diffuse cube map texture + bool mUseIBL; private: // Undefined copy constructor. diff --git a/dali-toolkit/internal/graphics/shaders/gltf-physically-based-shader.frag b/dali-toolkit/internal/graphics/shaders/gltf-physically-based-shader.frag index cefd19e..9477e40 100644 --- a/dali-toolkit/internal/graphics/shaders/gltf-physically-based-shader.frag +++ b/dali-toolkit/internal/graphics/shaders/gltf-physically-based-shader.frag @@ -1,13 +1,13 @@ uniform lowp vec3 uLightColor; uniform lowp vec4 uBaseColorFactor; uniform lowp vec2 uMetallicRoughnessFactors; -uniform lowp int alphaMode; uniform lowp float alphaCutoff; +uniform lowp float uAlphaMode; +uniform lowp float uHasLightSource; in lowp vec2 vUV[2]; in lowp mat3 vTBN; in lowp vec4 vColor; -flat in int visLight; in highp vec3 vLightDirection; in highp vec3 vPositionToCamera; @@ -30,20 +30,20 @@ const float c_MinRoughness = 0.04; vec3 getNormal() { #ifdef TEXTURE_NORMAL - lowp vec3 n = texture( uNormalSampler, vUV[uNormalTexCoordIndex] ).rgb; - n = normalize( vTBN * ( ( 2.0 * n - 1.0 ) * vec3( uNormalScale, uNormalScale, 1.0 ) ) ); + lowp vec3 n = texture(uNormalSampler, vUV[uNormalTexCoordIndex]).rgb; + n = normalize(vTBN * ((2.0 * n - 1.0) * vec3(uNormalScale, uNormalScale, 1.0))); #else lowp vec3 n = normalize( vTBN[2].xyz ); #endif return n; } -vec3 specularReflection( PBRInfo pbrInputs ) +vec3 specularReflection(PBRInfo pbrInputs) { - return pbrInputs.reflectance0 + ( pbrInputs.reflectance90 - pbrInputs.reflectance0 ) * pow( clamp( 1.0 - pbrInputs.VdotH, 0.0, 1.0 ), 5.0 ); + return pbrInputs.reflectance0 + (pbrInputs.reflectance90 - pbrInputs.reflectance0) * pow(clamp(1.0 - pbrInputs.VdotH, 0.0, 1.0), 5.0); } -float geometricOcclusion( PBRInfo pbrInputs ) +float geometricOcclusion(PBRInfo pbrInputs) { mediump float NdotL = pbrInputs.NdotL; mediump float NdotV = pbrInputs.NdotV; @@ -61,7 +61,7 @@ float microfacetDistribution(PBRInfo pbrInputs) return roughnessSq / (M_PI * f * f); } -vec3 linear( vec3 color ) +vec3 linear(vec3 color) { return pow(color,vec3(2.2)); } @@ -96,13 +96,13 @@ void main() lowp vec4 baseColor = vColor * uBaseColorFactor; #endif - if( alphaMode == 0 ) + if(uAlphaMode < 0.5f) { baseColor.w = 1.0; } - else if( alphaMode == 1 ) + else if(uAlphaMode < 1.5f) { - if( baseColor.w >= alphaCutoff ) + if(baseColor.w >= alphaCutoff) { baseColor.w = 1.0; } @@ -150,7 +150,7 @@ void main() // Calculate the shading terms for the microfacet specular shading model lowp vec3 color = vec3(0.0); - if( visLight == 1 ) + if( uHasLightSource > 0.5f ) { lowp vec3 F = specularReflection( pbrInputs ); lowp float G = geometricOcclusion( pbrInputs ); diff --git a/dali-toolkit/internal/graphics/shaders/gltf-physically-based-shader.vert b/dali-toolkit/internal/graphics/shaders/gltf-physically-based-shader.vert index 6628865..095e839 100644 --- a/dali-toolkit/internal/graphics/shaders/gltf-physically-based-shader.vert +++ b/dali-toolkit/internal/graphics/shaders/gltf-physically-based-shader.vert @@ -9,51 +9,34 @@ uniform mediump vec3 uSize; uniform mediump mat4 uModelMatrix; uniform mediump mat4 uViewMatrix; uniform mediump mat4 uProjection; -uniform lowp int uLightType; uniform mediump vec3 uLightVector; -uniform lowp int uIsColor; +uniform lowp float uIsPointLight; +uniform lowp float uHasVertexColor; out lowp vec2 vUV[2]; out lowp mat3 vTBN; out lowp vec4 vColor; -flat out int visLight; out highp vec3 vLightDirection; out highp vec3 vPositionToCamera; void main() { highp vec4 invY = vec4(1.0, -1.0, 1.0, 1.0); - highp vec4 positionW = uModelMatrix * vec4( aPosition * uSize, 1.0 ); - highp vec4 positionV = uViewMatrix * ( invY * positionW ); + highp vec4 positionW = uModelMatrix * vec4(aPosition * uSize, 1.0); + highp vec4 positionV = uViewMatrix * (invY * positionW); - vPositionToCamera = transpose( mat3( uViewMatrix ) ) * ( -vec3( positionV.xyz / positionV.w ) ); + vPositionToCamera = transpose(mat3(uViewMatrix)) * (-vec3(positionV.xyz / positionV.w)); vPositionToCamera *= invY.xyz; lowp vec3 bitangent = cross(aNormal, aTangent.xyz) * aTangent.w; - vTBN = mat3( uModelMatrix ) * mat3(aTangent.xyz, bitangent, aNormal); + vTBN = mat3(uModelMatrix) * mat3(aTangent.xyz, bitangent, aNormal); vUV[0] = aTexCoord0; vUV[1] = aTexCoord1; - visLight = 1; - if( uLightType == 1 ) - { - vLightDirection = ( invY.xyz * uLightVector ) - ( positionW.xyz / positionW.w ); - } - else if( uLightType == 2 ) - { - vLightDirection = -( invY.xyz * uLightVector ); - } - else - { - visLight = 0; - } - - vColor = vec4( 1.0 ); - if( uIsColor == 1 ) - { - vColor = aVertexColor; - } + vLightDirection = mix(-(invY.xyz * uLightVector), (invY.xyz * uLightVector) - (positionW.xyz / positionW.w), uIsPointLight); + + vColor = mix(vec4(1.0f), aVertexColor, uHasVertexColor); gl_Position = uProjection * positionV; // needs w for proper perspective correction gl_Position = gl_Position/gl_Position.w; -- 2.7.4