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" ;
23import type { Geometry } from "./geometry" ;
34
45
56export 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- }
0 commit comments