-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_pmx_model.js
154 lines (136 loc) · 4.85 KB
/
load_pmx_model.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import {MaterialFlags, PmxReader, PmxVisitor} from "./PmxVisitor.js";
import {TextureModel} from "./texture_model.js";
import {createRGBA8UNormConstantTextureView} from "./kits.js";
class PmxTextureModelVisitor extends PmxVisitor {
/**
* @type {Float32Array}
*/
positionData;
/**
* @type {Float32Array}
*/
normalData;
/**
* @type {Float32Array}
*/
uvData;
visitVertexCount(count) {
this.positionData = new Float32Array(count * 3);
this.normalData = new Float32Array(count * 3);
this.uvData = new Float32Array(count * 2);
}
visitVertex(index, data, skinInfo, edge) {
this.positionData[index * 3] = data[0];
this.positionData[index * 3 + 1] = data[1];
this.positionData[index * 3 + 2] = data[2];
this.normalData[index * 3] = data[3];
this.normalData[index * 3 + 1] = data[4];
this.normalData[index * 3 + 2] = data[5];
this.uvData[index * 2] = data[6];
this.uvData[index * 2 + 1] = data[7];
}
/**
* @type {Uint32Array}
*/
indexDate;
visitElementCount(count) {
this.indexDate = new Uint32Array(count);
}
visitElement(index, vertexIndex) {
this.indexDate[index] = vertexIndex;
}
/**
* @type {{path: string, texture: GPUTexture}[]}
*/
textures;
visitTextureCount(count) {
this.textures = new Array(count);
}
visitTexture(index, path) {
this.textures[index] = {
path,
};
}
/**
* @type {{localName:string, diffuse: number[], texture: number, elementCount: number, cullMode: GPUCullMode}[]}
*/
materials;
visitMaterialCount(count) {
this.materials = new Array(count);
}
visitMaterial(index, localName, name, diffuse, specular, ambient, flags, edge, texture, envTexture, mix, toonSource, toonTexture, comment, elementCount) {
this.materials[index] = {
localName,
diffuse,
texture,
elementCount,
cullMode: (flags & MaterialFlags.NO_CULL) ? "none" : "back",
};
}
}
/**
*
* @param {Renderer} renderer
* @param {URL} url
* @return {Promise<TextureModel[]>}
*/
export async function loadPmxTextureModel(renderer, url) {
let device = renderer.device;
let urlBase = new URL("", url);
let pmx = await (await fetch(url)).arrayBuffer();
let visitor = new PmxTextureModelVisitor();
new PmxReader(pmx).accept(visitor);
for (let texObj of visitor.textures) {
let file = await (await fetch(new URL(texObj.path.split(/[\\/]/).map(i => encodeURI(i)).join("/"), urlBase))).blob();
let bitmap = (await createImageBitmap(file, {}));
let texture = device.createTexture({
label: "loadPmxTextureModel texture",
size: [bitmap.width, bitmap.height, 1],
format: "rgba8unorm",
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
});
device.queue.copyExternalImageToTexture({source: bitmap}, {texture}, [bitmap.width, bitmap.height, 1]);
texObj.texture = texture;
}
let positionBuffer = device.createBuffer({
size: visitor.positionData.byteLength,
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
});
let normalBuffer = device.createBuffer({
size: visitor.normalData.byteLength,
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
});
let uvBuffer = device.createBuffer({
size: visitor.uvData.byteLength,
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
});
let indexBuffer = device.createBuffer({
size: visitor.indexDate.byteLength,
usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
});
device.queue.writeBuffer(positionBuffer, 0, visitor.positionData);
device.queue.writeBuffer(normalBuffer, 0, visitor.normalData);
device.queue.writeBuffer(uvBuffer, 0, visitor.uvData);
device.queue.writeBuffer(indexBuffer, 0, visitor.indexDate);
let models = [];
let sum = 0;
let zeroTexture = createRGBA8UNormConstantTextureView(device, [0, 0, 0, 255]);
for (let material of visitor.materials) {
let model = new TextureModel(renderer);
model.positionBuffer = positionBuffer;
model.normalBuffer = normalBuffer;
model.uvBuffer = uvBuffer;
model.indexBuffer = indexBuffer;
model.firstIndex = sum;
model.indexCount = material.elementCount;
let diffuseTexture = visitor.textures[material.texture].texture.createView({
label: "loadPmxTextureModel texture view",
});
// console.log(material.localName);
model.setTexture(diffuseTexture, zeroTexture);
models.push(model);
sum += material.elementCount;
renderer.models.push(model);
}
return models;
}