From 2f0dbc761a626473c19db7de561c7072b12953c5 Mon Sep 17 00:00:00 2001 From: robertphillips Date: Thu, 20 Aug 2015 05:15:06 -0700 Subject: Update SkLightingShader to support rotation This also: makes the SkLightingShader handle normal maps where the rects aren't aligned between the diffuse and normal maps. adds a light aggregating class (Lights) to SkLightingShader (along with a Builder nested class). Split out of https://codereview.chromium.org/1261433009/ (Add SkCanvas::drawLitAtlas call) Committed: https://skia.googlesource.com/skia/+/45b59ed6e4e231814dbdb9f707b3d2a7ee50de84 Review URL: https://codereview.chromium.org/1291783003 --- gm/lightingshader.cpp | 84 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 19 deletions(-) (limited to 'gm') diff --git a/gm/lightingshader.cpp b/gm/lightingshader.cpp index 7e33504477..c73ad5b4c8 100644 --- a/gm/lightingshader.cpp +++ b/gm/lightingshader.cpp @@ -19,7 +19,7 @@ static SkBitmap make_checkerboard(int texSize) { sk_tool_utils::draw_checkerboard(&canvas, sk_tool_utils::color_to_565(0x0), sk_tool_utils::color_to_565(0xFF804020), - 2); + 8); return bitmap; } @@ -58,10 +58,13 @@ public: LightingShaderGM() { this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); - fLight.fColor = SkColor3f::Make(1.0f, 1.0f, 1.0f); - fLight.fDirection = SkVector3::Make(0.0f, 0.0f, 1.0f); + SkLightingShader::Lights::Builder builder; - fAmbient = SkColor3f::Make(0.1f, 0.1f, 0.1f); + builder.add(SkLight(SkColor3f::Make(1.0f, 1.0f, 1.0f), + SkVector3::Make(1.0f, 0.0f, 0.0f))); + builder.add(SkLight(SkColor3f::Make(0.2f, 0.2f, 0.2f))); + + fLights.reset(builder.finish()); } protected: @@ -98,11 +101,16 @@ protected: SkMatrix matrix; matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); + const SkMatrix& ctm = canvas->getTotalMatrix(); + + // TODO: correctly pull out the pure rotation + SkVector invNormRotation = { ctm[SkMatrix::kMScaleX], ctm[SkMatrix::kMSkewY] }; + SkAutoTUnref fShader(SkLightingShader::Create( fDiffuse, fNormalMaps[mapType], - fLight, fAmbient, - &matrix)); + fLights, + invNormRotation, &matrix, &matrix)); SkPaint paint; paint.setShader(fShader); @@ -111,17 +119,56 @@ protected: } void onDraw(SkCanvas* canvas) override { - SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize)); - this->drawRect(canvas, r, kHemi_NormalMap); - - r.offset(kGMSize - kTexSize, 0); - this->drawRect(canvas, r, kFrustum_NormalMap); - - r.offset(0, kGMSize - kTexSize); - this->drawRect(canvas, r, kTetra_NormalMap); - - r.offset(kTexSize - kGMSize, 0); - this->drawRect(canvas, r, kHemi_NormalMap); + SkMatrix m; + SkRect r; + + { + r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize)); + this->drawRect(canvas, r, kHemi_NormalMap); + + canvas->save(); + m.setRotate(45.0f, r.centerX(), r.centerY()); + m.postTranslate(kGMSize/2.0f - kTexSize/2.0f, 0.0f); + canvas->setMatrix(m); + this->drawRect(canvas, r, kHemi_NormalMap); + canvas->restore(); + } + + { + r.offset(kGMSize - kTexSize, 0); + this->drawRect(canvas, r, kFrustum_NormalMap); + + canvas->save(); + m.setRotate(45.0f, r.centerX(), r.centerY()); + m.postTranslate(0.0f, kGMSize/2.0f - kTexSize/2.0f); + canvas->setMatrix(m); + this->drawRect(canvas, r, kFrustum_NormalMap); + canvas->restore(); + } + + { + r.offset(0, kGMSize - kTexSize); + this->drawRect(canvas, r, kTetra_NormalMap); + + canvas->save(); + m.setRotate(45.0f, r.centerX(), r.centerY()); + m.postTranslate(-kGMSize/2.0f + kTexSize/2.0f, 0.0f); + canvas->setMatrix(m); + this->drawRect(canvas, r, kTetra_NormalMap); + canvas->restore(); + } + + { + r.offset(kTexSize - kGMSize, 0); + this->drawRect(canvas, r, kHemi_NormalMap); + + canvas->save(); + m.setRotate(45.0f, r.centerX(), r.centerY()); + m.postTranslate(0.0f, -kGMSize/2.0f + kTexSize/2.0f); + canvas->setMatrix(m); + this->drawRect(canvas, r, kHemi_NormalMap); + canvas->restore(); + } } private: @@ -131,8 +178,7 @@ private: SkBitmap fDiffuse; SkBitmap fNormalMaps[kNormalMapCount]; - SkLightingShader::Light fLight; - SkColor3f fAmbient; + SkAutoTUnref fLights; typedef GM INHERITED; }; -- cgit v1.2.3