Debian Codegraph @

libs/gltfio/src/MaterialProvider.cpp

raw
1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gltfio/MaterialProvider.h>
18
19 #include <string>
20
21 namespace gltfio {
22
23 bool operator==(const MaterialKey& k1, const MaterialKey& k2) {
24 return
25 (k1.doubleSided == k2.doubleSided) &&
26 (k1.unlit == k2.unlit) &&
27 (k1.hasVertexColors == k2.hasVertexColors) &&
28 (k1.hasBaseColorTexture == k2.hasBaseColorTexture) &&
29 (k1.hasNormalTexture == k2.hasNormalTexture) &&
30 (k1.hasOcclusionTexture == k2.hasOcclusionTexture) &&
31 (k1.hasEmissiveTexture == k2.hasEmissiveTexture) &&
32 (k1.useSpecularGlossiness == k2.useSpecularGlossiness) &&
33 (k1.alphaMode == k2.alphaMode) &&
34 (k1.hasMetallicRoughnessTexture == k2.hasMetallicRoughnessTexture) &&
35 (k1.metallicRoughnessUV == k2.metallicRoughnessUV) &&
36 (k1.baseColorUV == k2.baseColorUV) &&
37 (k1.hasClearCoatTexture == k2.hasClearCoatTexture) &&
38 (k1.clearCoatUV == k2.clearCoatUV) &&
39 (k1.hasClearCoatRoughnessTexture == k2.hasClearCoatRoughnessTexture) &&
40 (k1.clearCoatRoughnessUV == k2.clearCoatRoughnessUV) &&
41 (k1.hasClearCoatNormalTexture == k2.hasClearCoatNormalTexture) &&
42 (k1.clearCoatNormalUV == k2.clearCoatNormalUV) &&
43 (k1.hasClearCoat == k2.hasClearCoat) &&
44 (k1.hasTransmission == k2.hasTransmission) &&
45 (k1.hasTextureTransforms == k2.hasTextureTransforms) &&
46 (k1.emissiveUV == k2.emissiveUV) &&
47 (k1.aoUV == k2.aoUV) &&
48 (k1.normalUV == k2.normalUV) &&
49 (k1.hasTransmissionTexture == k2.hasTransmissionTexture) &&
50 (k1.transmissionUV == k2.transmissionUV) &&
51 (k1.hasSheenColorTexture == k2.hasSheenColorTexture) &&
52 (k1.sheenColorUV == k2.sheenColorUV) &&
53 (k1.hasSheenRoughnessTexture == k2.hasSheenRoughnessTexture) &&
54 (k1.sheenRoughnessUV == k2.sheenRoughnessUV) &&
55 (k1.hasSheen == k2.hasSheen);
56 }
57
58 // Filament supports up to 2 UV sets. glTF has arbitrary texcoord set indices, but it allows
59 // implementations to support only 2 simultaneous sets. Here we build a mapping table with 1-based
60 // indices where 0 means unused. Note that the order in which we drop textures can affect the look
61 // of certain assets. This "order of degradation" is stipulated by the glTF 2.0 specification.
62 void constrainMaterial(MaterialKey* key, UvMap* uvmap) {
63 const int MAX_INDEX = 2;
64 UvMap retval {};
65 int index = 1;
66 if (key->hasBaseColorTexture) {
67 retval[key->baseColorUV] = (UvSet) index++;
68 }
69 if (key->hasMetallicRoughnessTexture && retval[key->metallicRoughnessUV] == UNUSED) {
70 retval[key->metallicRoughnessUV] = (UvSet) index++;
71 }
72 if (key->hasNormalTexture && retval[key->normalUV] == UNUSED) {
73 if (index > MAX_INDEX) {
74 key->hasNormalTexture = false;
75 } else {
76 retval[key->normalUV] = (UvSet) index++;
77 }
78 }
79 if (key->hasOcclusionTexture && retval[key->aoUV] == UNUSED) {
80 if (index > MAX_INDEX) {
81 key->hasOcclusionTexture = false;
82 } else {
83 retval[key->aoUV] = (UvSet) index++;
84 }
85 }
86 if (key->hasEmissiveTexture && retval[key->emissiveUV] == UNUSED) {
87 if (index > MAX_INDEX) {
88 key->hasEmissiveTexture = false;
89 } else {
90 retval[key->emissiveUV] = (UvSet) index++;
91 }
92 }
93 if (key->hasTransmissionTexture && retval[key->transmissionUV] == UNUSED) {
94 if (index > MAX_INDEX) {
95 key->hasTransmissionTexture = false;
96 } else {
97 retval[key->transmissionUV] = (UvSet) index++;
98 }
99 }
100 if (key->hasClearCoatTexture && retval[key->clearCoatUV] == UNUSED) {
101 if (index > MAX_INDEX) {
102 key->hasClearCoatTexture = false;
103 } else {
104 retval[key->clearCoatUV] = (UvSet) index++;
105 }
106 }
107 if (key->hasClearCoatRoughnessTexture && retval[key->clearCoatRoughnessUV] == UNUSED) {
108 if (index > MAX_INDEX) {
109 key->hasClearCoatRoughnessTexture = false;
110 } else {
111 retval[key->clearCoatRoughnessUV] = (UvSet) index++;
112 }
113 }
114 if (key->hasClearCoatNormalTexture && retval[key->clearCoatNormalUV] == UNUSED) {
115 if (index > MAX_INDEX) {
116 key->hasClearCoatNormalTexture = false;
117 } else {
118 retval[key->clearCoatNormalUV] = (UvSet) index++;
119 }
120 }
121 if (key->hasSheenColorTexture && retval[key->sheenColorUV] == UNUSED) {
122 if (index > MAX_INDEX) {
123 key->hasSheenColorTexture = false;
124 } else {
125 retval[key->sheenColorUV] = (UvSet) index++;
126 }
127 }
128 if (key->hasSheenRoughnessTexture && retval[key->sheenRoughnessUV] == UNUSED) {
129 if (index > MAX_INDEX) {
130 key->hasSheenRoughnessTexture = false;
131 } else {
132 retval[key->sheenRoughnessUV] = (UvSet) index++;
133 }
134 }
135 // NOTE: KHR_materials_clearcoat does not provide separate UVs, we'll assume UV0
136 *uvmap = retval;
137 }
138
139 void processShaderString(std::string* shader, const UvMap& uvmap, const MaterialKey& config) {
140 auto replaceAll = [shader](const std::string& from, const std::string& to) {
141 size_t pos = shader->find(from);
142 for (; pos != std::string::npos; pos = shader->find(from, pos)) {
143 shader->replace(pos, from.length(), to);
144 }
145 };
146 static const std::string uvstrings[] = { "vec2(0)", "getUV0()", "getUV1()" };
147 const auto& normalUV = uvstrings[uvmap[config.normalUV]];
148 const auto& baseColorUV = uvstrings[uvmap[config.baseColorUV]];
149 const auto& metallicRoughnessUV = uvstrings[uvmap[config.metallicRoughnessUV]];
150 const auto& emissiveUV = uvstrings[uvmap[config.emissiveUV]];
151 const auto& transmissionUV = uvstrings[uvmap[config.transmissionUV]];
152 const auto& aoUV = uvstrings[uvmap[config.aoUV]];
153 const auto& clearCoatUV = uvstrings[uvmap[config.clearCoatUV]];
154 const auto& clearCoatRoughnessUV = uvstrings[uvmap[config.clearCoatRoughnessUV]];
155 const auto& clearCoatNormalUV = uvstrings[uvmap[config.clearCoatNormalUV]];
156 const auto& sheenColorUV = uvstrings[uvmap[config.sheenColorUV]];
157 const auto& sheenRoughnessUV = uvstrings[uvmap[config.sheenRoughnessUV]];
158
159 replaceAll("${normal}", normalUV);
160 replaceAll("${color}", baseColorUV);
161 replaceAll("${metallic}", metallicRoughnessUV);
162 replaceAll("${ao}", aoUV);
163 replaceAll("${emissive}", emissiveUV);
164 replaceAll("${transmission}", transmissionUV);
165 replaceAll("${clearCoat}", clearCoatUV);
166 replaceAll("${clearCoatRoughness}", clearCoatRoughnessUV);
167 replaceAll("${clearCoatNormal}", clearCoatNormalUV);
168 replaceAll("${sheenColor}", sheenColorUV);
169 replaceAll("${sheenRoughness}", sheenRoughnessUV);
170 }
171
172 } // namespace gltfio