senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkPoint3.h" |
| 10 | #include "include/effects/SkLightingImageFilter.h" |
| 11 | #include "include/effects/SkOffsetImageFilter.h" |
| 12 | #include "tools/ToolUtils.h" |
| 13 | #include "tools/timer/AnimTimer.h" |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 14 | |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 15 | #define WIDTH 330 |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 16 | #define HEIGHT 660 |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 17 | |
| 18 | namespace skiagm { |
| 19 | |
| 20 | class ImageLightingGM : public GM { |
| 21 | public: |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 22 | ImageLightingGM() |
| 23 | : fAzimuth(SkIntToScalar(kStartAzimuth)) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 24 | this->setBGColor(0xFF000000); |
| 25 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 26 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 27 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | SkString onShortName() override { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 30 | return SkString("lighting"); |
| 31 | } |
| 32 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 33 | SkISize onISize() override { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 34 | return SkISize::Make(WIDTH, HEIGHT); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 35 | } |
| 36 | |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 37 | void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) { |
| 38 | canvas->save(); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 39 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 40 | canvas->clipRect(SkRect::MakeWH( |
| 41 | SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); |
| 42 | canvas->drawBitmap(fBitmap, 0, 0, &paint); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 43 | canvas->restore(); |
| 44 | } |
| 45 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 46 | void onOnceBeforeDraw() override { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 47 | fBitmap = ToolUtils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e"); |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 48 | } |
| 49 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 50 | void onDraw(SkCanvas* canvas) override { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 51 | canvas->clear(0xFF101010); |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 52 | SkPaint checkPaint; |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 53 | checkPaint.setColor(0xFF202020); |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 54 | for (int y = 0; y < HEIGHT; y += 16) { |
| 55 | for (int x = 0; x < WIDTH; x += 16) { |
| 56 | canvas->save(); |
| 57 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 58 | canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint); |
| 59 | canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint); |
| 60 | canvas->restore(); |
| 61 | } |
| 62 | } |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 63 | SkScalar sinAzimuth = SkScalarSin(SkDegreesToRadians(fAzimuth)), |
| 64 | cosAzimuth = SkScalarCos(SkDegreesToRadians(fAzimuth)); |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 65 | |
| 66 | SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0); |
| 67 | SkPoint3 spotLocation = SkPoint3::Make(spotTarget.fX + 70.7214f * cosAzimuth, |
| 68 | spotTarget.fY + 70.7214f * sinAzimuth, |
| 69 | spotTarget.fZ + SkIntToScalar(20)); |
| 70 | SkScalar spotExponent = SK_Scalar1; |
| 71 | |
| 72 | SkPoint3 pointLocation = SkPoint3::Make(spotTarget.fX + 50 * cosAzimuth, |
| 73 | spotTarget.fY + 50 * sinAzimuth, |
| 74 | SkIntToScalar(10)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 75 | SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5)); |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 76 | |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 77 | SkPoint3 distantDirection = SkPoint3::Make(cosAzimuth * SkScalarCos(elevationRad), |
| 78 | sinAzimuth * SkScalarCos(elevationRad), |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 79 | SkScalarSin(elevationRad)); |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 80 | SkScalar cutoffAngle = SkIntToScalar(15); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 81 | SkScalar kd = SkIntToScalar(2); |
| 82 | SkScalar ks = SkIntToScalar(1); |
| 83 | SkScalar shininess = SkIntToScalar(8); |
| 84 | SkScalar surfaceScale = SkIntToScalar(1); |
| 85 | SkColor white(0xFFFFFFFF); |
| 86 | SkPaint paint; |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 87 | |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 88 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65)); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 89 | SkImageFilter::CropRect fullSizeCropRect(SkRect::MakeXYWH(0, 0, 100, 100)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 90 | sk_sp<SkImageFilter> noopCropped(SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 91 | |
| 92 | int y = 0; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 93 | for (int i = 0; i < 3; i++) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 94 | const SkImageFilter::CropRect* cr = (i == 1) ? &cropRect : (i == 2) ? &fullSizeCropRect : nullptr; |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 95 | sk_sp<SkImageFilter> input = (i == 2) ? noopCropped : nullptr; |
| 96 | paint.setImageFilter(SkLightingImageFilter::MakePointLitDiffuse(pointLocation, |
| 97 | white, |
| 98 | surfaceScale, |
| 99 | kd, |
| 100 | input, |
| 101 | cr)); |
| 102 | drawClippedBitmap(canvas, paint, 0, y); |
| 103 | |
| 104 | paint.setImageFilter(SkLightingImageFilter::MakeDistantLitDiffuse(distantDirection, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 105 | white, |
| 106 | surfaceScale, |
| 107 | kd, |
| 108 | input, |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 109 | cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 110 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 111 | |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 112 | paint.setImageFilter(SkLightingImageFilter::MakeSpotLitDiffuse(spotLocation, |
| 113 | spotTarget, |
| 114 | spotExponent, |
| 115 | cutoffAngle, |
| 116 | white, |
| 117 | surfaceScale, |
| 118 | kd, |
| 119 | input, |
| 120 | cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 121 | drawClippedBitmap(canvas, paint, 220, y); |
| 122 | |
| 123 | y += 110; |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 124 | |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 125 | paint.setImageFilter(SkLightingImageFilter::MakePointLitSpecular(pointLocation, |
| 126 | white, |
| 127 | surfaceScale, |
| 128 | ks, |
| 129 | shininess, |
| 130 | input, |
| 131 | cr)); |
| 132 | drawClippedBitmap(canvas, paint, 0, y); |
| 133 | |
| 134 | paint.setImageFilter(SkLightingImageFilter::MakeDistantLitSpecular(distantDirection, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 135 | white, |
| 136 | surfaceScale, |
| 137 | ks, |
| 138 | shininess, |
| 139 | input, |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 140 | cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 141 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 142 | |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 143 | paint.setImageFilter(SkLightingImageFilter::MakeSpotLitSpecular(spotLocation, |
| 144 | spotTarget, |
| 145 | spotExponent, |
| 146 | cutoffAngle, |
| 147 | white, |
| 148 | surfaceScale, |
| 149 | ks, |
| 150 | shininess, |
| 151 | input, |
| 152 | cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 153 | drawClippedBitmap(canvas, paint, 220, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 154 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 155 | y += 110; |
| 156 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 157 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 158 | |
Mike Klein | cd5104e | 2019-03-20 11:55:08 -0500 | [diff] [blame] | 159 | bool onAnimate(const AnimTimer& timer) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 160 | constexpr SkScalar kDesiredDurationSecs = 15.0f; |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 161 | |
| 162 | fAzimuth = kStartAzimuth + timer.scaled(360.0f/kDesiredDurationSecs, 360.0f); |
| 163 | return true; |
| 164 | } |
| 165 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 166 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 167 | static constexpr int kStartAzimuth = 225; |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 168 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 169 | SkBitmap fBitmap; |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 170 | SkScalar fAzimuth; |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 171 | |
| 172 | typedef GM INHERITED; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | ////////////////////////////////////////////////////////////////////////////// |
| 176 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 177 | DEF_GM(return new ImageLightingGM;) |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 178 | } |