-
Notifications
You must be signed in to change notification settings - Fork 0
/
materials.ts
129 lines (109 loc) · 3.64 KB
/
materials.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { PNG } from "pngjs";
import { Gltf, GltfBuilder } from "../gltf";
import { Mbx } from "../mbx";
import { colors } from "./data/colors";
import { Options } from "./options";
export const convertMaterial = (
id: number,
normals: Mbx.TextureRef[] | undefined,
decoration: Mbx.PartDecoration | undefined,
gltf: GltfBuilder,
options: Options
): Gltf.Index<Gltf.Material> | undefined => {
normals ??= [];
decoration ??= {};
const isSimple =
(!options.normalMaps || normals.length === 0) &&
(!options.decals || Object.keys(decoration).length === 0);
const key = `/materials/${isSimple ? id : Math.random()}`;
if (isSimple && gltf.hasMaterial(key)) return gltf.getMaterialIndex(key);
const color = colors[id];
if (!color) {
console.warn("material not found: " + id);
return undefined;
}
let material: Gltf.Material | undefined;
switch (color.type) {
case "solid":
material = {
name: color.name,
pbrMetallicRoughness: {
baseColorFactor: color.color,
metallicFactor: 0.0,
roughnessFactor: 0.1,
},
};
break;
case "transparent":
material = {
name: color.name,
alphaMode: "BLEND",
pbrMetallicRoughness: {
baseColorFactor: color.color,
metallicFactor: 0.0,
roughnessFactor: 0.0,
},
};
break;
case "rubber":
material = {
name: color.name,
pbrMetallicRoughness: {
baseColorFactor: color.color,
metallicFactor: 0.0,
roughnessFactor: 0.75,
},
};
break;
default:
console.warn("add this material");
console.warn(color);
}
if (!material) return undefined;
if (options.decals && decoration.uv && decoration.color) {
const baseColor = material.pbrMetallicRoughness!.baseColorFactor!;
const imageData = Buffer.from(
gltf.getImage(`/textures/color/${decoration.color.name}`).uri!.split(",")[1],
"base64"
);
const png = PNG.sync.read(imageData);
for (let off = 0; off < png.data.length; off += 4) {
const alpha = png.data[off + 3] / 255;
png.data[off + 0] = Math.round(png.data[off + 0] * alpha + 255 * baseColor[0] * (1 - alpha));
png.data[off + 1] = Math.round(png.data[off + 1] * alpha + 255 * baseColor[1] * (1 - alpha));
png.data[off + 2] = Math.round(png.data[off + 2] * alpha + 255 * baseColor[2] * (1 - alpha));
png.data[off + 3] = Math.round(255 * baseColor[3]);
}
const newTextureDataUri = "data:image/png;base64," + PNG.sync.write(png).toString("base64");
material.pbrMetallicRoughness!.baseColorTexture = {
texCoord: decoration.uv,
index: gltf.addTexture(key + "#color", {
name: key + "#color",
source: gltf.addImage(key + "#color", {
name: key + "#color",
uri: newTextureDataUri,
}),
sampler: gltf.addSampler(key + "#color", {
name: key + "#color",
wrapS: Gltf.Const.CLAMP_TO_EDGE,
wrapT: Gltf.Const.CLAMP_TO_EDGE,
}),
}),
};
}
if (options.normalMaps && normals[0]) {
material.normalTexture = {
texCoord: normals[0].uv,
index: gltf.addTexture(key + "#normals", {
name: key + "#normals",
source: gltf.getImageIndex(`/textures/normal/${normals[0].file}`),
sampler: gltf.addSampler(key + "#normals", {
name: key + "#normals",
wrapS: normals[0].repeat ? Gltf.Const.REPEAT : Gltf.Const.CLAMP_TO_EDGE,
wrapT: normals[0].repeat ? Gltf.Const.REPEAT : Gltf.Const.CLAMP_TO_EDGE,
}),
}),
};
}
return gltf.addMaterial(key, material);
};