Robert Phillips | a8cdbd7 | 2018-07-17 12:30:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkPoint3.h" |
| 10 | #include "samplecode/DecodeFile.h" |
| 11 | #include "samplecode/Sample.h" |
| 12 | #include "src/core/SkNormalSource.h" |
| 13 | #include "src/shaders/SkLightingShader.h" |
| 14 | #include "tools/Resources.h" |
Robert Phillips | a8cdbd7 | 2018-07-17 12:30:40 -0400 | [diff] [blame] | 15 | |
| 16 | static sk_sp<SkLights> create_lights(SkScalar angle, SkScalar blue) { |
| 17 | |
| 18 | const SkVector3 dir = SkVector3::Make(SkScalarSin(angle)*SkScalarSin(SK_ScalarPI*0.25f), |
| 19 | SkScalarCos(angle)*SkScalarSin(SK_ScalarPI*0.25f), |
| 20 | SkScalarCos(SK_ScalarPI*0.25f)); |
| 21 | |
| 22 | SkLights::Builder builder; |
| 23 | |
| 24 | builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, blue), dir)); |
| 25 | builder.setAmbientLightColor(SkColor3f::Make(0.1f, 0.1f, 0.1f)); |
| 26 | |
| 27 | return builder.finish(); |
| 28 | } |
| 29 | |
| 30 | //////////////////////////////////////////////////////////////////////////// |
| 31 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 32 | class LightingView : public Sample { |
Robert Phillips | a8cdbd7 | 2018-07-17 12:30:40 -0400 | [diff] [blame] | 33 | public: |
Ben Wagner | ad9a391 | 2019-08-08 18:44:00 -0400 | [diff] [blame] | 34 | LightingView() : fLightAngle(0.0f), fColorFactor(0.0f) {} |
| 35 | |
| 36 | protected: |
| 37 | SkString name() override { return SkString("Lighting"); } |
| 38 | |
| 39 | void onOnceBeforeDraw() override { |
Robert Phillips | a8cdbd7 | 2018-07-17 12:30:40 -0400 | [diff] [blame] | 40 | { |
| 41 | SkBitmap diffuseBitmap; |
| 42 | SkAssertResult(GetResourceAsBitmap("images/brickwork-texture.jpg", &diffuseBitmap)); |
| 43 | |
| 44 | fRect = SkRect::MakeIWH(diffuseBitmap.width(), diffuseBitmap.height()); |
| 45 | |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 46 | fDiffuseShader = diffuseBitmap.makeShader(); |
Robert Phillips | a8cdbd7 | 2018-07-17 12:30:40 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | { |
| 50 | SkBitmap normalBitmap; |
| 51 | SkAssertResult(GetResourceAsBitmap("images/brickwork_normal-map.jpg", &normalBitmap)); |
| 52 | |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 53 | sk_sp<SkShader> normalMap = normalBitmap.makeShader(); |
Robert Phillips | a8cdbd7 | 2018-07-17 12:30:40 -0400 | [diff] [blame] | 54 | fNormalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap), SkMatrix::I()); |
| 55 | } |
| 56 | } |
| 57 | |
Robert Phillips | a8cdbd7 | 2018-07-17 12:30:40 -0400 | [diff] [blame] | 58 | void onDrawContent(SkCanvas* canvas) override { |
| 59 | sk_sp<SkLights> lights(create_lights(fLightAngle, fColorFactor)); |
| 60 | |
| 61 | SkPaint paint; |
| 62 | paint.setShader(SkLightingShader::Make(fDiffuseShader, |
| 63 | fNormalSource, |
| 64 | std::move(lights))); |
| 65 | paint.setColor(SK_ColorBLACK); |
| 66 | |
| 67 | canvas->drawRect(fRect, paint); |
| 68 | } |
| 69 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 70 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override { |
Robert Phillips | a8cdbd7 | 2018-07-17 12:30:40 -0400 | [diff] [blame] | 71 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 72 | } |
| 73 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 74 | bool onAnimate(double nanos) override { |
Robert Phillips | a8cdbd7 | 2018-07-17 12:30:40 -0400 | [diff] [blame] | 75 | fLightAngle += 0.015f; |
| 76 | fColorFactor += 0.01f; |
| 77 | if (fColorFactor > 1.0f) { |
| 78 | fColorFactor = 0.0f; |
| 79 | } |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | SkRect fRect; |
| 86 | sk_sp<SkShader> fDiffuseShader; |
| 87 | sk_sp<SkNormalSource> fNormalSource; |
| 88 | |
| 89 | SkScalar fLightAngle; |
| 90 | SkScalar fColorFactor; |
| 91 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 92 | typedef Sample INHERITED; |
Robert Phillips | a8cdbd7 | 2018-07-17 12:30:40 -0400 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | ////////////////////////////////////////////////////////////////////////////// |
| 96 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 97 | DEF_SAMPLE( return new LightingView(); ) |