I have a question, I don’t understand the meaning of 128 in ground.optimize(128). Is 128 the ground to be divided into 128*128 grids, why do it?
Hey,
The documentation says that it is the chunksCount
.
Do you need some more explanation?
Yes, the mesh is divided when BABYLON.Mesh.CreateGroundFromHeightMap is defined, which is divided again I don’t understand very well
Hello! The division in CreateGroundForHeightMap is different from the division in optimize. In the first one, the divisions parameter is used in creation, to control how many triangles will the final heightmap have (Babylon.js/groundBuilder.ts at 19e98d3a4c15b434740694b14f90c34b6b4294e3 · BabylonJS/Babylon.js · GitHub). The second one is utilized during picking, rendering, etc, to divide the created mesh into multiple submeshes (Babylon.js/mesh.ts at 19e98d3a4c15b434740694b14f90c34b6b4294e3 · BabylonJS/Babylon.js · GitHub), which will then be used in these operations.
Edit: I tried making a drawing to help visualize the difference
Thank you very much for your answer, I understand the meaning of the parameters, but now there is still a question, set the chunkSize parameter to create the internal octree, but if the chunkSize=100, the grid will be directly divided into 100 parts, which is the same as I The structure of the understood octree is not the same. When chunkSize=100, how is the grid organized according to the structure of the octree?
ChunkSize doesn’t divide by a grid, it divides by indices. For a concrete example, let’s say we had a plane mesh with 8 triangles. Dividing it by a 3x3 grid would lead to something like the right image. While what subdivide does is dividing the indices of the mesh in 3 submeshes, which in this example could mean that triangles 1-3 go to submesh 1, triangles 4-6 go to submesh 2 and triangles 7-8 go to submesh 3:
Also, it’s important to note that the chunkSize parameter controls the number of submeshes, not the division level of the octree. For the division level, there’s another parameter named octreeBlockSize: Babylon.js/groundMesh.ts at fcdad0b06c122db089a23d871d42b9a449f73309 · BabylonJS/Babylon.js · GitHub. The wording on the documentation is a bit misleading, so I’ll fix that.
Created a PR to fix the docstring Improve optimize docstring. by carolhmj · Pull Request #12950 · BabylonJS/Babylon.js (github.com)
Thanks again for your answer, that is, if there are 10,000 triangular faces and chunksize=100, then 100 sub-grids can be divided, and then these 100 sub-grids can be managed in the form of octrees, am I right?
You’re right!
Thank you very much for your patient answer!
It’s my pleasure
Hello, I saw this answer again today, and suddenly I don’t understand it again. I hope you can guide me again. That is, the triangles 1-3 in the figure are divided into grid 1, and the triangles 4-6 are divided into grid 2. How many times are divided into grid 1? First, you need to calculate the total number of triangles, and then divide it by 3.
The division of triangles in the bottom image doesn’t consider grids, it consider chunks. Chunks are basically just parts of the indices array of the mesh. You can visualize it like this. On that picture, we had 8 triangles, so the indices array had 8 * 3 = 24 indices:
[i1, i2, i3, i4, i5, i6, …, i22, i23, i24]
The chunks are just parts of this array. If we had 4 chunks, then each array inside a chunk will have 24 / 4 = 6 indices. Since each triangle is composed of 3 indices, that means each array has 6 / 3 = 2 triangles. So, we have:
Chunk 1: [i1, i2, i3, i4, i5, i6]
Chunk 2: [i7, i8, i9, i10, i11, i12]
Chunk 3: [i13, …, i18]
Chunk 4: [i19, …, i24]