Fix Scene3d-View light issue 34/273934/7
authorseungho <sbsh.baek@samsung.com>
Mon, 18 Apr 2022 09:02:30 +0000 (18:02 +0900)
committerseungho <sbsh.baek@samsung.com>
Wed, 27 Apr 2022 04:44:37 +0000 (13:44 +0900)
Change-Id: I61bd1ed2ec4dacec5952e82666ad476480fed774
Signed-off-by: seungho <sbsh.baek@samsung.com>
dali-toolkit/devel-api/controls/scene3d-view/scene3d-view.h
dali-toolkit/internal/controls/scene3d-view/gltf-loader.cpp
dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.cpp
dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.h
dali-toolkit/internal/graphics/shaders/gltf-physically-based-shader.frag
dali-toolkit/internal/graphics/shaders/gltf-physically-based-shader.vert

index 1f0effb..756a7a5 100644 (file)
@@ -80,15 +80,9 @@ public:
     // Scene doesn't use both of point and directional light\r
     NONE = 0,\r
     // Scene use point light\r
     // Scene doesn't use both of point and directional light\r
     NONE = 0,\r
     // Scene use point light\r
-    POINT_LIGHT,\r
+    POINT_LIGHT = 1,\r
     // Scene use directional light\r
     // Scene use directional light\r
-    DIRECTIONAL_LIGHT,\r
-    // Scene use Image Based Lighting\r
-    IMAGE_BASED_LIGHT,\r
-    // Scene use Image Based Lighting and point light\r
-    IMAGE_BASED_LIGHT_AND_POINT_LIGHT,\r
-    // Scene use Image Based Lighting and directional light\r
-    IMAGE_BASED_LIGHT_AND_DIRECTIONAL_LIGHT\r
+    DIRECTIONAL_LIGHT = 2\r
   };\r
 \r
   /**\r
   };\r
 \r
   /**\r
index 2c8de12..55dfd8d 100644 (file)
@@ -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();
 
     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];
     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);
 
     actor.RotateBy(orientation);
     actor.SetProperty(Actor::Property::POSITION, translation);
 
-    shader.RegisterProperty("uLightType", (scene3dView.GetLightType() & ~Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT));
+    float hasLightSource = static_cast<float>(!!(scene3dView.GetLightType() & (Toolkit::Scene3dView::LightType::POINT_LIGHT | Toolkit::Scene3dView::LightType::DIRECTIONAL_LIGHT)));
+    float isPointLight = static_cast<float>(!!(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());
 
     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];
     if(isMaterial)
     {
       MaterialInfo materialInfo = mMaterialArray[meshInfo.materialsIdx];
@@ -1605,15 +1608,15 @@ Actor Loader::AddNode(Scene3dView& scene3dView, uint32_t index)
 
       if(materialInfo.alphaMode == "OPAQUE")
       {
 
       if(materialInfo.alphaMode == "OPAQUE")
       {
-        actor.RegisterProperty("alphaMode", 0);
+        actor.RegisterProperty("uAlphaMode", 0.0f);
       }
       else if(materialInfo.alphaMode == "MASK")
       {
       }
       else if(materialInfo.alphaMode == "MASK")
       {
-        actor.RegisterProperty("alphaMode", 1);
+        actor.RegisterProperty("uAlphaMode", 1.0f);
       }
       else
       {
       }
       else
       {
-        actor.RegisterProperty("alphaMode", 2);
+        actor.RegisterProperty("uAlphaMode", 2.0f);
       }
       actor.RegisterProperty("alphaCutoff", materialInfo.alphaCutoff);
 
       }
       actor.RegisterProperty("alphaCutoff", materialInfo.alphaCutoff);
 
index aa8438a..6231b64 100644 (file)
@@ -54,7 +54,8 @@ Scene3dView::Scene3dView()
   mAnimationArray(),
   mLightType(Toolkit::Scene3dView::LightType::NONE),
   mLightVector(Vector3::ONE),
   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)
 {
 
 bool Scene3dView::SetLight(Toolkit::Scene3dView::LightType type, Vector3 lightVector, Vector3 lightColor)
 {
-  if(type > Toolkit::Scene3dView::LightType::DIRECTIONAL_LIGHT)
-  {
-    return false;
-  }
-
-  mLightType = static_cast<Toolkit::Scene3dView::LightType>(
-    (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)
   {
   mLightVector = lightVector;
   mLightColor  = lightColor;
 
   for(auto&& shader : mShaderArray)
   {
-    shader.RegisterProperty("uLightType", (GetLightType() & ~Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT));
+    float hasLightSource = static_cast<float>(!!(GetLightType() & (Toolkit::Scene3dView::LightType::POINT_LIGHT | Toolkit::Scene3dView::LightType::DIRECTIONAL_LIGHT)));
+    float isPointLight = static_cast<float>(!!(GetLightType() & Toolkit::Scene3dView::LightType::POINT_LIGHT));
+    shader.RegisterProperty("uHasLightSource", hasLightSource);
+    shader.RegisterProperty("uIsPointLight", isPointLight);
     shader.RegisterProperty("uLightVector", lightVector);
     shader.RegisterProperty("uLightColor", lightColor);
   }
     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)
 {
 
 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;
   // 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;
   mSpecularTexture.GenerateMipmaps();
 
   mIBLScaleFactor = scaleFactor;
+  mUseIBL = true;
 }
 
 bool Scene3dView::SetDefaultCamera(const Dali::Camera::Type type, const float nearPlane, const Vector3 cameraPosition)
 }
 
 bool Scene3dView::SetDefaultCamera(const Dali::Camera::Type type, const float nearPlane, const Vector3 cameraPosition)
@@ -318,6 +314,11 @@ Texture Scene3dView::GetSpecularTexture()
   return mSpecularTexture;
 }
 
   return mSpecularTexture;
 }
 
+bool Scene3dView::HasImageBasedLighting()
+{
+  return mUseIBL;
+}
+
 Texture Scene3dView::GetDiffuseTexture()
 {
   return mDiffuseTexture;
 Texture Scene3dView::GetDiffuseTexture()
 {
   return mDiffuseTexture;
index 6dbe859..bc731d3 100644 (file)
@@ -185,6 +185,11 @@ public:
    */\r
   Texture GetSpecularTexture();\r
 \r
    */\r
   Texture GetSpecularTexture();\r
 \r
