Skip to content

Commit

Permalink
Fix sdf demo with texture copy
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson committed Nov 9, 2024
1 parent 84d382b commit aa800ca
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
17 changes: 15 additions & 2 deletions example/sdfGeneration.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,37 @@ function updateSDF() {
sdfTex.texture.type = floatLinearExtSupported ? THREE.FloatType : THREE.HalfFloatType;
sdfTex.texture.minFilter = THREE.LinearFilter;
sdfTex.texture.magFilter = THREE.LinearFilter;
renderer.initRenderTarget( sdfTex );

// prep the sdf generation material pass
generateSdfPass.material.uniforms.bvh.value.updateFrom( bvh );
generateSdfPass.material.uniforms.matrix.value.copy( matrix );

// create a 2d render target to render in to
const scratchVec = new THREE.Vector3();
const scratchTarget = new THREE.WebGLRenderTarget( dim, dim );
scratchTarget.texture.format = THREE.RedFormat;
scratchTarget.texture.type = floatLinearExtSupported ? THREE.FloatType : THREE.HalfFloatType;

// render into each layer
for ( let i = 0; i < dim; i ++ ) {

generateSdfPass.material.uniforms.zValue.value = i * pxWidth + halfWidth;

renderer.setRenderTarget( sdfTex, i );
renderer.setRenderTarget( scratchTarget );
generateSdfPass.render( renderer );

// copy the data into the 3d texture since rendering directly into the target causes significant gpu artifacts
// See issue #720
scratchVec.z = i;
renderer.copyTextureToTexture( scratchTarget.texture, sdfTex.texture, null, scratchVec );

}

// initiate read back to get a rough estimate of time taken to generate the sdf
renderer.readRenderTargetPixels( sdfTex, 0, 0, 1, 1, new Float32Array( 4 ) );
renderer.readRenderTargetPixels( scratchTarget, 0, 0, 1, 1, new Float32Array( 4 ) );
renderer.setRenderTarget( null );
scratchTarget.dispose();

} else {

Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"simplex-noise": "^2.4.0",
"static-server": "^2.2.1",
"stats.js": "^0.17.0",
"three": "^0.166.1",
"three": "^0.170.0",
"typescript": "^5.1.3",
"vite": "^5.2.13"
}
Expand Down

0 comments on commit aa800ca

Please sign in to comment.