Skip to content

Commit df1a013

Browse files
committed
Depth properly working
1 parent 06e157a commit df1a013

6 files changed

Lines changed: 60 additions & 30 deletions

File tree

src/main.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import { StringBuffer } from "./utils/string_buffer";
66
import { Mesh } from "./rendering/mesh";
77
import { Scene } from "./rendering/scene";
88
import { mul_mat4 } from "./math/matrix_operators";
9+
import { build_scene } from "./to_html";
910

1011

1112
export function main_3d() {
1213
const target = document.getElementById('container');
1314
if (!target) return;
1415
const wireframe_el = document.getElementById('wireframe-mode') as HTMLInputElement;
1516
const do_wireframe:boolean = wireframe_el?.checked;
16-
const sun_mesh = create_sphere(1.5, 16, 16);
17-
const planet_mesh = create_sphere(0.5, 12, 12);
17+
const sun_mesh = create_sphere(1.5, 10, 10);
18+
const planet_mesh = create_sphere(0.5, 10, 10);
1819

1920
const scene:Scene = new Scene([sun_mesh.vertices,planet_mesh.vertices],[sun_mesh.indices,planet_mesh.indices]);
2021

@@ -37,10 +38,11 @@ export function main_3d() {
3738
planet_model = translate(planet_model, vec3(3.5, 0, 0));
3839
planet_model = rotate(planet_model, time * 3, vec3(1, 0, 1));
3940

40-
const planet_mvp = mul_mat4(planet_model,vp);
41-
const sun_mvp = mul_mat4(sun_model,vp);
41+
const planet_mvp = mul_mat4(vp,planet_model);
42+
const sun_mvp = mul_mat4(vp,sun_model);
4243

4344
render_scene(scene,[sun_mvp,planet_mvp],true);
45+
frame_html = build_scene(scene,do_wireframe,string_buffer);
4446

4547
target!.innerHTML = frame_html;
4648
requestAnimationFrame(loop);

src/rendering/primitive_assembler.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,11 @@ export function assemble_primitives(mesh:Mesh):void{
1313
let out_index = 0;
1414
for(let i = 0; i < indices.length; ++i){
1515
const index = indices[i];
16-
const v1 = indices[index * 3] * 4;
17-
const v2 = indices[index * 3 + 1] * 4;
18-
const v3 = indices[index * 3 + 2] * 4;
16+
const p_base = index * 4;
1917

20-
out[out_index++] = projected[v1];
21-
out[out_index++] = projected[v1 + 1];
22-
out[out_index++] = projected[v1 + 2];
23-
out[out_index++] = projected[v1 + 3];
24-
25-
out[out_index++] = projected[v2];
26-
out[out_index++] = projected[v2 + 1];
27-
out[out_index++] = projected[v2 + 2];
28-
out[out_index++] = projected[v2 + 3];
29-
30-
out[out_index++] = projected[v3];
31-
out[out_index++] = projected[v3 + 1];
32-
out[out_index++] = projected[v3 + 2];
33-
out[out_index++] = projected[v3 + 3];
18+
out[out_index++] = projected[p_base];
19+
out[out_index++] = projected[p_base + 1];
20+
out[out_index++] = projected[p_base + 2];
21+
out[out_index++] = projected[p_base + 3];
3422
}
3523
}

src/rendering/rasterizer.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ export function rasterize(mesh:Mesh, invert_y:boolean = true, stride:number = 4)
2222
let n1 = vec3(0,0,0);
2323
let n2 = vec3(0,0,0);
2424
let n3 = vec3(0,0,0);
25-
26-
for(let i = 0; i < mesh.visible_triangles_count; ++i){
25+
for(let i = 0; i < mesh.raster_buffer.length/(3*stride); ++i){
2726
const base = 3 * i * stride;
2827
c1[0] = vertices[base + 0]; c1[1] = vertices[base + 1]; c1[2] = vertices[base + 2]; c1[3] = vertices[base + 3];
2928
c2[0] = vertices[base + stride + 0]; c2[1] = vertices[base + stride + 1]; c2[2] = vertices[base + stride + 2]; c2[3] = vertices[base + stride + 3];
@@ -33,6 +32,9 @@ export function rasterize(mesh:Mesh, invert_y:boolean = true, stride:number = 4)
3332
const v2 = process_perspective_mutate(c2, n2);
3433
const v3 = process_perspective_mutate(c3, n3);
3534

35+
if(!v1 || !v2 || !v3){
36+
continue;
37+
}
3638
const x1 = n1[0];
3739
const y1 = n1[1] * sign;
3840
const z1 = n1[2];
@@ -51,10 +53,6 @@ export function rasterize(mesh:Mesh, invert_y:boolean = true, stride:number = 4)
5153
continue;
5254
}
5355

54-
if(!v1 || !v2 || !v3){
55-
continue;
56-
}
57-
5856
out[out_index++] = x1;
5957
out[out_index++] = y1;
6058
out[out_index++] = z1;

src/rendering/render.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export function render_scene(scene:Scene, mvp:mat4[], invert_y:boolean = true){
3535
}
3636
offset += mesh.raster_buffer.length;
3737
}
38-
scene.draw_order.subarray(0,index).sort((a,b)=>get_z_value(scene.scene_buffer,a) - get_z_value(scene.scene_buffer,b));
38+
scene.draw_order.subarray(0,index).sort((a,b)=>get_z_value(scene.scene_buffer,b) - get_z_value(scene.scene_buffer,a));
39+
scene.draw_end = index;
3940
}
4041

4142

src/rendering/scene.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Mesh } from "./mesh";
44
export class Scene{
55
scene_buffer:ArrayType;
66
draw_order: IndexingType;
7+
draw_end:number
78
meshes:Mesh[];
89

910
constructor(vertices:ArrayType[], indices:IndexingType[]){
@@ -19,6 +20,7 @@ export class Scene{
1920
this.scene_buffer = new ArrayType(index_size * 4);
2021
this.draw_order = new IndexingType(vert_size/3);
2122
this.meshes = new Array(vertices.length);
23+
this.draw_end = this.draw_order.length;
2224
let prev_length = 0;
2325
for(let i = 0; i < vertices.length; ++i){
2426
this.meshes[i] = new Mesh(vertices[i],indices[i],this.scene_buffer.subarray(prev_length,prev_length + indices[i].length*4));

src/to_html.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ArrayType } from "./math/types";
2+
import type { Scene } from "./rendering/scene";
23
import { character_to_uint, SB_TOKENS, string_to_uint, type StringBuffer } from "./utils/string_buffer";
34

45
export type vec3 = [number,number,number];
@@ -130,7 +131,7 @@ export function build_3d_svg_legacy(vertices:ArrayType, end:number, use_rect:boo
130131
const thickness = 0.005;
131132

132133

133-
for(let i = 0; i < n ; i+=6){
134+
for(let i = 0; i < n ; i+=9){
134135
const x1 = vertices[i];
135136
const y1 = vertices[i+1];
136137

@@ -168,7 +169,7 @@ const PATH_TOKENS = {
168169
export function build_3d_svg(vertices:ArrayType, end:number, wireframe_mode:boolean,buffer:StringBuffer):string{
169170
buffer.reset();
170171
buffer.write_chunk(PATH_TOKENS.HEAD);
171-
for(let i = 0; i < end; i+=6){
172+
for(let i = 0; i < end; i+=9){
172173
const x1 = vertices[i];
173174
const y1 = vertices[i+1];
174175

@@ -192,4 +193,42 @@ export function build_3d_svg(vertices:ArrayType, end:number, wireframe_mode:bool
192193
buffer.write_chunk(PATH_TOKENS.TAIL_SOLID);
193194
}
194195
return decoder.decode(buffer.buffer.subarray(0,buffer.cursor));
196+
}
197+
198+
199+
200+
201+
202+
export function build_scene(scene:Scene, wireframe_mode: boolean, buffer:StringBuffer):string{
203+
buffer.reset();
204+
if(wireframe_mode)
205+
buffer.write_chunk(PATH_TOKENS.HEAD);
206+
const vertices = scene.scene_buffer;
207+
for(let i = 0; i < scene.draw_end; i++){
208+
const index = scene.draw_order[i];
209+
const x1 = vertices[index];
210+
const y1 = vertices[index+1];
211+
212+
const x2 = vertices[index+3];
213+
const y2 = vertices[index+4];
214+
215+
const x3 = vertices[index+6];
216+
const y3 = vertices[index+7];
217+
218+
if(!wireframe_mode)
219+
buffer.write_chunk(PATH_TOKENS.HEAD);
220+
buffer.write_chunk(PATH_TOKENS.M);
221+
push_pair(x1,y1,buffer);
222+
buffer.write_chunk(PATH_TOKENS.L);
223+
push_pair(x2,y2,buffer);
224+
buffer.write_chunk(PATH_TOKENS.L);
225+
push_pair(x3,y3,buffer);
226+
buffer.write_chunk(PATH_TOKENS.Z);
227+
if(!wireframe_mode)
228+
buffer.write_chunk(PATH_TOKENS.TAIL_SOLID);
229+
}
230+
if(wireframe_mode){
231+
buffer.write_chunk(PATH_TOKENS.TAIL_WIREFRAME);
232+
}
233+
return decoder.decode(buffer.buffer.subarray(0,buffer.cursor));
195234
}

0 commit comments

Comments
 (0)