Print at Dec 18, 2025, 6:09:47 PM

Posted by Puybaret at Mar 1, 2024, 12:19:55 PM
Re: Optimizing objects for display in the htmlviewer?
Optimizing 3D models can be done on various parameters:
- its file size
- its file reading process
- its data in memory
- its display process

Optimizing the file size may consist of reducing the file size used by the 3D model on the hard drive where it's saved, the bandwidth used to download it to the browser and the size it occupies in the cache of the browser.

Optimizing the file reading process may consist of reducing the time required to read a 3D model file to transform it to the internal data required to display it. It includes unzipping the 3D model, reading and converting the decimal numbers it contains to binary data, transforming the polygons it may contain to triangles, and generating normals if the 3D model doesn't specify them.

Optimizing data in memory may consist of reducing the texture coordinates data required to map textures on the model. When texture coordinates are not provided in the 3D model size, they will be computed automatically at the moment a 3D model with textures is displayed (and this may look not as perfect as if you specify these coordinates yourself). Vertices and normals (read or generated) always exist in memory and the numbers they handle always occupy the same space, whatever the number of decimals they have.

Optimizing display process may consist of reducing the time required to display 3D models at screen. The more shapes are handled, the more time it will take to display them. For example, a monochrome model based on one shape of 1000 triangles will occupy a similar space in memory as the same model based on 100 shapes of 10 triangles, but the first one will be displayed faster, moreover if it's half transparent.

Some of these optimizations are bound to each other:
When normals are quite straight forward to compute, a good way to reduce the size of a 3D model file is to remove them. The file will be smaller to download, it will contain less decimal numbers to decode but the processor will have to generate normals (not a complicated process which consists of computing cross products).
Using more polygons (for example for the base and the top of a cylinder) will reduce the size of a 3D model file too but will oblige Sweet Home 3D to transform them to triangles.


In the examples you gave, formats 1 and 2 are handled at the same speed.
The format 3 is smaller and is displayed as fast as formats 1 and 2 in Sweet Home 3D JS / HTMLViewer, because all the shapes of a 3D model are actually grouped by material, except the ones which name starts by "sweethome3d_".
Format 4 will be displayed also at the same speed, because line shapes starting by "o" are handled the same way as the line shapes starting by "g", and missing normals are generated in memory when the 3D model is read (otherwise objects would look flat). Lines starting by "vn" don't influence the display processing efficiency.
Note that in Blender, there's an option to generate normals or not.

If 3D models at the 4 formats are displayed as fast (except if they contain moving parts), how can you optimize the display of a large project? The key is to reduce the number of shapes to display.
For example, let's say you have 10 rows of 20 seats in a theater, and each seat has 2 materials. If you use one 3D model seat, the 3D view will display 10 x 20 x 2 = 400 shapes. If you use a 3D model consisting of a row of 20 seats, the 3D view will have to display only 10 x 2 = 20 shapes and this will run faster (but probably not 20 times faster because the graphics card will have to handle more triangles for each shape).
So it's up to you to find the best tradeoff, because in that case, the viewer will use 20 times more memory to handle the coordinates of a whole row of seats. But depending on the file format you use, the 3D model file won't be necessarily 20 times bigger: in OBJ format, it’s not mandatory to repeat normals and texture coordinates when they are equal ("vn" and "vt" lines), and in DAE format, it’s possible to avoid repeating the data for each seat because that format is able to describe a model made of x times the same shape (if the software which exports to DAE format is programmed to handle this kind of assembly).


Note that HugeFileCLeaner won't optimize anything on 3D models size. It's an utility which removes entries in a SH3D file which were useless before version 5.2.
If you want to reduce OBJ file size, you can use another utility I just published at https://www.sweethome3d.com/downloads/OBJFileSizeOptimizer-1.0.jar (source code included). I programmed it to optimize OBJ files by removing trailing decimals and equal normals / texture coordinates. I use it to reduce the size of the 3D models that I add to the free models page. As it overwrites the optimized OBJ file, be sure to make some backups first.
----------------------------------------
Emmanuel Puybaret, Sweet Home 3D creator