blob: 982e46a655012a360144ac0250fac822c8a7210a [file] [log] [blame]
Robert Phillipsa8cdbd72018-07-17 12:30:40 -04001/*
2 * Copyright 2016 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
8#include "gm.h"
9#include "sk_tool_utils.h"
10#include "SkLightingShader.h"
11#include "SkNormalSource.h"
12#include "SkPoint3.h"
13#include "SkShader.h"
14#include "SkTypeface.h"
15
16// Create a truncated pyramid normal map
17static SkBitmap make_frustum_normalmap(int texSize) {
18 SkBitmap frustum;
19 frustum.allocN32Pixels(texSize, texSize);
20
21 sk_tool_utils::create_frustum_normal_map(&frustum, SkIRect::MakeWH(texSize, texSize));
22 return frustum;
23}
24
25namespace skiagm {
26
27// This GM exercises lighting shaders. Specifically, nullptr arguments, scaling when using
28// normal maps, paint transparency, zero directional lights, multiple directional lights.
29class LightingShader2GM : public GM {
30public:
31 LightingShader2GM() : fRect(SkRect::MakeIWH(kTexSize, kTexSize)) {
32 this->setBGColor(sk_tool_utils::color_to_565(0xFF0000CC));
33 }
34
35protected:
36 SkString onShortName() override {
37 return SkString("lightingshader2");
38 }
39
40 SkISize onISize() override {
41 return SkISize::Make(600, 740);
42 }
43
44 void onOnceBeforeDraw() override {
45 // The light direction is towards the light with +Z coming out of the screen
46 const SkVector3 kLightFromUpperRight = SkVector3::Make(0.788f, 0.394f, 0.473f);
47 const SkVector3 kLightFromUpperLeft = SkVector3::Make(-0.788f, 0.394f, 0.473f);
48
49 // Standard set of lights
50 {
51 SkLights::Builder builder;
52 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f),
53 kLightFromUpperRight));
54 builder.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f));
55 fLights = builder.finish();
56 }
57
58 // No directional lights
59 {
60 SkLights::Builder builder;
61 builder.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f));
62 fLightsNoDir = builder.finish();
63 }
64
65 // Two directional lights
66 {
67 SkLights::Builder builder;
68 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 0.0f, 0.0f),
69 kLightFromUpperRight));
70 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(0.0f, 1.0f, 0.0f),
71 kLightFromUpperLeft));
72 builder.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f));
73 fLightsTwoDir = builder.finish();
74 }
75
76 SkMatrix matrix;
77 SkRect bitmapBounds = SkRect::MakeIWH(kTexSize, kTexSize);
78 matrix.setRectToRect(bitmapBounds, fRect, SkMatrix::kFill_ScaleToFit);
79
80 SkBitmap opaqueDiffuseMap = sk_tool_utils::create_checkerboard_bitmap(
81 kTexSize, kTexSize, SK_ColorBLACK,
Mike Kleind46dce32018-08-16 10:17:03 -040082 0xFF808080,
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040083 8);
84 fOpaqueDiffuse = SkShader::MakeBitmapShader(opaqueDiffuseMap, SkShader::kClamp_TileMode,
85 SkShader::kClamp_TileMode, &matrix);
86
87 SkBitmap translucentDiffuseMap = sk_tool_utils::create_checkerboard_bitmap(
88 kTexSize, kTexSize,
89 SkColorSetARGB(0x55, 0x00, 0x00, 0x00),
90 SkColorSetARGB(0x55, 0x80, 0x80, 0x80),
91 8);
92 fTranslucentDiffuse = SkShader::MakeBitmapShader(translucentDiffuseMap,
93 SkShader::kClamp_TileMode,
94 SkShader::kClamp_TileMode, &matrix);
95
96 SkBitmap normalMap = make_frustum_normalmap(kTexSize);
97 fNormalMapShader = SkShader::MakeBitmapShader(normalMap, SkShader::kClamp_TileMode,
98 SkShader::kClamp_TileMode, &matrix);
99
100 }
101
102 // Scales shape around origin, rotates shape around origin, then translates shape to origin
103 void positionCTM(SkCanvas *canvas, SkScalar scaleX, SkScalar scaleY, SkScalar rotate) const {
104 canvas->translate(kTexSize/2.0f, kTexSize/2.0f);
105 canvas->scale(scaleX, scaleY);
106 canvas->rotate(rotate);
107 canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f);
108 }
109
110 void drawRect(SkCanvas* canvas, SkScalar scaleX, SkScalar scaleY,
111 SkScalar rotate, bool useNormalSource, bool useDiffuseShader,
112 bool useTranslucentPaint, bool useTranslucentShader, sk_sp<SkLights> lights) {
113 canvas->save();
114
115 this->positionCTM(canvas, scaleX, scaleY, rotate);
116
117 const SkMatrix& ctm = canvas->getTotalMatrix();
118
119 SkPaint paint;
120 sk_sp<SkNormalSource> normalSource = nullptr;
121 sk_sp<SkShader> diffuseShader = nullptr;
122
123 if (useNormalSource) {
124 normalSource = SkNormalSource::MakeFromNormalMap(fNormalMapShader, ctm);
125 }
126
127 if (useDiffuseShader) {
128 diffuseShader = (useTranslucentShader) ? fTranslucentDiffuse : fOpaqueDiffuse;
129 } else {
130 paint.setColor(SK_ColorGREEN);
131 }
132
133 if (useTranslucentPaint) {
134 paint.setAlpha(0x99);
135 }
136
137 paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::move(normalSource),
138 std::move(lights)));
139 canvas->drawRect(fRect, paint);
140
141 canvas->restore();
142 }
143
144 void onDraw(SkCanvas* canvas) override {
145 SkPaint labelPaint;
146 labelPaint.setTypeface(sk_tool_utils::create_portable_typeface("sans-serif",SkFontStyle()));
147 labelPaint.setAntiAlias(true);
148 labelPaint.setTextSize(kLabelSize);
149
150 int gridNum = 0;
151
152 // Running through all possible bool parameter combinations
153 for (bool useNormalSource : {true, false}) {
154 for (bool useDiffuseShader : {true, false}) {
155 for (bool useTranslucentPaint : {true, false}) {
156 for (bool useTranslucentShader : {true, false}) {
157
158 // Determining position
159 SkScalar xPos = (gridNum % kGridColumnNum) * kGridCellWidth;
160 SkScalar yPos = (gridNum / kGridColumnNum) * kGridCellWidth;
161
162 canvas->save();
163
164 canvas->translate(xPos, yPos);
165 this->drawRect(canvas, 1.0f, 1.0f, 0.f, useNormalSource, useDiffuseShader,
166 useTranslucentPaint, useTranslucentShader, fLights);
167 // Drawing labels
168 canvas->translate(0.0f, SkIntToScalar(kTexSize));
169 {
170 canvas->translate(0.0f, kLabelSize);
171 SkString label;
172 label.appendf("useNormalSource: %d", useNormalSource);
173 canvas->drawString(label, 0.0f, 0.0f, labelPaint);
174 }
175 {
176 canvas->translate(0.0f, kLabelSize);
177 SkString label;
178 label.appendf("useDiffuseShader: %d", useDiffuseShader);
179 canvas->drawString(label, 0.0f, 0.0f, labelPaint);
180 }
181 {
182 canvas->translate(0.0f, kLabelSize);
183 SkString label;
184 label.appendf("useTranslucentPaint: %d", useTranslucentPaint);
185 canvas->drawString(label, 0.0f, 0.0f, labelPaint);
186 }
187 {
188 canvas->translate(0.0f, kLabelSize);
189 SkString label;
190 label.appendf("useTranslucentShader: %d", useTranslucentShader);
191 canvas->drawString(label, 0.0f, 0.0f, labelPaint);
192 }
193
194 canvas->restore();
195
196 gridNum++;
197 }
198 }
199 }
200 }
201
202
203 // Rotation/scale test
204 {
205 SkScalar xPos = (gridNum % kGridColumnNum) * kGridCellWidth;
206 SkScalar yPos = (gridNum / kGridColumnNum) * kGridCellWidth;
207
208 canvas->save();
209 canvas->translate(xPos, yPos);
210 this->drawRect(canvas, 0.6f, 0.6f, 45.0f, true, true, true, true, fLights);
211 canvas->restore();
212
213 gridNum++;
214 }
215
216 // Anisotropic scale test
217 {
218 SkScalar xPos = (gridNum % kGridColumnNum) * kGridCellWidth;
219 SkScalar yPos = (gridNum / kGridColumnNum) * kGridCellWidth;
220
221 canvas->save();
222 canvas->translate(xPos, yPos);
223 this->drawRect(canvas, 0.6f, 0.4f, 30.0f, true, true, true, true, fLights);
224 canvas->restore();
225
226 gridNum++;
227 }
228
229 // No directional lights test
230 {
231 SkScalar xPos = (gridNum % kGridColumnNum) * kGridCellWidth;
232 SkScalar yPos = (gridNum / kGridColumnNum) * kGridCellWidth;
233
234 canvas->save();
235 canvas->translate(xPos, yPos);
236 this->drawRect(canvas, 1.0f, 1.0f, 0.0f, true, true, false, false, fLightsNoDir);
237 canvas->restore();
238
239 gridNum++;
240 }
241
242 // Two directional lights test
243 {
244 SkScalar xPos = (gridNum % kGridColumnNum) * kGridCellWidth;
245 SkScalar yPos = (gridNum / kGridColumnNum) * kGridCellWidth;
246
247 canvas->save();
248 canvas->translate(xPos, yPos);
249 this->drawRect(canvas, 1.0f, 1.0f, 0.0f, true, true, false, false, fLightsTwoDir);
250 canvas->restore();
251
252 gridNum++;
253 }
254 }
255
256private:
257 static constexpr int kTexSize = 96;
258 static constexpr int kNumBooleanParams = 4;
259 static constexpr SkScalar kLabelSize = 10.0f;
260 static constexpr int kGridColumnNum = 4;
261 static constexpr SkScalar kGridCellWidth = kTexSize + 20.0f + kNumBooleanParams * kLabelSize;
262
263 sk_sp<SkShader> fOpaqueDiffuse;
264 sk_sp<SkShader> fTranslucentDiffuse;
265 sk_sp<SkShader> fNormalMapShader;
266
267 const SkRect fRect;
268 sk_sp<SkLights> fLights;
269 sk_sp<SkLights> fLightsNoDir;
270 sk_sp<SkLights> fLightsTwoDir;
271
272 typedef GM INHERITED;
273};
274
275//////////////////////////////////////////////////////////////////////////////
276
277DEF_GM(return new LightingShader2GM;)
278}