-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtfxLoader.ts
More file actions
164 lines (136 loc) · 4.55 KB
/
tfxLoader.ts
File metadata and controls
164 lines (136 loc) · 4.55 KB
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
155
156
157
158
159
160
161
162
163
164
import {fromValues as Vec3, vec3} from 'gl-vec3';
import {TfxComponent, IndexBuffer} from 'ecs';
import {
Buffer, BufferType, BufferUsage,
Texture, TextureBindingState, TextureType, createTextureOpts,
TextureFilterMin, TextureFilterMag
} from 'resources';
import {getSizeOfBufferAsTexture, subtractNorm, addNorm} from 'gl-utils';
import {TfxFileData, TfxFileHeader} from './tfxParser';
const writeTexture = (
gl: Webgl, tbs: TextureBindingState,
texture: Texture, data: Float32Array
) => {
const writePoint = {
start: Vec3(0, 0, 0),
dimensions: texture.dimensions,
};
const writeSource = {
unsizedPixelFormat: gl.RGBA,
perChannelType: gl.FLOAT,
data: data,
};
texture.write(gl, tbs, 0, writePoint, writeSource);
};
/** see TressFXAsset::FillTriangleIndexArray */
const createIndexBuffer = (gl: Webgl, tfxHeader: TfxFileHeader): IndexBuffer => {
const idxElements = tfxHeader.numHairStrands * tfxHeader.numVerticesPerStrand;
const idxCpu = Array(idxElements * 6).fill(0);
let id = 0;
let iCount = 0; // actually a vertices count
for (let i = 0; i < tfxHeader.numHairStrands; i++) {
for (let j = 0; j < tfxHeader.numVerticesPerStrand - 1; j++) {
idxCpu[iCount++] = 2 * id;
idxCpu[iCount++] = 2 * id + 1;
idxCpu[iCount++] = 2 * id + 2;
idxCpu[iCount++] = 2 * id + 2;
idxCpu[iCount++] = 2 * id + 1;
idxCpu[iCount++] = 2 * id + 3;
id++;
}
id++;
}
const idxData = Uint32Array.from(idxCpu);
const indexBuffer = Buffer.fromData(gl,
BufferType.IndexBuffer, BufferUsage.STATIC_DRAW,
idxData
);
indexBuffer.bind(gl);
return {
indexBuffer,
indexGlType: gl.UNSIGNED_INT,
triangleCnt: Math.floor(iCount / 3),
};
};
const writePerVertexDataToTexture = (
gl: Webgl, tbs: TextureBindingState, tfxFile: TfxFileData,
data: Float32Array
) => {
const {totalVertices} = tfxFile;
// each texture channel RGBA holds XYZW of the vertex
const texSize = getSizeOfBufferAsTexture(gl, totalVertices);
const texture = new Texture(
gl, tbs,
TextureType.Texture2d,
Vec3(texSize.width, texSize.height, 0),
0,
gl.RGBA32F,
createTextureOpts({
filterMin: TextureFilterMin.Nearest,
filterMag: TextureFilterMag.Nearest,
}),
);
writeTexture(gl, tbs, texture, data);
return texture;
};
const writeTangentsToTexture = (
gl: Webgl, tbs: TextureBindingState, tfxFile: TfxFileData
) => {
const {
vertexPositionsBuffer,
header: {numHairStrands, numVerticesPerStrand}
} = tfxFile;
// NOTE: positions are in XYZW, so 4 components
const tangents = new Float32Array(vertexPositionsBuffer.length);
const getVertexPos = (idx: number) => Vec3(
vertexPositionsBuffer[idx * 4],
vertexPositionsBuffer[idx * 4 + 1],
vertexPositionsBuffer[idx * 4 + 2]
);
const setTangent = (idx: number, t: vec3) => {
tangents[idx * 4 + 0] = t[0];
tangents[idx * 4 + 1] = t[1];
tangents[idx * 4 + 2] = t[2];
};
for (let iStrand = 0; iStrand < numHairStrands; ++iStrand) {
const indexRootVertMaster = iStrand * numVerticesPerStrand;
// vertex 0
const vert_0 = getVertexPos(indexRootVertMaster);
const vert_1 = getVertexPos(indexRootVertMaster + 1);
const tangent = subtractNorm(vert_1, vert_0);
setTangent(indexRootVertMaster, tangent);
// vertex 1 through n-1
for (let i = 1; i < numVerticesPerStrand; i++) {
const vert_i_minus_1 = getVertexPos(indexRootVertMaster + i - 1);
const vert_i = getVertexPos(indexRootVertMaster + i);
const vert_i_plus_1 = getVertexPos(indexRootVertMaster + i + 1);
const tangent_pre = subtractNorm(vert_i, vert_i_minus_1);
const tangent_next = subtractNorm(vert_i_plus_1, vert_i);
let tangent = addNorm(tangent_pre, tangent_next);
// fix the tips, we also changed for-loop range to be bigger
if (i === numVerticesPerStrand - 1) {
tangent = tangent_pre;
}
setTangent(indexRootVertMaster + i, tangent);
}
}
//
return writePerVertexDataToTexture(gl, tbs, tfxFile, tangents);
};
export const prepareTfxData = (
gl: Webgl, tbs: TextureBindingState, tfxFile: TfxFileData
): TfxComponent => {
const {header} = tfxFile;
const positionsTexture = writePerVertexDataToTexture(
gl, tbs, tfxFile, tfxFile.vertexPositionsBuffer
);
const tangentsTexture = writeTangentsToTexture(gl, tbs, tfxFile);
const indexBuffer = createIndexBuffer(gl, tfxFile.header);
return new TfxComponent(
header.numHairStrands,
header.numVerticesPerStrand,
positionsTexture,
tangentsTexture,
indexBuffer,
);
};