OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #ifndef SkLights_DEFINED | 9 #ifndef SkLights_DEFINED |
10 #define SkLights_DEFINED | 10 #define SkLights_DEFINED |
(...skipping 12 matching lines...) Expand all Loading... |
23 public: | 23 public: |
24 enum LightType { | 24 enum LightType { |
25 kAmbient_LightType, // only 'fColor' is used | 25 kAmbient_LightType, // only 'fColor' is used |
26 kDirectional_LightType, | 26 kDirectional_LightType, |
27 kPoint_LightType | 27 kPoint_LightType |
28 }; | 28 }; |
29 | 29 |
30 Light(const Light& other) | 30 Light(const Light& other) |
31 : fType(other.fType) | 31 : fType(other.fType) |
32 , fColor(other.fColor) | 32 , fColor(other.fColor) |
33 , fDirection(other.fDirection) | 33 , fDirOrPos(other.fDirOrPos) |
| 34 , fIntensity(other.fIntensity) |
34 , fShadowMap(other.fShadowMap) { | 35 , fShadowMap(other.fShadowMap) { |
35 } | 36 } |
36 | 37 |
37 Light(Light&& other) | 38 Light(Light&& other) |
38 : fType(other.fType) | 39 : fType(other.fType) |
39 , fColor(other.fColor) | 40 , fColor(other.fColor) |
40 , fDirection(other.fDirection) | 41 , fDirOrPos(other.fDirOrPos) |
| 42 , fIntensity(other.fIntensity) |
41 , fShadowMap(std::move(other.fShadowMap)) { | 43 , fShadowMap(std::move(other.fShadowMap)) { |
42 } | 44 } |
43 | 45 |
44 static Light MakeAmbient(const SkColor3f& color) { | 46 static Light MakeAmbient(const SkColor3f& color) { |
45 return Light(kAmbient_LightType, color, SkVector3::Make(0.0f, 0.0f,
1.0f)); | 47 return Light(kAmbient_LightType, color, SkVector3::Make(0.0f, 0.0f,
1.0f)); |
46 } | 48 } |
47 | 49 |
48 static Light MakeDirectional(const SkColor3f& color, const SkVector3& di
r) { | 50 static Light MakeDirectional(const SkColor3f& color, const SkVector3& di
r) { |
49 Light light(kDirectional_LightType, color, dir); | 51 Light light(kDirectional_LightType, color, dir); |
50 if (!light.fDirection.normalize()) { | 52 if (!light.fDirOrPos.normalize()) { |
51 light.fDirection.set(0.0f, 0.0f, 1.0f); | 53 light.fDirOrPos.set(0.0f, 0.0f, 1.0f); |
52 } | 54 } |
53 return light; | 55 return light; |
54 } | 56 } |
55 | 57 |
56 static Light MakePoint(const SkColor3f& color, const SkPoint3& pos) { | 58 static Light MakePoint(const SkColor3f& color, const SkPoint3& pos, SkSc
alar intensity) { |
57 return Light(kPoint_LightType, color, pos); | 59 return Light(kPoint_LightType, color, pos, intensity); |
58 } | 60 } |
59 | 61 |
60 LightType type() const { return fType; } | 62 LightType type() const { return fType; } |
61 const SkColor3f& color() const { return fColor; } | 63 const SkColor3f& color() const { return fColor; } |
62 const SkVector3& dir() const { | 64 const SkVector3& dir() const { |
63 SkASSERT(kDirectional_LightType == fType); | 65 SkASSERT(kDirectional_LightType == fType); |
64 return fDirection; | 66 return fDirOrPos; |
65 } | 67 } |
66 const SkPoint3& pos() const { | 68 const SkPoint3& pos() const { |
67 SkASSERT(kPoint_LightType == fType); | 69 SkASSERT(kPoint_LightType == fType); |
68 return fDirection; | 70 return fDirOrPos; |
| 71 } |
| 72 SkScalar intensity() const { |
| 73 SkASSERT(kPoint_LightType == fType); |
| 74 return fIntensity; |
69 } | 75 } |
70 | 76 |
71 void setShadowMap(sk_sp<SkImage> shadowMap) { | 77 void setShadowMap(sk_sp<SkImage> shadowMap) { |
72 fShadowMap = std::move(shadowMap); | 78 fShadowMap = std::move(shadowMap); |
73 } | 79 } |
74 | 80 |
75 SkImage* getShadowMap() const { | 81 SkImage* getShadowMap() const { |
76 return fShadowMap.get(); | 82 return fShadowMap.get(); |
77 } | 83 } |
78 | 84 |
79 Light& operator= (const Light& b) { | 85 Light& operator= (const Light& b) { |
80 if (this == &b) { | 86 if (this == &b) { |
81 return *this; | 87 return *this; |
82 } | 88 } |
83 | 89 |
84 fColor = b.fColor; | 90 fColor = b.fColor; |
85 fType = b.fType; | 91 fType = b.fType; |
86 fDirection = b.fDirection; | 92 fDirOrPos = b.fDirOrPos; |
| 93 fIntensity = b.fIntensity; |
87 fShadowMap = b.fShadowMap; | 94 fShadowMap = b.fShadowMap; |
88 return *this; | 95 return *this; |
89 } | 96 } |
90 private: | 97 private: |
91 LightType fType; | 98 LightType fType; |
92 SkColor3f fColor; // linear (unpremul) color. Range is 0..1
in each channel. | 99 SkColor3f fColor; // linear (unpremul) color. Range is 0..1
in each channel. |
93 SkVector3 fDirection; // For directional lights, holds the direc
tion towards the | 100 |
| 101 SkVector3 fDirOrPos; // For directional lights, holds the direc
tion towards the |
94 // light (+Z is out of the screen). | 102 // light (+Z is out of the screen). |
95 // If degenerate, it will be replaced with
(0, 0, 1). | 103 // If degenerate, it will be replaced with
(0, 0, 1). |
96 // For point lights, holds location of poi
nt light | 104 // For point lights, holds location of poi
nt light |
| 105 |
| 106 SkScalar fIntensity; // For point lights, dictates the light in
tensity. |
| 107 // Simply a multiplier to the final light
output value. |
97 sk_sp<SkImage> fShadowMap; | 108 sk_sp<SkImage> fShadowMap; |
98 | 109 |
99 Light(LightType type, const SkColor3f& color, const SkVector3& dir) { | 110 Light(LightType type, const SkColor3f& color, |
| 111 const SkVector3& dir, SkScalar intensity = 0.0f) { |
100 fType = type; | 112 fType = type; |
101 fColor = color; | 113 fColor = color; |
102 fDirection = dir; | 114 fDirOrPos = dir; |
| 115 fIntensity = intensity; |
103 } | 116 } |
104 }; | 117 }; |
105 | 118 |
106 class Builder { | 119 class Builder { |
107 public: | 120 public: |
108 Builder() : fLights(new SkLights) { } | 121 Builder() : fLights(new SkLights) { } |
109 | 122 |
110 void add(const Light& light) { | 123 void add(const Light& light) { |
111 if (fLights) { | 124 if (fLights) { |
112 fLights->fLights.push_back(light); | 125 fLights->fLights.push_back(light); |
(...skipping 30 matching lines...) Expand all Loading... |
143 | 156 |
144 void flatten(SkWriteBuffer& buf) const; | 157 void flatten(SkWriteBuffer& buf) const; |
145 | 158 |
146 private: | 159 private: |
147 SkLights() {} | 160 SkLights() {} |
148 SkTArray<Light> fLights; | 161 SkTArray<Light> fLights; |
149 typedef SkRefCnt INHERITED; | 162 typedef SkRefCnt INHERITED; |
150 }; | 163 }; |
151 | 164 |
152 #endif | 165 #endif |
OLD | NEW |