Skip to content

Commit 8cae9ff

Browse files
committed
simplify material parsing
1 parent d3bef02 commit 8cae9ff

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

components/lib/processMaterial.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
import { processColor } from 'react-native';
2-
import { intersection } from 'lodash';
2+
import { isString, mapValues, set } from 'lodash';
33

44
// https://developer.apple.com/documentation/scenekit/scnmaterial
5-
const materialPropertiesWithMaps = [
6-
'normal',
7-
'diffuse',
8-
'displacement',
9-
'specular',
10-
];
5+
const propsWithMaps = ['normal', 'diffuse', 'displacement', 'specular'];
116

127
export default function processMaterial(material) {
13-
const propsToUpdate = intersection(
14-
Object.keys(material),
15-
materialPropertiesWithMaps,
16-
);
8+
// previously it was possible to set { material: { color:'colorstring'}}... translate this to { material: { diffuse: { color: 'colorstring'}}}
9+
if (material.color) {
10+
set(material, 'diffuse.color', material.color);
11+
}
1712

18-
return propsToUpdate.reduce(
19-
(prev, curr) => ({
20-
...prev,
21-
[curr]: {
22-
...prev[curr],
23-
color: processColor(
24-
curr === 'diffuse' && typeof prev[curr] === 'string'
25-
? prev[curr]
26-
: prev[curr].color,
27-
),
28-
},
29-
}),
13+
return mapValues(
3014
material,
15+
(prop, key) =>
16+
propsWithMaps.includes(key)
17+
? {
18+
...prop,
19+
color: processColor(
20+
// allow for setting a diffuse colorstring { diffuse: 'colorstring'}
21+
key === 'diffuse' && isString(prop) ? prop : prop.color,
22+
),
23+
}
24+
: prop,
3125
);
3226
}

0 commit comments

Comments
 (0)