blob: 24a0a64a4995ae9849acb6742046a5a44673a34d [file] [log] [blame]
Robert Phillipsa8cdbd72018-07-17 12:30:40 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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 Phillipsa8cdbd72018-07-17 12:30:40 -040015
16static 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 Wagnerb2c4ea62018-08-08 11:36:17 -040032class LightingView : public Sample {
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040033public:
Ben Wagnerad9a3912019-08-08 18:44:00 -040034 LightingView() : fLightAngle(0.0f), fColorFactor(0.0f) {}
35
36protected:
37 SkString name() override { return SkString("Lighting"); }
38
39 void onOnceBeforeDraw() override {
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040040 {
41 SkBitmap diffuseBitmap;
42 SkAssertResult(GetResourceAsBitmap("images/brickwork-texture.jpg", &diffuseBitmap));
43
44 fRect = SkRect::MakeIWH(diffuseBitmap.width(), diffuseBitmap.height());
45
Mike Reed50acf8f2019-04-08 13:20:23 -040046 fDiffuseShader = diffuseBitmap.makeShader();
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040047 }
48
49 {
50 SkBitmap normalBitmap;
51 SkAssertResult(GetResourceAsBitmap("images/brickwork_normal-map.jpg", &normalBitmap));
52
Mike Reed50acf8f2019-04-08 13:20:23 -040053 sk_sp<SkShader> normalMap = normalBitmap.makeShader();
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040054 fNormalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap), SkMatrix::I());
55 }
56 }
57
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040058 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 Canaryb1f411a2019-08-29 10:39:22 -040070 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040071 return this->INHERITED::onFindClickHandler(x, y, modi);
72 }
73
Hal Canary41248072019-07-11 16:32:53 -040074 bool onAnimate(double nanos) override {
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040075 fLightAngle += 0.015f;
76 fColorFactor += 0.01f;
77 if (fColorFactor > 1.0f) {
78 fColorFactor = 0.0f;
79 }
80
81 return true;
82 }
83
84private:
85 SkRect fRect;
86 sk_sp<SkShader> fDiffuseShader;
87 sk_sp<SkNormalSource> fNormalSource;
88
89 SkScalar fLightAngle;
90 SkScalar fColorFactor;
91
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040092 typedef Sample INHERITED;
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040093};
94
95//////////////////////////////////////////////////////////////////////////////
96
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040097DEF_SAMPLE( return new LightingView(); )