OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2016 Google Inc. | 3 * Copyright 2016 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 #include "SkLights.h" | 9 #include "SkLights.h" |
10 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
11 | 11 |
12 sk_sp<SkLights> SkLights::MakeFromBuffer(SkReadBuffer& buf) { | 12 sk_sp<SkLights> SkLights::MakeFromBuffer(SkReadBuffer& buf) { |
13 Builder builder; | |
14 | |
15 SkColor3f ambColor; | |
16 if (!buf.readScalarArray(&ambColor.fX, 3)) { | |
17 return nullptr; | |
18 } | |
19 | |
20 builder.setAmbientLightColor(ambColor); | |
21 | |
22 int numLights = buf.readInt(); | 13 int numLights = buf.readInt(); |
23 | 14 |
| 15 Builder builder; |
24 for (int l = 0; l < numLights; ++l) { | 16 for (int l = 0; l < numLights; ++l) { |
| 17 bool isAmbient = buf.readBool(); |
25 bool isPoint = buf.readBool(); | 18 bool isPoint = buf.readBool(); |
26 | 19 |
27 SkColor3f color; | 20 SkColor3f color; |
28 if (!buf.readScalarArray(&color.fX, 3)) { | 21 if (!buf.readScalarArray(&color.fX, 3)) { |
29 return nullptr; | 22 return nullptr; |
30 } | 23 } |
31 | 24 |
32 SkVector3 dirOrPos; | 25 if (isAmbient) { |
33 if (!buf.readScalarArray(&dirOrPos.fX, 3)) { | 26 builder.add(Light::MakeAmbient(color)); |
34 return nullptr; | 27 } else { |
35 } | 28 SkVector3 dirOrPos; |
36 | 29 if (!buf.readScalarArray(&dirOrPos.fX, 3)) { |
37 sk_sp<SkImage> depthMap; | |
38 bool hasShadowMap = buf.readBool(); | |
39 if (hasShadowMap) { | |
40 if (!(depthMap = buf.readImage())) { | |
41 return nullptr; | 30 return nullptr; |
42 } | 31 } |
43 } | 32 SkScalar intensity = 0.0f; |
| 33 if (isPoint) { |
| 34 intensity = buf.readScalar(); |
| 35 } |
44 | 36 |
45 if (isPoint) { | 37 sk_sp<SkImage> depthMap; |
46 SkScalar intensity = 0.0f; | 38 bool hasShadowMap = buf.readBool(); |
47 intensity = buf.readScalar(); | 39 if (hasShadowMap) { |
48 Light light = Light::MakePoint(color, dirOrPos, intensity); | 40 if (!(depthMap = buf.readImage())) { |
49 light.setShadowMap(depthMap); | 41 return nullptr; |
50 builder.add(light); | 42 } |
51 } else { | 43 } |
52 Light light = Light::MakeDirectional(color, dirOrPos); | 44 |
53 light.setShadowMap(depthMap); | 45 if (isPoint) { |
54 builder.add(light); | 46 Light light = Light::MakePoint(color, dirOrPos, intensity); |
| 47 light.setShadowMap(depthMap); |
| 48 builder.add(light); |
| 49 } else { |
| 50 Light light = Light::MakeDirectional(color, dirOrPos); |
| 51 light.setShadowMap(depthMap); |
| 52 builder.add(light); |
| 53 } |
55 } | 54 } |
56 } | 55 } |
57 | 56 |
58 return builder.finish(); | 57 return builder.finish(); |
59 } | 58 } |
60 | 59 |
61 void SkLights::flatten(SkWriteBuffer& buf) const { | 60 void SkLights::flatten(SkWriteBuffer& buf) const { |
62 buf.writeScalarArray(&this->ambientLightColor().fX, 3); | |
63 | 61 |
64 buf.writeInt(this->numLights()); | 62 buf.writeInt(this->numLights()); |
65 for (int l = 0; l < this->numLights(); ++l) { | 63 for (int l = 0; l < this->numLights(); ++l) { |
66 const Light& light = this->light(l); | 64 const Light& light = this->light(l); |
67 | 65 |
| 66 bool isAmbient = Light::kAmbient_LightType == light.type(); |
68 bool isPoint = Light::kPoint_LightType == light.type(); | 67 bool isPoint = Light::kPoint_LightType == light.type(); |
69 | 68 |
| 69 buf.writeBool(isAmbient); |
70 buf.writeBool(isPoint); | 70 buf.writeBool(isPoint); |
71 buf.writeScalarArray(&light.color().fX, 3); | 71 buf.writeScalarArray(&light.color().fX, 3); |
72 buf.writeScalarArray(&light.dir().fX, 3); | 72 if (!isAmbient) { |
73 bool hasShadowMap = light.getShadowMap() != nullptr; | 73 if (isPoint) { |
74 buf.writeBool(hasShadowMap); | 74 buf.writeScalarArray(&light.pos().fX, 3); |
75 if (hasShadowMap) { | 75 buf.writeScalar(light.intensity()); |
76 buf.writeImage(light.getShadowMap()); | 76 } else { |
77 } | 77 buf.writeScalarArray(&light.dir().fX, 3); |
78 if (isPoint) { | 78 } |
79 buf.writeScalar(light.intensity()); | 79 bool hasShadowMap = light.getShadowMap() != nullptr; |
| 80 buf.writeBool(hasShadowMap); |
| 81 if (hasShadowMap) { |
| 82 buf.writeImage(light.getShadowMap()); |
| 83 } |
80 } | 84 } |
81 } | 85 } |
82 } | 86 } |
OLD | NEW |