[ThreeJS Force Graph] Dispose deallocated Material array bugfix
This contribution is a bugfix to ThreeJS Force-Directed Graph Javascript library authored by . It is a WebGL subclass of three.js Javascript 3D library to represent graph data in 3-dimensional geometry using force-directed physics.
Repository
https://github.com/vasturiano/three-forcegraph
Lastly I was studying how to interact with 3d-oriented graph data using three.js and 's 3D Force-Directed Graph. During building complex data structues I decided to represent some of graph nodes as BoxGeometry
Although the task is simple, I couldn't get it to work.
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );
Above code works fine. The difference occurs when I had to set an array of materials to describe each of 6-side box separately - THREE.Mesh allows this kind of usage but it bring unexpected consequences.
Issue
.nodeThreeObject(node => {
var box = new THREE.BoxGeometry(10, 10, 10);
var materials = [
new THREE.MeshStandardMaterial({color: 'red'}),
new THREE.MeshStandardMaterial({color: 'green'}),
new THREE.MeshStandardMaterial({color: 'blue'}),
new THREE.MeshStandardMaterial({color: 'cyan'}),
new THREE.MeshStandardMaterial({color: 'magenta'}),
new THREE.MeshStandardMaterial({color: 'yellow'}),
];
// 'materials' is of type Array()
return new THREE.Mesh(box, materials);
});
https://gist.github.com/mys/ff6100af32790ed99890cf4378a14020
If we set custom cube, set of more than one material and modify graphData after, error occurs:
Through reversing code libraries and picking out whether it is issue of Texture class, Material or Mesh I have finally found dispose() function throwing error:
// Clear the scene
for (var t = function t(e) {
e.geometry && e.geometry.dispose(),
e.material && e.material.dispose(), << error
e.texture && e.texture.dispose(),
e.children && e.children.forEach(t)
It seemed that e.material was type of Array(). Like we all know, javascript arrays don't have dispose() activity. All is clear now.
's comment: Ah, I wasn't aware a mesh could have an array of materials. Neat.
Solution
My idea was to check whether we deal with an array or object type. Then each children element (material) dispose in a loop.
if (obj.material) {
if (Array.isArray(obj.material)) {
obj.material.forEach(material => {
material.dispose();
});
}
else {
obj.material.dispose();
}
}
's did only one correction for performance.
instanceof Array is much more effective than isArray(). Often we deal with tens of thousands objects iteracting each other. Every single performance is significant in 3D rendering.
Good to know there is much difference between instanceof Array and isArray(), even if they give same result.
I don't mind if project's author merged this code by himself. I just wanted to make sure this library suit my needs for my new Steem project (incoming).
if (obj.material) { obj.material instanceof Array ? obj.material.forEach(m => m.dispose()) : obj.material.dispose(); }
Final change
Links
Leave [ThreeJS Force Graph] Dispose deallocated Material array bugfix to:
Read more #utopian-io posts
Best Posts From mys
We have not curated any of mys's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From mys
- @mys as Steem Monsters TOP 5 trusted seller
- How 'Above 99' player outplayed Epic Dice
- Mysiowy raport z akcji Perpetuum Mobile
- Mysiowe urodzinki na Steem 🐬
- Check my latest fight !
- Kwadrans po pierwszej, sam
- Kiedy jest mnie za mało...
- Zamknięcie najbardziej intensywnego roku #2018
- Steem-apr - Check out Steem best delegation APR%
- Steemverse - Graph optimizations and decorating details
- Ukryci polscy kolekcjonerzy Steem Monsters
- Development of Goblin Tower 13k for Steem Monsters Game Jam
- Goblin Tower 13k - 13 kilobyte minigame for Steem Monsters Game Jam
- Steemverse - top communites 3D force-directed graph visualization
- Kuratorstwo na Steem od strony technicznej. Prawdy i mity.
- [ThreeJS Force Graph] Dispose deallocated Material array bugfix
- Od zera do pół-delfina 🐬 Pierwsza rocznica na Steem 🎂
- How to achieve unlimited score in 🎮 Hodl the Coin Steem game [Hack]
- Steem Voting weight % is not that linear as we may think
- [SteemNova Story] Pokłosie inwazji na Świat Ligi Izoldy