Introduction
Using a cache to identify if your model has actually changed increases performance across the board. This is something that should be used to not create new unnecessary instances and instead pull out the cached model. This can be used for snappers, but also for other things that can cache a model.
Using Effective Cache Keys for Snappers
There are several cache syntaxes in cm/core3D/cache3D.syntax.cm that can be used to cache a snapper's 3D model. The key used by these syntaxes to identify models is known as the cache key, which should be constructed based on parameters that are required to build the snapper's base model. Using parameters that are unique, or not required to build the snapper's model might result in the cache system losing its effectiveness.
In the following examples, we will look at two cache keys for the model of a simple table surface, a good, well-defined cache key and a not so good one.
The Good
/** * Build3D. (The good one) */ public void build3D(FetchEnv3D env) { Primitive3D[] prims(); Primitive3D cached = cache3D(w, d) { Primitive3D res = Box3D(box((0, 0, h-th), (w, d, h))); result res; }; cached.setMaterial(material3D); prims << cached; this.model = Instance3D(prims, this); }
It consists of the width, and the depth of the snapper, meaning if another instance of the table is created with the same width and depth, the same model could be used, and a new model need not be created.
The Bad
/** * Build3D. (The bad one) */ public void build3D(FetchEnv3D env) { Primitive3D[] prims(); Primitive3D cached = cache3D(w, d, pos) { Primitive3D res = Box3D(box((0, 0, h-th), (w, d, h))); result res; }; cached.setMaterial(material3D); prims << cached; this.model = Instance3D(prims, this); }
Adding the position of the snapper to the key makes it fairly unique (it's quite unlikely that you will end up with multiple snappers with the same position), meaning a new model will have to be created for each instance of the table snapper, although they have the same dimensions, and should look exactly the same.
The Less Obvious
/** * Leg type. */ public LegType legType; /** * Constructor. */ public constructor() { super(); init legType(); } /** * Build3D. */ public void build3D(FetchEnv3D env) { Primitive3D[] prims(); Primitive3D cached = cache3D(w, d, legType) { Primitive3D res = Box3D(box((0, 0, h-th), (w, d, h))); res.setMaterial(material3D); result res; }; prims << cached; this.model = Instance3D(prims, this); }
Since the cache key, in this case, contains the legType
object, which is of course, unique, each model would be treated as a brand new, unique model by the system.
Comments
0 comments
Please sign in to leave a comment.