Skip to content

Commit b7ce605

Browse files
committed
Added normals
1 parent 795addc commit b7ce605

8 files changed

Lines changed: 74 additions & 15 deletions

File tree

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function main_3d() {
1717
const sun_mesh = create_sphere(1.5, 32, 32);
1818
const planet_mesh = create_sphere(0.5, 16, 16);
1919

20-
const scene:Scene = new Scene([sun_mesh.vertices,planet_mesh.vertices],[sun_mesh.indices,planet_mesh.indices]);
20+
const scene:Scene = new Scene([sun_mesh,planet_mesh]);
2121

2222
console.log(scene.draw_end)
2323

src/math/matrix_operators.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ export function add_vec3(u:vec3, v:vec3): vec3{
222222
return vec3(u[0]+v[0],u[1]+v[1],u[2]+v[2]);
223223
}
224224

225+
export function sub_vec3(u:vec3,v:vec3):vec3{
226+
return vec3(u[0]-v[0],u[1]-v[1],u[2]-v[2]);
227+
}
228+
225229

226230
export function add_mat(u:ArrayType, v:ArrayType):ArrayType{
227231
const out:ArrayType = new ArrayType(u.length);

src/rendering/process_lighting.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { length_vec3, mul_mat4_vec4, mul_mat4_vec4_mut } from "../math/matrix_operators";
1+
import { length_vec3, mul_mat4_vec4, mul_mat4_vec4_mut, sub_vec3 } from "../math/matrix_operators";
22
import { ArrayType, mat4, vec3, vec4 } from "../math/types";
33
import type { Light } from "./types/light";
44
import type { Mesh } from "./types/mesh";
@@ -9,18 +9,17 @@ export function process_lighting(mesh:Mesh, scene:Scene){
99
const world_coordinates = mesh.projected_buffer;
1010
const lights = scene.lights;
1111
let vertex:vec3 = vec3(0,0,0);
12+
const k = 0.01;
1213
for(let i = 0; i < world_coordinates.length; i+=4){
1314
vertex[0] = world_coordinates[i];
1415
vertex[1] = world_coordinates[i+1];
1516
vertex[2] = world_coordinates[i+2];
1617
let color = vec3(0,0,0);
17-
let intensity = 0;
1818
for(const light of lights){
19-
vertex[0] -= light.position[0];
20-
vertex[1] -= light.position[1];
21-
vertex[2] -= light.position[2];
22-
const dist = length_vec3(vertex);
23-
19+
const L = sub_vec3(vertex,light.position);
20+
const dist = length_vec3(L);
21+
if (dist > light.radius) continue;
22+
2423
}
2524
}
2625
}

src/rendering/types/geometry.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,46 @@ import { ArrayType, IndexingType } from "../../math/types";
44
export class Geometry{
55
vertices:ArrayType;
66
indices:IndexingType;
7-
7+
normals:ArrayType;
88
constructor(vertices:ArrayType, indices:IndexingType){
99
this.vertices = vertices;
10-
this.indices = indices;
10+
this.indices = indices;
11+
this.normals = new ArrayType(this.vertices.length);
12+
this.compute_normals();
13+
}
14+
private compute_normals() {
15+
const inds = this.indices;
16+
const verts = this.vertices;
17+
const norms = this.normals;
18+
19+
norms.fill(0);
20+
21+
for (let i = 0; i < inds.length; i += 3) {
22+
const i1 = inds[i] * 3;
23+
const i2 = inds[i+1] * 3;
24+
const i3 = inds[i+2] * 3;
25+
26+
const ux = verts[i2] - verts[i1];
27+
const uy = verts[i2+1] - verts[i1+1];
28+
const uz = verts[i2+2] - verts[i1+2];
29+
30+
const vx = verts[i3] - verts[i1];
31+
const vy = verts[i3+1] - verts[i1+1];
32+
const vz = verts[i3+2] - verts[i1+2];
33+
34+
const nx = uy * vz - uz * vy;
35+
const ny = uz * vx - ux * vz;
36+
const nz = ux * vy - uy * vx;
37+
38+
norms[i1] += nx; norms[i1+1] += ny; norms[i1+2] += nz;
39+
norms[i2] += nx; norms[i2+1] += ny; norms[i2+2] += nz;
40+
norms[i3] += nx; norms[i3+1] += ny; norms[i3+2] += nz;
41+
}
42+
for (let i = 0; i < norms.length; i += 3) {
43+
const len = Math.sqrt(norms[i]**2 + norms[i+1]**2 + norms[i+2]**2);
44+
if (len > 0.0001) {
45+
norms[i] /= len; norms[i+1] /= len; norms[i+2] /= len;
46+
}
47+
}
1148
}
1249
}

src/rendering/types/mesh.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
import { ArrayType, IndexingType, vec3 } from "../../math/types";
1+
import { mul_mat4_vec4 } from "../../math/matrix_operators";
2+
import { ArrayType, IndexingType, mat4, vec3, vec4 } from "../../math/types";
23
import type { Geometry } from "./geometry";
34

45

56
export class Mesh{
67
vertices: ArrayType
78
indices: IndexingType
9+
local_normals: ArrayType
810
projected_buffer: ArrayType
911
raster_buffer: ArrayType
1012
color_buffer: ArrayType
13+
normals: ArrayType;
1114
albedo: vec3;
1215
raster_end:number
1316
visible_triangles_count:number
1417

1518
constructor(geometry:Geometry, albedo:vec3 = vec3(0,0,0), raster_buffer:ArrayType | null = null, projected_buffer:ArrayType | null = null, color_buffer:ArrayType | null = null){
1619
this.vertices = geometry.vertices;
1720
this.indices = geometry.indices;
21+
this.local_normals = geometry.normals;
22+
this.normals = new ArrayType(geometry.normals.length);
1823
if(projected_buffer === null)
1924
this.projected_buffer = new ArrayType(this.vertices.length * 4 / 3);
2025
else
@@ -33,5 +38,18 @@ export class Mesh{
3338
this.raster_end = this.indices.length * 4;
3439
this.visible_triangles_count = 0;
3540
}
41+
update_normals(model: mat4) {
42+
let temp_n = vec4(0,0,0,0);
43+
for (let i = 0; i < this.normals.length; i += 3) {
44+
temp_n[0] = this.local_normals[i];
45+
temp_n[1] = this.local_normals[i+1];
46+
temp_n[2] = this.local_normals[i+2];
47+
temp_n[3] = 0;
48+
const world_n = mul_mat4_vec4(model, temp_n);
49+
this.normals[i] = world_n[0];
50+
this.normals[i+1] = world_n[1];
51+
this.normals[i+2] = world_n[2];
52+
}
53+
}
54+
}
3655

37-
}

src/rendering/types/scene.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ export class Scene{
4343
* @param max_triangles The maximum number of triangles this Scene object can store.
4444
*/
4545
reserve(max_triangles:number){
46-
this.scene_buffer = new ArrayType(max_triangles * 9);
46+
this.scene_buffer = new ArrayType(max_triangles * 12);
4747
this.projected_buffer = new ArrayType(max_triangles * 12);
4848
this.draw_order = new IndexingType(max_triangles);
49+
this.color_buffer = new ArrayType(max_triangles*3);
4950
this.draw_end = 0;
5051
this.meshes = [];
5152
this.lights = [];

src/rendering/utils/primitives.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ArrayType, IndexingType } from "../../math/types";
22
import { Geometry } from "../types/geometry";
33

4-
export function create_sphere(radius: number, rings: number, slices: number):{vertices:ArrayType, indices: IndexingType} {
4+
export function create_sphere(radius: number, rings: number, slices: number):Geometry {
55
const vertice_count = (rings + 1) * (slices + 1) * 3;
66
const vertices = new ArrayType(vertice_count);
77
const indices = new IndexingType(rings * slices * 6);

src/to_html.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ArrayType } from "./math/types";
2-
import type { Scene } from "./rendering/scene";
2+
import type { Scene } from "./rendering/types/scene";
33
import { character_to_uint, SB_TOKENS, string_to_uint, type StringBuffer } from "./utils/string_buffer";
44

55
export type vec3 = [number,number,number];

0 commit comments

Comments
 (0)