From 9d65bb4795d14990daf08fba5a3de9404df4b913 Mon Sep 17 00:00:00 2001 From: dvonbeck Date: Tue, 2 Aug 2016 14:46:34 -0700 Subject: Fixed ambient lighting calculations on SkLightingShader BUG=skia:5520 GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2202233003 Review-Url: https://codereview.chromium.org/2202233003 --- src/core/SkLightingShader.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'src/core/SkLightingShader.cpp') diff --git a/src/core/SkLightingShader.cpp b/src/core/SkLightingShader.cpp index b478ee7eca..e35ede3a17 100644 --- a/src/core/SkLightingShader.cpp +++ b/src/core/SkLightingShader.cpp @@ -180,9 +180,8 @@ public: lightDirUniName); // diffuse light fragBuilder->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;", lightColorUniName); - // ambient light (multiplied by input color's alpha because we're working in premul'd - // space) - fragBuilder->codeAppendf("result += diffuseColor.a * %s;", ambientColorUniName); + // ambient light + fragBuilder->codeAppendf("result += %s * diffuseColor.rgb;", ambientColorUniName); // Clamping to alpha (equivalent to an unpremul'd clamp to 1.0) fragBuilder->codeAppendf("%s = vec4(clamp(result.rgb, 0.0, diffuseColor.a), " @@ -379,18 +378,18 @@ void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y, for (int l = 0; l < lightShader.fLights->numLights(); ++l) { const SkLights::Light& light = lightShader.fLights->light(l); - if (SkLights::Light::kAmbient_LightType == light.type()) { - accum += light.color().makeScale(255.0f); - } else { - SkScalar NdotL = normals[i].dot(light.dir()); - if (NdotL < 0.0f) { - NdotL = 0.0f; - } + SkScalar illuminanceScalingFactor = 1.0f; - accum.fX += light.color().fX * SkColorGetR(diffColor) * NdotL; - accum.fY += light.color().fY * SkColorGetG(diffColor) * NdotL; - accum.fZ += light.color().fZ * SkColorGetB(diffColor) * NdotL; + if (SkLights::Light::kDirectional_LightType == light.type()) { + illuminanceScalingFactor = normals[i].dot(light.dir()); + if (illuminanceScalingFactor < 0.0f) { + illuminanceScalingFactor = 0.0f; + } } + + accum.fX += light.color().fX * SkColorGetR(diffColor) * illuminanceScalingFactor; + accum.fY += light.color().fY * SkColorGetG(diffColor) * illuminanceScalingFactor; + accum.fZ += light.color().fZ * SkColorGetB(diffColor) * illuminanceScalingFactor; } // convert() premultiplies the accumulate color with alpha -- cgit v1.2.3