diff --git a/examples/webgpu_compute_particles_snow.html b/examples/webgpu_compute_particles_snow.html index 4d9ab0a943bc3c..67bdf617415c18 100644 --- a/examples/webgpu_compute_particles_snow.html +++ b/examples/webgpu_compute_particles_snow.html @@ -152,11 +152,11 @@ const rippleOnSurface = texture( collisionPosRT.texture, getCoord( position.xz ) ).toInspector( 'Collision Test', () => { - return texture( collisionPosRT.texture ).r; // .div( collisionCamera.position.y ); + return texture( collisionPosRT.texture ); // .div( collisionCamera.position.y ); } ); - const rippleFloorArea = rippleOnSurface.r.add( scale.x.mul( surfaceOffset ) ); + const rippleFloorArea = rippleOnSurface.add( scale.x.mul( surfaceOffset ) ); If( position.y.greaterThan( rippleFloorArea ), () => { diff --git a/examples/webgpu_postprocessing_ssgi.html b/examples/webgpu_postprocessing_ssgi.html index 555da51624558c..b5458eb93a4c05 100644 --- a/examples/webgpu_postprocessing_ssgi.html +++ b/examples/webgpu_postprocessing_ssgi.html @@ -121,7 +121,7 @@ const ao = giPass.getAONode().toInspector( 'SSGI.AO' ); const gi = giPass.getGINode().toInspector( 'SSGI.GI' ); - const compositePass = vec4( add( scenePassColor.rgb.mul( ao.r ), ( scenePassDiffuse.rgb.mul( gi.rgb ) ) ), scenePassColor.a ); + const compositePass = vec4( add( scenePassColor.rgb.mul( ao ), ( scenePassDiffuse.rgb.mul( gi.rgb ) ) ), scenePassColor.a ); compositePass.name = 'Composite'; // traa diff --git a/examples/webgpu_postprocessing_ssgi_ballpool.html b/examples/webgpu_postprocessing_ssgi_ballpool.html index baabd6668013a6..24e739fd82e154 100644 --- a/examples/webgpu_postprocessing_ssgi_ballpool.html +++ b/examples/webgpu_postprocessing_ssgi_ballpool.html @@ -141,7 +141,7 @@ const gi = giPass.getGINode().toInspector( 'SSGI.GI' ); const compositePass = vec4( - add( scenePassColor.rgb.mul( ao.r ), scenePassDiffuse.rgb.mul( gi.rgb ) ), + add( scenePassColor.rgb.mul( ao ), scenePassDiffuse.rgb.mul( gi.rgb ) ), scenePassColor.a ); diff --git a/src/nodes/accessors/TextureNode.js b/src/nodes/accessors/TextureNode.js index f886ed21397ab3..5e2a713fc372ec 100644 --- a/src/nodes/accessors/TextureNode.js +++ b/src/nodes/accessors/TextureNode.js @@ -7,6 +7,7 @@ import { maxMipLevel } from '../utils/MaxMipLevelNode.js'; import { nodeProxy, vec3, nodeObject, int, Fn } from '../tsl/TSLBase.js'; import { step } from '../math/MathNode.js'; import { NodeUpdateType } from '../core/constants.js'; +import { getTextureType } from '../core/NodeUtils.js'; import { Compatibility, GreaterCompare, GreaterEqualCompare, IntType, LessCompare, LessEqualCompare, NearestFilter, UnsignedIntType } from '../../constants.js'; @@ -228,25 +229,13 @@ class TextureNode extends UniformNode { */ generateNodeType( /*builder*/ ) { - if ( this.value.isDepthTexture === true ) { - - if ( this.gatherNode === null ) return 'float'; + if ( this.gatherNode !== null ) { return 'vec4'; } - if ( this.value.type === UnsignedIntType ) { - - return 'uvec4'; - - } else if ( this.value.type === IntType ) { - - return 'ivec4'; - - } - - return 'vec4'; + return getTextureType( this.value ); } @@ -566,7 +555,7 @@ class TextureNode extends UniformNode { const nodeData = builder.getDataFromNode( this ); - let nodeType = this.getNodeType( builder ); + const nodeType = this.getNodeType( builder ); let propertyName = nodeData.propertyName; @@ -585,12 +574,6 @@ class TextureNode extends UniformNode { const offsetSnippet = offsetNode ? this.generateOffset( builder, offsetNode ) : null; const flipYSnippet = this._flipYUniform ? this._flipYUniform.build( builder, 'bool' ) : null; - if ( gatherSnippet ) { - - nodeType = 'vec4'; - - } - let finalDepthSnippet = depthSnippet; if ( finalDepthSnippet === null && texture.isArrayTexture && this.isTexture3DNode !== true ) { @@ -605,6 +588,20 @@ class TextureNode extends UniformNode { let snippet = this.generateSnippet( builder, textureProperty, uvSnippet, levelSnippet, biasSnippet, finalDepthSnippet, compareSnippet, gradSnippet, gatherSnippet, offsetSnippet, flipYSnippet ); + let snippetType; + + if ( texture.isDepthTexture === true && gatherSnippet === null ) { + + snippetType = 'float'; + + } else { + + snippetType = texture.type === UnsignedIntType ? 'uvec4' : ( texture.type === IntType ? 'ivec4' : 'vec4' ); + + } + + snippet = builder.format( snippet, snippetType, nodeType ); + if ( compareStepSnippet !== null ) { const compareFunction = texture.compareFunction; diff --git a/src/nodes/core/NodeBuilder.js b/src/nodes/core/NodeBuilder.js index 50ef6dafef541c..86e5fe44fb76d5 100644 --- a/src/nodes/core/NodeBuilder.js +++ b/src/nodes/core/NodeBuilder.js @@ -8,7 +8,7 @@ import ParameterNode from './ParameterNode.js'; import StructType from './StructType.js'; import FunctionNode from '../code/FunctionNode.js'; import NodeMaterial from '../../materials/nodes/NodeMaterial.js'; -import { getDataFromObject, getTypeFromLength, hashString } from './NodeUtils.js'; +import { getDataFromObject, getTypeFromLength, getTextureType, hashString } from './NodeUtils.js'; import { NodeUpdateType, defaultBuildStages, shaderStages } from './constants.js'; import { @@ -23,7 +23,7 @@ import CubeRenderTarget from '../../renderers/common/CubeRenderTarget.js'; import BindGroup from '../../renderers/common/BindGroup.js'; -import { REVISION, IntType, UnsignedIntType, LinearFilter, LinearMipmapNearestFilter, NearestMipmapLinearFilter, LinearMipmapLinearFilter, NormalBlending, RedFormat, RGFormat, RGBFormat, RedIntegerFormat, RGIntegerFormat, RGBIntegerFormat } from '../../constants.js'; +import { REVISION, IntType, UnsignedIntType, LinearFilter, LinearMipmapNearestFilter, NearestMipmapLinearFilter, LinearMipmapLinearFilter, NormalBlending } from '../../constants.js'; import { RenderTarget } from '../../core/RenderTarget.js'; import { Color } from '../../math/Color.js'; import { Vector2 } from '../../math/Vector2.js'; @@ -573,50 +573,15 @@ class NodeBuilder { */ getOutputType( index = 0 ) { - let type = 'vec4'; - const renderTarget = this.renderer.getRenderTarget(); if ( renderTarget !== null ) { - const renderTargetType = renderTarget.textures[ index ].type; - const renderTargetFormat = renderTarget.textures[ index ].format; - - let typeStr = 'vec'; - - if ( renderTargetType === IntType ) { - - typeStr = 'ivec'; - - } else if ( renderTargetType === UnsignedIntType ) { - - typeStr = 'uvec'; - - } - - if ( renderTargetFormat === RedFormat || renderTargetFormat === RedIntegerFormat ) { - - if ( renderTargetType === IntType ) type = 'int'; - else if ( renderTargetType === UnsignedIntType ) type = 'uint'; - else type = 'float'; - - } else if ( renderTargetFormat === RGFormat || renderTargetFormat === RGIntegerFormat ) { - - type = `${ typeStr }2`; - - } else if ( renderTargetFormat === RGBFormat || renderTargetFormat === RGBIntegerFormat ) { - - type = `${ typeStr }3`; - - } else { - - type = `${ typeStr }4`; - - } + return getTextureType( renderTarget.textures[ index ] ); } - return type; + return 'vec4'; } diff --git a/src/nodes/core/NodeUtils.js b/src/nodes/core/NodeUtils.js index 73ab12f5a0a59e..0b4c3495f8e0b1 100644 --- a/src/nodes/core/NodeUtils.js +++ b/src/nodes/core/NodeUtils.js @@ -6,6 +6,20 @@ import { Vector2 } from '../../math/Vector2.js'; import { Vector3 } from '../../math/Vector3.js'; import { Vector4 } from '../../math/Vector4.js'; +import { + IntType, + UnsignedIntType, + AlphaFormat, + RedFormat, + RedIntegerFormat, + RGFormat, + RGIntegerFormat, + RGBFormat, + RGBIntegerFormat, + DepthFormat, + DepthStencilFormat +} from '../../constants.js'; + import { error } from '../../utils.js'; import StackTrace from '../core/StackTrace.js'; @@ -276,6 +290,74 @@ export function getValueType( value ) { } +/** + * Returns the node data type for the given texture. + * + * @private + * @method + * @param {Texture} texture - The texture. + * @return {string} The data type. + */ +export function getTextureType( texture ) { + + if ( texture.isDepthTexture === true ) { + + return 'float'; + + } + + const format = texture.format; + + let length; + + if ( format === RedFormat || format === RedIntegerFormat || format === DepthFormat || format === DepthStencilFormat || format === AlphaFormat ) { + + length = 1; + + } else if ( format === RGFormat || format === RGIntegerFormat ) { + + length = 2; + + } else if ( format === RGBFormat || format === RGBIntegerFormat ) { + + length = 3; + + } else { + + length = 4; + + } + + let componentType; + + if ( texture.type === UnsignedIntType ) { + + componentType = 'uint'; + + } else if ( texture.type === IntType ) { + + componentType = 'int'; + + } else { + + componentType = 'float'; + + } + + if ( length === 1 ) return componentType; + + let baseType = getTypeFromLength( length ); + + if ( componentType !== 'float' ) { + + baseType = componentType[ 0 ] + baseType; + + } + + return baseType; + +} + /** * Returns the value/object for the given data type and parameters. *