MCPcopy Create free account
hub / github.com/Bloom-Engine/engine / gen_mesh_heightmap

Method gen_mesh_heightmap

native/shared/src/models.rs:198–269  ·  view source on GitHub ↗
(&mut self, image_data: &[u8], img_w: u32, img_h: u32, size_x: f32, size_y: f32, size_z: f32)

Source from the content-addressed store, hash-verified

196 }
197
198 pub fn gen_mesh_heightmap(&mut self, image_data: &[u8], img_w: u32, img_h: u32, size_x: f32, size_y: f32, size_z: f32) -> f64 {
199 let cols = img_w as usize;
200 let rows = img_h as usize;
201 if cols < 2 || rows < 2 { return 0.0; }
202
203 let mut vertices = Vec::with_capacity(cols * rows);
204 let white = [1.0, 1.0, 1.0, 1.0];
205
206 for z in 0..rows {
207 for x in 0..cols {
208 let pixel_idx = (z * cols + x) * 4;
209 let luminance = if pixel_idx + 2 < image_data.len() {
210 (image_data[pixel_idx] as f32 * 0.299
211 + image_data[pixel_idx + 1] as f32 * 0.587
212 + image_data[pixel_idx + 2] as f32 * 0.114) / 255.0
213 } else {
214 0.0
215 };
216
217 let px = (x as f32 / (cols - 1) as f32 - 0.5) * size_x;
218 let py = luminance * size_y;
219 let pz = (z as f32 / (rows - 1) as f32 - 0.5) * size_z;
220 let u = x as f32 / (cols - 1) as f32;
221 let v = z as f32 / (rows - 1) as f32;
222
223 vertices.push(Vertex3D {
224 position: [px, py, pz],
225 normal: [0.0, 1.0, 0.0],
226 color: white,
227 uv: [u, v],
228 joints: [0.0; 4],
229 weights: [0.0; 4],
230 tangent: [0.0; 4],
231 });
232 }
233 }
234
235 // Compute normals from neighboring heights
236 for z in 0..rows {
237 for x in 0..cols {
238 let idx = z * cols + x;
239 let left = if x > 0 { vertices[z * cols + x - 1].position[1] } else { vertices[idx].position[1] };
240 let right = if x < cols - 1 { vertices[z * cols + x + 1].position[1] } else { vertices[idx].position[1] };
241 let up = if z > 0 { vertices[(z - 1) * cols + x].position[1] } else { vertices[idx].position[1] };
242 let down = if z < rows - 1 { vertices[(z + 1) * cols + x].position[1] } else { vertices[idx].position[1] };
243 let sx = size_x / (cols - 1) as f32;
244 let sz = size_z / (rows - 1) as f32;
245 let nx = (left - right) / (2.0 * sx);
246 let nz = (up - down) / (2.0 * sz);
247 let len = (nx * nx + 1.0 + nz * nz).sqrt();
248 vertices[idx].normal = [nx / len, 1.0 / len, nz / len];
249 }
250 }
251
252 let mut indices = Vec::with_capacity((cols - 1) * (rows - 1) * 6);
253 for z in 0..rows - 1 {
254 for x in 0..cols - 1 {
255 let tl = (z * cols + x) as u32;

Callers 7

bloom_gen_mesh_heightmapFunction · 0.80
bloom_gen_mesh_heightmapFunction · 0.80
bloom_gen_mesh_heightmapFunction · 0.80
bloom_gen_mesh_heightmapFunction · 0.80
bloom_gen_mesh_heightmapFunction · 0.80
bloom_gen_mesh_heightmapFunction · 0.80
bloom_gen_mesh_heightmapFunction · 0.80

Calls 2

allocMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected