@@ -4,7 +4,7 @@ module Materials3DModule
44 using .. Math3DModule
55 using .. Geometry3DModule
66
7- export RenderMaterial, MaterialFace, RenderBox, RenderMesh
7+ export RenderMaterial, MaterialFace, RenderBox, RenderMesh, compute_mesh_bounds!
88
99 # Material for faces
1010 mutable struct RenderMaterial
@@ -14,14 +14,18 @@ module Materials3DModule
1414 alpha:: Float64
1515 has_texture:: Bool
1616 texture_path:: String
17+ # Cached texture file existence (computed once at load time for performance)
18+ texture_file_exists:: Bool
1719
1820 function RenderMaterial (diffuse:: Vec3D = Vec3D (0.8 , 0.8 , 0.8 ),
1921 ambient:: Vec3D = Vec3D (0.2 , 0.2 , 0.2 ),
2022 specular:: Vec3D = Vec3D (0.0 , 0.0 , 0.0 ),
2123 alpha:: Float64 = 1.0 ,
2224 has_texture:: Bool = false ,
2325 texture_path:: String = " " )
24- new (diffuse, ambient, specular, alpha, has_texture, texture_path)
26+ # Compute texture file existence once at creation
27+ texture_file_exists = has_texture && texture_path != " " && isfile (texture_path)
28+ new (diffuse, ambient, specular, alpha, has_texture, texture_path, texture_file_exists)
2529 end
2630 end
2731
@@ -66,6 +70,9 @@ module Materials3DModule
6670 default_stroke_color:: SDL_Color
6771 file_path:: String
6872 normalize_uv_coordinates:: Bool # Whether to normalize UV coordinates to [0,1] range
73+ # Cached bounding box (computed once, used for shadow calculations)
74+ cached_bounds_min:: Union{Nothing, Vec3D}
75+ cached_bounds_max:: Union{Nothing, Vec3D}
6976
7077 function RenderMesh (file_path:: String = " " ,
7178 position:: Vec3D = Vec3D (0 , 0 , 0 ),
@@ -74,8 +81,30 @@ module Materials3DModule
7481 fill_color:: SDL_Color = SDL_Color (255 , 255 , 255 , 255 ),
7582 stroke_color:: SDL_Color = SDL_Color (0 , 0 , 0 , 255 ),
7683 normalize_uv:: Bool = false )
77- new (Vec3D[], UV[], MaterialFace[], Dict {String, RenderMaterial} (), false , position, rotation, scale, fill_color, stroke_color, file_path, normalize_uv)
84+ new (Vec3D[], UV[], MaterialFace[], Dict {String, RenderMaterial} (), false , position, rotation, scale, fill_color, stroke_color, file_path, normalize_uv, nothing , nothing )
7885 end
7986 end
8087
88+ # Compute and cache bounding box for a mesh (call this when mesh is loaded/updated)
89+ function compute_mesh_bounds! (mesh:: RenderMesh )
90+ if isempty (mesh. vertices)
91+ mesh. cached_bounds_min = nothing
92+ mesh. cached_bounds_max = nothing
93+ return
94+ end
95+
96+ # Calculate simple bounding box
97+ min_bounds = mesh. vertices[1 ]
98+ max_bounds = mesh. vertices[1 ]
99+
100+ for vertex in mesh. vertices
101+ min_bounds = Vec3D (min (min_bounds. x, vertex. x), min (min_bounds. y, vertex. y), min (min_bounds. z, vertex. z))
102+ max_bounds = Vec3D (max (max_bounds. x, vertex. x), max (max_bounds. y, vertex. y), max (max_bounds. z, vertex. z))
103+ end
104+
105+ # Cache the bounds
106+ mesh. cached_bounds_min = min_bounds
107+ mesh. cached_bounds_max = max_bounds
108+ end
109+
81110end
0 commit comments