-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WebGPURenderer: support using 3d textures in shaders with texture3d()…
… and add VolumeNodeMaterial() and examples. (#28418) * support 3d textures * remove unused import * whitespace fixes * export texture3D * use new tsl * replacing to updateValue() --------- Co-authored-by: aardgoose <[email protected]>
- Loading branch information
Showing
17 changed files
with
642 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import TextureNode from './TextureNode.js'; | ||
import { addNodeClass } from '../core/Node.js'; | ||
import { nodeProxy, vec3, tslFn, If } from '../shadernode/ShaderNode.js'; | ||
|
||
const normal = tslFn( ( { texture, uv } ) => { | ||
|
||
const epsilon = 0.0001; | ||
|
||
const ret = vec3().temp(); | ||
|
||
If( uv.x.lessThan( epsilon ), () => { | ||
|
||
ret.assign( vec3( 1, 0, 0 ) ); | ||
|
||
} ).elseif( uv.y.lessThan( epsilon ), () => { | ||
|
||
ret.assign( vec3( 0, 1, 0 ) ); | ||
|
||
} ).elseif( uv.z.lessThan( epsilon ), () => { | ||
|
||
ret.assign( vec3( 0, 0, 1 ) ); | ||
|
||
} ).elseif( uv.x.greaterThan( 1 - epsilon ), () => { | ||
|
||
ret.assign( vec3( - 1, 0, 0 ) ); | ||
|
||
} ).elseif( uv.y.greaterThan( 1 - epsilon ), () => { | ||
|
||
ret.assign( vec3( 0, - 1, 0 ) ); | ||
|
||
} ).elseif( uv.z.greaterThan( 1 - epsilon ), () => { | ||
|
||
ret.assign( vec3( 0, 0, - 1 ) ); | ||
|
||
} ).else( () => { | ||
|
||
const step = 0.01; | ||
|
||
const x = texture.uv( uv.add( vec3( - step, 0.0, 0.0 ) ) ).r.sub( texture.uv( uv.add( vec3( step, 0.0, 0.0 ) ) ).r ); | ||
const y = texture.uv( uv.add( vec3( 0.0, - step, 0.0 ) ) ).r.sub( texture.uv( uv.add( vec3( 0.0, step, 0.0 ) ) ).r ); | ||
const z = texture.uv( uv.add( vec3( 0.0, 0.0, - step ) ) ).r.sub( texture.uv( uv.add( vec3( 0.0, 0.0, step ) ) ).r ); | ||
|
||
ret.assign( vec3( x, y, z ) ); | ||
|
||
} ); | ||
|
||
return ret.normalize(); | ||
|
||
} ); | ||
|
||
|
||
class Texture3DNode extends TextureNode { | ||
|
||
constructor( value, uvNode = null, levelNode = null ) { | ||
|
||
super( value, uvNode, levelNode ); | ||
|
||
this.isTexture3DNode = true; | ||
|
||
} | ||
|
||
getInputType( /*builder*/ ) { | ||
|
||
return 'texture3D'; | ||
|
||
} | ||
|
||
getDefaultUV() { | ||
|
||
return vec3( 0.5, 0.5, 0.5 ); | ||
|
||
} | ||
|
||
setUpdateMatrix( /*updateMatrix*/ ) { } // Ignore .updateMatrix for 3d TextureNode | ||
|
||
setupUV( builder, uvNode ) { | ||
|
||
return uvNode; | ||
|
||
} | ||
|
||
generateUV( builder, uvNode ) { | ||
|
||
return uvNode.build( builder, 'vec3' ); | ||
|
||
} | ||
|
||
normal( uvNode ) { | ||
|
||
return normal( { texture: this, uv: uvNode } ); | ||
|
||
} | ||
|
||
} | ||
|
||
export default Texture3DNode; | ||
|
||
export const texture3D = nodeProxy( Texture3DNode ); | ||
|
||
addNodeClass( 'Texture3DNode', Texture3DNode ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js'; | ||
import { varying } from '../core/VaryingNode.js'; | ||
import { property } from '../core/PropertyNode.js'; | ||
import { materialReference } from '../accessors/MaterialReferenceNode.js'; | ||
import { modelWorldMatrixInverse } from '../accessors/ModelNode.js'; | ||
import { cameraPosition } from '../accessors/CameraNode.js'; | ||
import { positionGeometry } from '../accessors/PositionNode.js'; | ||
import { tslFn, vec2, vec3, vec4 } from '../shadernode/ShaderNode.js'; | ||
import { min, max } from '../math/MathNode.js'; | ||
import { loop, Break } from '../utils/LoopNode.js'; | ||
import { texture3D } from '../accessors/Texture3DNode.js'; | ||
|
||
class VolumeNodeMaterial extends NodeMaterial { | ||
|
||
constructor( params = {} ) { | ||
|
||
super(); | ||
|
||
this.normals = false; | ||
this.lights = false; | ||
this.isVolumeNodeMaterial = true; | ||
this.testNode = null; | ||
|
||
this.setValues( params ); | ||
|
||
} | ||
|
||
setup( builder ) { | ||
|
||
const map = texture3D( this.map, null, 0 ); | ||
|
||
const hitBox = tslFn( ( { orig, dir } ) => { | ||
|
||
const box_min = vec3( - 0.5 ); | ||
const box_max = vec3( 0.5 ); | ||
|
||
const inv_dir = dir.reciprocal(); | ||
|
||
const tmin_tmp = box_min.sub( orig ).mul( inv_dir ); | ||
const tmax_tmp = box_max.sub( orig ).mul( inv_dir ); | ||
|
||
const tmin = min( tmin_tmp, tmax_tmp ); | ||
const tmax = max( tmin_tmp, tmax_tmp ); | ||
|
||
const t0 = max( tmin.x, max( tmin.y, tmin.z ) ); | ||
const t1 = min( tmax.x, min( tmax.y, tmax.z ) ); | ||
|
||
return vec2( t0, t1 ); | ||
|
||
} ); | ||
|
||
this.fragmentNode = tslFn( () => { | ||
|
||
const vOrigin = varying( vec3( modelWorldMatrixInverse.mul( vec4( cameraPosition, 1.0 ) ) ) ); | ||
const vDirection = varying( positionGeometry.sub( vOrigin ) ); | ||
|
||
const rayDir = vDirection.normalize(); | ||
const bounds = property( 'vec2', 'bounds' ).assign( hitBox( { orig: vOrigin, dir: rayDir } ) ); | ||
|
||
bounds.x.greaterThan( bounds.y ).discard(); | ||
|
||
bounds.assign( vec2( max( bounds.x, 0.0 ), bounds.y ) ); | ||
|
||
const p = property( 'vec3', 'p' ).assign( vOrigin.add( bounds.x.mul( rayDir ) ) ); | ||
const inc = property( 'vec3', 'inc' ).assign( vec3( rayDir.abs().reciprocal() ) ); | ||
const delta = property( 'float', 'delta' ).assign( min( inc.x, min( inc.y, inc.z ) ) ); | ||
|
||
delta.divAssign( materialReference( 'steps', 'float' ) ); | ||
|
||
const ac = property( 'vec4', 'ac' ).assign( vec4( materialReference( 'base', 'color' ), 0.0 ) ); | ||
|
||
loop( { type: 'float', start: bounds.x, end: bounds.y, update: '+= delta' }, () => { | ||
|
||
const d = property( 'float', 'd' ).assign( map.uv( p.add( 0.5 ) ).r ); | ||
|
||
if ( this.testNode !== null ) { | ||
|
||
this.testNode( { map: map, mapValue: d, probe: p, finalColor: ac } ).append(); | ||
|
||
} else { | ||
|
||
// default to show surface of mesh | ||
ac.a.assign( 1 ); | ||
Break(); | ||
|
||
} | ||
|
||
p.addAssign( rayDir.mul( delta ) ); | ||
|
||
} ); | ||
|
||
ac.a.equal( 0 ).discard(); | ||
|
||
return vec4( ac ); | ||
|
||
} )(); | ||
|
||
super.setup( builder ); | ||
|
||
} | ||
|
||
} | ||
|
||
export default VolumeNodeMaterial; | ||
|
||
addNodeMaterial( 'VolumeNodeMaterial', VolumeNodeMaterial ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.