2016-05-31 13:56:28 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkLights_DEFINED
|
|
|
|
#define SkLights_DEFINED
|
|
|
|
|
2016-08-12 18:38:47 +00:00
|
|
|
#include "../private/SkTArray.h"
|
2017-02-07 19:09:38 +00:00
|
|
|
#include "SkImage.h"
|
2016-05-31 13:56:28 +00:00
|
|
|
#include "SkPoint3.h"
|
|
|
|
#include "SkRefCnt.h"
|
2016-08-12 18:38:47 +00:00
|
|
|
|
|
|
|
class SkReadBuffer;
|
|
|
|
class SkWriteBuffer;
|
2016-05-31 13:56:28 +00:00
|
|
|
|
|
|
|
class SK_API SkLights : public SkRefCnt {
|
|
|
|
public:
|
|
|
|
class Light {
|
|
|
|
public:
|
|
|
|
enum LightType {
|
2016-08-12 18:38:47 +00:00
|
|
|
kDirectional_LightType,
|
|
|
|
kPoint_LightType
|
2016-05-31 13:56:28 +00:00
|
|
|
};
|
|
|
|
|
2016-08-03 16:40:39 +00:00
|
|
|
Light(const Light& other)
|
|
|
|
: fType(other.fType)
|
|
|
|
, fColor(other.fColor)
|
2016-08-26 15:49:46 +00:00
|
|
|
, fDirOrPos(other.fDirOrPos)
|
|
|
|
, fIntensity(other.fIntensity)
|
2016-09-06 20:03:30 +00:00
|
|
|
, fShadowMap(other.fShadowMap)
|
|
|
|
, fIsRadial(other.fIsRadial) {
|
2016-08-03 16:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Light(Light&& other)
|
|
|
|
: fType(other.fType)
|
|
|
|
, fColor(other.fColor)
|
2016-08-26 15:49:46 +00:00
|
|
|
, fDirOrPos(other.fDirOrPos)
|
|
|
|
, fIntensity(other.fIntensity)
|
2016-09-06 20:03:30 +00:00
|
|
|
, fShadowMap(std::move(other.fShadowMap))
|
|
|
|
, fIsRadial(other.fIsRadial) {
|
2016-08-03 16:40:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 20:03:30 +00:00
|
|
|
static Light MakeDirectional(const SkColor3f& color, const SkVector3& dir,
|
|
|
|
bool isRadial = false) {
|
|
|
|
Light light(kDirectional_LightType, color, dir, isRadial);
|
2016-08-26 15:49:46 +00:00
|
|
|
if (!light.fDirOrPos.normalize()) {
|
|
|
|
light.fDirOrPos.set(0.0f, 0.0f, 1.0f);
|
2016-05-31 13:56:28 +00:00
|
|
|
}
|
2016-08-12 18:38:47 +00:00
|
|
|
return light;
|
|
|
|
}
|
|
|
|
|
2016-09-06 20:03:30 +00:00
|
|
|
static Light MakePoint(const SkColor3f& color, const SkPoint3& pos, SkScalar intensity,
|
|
|
|
bool isRadial = false) {
|
|
|
|
return Light(kPoint_LightType, color, pos, intensity, isRadial);
|
2016-05-31 13:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LightType type() const { return fType; }
|
|
|
|
const SkColor3f& color() const { return fColor; }
|
2016-08-12 18:38:47 +00:00
|
|
|
const SkVector3& dir() const {
|
|
|
|
SkASSERT(kDirectional_LightType == fType);
|
2016-08-26 15:49:46 +00:00
|
|
|
return fDirOrPos;
|
2016-08-12 18:38:47 +00:00
|
|
|
}
|
|
|
|
const SkPoint3& pos() const {
|
|
|
|
SkASSERT(kPoint_LightType == fType);
|
2016-08-26 15:49:46 +00:00
|
|
|
return fDirOrPos;
|
|
|
|
}
|
|
|
|
SkScalar intensity() const {
|
|
|
|
SkASSERT(kPoint_LightType == fType);
|
|
|
|
return fIntensity;
|
2016-05-31 13:56:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 17:25:54 +00:00
|
|
|
void setShadowMap(sk_sp<SkImage> shadowMap) {
|
|
|
|
fShadowMap = std::move(shadowMap);
|
|
|
|
}
|
|
|
|
|
2016-08-03 16:40:39 +00:00
|
|
|
SkImage* getShadowMap() const {
|
|
|
|
return fShadowMap.get();
|
2016-07-21 17:25:54 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 20:03:30 +00:00
|
|
|
bool isRadial() const { return fIsRadial; }
|
|
|
|
|
2016-07-21 17:25:54 +00:00
|
|
|
Light& operator= (const Light& b) {
|
2016-08-03 16:40:39 +00:00
|
|
|
if (this == &b) {
|
2016-07-21 17:25:54 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-08-03 16:40:39 +00:00
|
|
|
fColor = b.fColor;
|
|
|
|
fType = b.fType;
|
2016-08-26 15:49:46 +00:00
|
|
|
fDirOrPos = b.fDirOrPos;
|
|
|
|
fIntensity = b.fIntensity;
|
2016-08-03 16:40:39 +00:00
|
|
|
fShadowMap = b.fShadowMap;
|
2016-09-06 20:03:30 +00:00
|
|
|
fIsRadial = b.fIsRadial;
|
2016-07-21 17:25:54 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2016-08-15 18:35:55 +00:00
|
|
|
|
|
|
|
bool operator== (const Light& b) {
|
|
|
|
if (this == &b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (fColor == b.fColor) &&
|
|
|
|
(fType == b.fType) &&
|
2016-08-26 15:49:46 +00:00
|
|
|
(fDirOrPos == b.fDirOrPos) &&
|
|
|
|
(fShadowMap == b.fShadowMap) &&
|
2016-09-06 20:03:30 +00:00
|
|
|
(fIntensity == b.fIntensity) &&
|
|
|
|
(fIsRadial == b.fIsRadial);
|
2016-08-15 18:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!= (const Light& b) { return !(this->operator==(b)); }
|
|
|
|
|
2016-05-31 13:56:28 +00:00
|
|
|
private:
|
|
|
|
LightType fType;
|
|
|
|
SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel.
|
2016-08-26 15:49:46 +00:00
|
|
|
|
|
|
|
SkVector3 fDirOrPos; // For directional lights, holds the direction towards the
|
2016-08-12 18:38:47 +00:00
|
|
|
// light (+Z is out of the screen).
|
2016-05-31 13:56:28 +00:00
|
|
|
// If degenerate, it will be replaced with (0, 0, 1).
|
2016-08-12 18:38:47 +00:00
|
|
|
// For point lights, holds location of point light
|
2016-08-26 15:49:46 +00:00
|
|
|
|
|
|
|
SkScalar fIntensity; // For point lights, dictates the light intensity.
|
|
|
|
// Simply a multiplier to the final light output value.
|
2016-07-21 17:25:54 +00:00
|
|
|
sk_sp<SkImage> fShadowMap;
|
2016-09-06 20:03:30 +00:00
|
|
|
bool fIsRadial; // Whether the light is radial or not. Radial lights will
|
|
|
|
// cast shadows and lights radially outwards.
|
2016-08-12 18:38:47 +00:00
|
|
|
|
2016-09-06 20:03:30 +00:00
|
|
|
Light(LightType type, const SkColor3f& color, const SkVector3& dirOrPos,
|
|
|
|
SkScalar intensity = 0.0f, bool isRadial = false) {
|
2016-08-12 18:38:47 +00:00
|
|
|
fType = type;
|
|
|
|
fColor = color;
|
2016-08-29 17:22:09 +00:00
|
|
|
fDirOrPos = dirOrPos;
|
2016-08-26 15:49:46 +00:00
|
|
|
fIntensity = intensity;
|
2016-09-06 20:03:30 +00:00
|
|
|
fIsRadial = isRadial;
|
2016-08-12 18:38:47 +00:00
|
|
|
}
|
2016-05-31 13:56:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Builder {
|
|
|
|
public:
|
2016-08-29 17:22:09 +00:00
|
|
|
Builder() : fLights(new SkLights) {}
|
2016-08-12 18:38:47 +00:00
|
|
|
|
2016-05-31 13:56:28 +00:00
|
|
|
void add(const Light& light) {
|
|
|
|
if (fLights) {
|
2016-08-03 16:40:39 +00:00
|
|
|
fLights->fLights.push_back(light);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(Light&& light) {
|
|
|
|
if (fLights) {
|
|
|
|
fLights->fLights.push_back(std::move(light));
|
2016-05-31 13:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:22:09 +00:00
|
|
|
void setAmbientLightColor(const SkColor3f& color) {
|
|
|
|
if (fLights) {
|
|
|
|
fLights->fAmbientLightColor = color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 17:12:08 +00:00
|
|
|
sk_sp<SkLights> finish() {
|
2016-08-03 16:40:39 +00:00
|
|
|
return std::move(fLights);
|
2016-05-31 13:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sk_sp<SkLights> fLights;
|
|
|
|
};
|
|
|
|
|
|
|
|
int numLights() const {
|
|
|
|
return fLights.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Light& light(int index) const {
|
|
|
|
return fLights[index];
|
|
|
|
}
|
|
|
|
|
2016-07-21 17:25:54 +00:00
|
|
|
Light& light(int index) {
|
|
|
|
return fLights[index];
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:22:09 +00:00
|
|
|
const SkColor3f& ambientLightColor() const {
|
|
|
|
return fAmbientLightColor;
|
|
|
|
}
|
|
|
|
|
2016-08-12 18:38:47 +00:00
|
|
|
static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf);
|
|
|
|
|
|
|
|
void flatten(SkWriteBuffer& buf) const;
|
|
|
|
|
2016-05-31 13:56:28 +00:00
|
|
|
private:
|
2016-08-29 17:22:09 +00:00
|
|
|
SkLights() {
|
|
|
|
fAmbientLightColor.set(0.0f, 0.0f, 0.0f);
|
|
|
|
}
|
2016-08-03 16:40:39 +00:00
|
|
|
SkTArray<Light> fLights;
|
2016-08-29 17:22:09 +00:00
|
|
|
SkColor3f fAmbientLightColor;
|
2016-08-03 16:40:39 +00:00
|
|
|
typedef SkRefCnt INHERITED;
|
2016-05-31 13:56:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|