Skip to content

Commit

Permalink
Fix small material leaks in arcs/paths layers
Browse files Browse the repository at this point in the history
  • Loading branch information
vasturiano committed Dec 10, 2024
1 parent 93b3ae2 commit 08608a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
24 changes: 13 additions & 11 deletions src/layers/arcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,29 @@ export default Kapsule({
state.ticker?.resume();
},
_destructor: function(state) {
state.sharedMaterial.dispose();
state.ticker?.dispose();
}
},

init(threeObj, state, { tweenGroup }) {
stateInit: ({ tweenGroup }) => ({
tweenGroup,
ticker: new FrameTicker(),
sharedMaterial: new THREE.ShaderMaterial({
...(dashedLineShaders()),
transparent: true,
blending: THREE.NormalBlending
})
}),

init(threeObj, state) {
// Clear the scene
emptyObject(threeObj);

// Main three object to manipulate
state.scene = threeObj;

state.tweenGroup = tweenGroup;

// Kick-off dash animations
state.ticker = new FrameTicker();
state.ticker.onTick.add((_, timeDelta) => {
state.arcsData
.filter(d => d.__threeObj && d.__threeObj.children.length && d.__threeObj.children[0].material && d.__threeObj.children[0].__dashAnimateStep)
Expand Down Expand Up @@ -115,12 +123,6 @@ export default Kapsule({
const dashInitialGapAccessor = accessorFn(state.arcDashInitialGap);
const dashAnimateTimeAccessor = accessorFn(state.arcDashAnimateTime);

const sharedMaterial = new THREE.ShaderMaterial({
...(dashedLineShaders()),
transparent: true,
blending: THREE.NormalBlending
});

threeDigest(state.arcsData, state.scene, {
createObj: () => {
const obj = new THREE.Group(); // populated in updateObj
Expand All @@ -140,7 +142,7 @@ export default Kapsule({
? new THREE.Mesh()
: new THREE.Line(new THREE.BufferGeometry());

obj.material = sharedMaterial.clone(); // Separate material instance per object to have dedicated uniforms (but shared shaders)
obj.material = state.sharedMaterial.clone(); // Separate material instance per object to have dedicated uniforms (but shared shaders)

group.add(obj);
}
Expand Down
23 changes: 12 additions & 11 deletions src/layers/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,24 @@ export default Kapsule({
}
},

init(threeObj, state, { tweenGroup }) {
stateInit: ({ tweenGroup }) => ({
tweenGroup,
ticker: new FrameTicker(),
sharedMaterial: new THREE.ShaderMaterial({
...(dashedLineShaders()),
transparent: true,
blending: THREE.NormalBlending
})
}),

init(threeObj, state) {
// Clear the scene
emptyObject(threeObj);

// Main three object to manipulate
state.scene = threeObj;

state.tweenGroup = tweenGroup;

// Kick-off dash animations
state.ticker = new FrameTicker();
state.ticker.onTick.add((_, timeDelta) => {
state.pathsData
.filter(d => d.__threeObj && d.__threeObj.children.length && d.__threeObj.children[0].material && d.__threeObj.children[0].__dashAnimateStep)
Expand Down Expand Up @@ -115,12 +122,6 @@ export default Kapsule({
const dashInitialGapAccessor = accessorFn(state.pathDashInitialGap);
const dashAnimateTimeAccessor = accessorFn(state.pathDashAnimateTime);

const sharedShaderMaterial = new THREE.ShaderMaterial({
...(dashedLineShaders()),
transparent: true,
blending: THREE.NormalBlending
});

threeDigest(state.pathsData, state.scene, {
createObj: () => {
const obj = new THREE.Group(); // populated in updateObj
Expand All @@ -140,7 +141,7 @@ export default Kapsule({
? new Line2(new LineGeometry(), new LineMaterial())
: new THREE.Line(
new THREE.BufferGeometry(),
sharedShaderMaterial.clone() // Separate material instance per object to have dedicated uniforms (but shared shaders)
state.sharedMaterial.clone() // Separate material instance per object to have dedicated uniforms (but shared shaders)
);

group.add(obj);
Expand Down

0 comments on commit 08608a3

Please sign in to comment.