2018-07-17 16:30:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkPoint3.h"
|
|
|
|
#include "samplecode/DecodeFile.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/core/SkNormalSource.h"
|
|
|
|
#include "src/shaders/SkLightingShader.h"
|
|
|
|
#include "tools/Resources.h"
|
2018-07-17 16:30:40 +00:00
|
|
|
|
|
|
|
static sk_sp<SkLights> create_lights(SkScalar angle, SkScalar blue) {
|
|
|
|
|
|
|
|
const SkVector3 dir = SkVector3::Make(SkScalarSin(angle)*SkScalarSin(SK_ScalarPI*0.25f),
|
|
|
|
SkScalarCos(angle)*SkScalarSin(SK_ScalarPI*0.25f),
|
|
|
|
SkScalarCos(SK_ScalarPI*0.25f));
|
|
|
|
|
|
|
|
SkLights::Builder builder;
|
|
|
|
|
|
|
|
builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, blue), dir));
|
|
|
|
builder.setAmbientLightColor(SkColor3f::Make(0.1f, 0.1f, 0.1f));
|
|
|
|
|
|
|
|
return builder.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class LightingView : public Sample {
|
2018-07-17 16:30:40 +00:00
|
|
|
public:
|
2019-08-08 22:44:00 +00:00
|
|
|
LightingView() : fLightAngle(0.0f), fColorFactor(0.0f) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString name() override { return SkString("Lighting"); }
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
2018-07-17 16:30:40 +00:00
|
|
|
{
|
|
|
|
SkBitmap diffuseBitmap;
|
|
|
|
SkAssertResult(GetResourceAsBitmap("images/brickwork-texture.jpg", &diffuseBitmap));
|
|
|
|
|
|
|
|
fRect = SkRect::MakeIWH(diffuseBitmap.width(), diffuseBitmap.height());
|
|
|
|
|
2019-04-08 17:20:23 +00:00
|
|
|
fDiffuseShader = diffuseBitmap.makeShader();
|
2018-07-17 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SkBitmap normalBitmap;
|
|
|
|
SkAssertResult(GetResourceAsBitmap("images/brickwork_normal-map.jpg", &normalBitmap));
|
|
|
|
|
2019-04-08 17:20:23 +00:00
|
|
|
sk_sp<SkShader> normalMap = normalBitmap.makeShader();
|
2018-07-17 16:30:40 +00:00
|
|
|
fNormalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap), SkMatrix::I());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
sk_sp<SkLights> lights(create_lights(fLightAngle, fColorFactor));
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(SkLightingShader::Make(fDiffuseShader,
|
|
|
|
fNormalSource,
|
|
|
|
std::move(lights)));
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
|
|
|
|
canvas->drawRect(fRect, paint);
|
|
|
|
}
|
|
|
|
|
2019-07-08 20:07:57 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi) override {
|
2018-07-17 16:30:40 +00:00
|
|
|
return this->INHERITED::onFindClickHandler(x, y, modi);
|
|
|
|
}
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
2018-07-17 16:30:40 +00:00
|
|
|
fLightAngle += 0.015f;
|
|
|
|
fColorFactor += 0.01f;
|
|
|
|
if (fColorFactor > 1.0f) {
|
|
|
|
fColorFactor = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkRect fRect;
|
|
|
|
sk_sp<SkShader> fDiffuseShader;
|
|
|
|
sk_sp<SkNormalSource> fNormalSource;
|
|
|
|
|
|
|
|
SkScalar fLightAngle;
|
|
|
|
SkScalar fColorFactor;
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2018-07-17 16:30:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new LightingView(); )
|