added radial lights to SkLights

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2301173004

Review-Url: https://codereview.chromium.org/2301173004
This commit is contained in:
vjiaoblack 2016-09-06 13:03:30 -07:00 committed by Commit bot
parent 45d1b442f1
commit c1a50e1b73
2 changed files with 29 additions and 12 deletions

View File

@ -31,7 +31,8 @@ public:
, fColor(other.fColor) , fColor(other.fColor)
, fDirOrPos(other.fDirOrPos) , fDirOrPos(other.fDirOrPos)
, fIntensity(other.fIntensity) , fIntensity(other.fIntensity)
, fShadowMap(other.fShadowMap) { , fShadowMap(other.fShadowMap)
, fIsRadial(other.fIsRadial) {
} }
Light(Light&& other) Light(Light&& other)
@ -39,19 +40,22 @@ public:
, fColor(other.fColor) , fColor(other.fColor)
, fDirOrPos(other.fDirOrPos) , fDirOrPos(other.fDirOrPos)
, fIntensity(other.fIntensity) , fIntensity(other.fIntensity)
, fShadowMap(std::move(other.fShadowMap)) { , fShadowMap(std::move(other.fShadowMap))
, fIsRadial(other.fIsRadial) {
} }
static Light MakeDirectional(const SkColor3f& color, const SkVector3& dir) { static Light MakeDirectional(const SkColor3f& color, const SkVector3& dir,
Light light(kDirectional_LightType, color, dir); bool isRadial = false) {
Light light(kDirectional_LightType, color, dir, isRadial);
if (!light.fDirOrPos.normalize()) { if (!light.fDirOrPos.normalize()) {
light.fDirOrPos.set(0.0f, 0.0f, 1.0f); light.fDirOrPos.set(0.0f, 0.0f, 1.0f);
} }
return light; return light;
} }
static Light MakePoint(const SkColor3f& color, const SkPoint3& pos, SkScalar intensity) { static Light MakePoint(const SkColor3f& color, const SkPoint3& pos, SkScalar intensity,
return Light(kPoint_LightType, color, pos, intensity); bool isRadial = false) {
return Light(kPoint_LightType, color, pos, intensity, isRadial);
} }
LightType type() const { return fType; } LightType type() const { return fType; }
@ -77,6 +81,8 @@ public:
return fShadowMap.get(); return fShadowMap.get();
} }
bool isRadial() const { return fIsRadial; }
Light& operator= (const Light& b) { Light& operator= (const Light& b) {
if (this == &b) { if (this == &b) {
return *this; return *this;
@ -87,6 +93,7 @@ public:
fDirOrPos = b.fDirOrPos; fDirOrPos = b.fDirOrPos;
fIntensity = b.fIntensity; fIntensity = b.fIntensity;
fShadowMap = b.fShadowMap; fShadowMap = b.fShadowMap;
fIsRadial = b.fIsRadial;
return *this; return *this;
} }
@ -99,7 +106,8 @@ public:
(fType == b.fType) && (fType == b.fType) &&
(fDirOrPos == b.fDirOrPos) && (fDirOrPos == b.fDirOrPos) &&
(fShadowMap == b.fShadowMap) && (fShadowMap == b.fShadowMap) &&
(fIntensity == b.fIntensity); (fIntensity == b.fIntensity) &&
(fIsRadial == b.fIsRadial);
} }
bool operator!= (const Light& b) { return !(this->operator==(b)); } bool operator!= (const Light& b) { return !(this->operator==(b)); }
@ -116,13 +124,16 @@ public:
SkScalar fIntensity; // For point lights, dictates the light intensity. SkScalar fIntensity; // For point lights, dictates the light intensity.
// Simply a multiplier to the final light output value. // Simply a multiplier to the final light output value.
sk_sp<SkImage> fShadowMap; sk_sp<SkImage> fShadowMap;
bool fIsRadial; // Whether the light is radial or not. Radial lights will
// cast shadows and lights radially outwards.
Light(LightType type, const SkColor3f& color, Light(LightType type, const SkColor3f& color, const SkVector3& dirOrPos,
const SkVector3& dirOrPos, SkScalar intensity = 0.0f) { SkScalar intensity = 0.0f, bool isRadial = false) {
fType = type; fType = type;
fColor = color; fColor = color;
fDirOrPos = dirOrPos; fDirOrPos = dirOrPos;
fIntensity = intensity; fIntensity = intensity;
fIsRadial = isRadial;
} }
}; };

View File

@ -42,14 +42,15 @@ sk_sp<SkLights> SkLights::MakeFromBuffer(SkReadBuffer& buf) {
} }
} }
bool isRadial = buf.readBool();
if (isPoint) { if (isPoint) {
SkScalar intensity = 0.0f; SkScalar intensity;
intensity = buf.readScalar(); intensity = buf.readScalar();
Light light = Light::MakePoint(color, dirOrPos, intensity); Light light = Light::MakePoint(color, dirOrPos, intensity, isRadial);
light.setShadowMap(depthMap); light.setShadowMap(depthMap);
builder.add(light); builder.add(light);
} else { } else {
Light light = Light::MakeDirectional(color, dirOrPos); Light light = Light::MakeDirectional(color, dirOrPos, isRadial);
light.setShadowMap(depthMap); light.setShadowMap(depthMap);
builder.add(light); builder.add(light);
} }
@ -70,8 +71,13 @@ void SkLights::flatten(SkWriteBuffer& buf) const {
buf.writeBool(isPoint); buf.writeBool(isPoint);
buf.writeScalarArray(&light.color().fX, 3); buf.writeScalarArray(&light.color().fX, 3);
buf.writeScalarArray(&light.dir().fX, 3); buf.writeScalarArray(&light.dir().fX, 3);
bool hasShadowMap = light.getShadowMap() != nullptr; bool hasShadowMap = light.getShadowMap() != nullptr;
buf.writeBool(hasShadowMap); buf.writeBool(hasShadowMap);
bool isRadial = light.isRadial();
buf.writeBool(isRadial);
if (hasShadowMap) { if (hasShadowMap) {
buf.writeImage(light.getShadowMap()); buf.writeImage(light.getShadowMap());
} }