|
1 | | -import { ArrayType } from "../math/types"; |
| 1 | +import { depth_sort } from "./depth_sorting"; |
2 | 2 | import type { Mesh } from "./mesh"; |
3 | 3 |
|
4 | 4 | /** |
5 | 5 | * |
6 | 6 | * @param mesh Mutated |
7 | 7 | * @param stride |
8 | 8 | */ |
9 | | -export function assemble_primitives(mesh:Mesh, stride:number = 4):void{ |
10 | | - const vertices = mesh.projected_buffer; |
| 9 | +export function assemble_primitives(mesh:Mesh):void{ |
| 10 | + const visible_count = depth_sort(mesh); |
| 11 | + const projected = mesh.projected_buffer; |
11 | 12 | const indices = mesh.indices; |
12 | | - const required_space = indices.length * stride; |
13 | | - if (mesh.raster_buffer.length < required_space){ |
14 | | - console.warn("Resizing the raster buffer's out - this generally shouldn't happen"); |
15 | | - mesh.raster_buffer = new ArrayType(required_space); |
16 | | - } |
| 13 | + const order = mesh.draw_order; |
| 14 | + |
17 | 15 | const out = mesh.raster_buffer |
18 | 16 | let out_index = 0; |
19 | | - for(let i = 0; i < indices.length; ++i){ |
20 | | - const index = indices[i]; |
21 | | - const start = index * stride; |
| 17 | + for(let i = 0; i < visible_count; ++i){ |
| 18 | + const index = order[i]; |
| 19 | + |
| 20 | + const v1 = indices[index * 3] * 4; |
| 21 | + const v2 = indices[index * 3 + 1] * 4; |
| 22 | + const v3 = indices[index * 3 + 2] * 4; |
22 | 23 |
|
23 | | - for(let k = 0; k < stride; ++k){ |
24 | | - out[out_index++] = vertices[start + k]; |
25 | | - } |
| 24 | + out[out_index++] = projected[v1]; |
| 25 | + out[out_index++] = projected[v1 + 1]; |
| 26 | + out[out_index++] = projected[v1 + 2]; |
| 27 | + out[out_index++] = projected[v1 + 3]; |
| 28 | + |
| 29 | + out[out_index++] = projected[v2]; |
| 30 | + out[out_index++] = projected[v2 + 1]; |
| 31 | + out[out_index++] = projected[v2 + 2]; |
| 32 | + out[out_index++] = projected[v2 + 3]; |
| 33 | + |
| 34 | + out[out_index++] = projected[v3]; |
| 35 | + out[out_index++] = projected[v3 + 1]; |
| 36 | + out[out_index++] = projected[v3 + 2]; |
| 37 | + out[out_index++] = projected[v3 + 3]; |
26 | 38 | } |
| 39 | + mesh.visible_triangles_count = visible_count; |
27 | 40 | } |
0 commit comments