Maybe rework the layer mask. Don’t use the 32 bits of number for the layer mask, but use a nicer API.
At the moment the default mask is 0x0FFFFFFF so there a only 4 bits left for hiding from default cameras. My problem here is that i have to change the mask of every mesh and camera to use more than the 4 bits left.
For example the camera and mesh could use a Set<SomeKindOfIdentifierType>. The Set# isDisjointFrom() method can do the intersection checks and should be fast.
Working compute shaders, raytracing.
What do you mean by working compute shaders? We already support compute shaders:
https://doc.babylonjs.com/features/featuresDeepDive/materials/shaders/computeShader
oh yeah, you are right. If you really do shrink it, perhaps, I’d just code what I needed instead of importing funcs. But that’s me…
I’ve theorized about creating a proper treeshaking algorithm in the past… but the work involved would be insane ![]()
I’m still hoping for it to be developed, these days maybe even AI assisted, imagine it traverses all your code from a starting point, simular to how a browser does and then excludes all pieces of code and functions etc that are never used ![]()
cooking it down;
say you import only the Vector3 math library, and only ever access the x/y/z values in your code
quick and dirty, the exported result would be something like
class Vector3 {
constructor( x = 0, y = 0, z = 0){
this._x = x;
this._y = y;
this._z = z;
this._isDirty;
}
// getters & setters for _x/_y/_z, dirty mechanism, etc
}
Well, my letter to Santa is this short list:
1.) Better, working GI like Voxel GI. The (cascaded) voxelization could be calculated as a worker, by chunks (groups of meshes), lighting by voxel tracing on GPU. A lot of examples exist now, for example:
-
In “Ashes” WebGL engine:
https://but0n.github.io/webgl-gi/ -
In WickedEngine (written by a fellow hungarian guy):
https://wickedengine.net/2017/08/voxel-based-global-illumination/
He is even doing SSGI, Screen space can also look good. -
There is/was a THREE.js example somewhere but I can’t find it anymore. (Maybe some Mandela effect?
I’m sure I saw it somewhere!)
2.) A good tutorial/series that shows how to make different types of games, from Blender and animated assets to rendering, setting up lighting, shadows, effects, particles, physics and game logic, also with some performance tips like instancing and batching.
That’s it. That is my list. ![]()
BABYLON is already awesome! Thanks for all the years of work!
About the topic, I think there could be an LTS version that is backwards compatible, and a ‘New API’ that is the “Bleeding Edge”, that works for new projects. Like Vue 2 and 3, they are totally different API, fully rewritten, but there is no stress, You can still use Vue 2, but you start new projects with Vue 3.
I guess I have outdated knowledge. Vaguely remember something was not ready or working.
I will run some tests and get back to you!
Refactoring the core async handling code like loaders to Promises, and impl the compatible layer of callback-based api on promise-based api, so no more async uncaught errors like this:
Also I think the side-effects needed for es6 imports with tree shaking feels very hacky. I would love if we could just import the stuff needed without needing to find the exact side effect in the doc or the code base in Babylon X
Taking Vector3 as an example, why do we need it to be a class? Surely it can be a type, simplifying compilation. I’m just thinking how many bundled exports that could be omitted/the value of doing this (probably minimal?)
Can you tell me more about your idea?
What would that mean in reality?
Some thoughts:
-
If there are many class instances in memory, could we possibly improve performance by replacing with objects or functions. I haven’t benchmarked a typical BJS app so would need to, to back this up.
-
Importing types or interfaces can avoid the need of tree shaking, as apposed to importing compiled functions. I haven’t thought this through yet but if primitive Tuples exist or if a transpiler could convert a property like this…
position.x>position[0]
This is an interesting proposal:
I may be talking silly and jumping the gun, but I like challenging things
Does this mean something like gl-matrix?
Also, Records and Tuples meant to be immutable, which in case of 3d rendering, can hurt gc performance. If arrays or pointers does not meet the requirement, have a look at proposal-structs , which is sealed but not immutable.
As a breaking change, I think gpu particle subemitter would be the most beneficial.
Some areas we’ve seen that could use improvement in terms of perf.
On the high level, anything that could speed up the time to that first paint would be amazing. That first paint being in a state where the scene is fully intractable thereafter (so not using a trick like a proxy image).
- Speed up loading of .envs (we’ve noticed these can be slow at times)
- Utilizing shader caching or find a way to split up PBRMaterial somehow to speed up shader compile times.
- Explore geometry virtualization techniques and built easy to use tools around them (voxels, splatting,UE Nanite style virtualized geometry etc)
** Note: Im sure some of the above would only be possible via webGPU.
Developer experience related enhancements: (sorry, can move to a different thread if this is off topic)
- Performance and debugging deepdive video on BJS Chronicles. Yes there are some docs on this but a in-depth discussion on performance considerations and debugging tools would be great. Discussing caveats to watch out for etc. Lots to unwrap here but just more education about performance would be good. There is some stuff out there but could use more.
- More advanced shader tutorials using Material Plugins and working with raw shader code (not NME). Anything with examples or boilerplate code to work from is always handy. Almost feel like monthly ‘tech-art’ style vlogging on advanced effects would be good.
- More indepth documentation on how the render pipeline works eg: when certain things occur, the order in which they occur, the architecture of the system as a whole (such as PostProcess pipeline). Supporting visuals, sample code, vlogs and videos would be welcome.
IMO, performance isnt always about optimizing code, a lot of times there are gaps in knowlege that lead to bad performance and therefore educating the developer to help them have a firm graspof the architecture is key.
This could also make it possible for pre thin instance culling.
Another thing to consider when breaking changes are allowed it to move from arrays to Set or Map, e.g. scene.getMeshById, which is O(n) in time, can be O(1) if scene.meshes is a Map.
We are not aiming toward a big change as we saw with recent updates that eve small changes are painful for our users
I wonder if we can use a set internally somehow still
To keep compatible with existing array-based api while switching to maps would introduce extra complexity of making a Proxy to be ArrayLike, and handle data change for set, push, splice, and all methods like this.
Some AI code to show the complexity
// Define Nullable type for clarity
type Nullable<T> = T | null | undefined;
class Mesh {
readonly id: string;
constructor(id: string) {
this.id = id;
}
}
class Scene {
_meshMap: Map<string, Mesh[]>;
_meshes: Mesh[];
constructor(initialMeshes: Mesh[] = []) {
this._meshes = initialMeshes;
this._meshMap = new Map<string, Mesh[]>();
this.rebuildMeshMap(); // Initial population of the map
}
/**
* Rebuilds the internal _meshMap based on the current _meshes array.
* This is a fallback or initial population method.
*/
private rebuildMeshMap(): void {
this._meshMap = new Map<string, Mesh[]>();
if (!this._meshes) {
return;
}
for (const mesh of this._meshes) {
if (!this._meshMap.has(mesh.id)) {
this._meshMap.set(mesh.id, []);
}
this._meshMap.get(mesh.id)!.push(mesh);
}
// console.log("Map rebuilt. Current map:", this._meshMap); // For debugging
}
/**
* Adds a mesh to the internal map.
* @param mesh The mesh to add.
*/
private addMeshToMap(mesh: Mesh): void {
if (!this._meshMap.has(mesh.id)) {
this._meshMap.set(mesh.id, []);
}
this._meshMap.get(mesh.id)!.push(mesh);
// console.log(`Added mesh ${mesh.id} to map. Map state:`, this._meshMap); // For debugging
}
/**
* Removes a mesh from the internal map.
* @param mesh The mesh to remove.
*/
private removeMeshFromMap(mesh: Mesh): void {
if (this._meshMap.has(mesh.id)) {
const meshList = this._meshMap.get(mesh.id)!;
const indexToRemove = meshList.indexOf(mesh);
if (indexToRemove > -1) {
meshList.splice(indexToRemove, 1);
// If the list for this ID becomes empty, remove the ID from the map
if (meshList.length === 0) {
this._meshMap.delete(mesh.id);
}
// console.log(`Removed mesh ${mesh.id} from map. Map state:`, this._meshMap); // For debugging
}
}
}
/**
* Provides a proxied version of the meshes array.
* This proxy intercepts array mutation operations and assignments
* to ensure that _meshMap is updated incrementally.
*/
get meshes(): Mesh[] {
const that = this; // Capture 'this' context for use within the proxy handler
return new Proxy(this._meshes, {
/**
* Intercepts property access on the array.
* Wraps mutation methods to trigger incremental map updates.
*/
get(target: Mesh[], prop: string | symbol, receiver: any): any {
// Define methods that mutate the array and can be handled incrementally
const incrementalMethods = {
'push': (targetArray: Mesh[], args: any[]) => {
const addedMeshes: Mesh[] = [];
for (const arg of args) {
// Handle potential array arguments for push (e.g., push(...otherArray))
if (Array.isArray(arg)) {
for (const item of arg) {
if (item instanceof Mesh) {
addedMeshes.push(item);
that.addMeshToMap(item);
}
}
} else if (arg instanceof Mesh) {
addedMeshes.push(arg);
that.addMeshToMap(arg);
}
}
// Return the result of the original push operation
return Reflect.get(targetArray, prop, receiver).apply(targetArray, args);
},
'pop': (targetArray: Mesh[]) => {
const removedMesh = targetArray[targetArray.length - 1]; // Get the last item before it's removed
const result = Reflect.get(targetArray, prop, receiver).apply(targetArray);
if (removedMesh instanceof Mesh) {
that.removeMeshFromMap(removedMesh);
}
return result;
},
'shift': (targetArray: Mesh[]) => {
const removedMesh = targetArray[0]; // Get the first item before it's removed
const result = Reflect.get(targetArray, prop, receiver).apply(targetArray);
if (removedMesh instanceof Mesh) {
that.removeMeshFromMap(removedMesh);
}
return result;
},
'unshift': (targetArray: Mesh[], args: any[]) => {
const addedMeshes: Mesh[] = [];
for (const arg of args) {
if (arg instanceof Mesh) {
addedMeshes.push(arg);
that.addMeshToMap(arg);
}
}
// Return the result of the original unshift operation
return Reflect.get(targetArray, prop, receiver).apply(targetArray, args);
},
// For methods like splice, sort, reverse, etc., a full rebuild is often simpler
// or required for correctness if not carefully managed.
// We'll fall back to rebuild for these.
'splice': (targetArray: Mesh[], args: any[]) => {
const removedItems = targetArray.slice(args[0], args[0] + args[1]); // Capture items to be removed
const result = Reflect.get(targetArray, prop, receiver).apply(targetArray, args);
const addedItems = args.slice(2); // Get items that were added
// Remove old items from map
for (const item of removedItems) {
if (item instanceof Mesh) {
that.removeMeshFromMap(item);
}
}
// Add new items to map
for (const item of addedItems) {
if (item instanceof Mesh) {
that.addMeshToMap(item);
}
}
return result;
},
'sort': (targetArray: Mesh[]) => {
// Sorting can change the order of elements within the _meshMap's arrays
// if the sorting criteria is not just ID. A full rebuild is safer here.
const result = Reflect.get(targetArray, prop, receiver).apply(targetArray);
that.rebuildMeshMap(); // Fallback to full rebuild
return result;
},
'reverse': (targetArray: Mesh[]) => {
// Similar to sort, reverse changes order. Rebuild for safety.
const result = Reflect.get(targetArray, prop, receiver).apply(targetArray);
that.rebuildMeshMap(); // Fallback to full rebuild
return result;
},
'fill': (targetArray: Mesh[], args: any[]) => {
// Fill can replace multiple elements, making incremental updates complex.
// Rebuild for safety.
const result = Reflect.get(targetArray, prop, receiver).apply(targetArray, args);
that.rebuildMeshMap(); // Fallback to full rebuild
return result;
},
'copyWithin': (targetArray: Mesh[], args: any[]) => {
// Similar to splice, copyWithin can affect multiple elements. Rebuild.
const result = Reflect.get(targetArray, prop, receiver).apply(targetArray, args);
that.rebuildMeshMap(); // Fallback to full rebuild
return result;
}
};
// If the accessed property is one of our handled methods
if (typeof prop === 'string' && incrementalMethods[prop as keyof typeof incrementalMethods]) {
const handler = incrementalMethods[prop as keyof typeof incrementalMethods];
// Return a function that calls the handler, which in turn calls the original method and updates the map
return function(...args: any[]) {
// Special handling for `push` and `unshift` to correctly process multiple arguments or spread syntax
if (prop === 'push' || prop === 'unshift') {
return handler(target, args);
} else if (prop === 'splice') {
// Splice needs careful handling of removed and added items
return handler(target, args);
} else {
// For pop, shift, sort, reverse, fill, copyWithin, call handler with target array
return handler(target);
}
};
}
// For all other property accesses (e.g., .length, accessing elements by index),
// use Reflect.get to perform the default behavior.
return Reflect.get(target, prop, receiver);
},
/**
* Intercepts property setting on the array.
* This handles direct assignment to array elements, e.g., `meshes[0] = newMesh`.
*/
set(target: Mesh[], prop: string | symbol, value: any, receiver: any): boolean {
const isNumericIndex = typeof prop === 'number' || (typeof prop === 'string' && !isNaN(Number(prop)));
if (isNumericIndex) {
// Get the old mesh at this index to remove it from the map
const oldMesh = target[prop as number];
// Perform the actual assignment
const success = Reflect.set(target, prop, value, receiver);
if (success) {
// If the assignment was successful, update the map
if (oldMesh instanceof Mesh) {
that.removeMeshFromMap(oldMesh);
}
if (value instanceof Mesh) {
that.addMeshToMap(value);
}
}
return success;
}
// Handle setting the 'length' property, which can also mutate the array
if (prop === 'length') {
const oldLength = target.length;
const success = Reflect.set(target, prop, value, receiver);
if (success) {
if (value < oldLength) {
// If length was reduced, remove the excess elements from the map
for (let i = value; i < oldLength; i++) {
const removedMesh = target[i]; // Note: target[i] might be undefined if length was set to 0
if (removedMesh instanceof Mesh) {
that.removeMeshFromMap(removedMesh);
}
}
} else if (value > oldLength) {
// If length was increased, new elements are undefined. No map update needed for these.
// If the user then assigns actual Mesh objects to these new indices,
// the `set` trap for numeric indices will handle it.
}
}
return success;
}
// For any other property setting, perform the default behavior.
return Reflect.set(target, prop, value, receiver);
}
});
}
/**
* Setter for the meshes property.
* When a new array of meshes is assigned, it updates the internal _meshes
* and rebuilds the _meshMap accordingly.
*/
set meshes(newMeshes: Mesh[]) {
// Assign the new array to the internal _meshes property
this._meshes = newMeshes;
// Rebuild the map to reflect the new array of meshes
this.rebuildMeshMap();
}
/**
* Gets a list of meshes using their Id.
* @param id defines the Id to search for.
* @returns a list of meshes, or an empty array if none are found.
*/
getMeshesById(id: string): Array<Mesh> {
return this._meshMap.get(id) || [];
}
/**
* Gets the first added mesh found of a given Id.
* @param id defines the Id to search for.
* @returns the mesh found or null if not found at all.
*/
getMeshById(id: string): Nullable<Mesh> {
const meshes = this.getMeshesById(id);
return meshes.length > 0 ? meshes[0] : null;
}
/**
* Gets the last added mesh using a given Id.
* @param id defines the Id to search for.
* @returns the found mesh or null if not found at all.
*/
getLastMeshById(id: string): Nullable<Mesh> {
const meshes = this.getMeshesById(id);
return meshes.length > 0 ? meshes[meshes.length - 1] : null;
}
}
// // Example Usage:
// const scene = new Scene();
// const mesh1 = new Mesh("cube");
// const mesh2 = new Mesh("sphere");
// const mesh3 = new Mesh("cube");
// const mesh4 = new Mesh("cylinder");
// const mesh5 = new Mesh("sphere");
// const mesh6 = new Mesh("cube");
// // Using the setter
// scene.meshes = [mesh1, mesh2, mesh3];
// console.log("Initial map:", scene._meshMap);
// // Using the getter and push (which is proxied)
// console.log("\n--- Testing push ---");
// const currentMeshes = scene.meshes;
// currentMeshes.push(mesh4); // Pushes mesh4 (cylinder)
// console.log("Map after push:", scene._meshMap);
// // Using the getter and push with spread syntax
// console.log("\n--- Testing push with spread ---");
// currentMeshes.push(mesh5, mesh6); // Pushes mesh5 (sphere) and mesh6 (cube)
// console.log("Map after push (spread):", scene._meshMap);
// // Using direct element assignment via getter (which is proxied)
// console.log("\n--- Testing element assignment ---");
// const mesh7 = new Mesh("triangle");
// currentMeshes[0] = mesh7; // Replaces the first mesh (originally mesh1, a cube) with mesh7 (triangle)
// console.log("Map after element assignment:", scene._meshMap);
// // --- Testing the new getter methods ---
// console.log("\n--- Testing new getter methods ---");
// console.log("getMeshesById('cube'):", scene.getMeshesById("cube"));
// console.log("getMeshById('sphere'):", scene.getMeshById("sphere"));
// console.log("getLastMeshById('sphere'):", scene.getLastMeshById("sphere"));
// console.log("getMeshById('nonexistent'):", scene.getMeshById("nonexistent"));
// console.log("getLastMeshById('nonexistent'):", scene.getLastMeshById("nonexistent"));
// console.log("getMeshesById('nonexistent'):", scene.getMeshesById("nonexistent"));
// // Using pop
// console.log("\n--- Testing pop ---");
// const poppedMesh = currentMeshes.pop(); // Removes the last mesh (mesh6, a cube)
// console.log("Popped mesh:", poppedMesh?.id);
// console.log("Map after pop:", scene._meshMap);
// // Using shift
// console.log("\n--- Testing shift ---");
// const shiftedMesh = currentMeshes.shift(); // Removes the first mesh (mesh7, a triangle)
// console.log("Shifted mesh:", shiftedMesh?.id);
// console.log("Map after shift:", scene._meshMap);
// // Using unshift
// console.log("\n--- Testing unshift ---");
// const mesh8 = new Mesh("pentagon");
// currentMeshes.unshift(mesh8); // Adds mesh8 (pentagon) to the beginning
// console.log("Map after unshift:", scene._meshMap);
// // Using splice to remove and add
// console.log("\n--- Testing splice ---");
// const mesh9 = new Mesh("hexagon");
// const mesh10 = new Mesh("heptagon");
// // Remove 1 item starting from index 1 (the 'cube'), and insert mesh9 and mesh10
// const removedFromSplice = currentMeshes.splice(1, 1, mesh9, mesh10);
// console.log("Removed by splice:", removedFromSplice.map(m => m.id));
// console.log("Map after splice:", scene._meshMap);
// // Testing a method that triggers full rebuild (sort)
// console.log("\n--- Testing sort ---");
// currentMeshes.sort((a, b) => a.id.localeCompare(b.id)); // Sorts the array
// console.log("Array after sort:", currentMeshes.map(m => m.id));
// console.log("Map after sort:", scene._meshMap);
// // Expected: Map { 'hexagon' => [ Mesh { id: 'hexagon' } ], 'heptagon' => [ Mesh { id: 'heptagon' } ], 'pentagon' => [ Mesh { id: 'pentagon' } ], 'sphere' => [ Mesh { id: 'sphere' }, Mesh { id: 'sphere' } ] }
// // Note: The order within the map's arrays might not change unless the sort criteria directly affects it.
// // The key is that the map is rebuilt, ensuring consistency.
// console.log("\n--- Final check ---");
// console.log("Meshes by ID 'cube':", scene.getMeshesById("cube")); // Should be empty or gone if all cubes removed
// console.log("Meshes by ID 'sphere':", scene.getMeshesById("sphere"));
// console.log("Meshes by ID 'triangle':", scene.getMeshesById("triangle")); // Should be empty or gone if all triangles removed
// console.log("Meshes by ID 'pentagon':", scene.getMeshesById("pentagon"));
// console.log("Meshes by ID 'hexagon':", scene.getMeshesById("hexagon"));
// console.log("Meshes by ID 'heptagon':", scene.getMeshesById("heptagon"));
Yeah lol I was afraid of that. So maybe not for a while