+  /**\r
+   * @brief Get whether the scene has image based rendering or not.\r
+   */\r
+  bool HasImageBasedLighting();\r
+\r
 private:\r
   /**\r
    * @brief Get Cropped image buffer.\r
 private:\r
   /**\r
    * @brief Get Cropped image buffer.\r
@@ -232,6 +237,7 @@ private:
   Texture mBRDFTexture;     // BRDF texture for the PBR rendering\r
   Texture mSpecularTexture; // Specular cube map texture\r
   Texture mDiffuseTexture;  // Diffuse cube map texture\r
   Texture mBRDFTexture;     // BRDF texture for the PBR rendering\r
   Texture mSpecularTexture; // Specular cube map texture\r
   Texture mDiffuseTexture;  // Diffuse cube map texture\r
+  bool    mUseIBL;\r
 \r
 private:\r
   // Undefined copy constructor.\r
 \r
 private:\r
   // Undefined copy constructor.\r
index cefd19e..9477e40 100644 (file)
@@ -1,13 +1,13 @@
 uniform lowp vec3 uLightColor;
 uniform lowp vec4 uBaseColorFactor;
 uniform lowp vec2 uMetallicRoughnessFactors;
 uniform lowp vec3 uLightColor;
 uniform lowp vec4 uBaseColorFactor;
 uniform lowp vec2 uMetallicRoughnessFactors;
-uniform lowp int alphaMode;
 uniform lowp float alphaCutoff;
 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;
 
 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;
 
 in highp vec3 vLightDirection;
 in highp vec3 vPositionToCamera;
 
@@ -30,20 +30,20 @@ const float c_MinRoughness = 0.04;
 vec3 getNormal()
 {
 #ifdef TEXTURE_NORMAL
 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;
 }
 
 #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;
 {
   mediump float NdotL = pbrInputs.NdotL;
   mediump float NdotV = pbrInputs.NdotV;
@@ -61,7 +61,7 @@ float microfacetDistribution(PBRInfo pbrInputs)
   return roughnessSq / (M_PI * f * f);
 }
 
   return roughnessSq / (M_PI * f * f);
 }
 
-vec3 linear( vec3 color )
+vec3 linear(vec3 color)
 {
   return pow(color,vec3(2.2));
 }
 {
   return pow(color,vec3(2.2));
 }
@@ -96,13 +96,13 @@ void main()
   lowp vec4 baseColor = vColor * uBaseColorFactor;
 #endif
 
   lowp vec4 baseColor = vColor * uBaseColorFactor;
 #endif
 
-  if( alphaMode == 0 )
+  if(uAlphaMode < 0.5f)
   {
     baseColor.w = 1.0;
   }
   {
     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;
     }
     {
       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);
 
   // 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 );
   {
     lowp vec3 F = specularReflection( pbrInputs );
     lowp float G = geometricOcclusion( pbrInputs );
index 6628865..095e839 100644 (file)
@@ -9,51 +9,34 @@ uniform mediump vec3 uSize;
 uniform mediump mat4 uModelMatrix;
 uniform mediump mat4 uViewMatrix;
 uniform mediump mat4 uProjection;
 uniform mediump mat4 uModelMatrix;
 uniform mediump mat4 uViewMatrix;
 uniform mediump mat4 uProjection;
-uniform lowp int uLightType;
 uniform mediump vec3 uLightVector;
 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;
 
 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);
 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;
   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;
 
 
   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;
 
   gl_Position = uProjection * positionV; // needs w for proper perspective correction
   gl_Position = gl_Position/gl_Position.w;