2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
10 #include "SkAnimTimer.h"
11 #include "SkLightingImageFilter.h"
12 #include "SkOffsetImageFilter.h"
20 class ImageLightingGM : public GM {
23 : fAzimuth(SkIntToScalar(kStartAzimuth)) {
24 this->setBGColor(0xFF000000);
29 SkString onShortName() override {
30 return SkString("lighting");
33 SkISize onISize() override {
34 return SkISize::Make(WIDTH, HEIGHT);
37 void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
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);
46 void onOnceBeforeDraw() override {
47 fBitmap = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
50 void onDraw(SkCanvas* canvas) override {
51 canvas->clear(sk_tool_utils::color_to_565(0xFF101010));
53 checkPaint.setColor(sk_tool_utils::color_to_565(0xFF202020));
54 for (int y = 0; y < HEIGHT; y += 16) {
55 for (int x = 0; x < WIDTH; x += 16) {
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);
64 SkScalar sinAzimuth = SkScalarSinCos(SkDegreesToRadians(fAzimuth), &cosAzimuth);
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;
72 SkPoint3 pointLocation = SkPoint3::Make(spotTarget.fX + 50 * cosAzimuth,
73 spotTarget.fY + 50 * sinAzimuth,
75 SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
77 SkPoint3 distantDirection = SkPoint3::Make(cosAzimuth * SkScalarCos(elevationRad),
78 sinAzimuth * SkScalarCos(elevationRad),
79 SkScalarSin(elevationRad));
80 SkScalar cutoffAngle = SkIntToScalar(15);
81 SkScalar kd = SkIntToScalar(2);
82 SkScalar ks = SkIntToScalar(1);
83 SkScalar shininess = SkIntToScalar(8);
84 SkScalar surfaceScale = SkIntToScalar(1);
85 SkColor white(0xFFFFFFFF);
88 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65));
89 SkImageFilter::CropRect fullSizeCropRect(SkRect::MakeXYWH(0, 0, 100, 100));
90 sk_sp<SkImageFilter> noopCropped(SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect));
93 for (int i = 0; i < 3; i++) {
94 const SkImageFilter::CropRect* cr = (i == 1) ? &cropRect : (i == 2) ? &fullSizeCropRect : nullptr;
95 sk_sp<SkImageFilter> input = (i == 2) ? noopCropped : nullptr;
96 paint.setImageFilter(SkLightingImageFilter::MakePointLitDiffuse(pointLocation,
102 drawClippedBitmap(canvas, paint, 0, y);
104 paint.setImageFilter(SkLightingImageFilter::MakeDistantLitDiffuse(distantDirection,
110 drawClippedBitmap(canvas, paint, 110, y);
112 paint.setImageFilter(SkLightingImageFilter::MakeSpotLitDiffuse(spotLocation,
121 drawClippedBitmap(canvas, paint, 220, y);
125 paint.setImageFilter(SkLightingImageFilter::MakePointLitSpecular(pointLocation,
132 drawClippedBitmap(canvas, paint, 0, y);
134 paint.setImageFilter(SkLightingImageFilter::MakeDistantLitSpecular(distantDirection,
141 drawClippedBitmap(canvas, paint, 110, y);
143 paint.setImageFilter(SkLightingImageFilter::MakeSpotLitSpecular(spotLocation,
153 drawClippedBitmap(canvas, paint, 220, y);
159 bool onAnimate(const SkAnimTimer& timer) override {
160 constexpr SkScalar kDesiredDurationSecs = 15.0f;
162 fAzimuth = kStartAzimuth + timer.scaled(360.0f/kDesiredDurationSecs, 360.0f);
167 static constexpr int kStartAzimuth = 225;
172 typedef GM INHERITED;
175 //////////////////////////////////////////////////////////////////////////////
177 DEF_GM(return new ImageLightingGM;)