dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 1 | /* |
| 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" |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 9 | #include "SkLightingShader.h" |
| 10 | #include "SkNormalSource.h" |
| 11 | #include "SkPoint3.h" |
| 12 | #include "SkShader.h" |
| 13 | |
| 14 | // Create a truncated pyramid normal map |
| 15 | static SkBitmap make_frustum_normalmap(int texSize) { |
| 16 | SkBitmap frustum; |
| 17 | frustum.allocN32Pixels(texSize, texSize); |
| 18 | |
| 19 | sk_tool_utils::create_frustum_normal_map(&frustum, SkIRect::MakeWH(texSize, texSize)); |
| 20 | return frustum; |
| 21 | } |
| 22 | |
| 23 | namespace skiagm { |
| 24 | |
| 25 | // This GM exercises lighting shaders. Specifically, nullptr arguments, scaling when using |
| 26 | // normal maps, and paint transparency. |
| 27 | class LightingShader2GM : public GM { |
| 28 | public: |
| 29 | LightingShader2GM() { |
| 30 | this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
| 31 | } |
| 32 | |
| 33 | protected: |
| 34 | SkString onShortName() override { |
| 35 | return SkString("lightingshader2"); |
| 36 | } |
| 37 | |
| 38 | SkISize onISize() override { |
dvonbeck | 680e2e9 | 2016-07-21 12:19:54 -0700 | [diff] [blame] | 39 | return SkISize::Make(600, 740); |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void onOnceBeforeDraw() override { |
| 43 | SkLights::Builder builder; |
| 44 | const SkVector3 kLightFromUpperRight = SkVector3::Make(0.788f, 0.394f, 0.473f); |
| 45 | |
| 46 | builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), |
| 47 | kLightFromUpperRight)); |
| 48 | builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f))); |
| 49 | fLights = builder.finish(); |
| 50 | |
| 51 | fRect = SkRect::MakeIWH(kTexSize, kTexSize); |
| 52 | SkMatrix matrix; |
| 53 | SkRect bitmapBounds = SkRect::MakeIWH(kTexSize, kTexSize); |
| 54 | matrix.setRectToRect(bitmapBounds, fRect, SkMatrix::kFill_ScaleToFit); |
| 55 | |
| 56 | SkBitmap opaqueDiffuseMap = sk_tool_utils::create_checkerboard_bitmap( |
| 57 | kTexSize, kTexSize, |
| 58 | sk_tool_utils::color_to_565(0x0), |
| 59 | sk_tool_utils::color_to_565(0xFF804020), |
| 60 | 8); |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 61 | fOpaqueDiffuse = SkShader::MakeBitmapShader(opaqueDiffuseMap, SkShader::kClamp_TileMode, |
| 62 | SkShader::kClamp_TileMode, &matrix); |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 63 | |
| 64 | SkBitmap translucentDiffuseMap = sk_tool_utils::create_checkerboard_bitmap( |
| 65 | kTexSize, kTexSize, |
| 66 | SkColorSetARGB(0x55, 0x00, 0x00, 0x00), |
| 67 | SkColorSetARGB(0x55, 0x80, 0x40, 0x20), |
| 68 | 8); |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 69 | fTranslucentDiffuse = SkShader::MakeBitmapShader(translucentDiffuseMap, |
| 70 | SkShader::kClamp_TileMode, |
| 71 | SkShader::kClamp_TileMode, &matrix); |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 72 | |
| 73 | SkBitmap normalMap = make_frustum_normalmap(kTexSize); |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 74 | fNormalMapShader = SkShader::MakeBitmapShader(normalMap, SkShader::kClamp_TileMode, |
| 75 | SkShader::kClamp_TileMode, &matrix); |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 76 | |
| 77 | } |
| 78 | |
| 79 | // Scales shape around origin, rotates shape around origin, then translates shape to origin |
| 80 | void positionCTM(SkCanvas *canvas, SkScalar scaleX, SkScalar scaleY, SkScalar rotate) const { |
| 81 | canvas->translate(kTexSize/2.0f, kTexSize/2.0f); |
| 82 | canvas->scale(scaleX, scaleY); |
| 83 | canvas->rotate(rotate); |
| 84 | canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f); |
| 85 | } |
| 86 | |
| 87 | static constexpr int NUM_BOOLEAN_PARAMS = 4; |
| 88 | void drawRect(SkCanvas* canvas, SkScalar scaleX, SkScalar scaleY, |
| 89 | SkScalar rotate, bool useNormalSource, bool useDiffuseShader, |
| 90 | bool useTranslucentPaint, bool useTranslucentShader) { |
| 91 | canvas->save(); |
| 92 | |
| 93 | this->positionCTM(canvas, scaleX, scaleY, rotate); |
| 94 | |
| 95 | const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 96 | |
| 97 | SkPaint paint; |
| 98 | sk_sp<SkNormalSource> normalSource = nullptr; |
| 99 | sk_sp<SkShader> diffuseShader = nullptr; |
| 100 | |
| 101 | if (useNormalSource) { |
| 102 | normalSource = SkNormalSource::MakeFromNormalMap(fNormalMapShader, ctm); |
| 103 | } |
| 104 | |
| 105 | if (useDiffuseShader) { |
| 106 | diffuseShader = (useTranslucentShader) ? fTranslucentDiffuse : fOpaqueDiffuse; |
| 107 | } else { |
| 108 | paint.setColor(0xFF00FF00); |
| 109 | } |
| 110 | |
| 111 | if (useTranslucentPaint) { |
| 112 | paint.setAlpha(0x99); |
| 113 | } |
| 114 | |
| 115 | paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::move(normalSource), |
| 116 | fLights)); |
| 117 | canvas->drawRect(fRect, paint); |
| 118 | |
| 119 | canvas->restore(); |
| 120 | } |
| 121 | |
| 122 | void onDraw(SkCanvas* canvas) override { |
| 123 | |
| 124 | constexpr SkScalar LABEL_SIZE = 10.0f; |
| 125 | SkPaint labelPaint; |
| 126 | labelPaint.setTypeface(sk_tool_utils::create_portable_typeface("sans-serif", |
| 127 | SkFontStyle())); |
| 128 | labelPaint.setAntiAlias(true); |
| 129 | labelPaint.setTextSize(LABEL_SIZE); |
| 130 | |
| 131 | constexpr int GRID_COLUMN_NUM = 4; |
| 132 | constexpr SkScalar GRID_CELL_WIDTH = kTexSize + 20.0f + NUM_BOOLEAN_PARAMS * LABEL_SIZE; |
| 133 | |
| 134 | int gridNum = 0; |
| 135 | |
| 136 | // Running through all possible bool parameter combinations |
| 137 | for (bool useNormalSource : {true, false}) { |
| 138 | for (bool useDiffuseShader : {true, false}) { |
| 139 | for (bool useTranslucentPaint : {true, false}) { |
| 140 | for (bool useTranslucentShader : {true, false}) { |
| 141 | |
| 142 | // Determining position |
| 143 | SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH; |
| 144 | SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH; |
| 145 | |
| 146 | canvas->save(); |
| 147 | |
| 148 | canvas->translate(xPos, yPos); |
| 149 | this->drawRect(canvas, 1.0f, 1.0f, 0.f, useNormalSource, useDiffuseShader, |
| 150 | useTranslucentPaint, useTranslucentShader); |
| 151 | // Drawing labels |
| 152 | canvas->translate(0.0f, SkIntToScalar(kTexSize)); |
| 153 | { |
| 154 | canvas->translate(0.0f, LABEL_SIZE); |
| 155 | SkString label; |
| 156 | label.appendf("useNormalSource: %d", useNormalSource); |
| 157 | canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint); |
| 158 | } |
| 159 | { |
| 160 | canvas->translate(0.0f, LABEL_SIZE); |
| 161 | SkString label; |
| 162 | label.appendf("useDiffuseShader: %d", useDiffuseShader); |
| 163 | canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint); |
| 164 | } |
| 165 | { |
| 166 | canvas->translate(0.0f, LABEL_SIZE); |
| 167 | SkString label; |
| 168 | label.appendf("useTranslucentPaint: %d", useTranslucentPaint); |
| 169 | canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint); |
| 170 | } |
| 171 | { |
| 172 | canvas->translate(0.0f, LABEL_SIZE); |
| 173 | SkString label; |
| 174 | label.appendf("useTranslucentShader: %d", useTranslucentShader); |
| 175 | canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint); |
| 176 | } |
| 177 | |
| 178 | canvas->restore(); |
| 179 | |
| 180 | gridNum++; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | // Rotation/scale test |
| 188 | { |
| 189 | SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH; |
| 190 | SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH; |
| 191 | |
| 192 | canvas->save(); |
| 193 | canvas->translate(xPos, yPos); |
| 194 | this->drawRect(canvas, 0.6f, 0.6f, 45.0f, true, true, true, true); |
| 195 | canvas->restore(); |
| 196 | |
| 197 | gridNum++; |
| 198 | } |
| 199 | |
| 200 | // Anisotropic scale test |
| 201 | { |
| 202 | SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH; |
| 203 | SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH; |
| 204 | |
| 205 | canvas->save(); |
| 206 | canvas->translate(xPos, yPos); |
| 207 | this->drawRect(canvas, 0.6f, 0.4f, 30.0f, true, true, true, true); |
| 208 | canvas->restore(); |
| 209 | |
| 210 | gridNum++; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | private: |
| 215 | static const int kTexSize = 96; |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 216 | |
| 217 | sk_sp<SkShader> fOpaqueDiffuse; |
| 218 | sk_sp<SkShader> fTranslucentDiffuse; |
| 219 | sk_sp<SkShader> fNormalMapShader; |
| 220 | |
| 221 | SkRect fRect; |
| 222 | sk_sp<SkLights> fLights; |
| 223 | |
| 224 | typedef GM INHERITED; |
| 225 | }; |
| 226 | |
| 227 | ////////////////////////////////////////////////////////////////////////////// |
| 228 | |
| 229 | DEF_GM(return new LightingShader2GM;) |
| 230 | } |