Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShadowMaps for custom wgslFn shaders #30685

Open
Spiri0 opened this issue Mar 8, 2025 · 1 comment
Open

ShadowMaps for custom wgslFn shaders #30685

Spiri0 opened this issue Mar 8, 2025 · 1 comment
Assignees
Labels
TSL Three.js Shading Language

Comments

@Spiri0
Copy link
Contributor

Spiri0 commented Mar 8, 2025

Description

I looked through the examples for shadows and didn't find one where a shadow map with a custom shader is used. Since WebGPU allows you to create multiple geometries with compute shaders in the GPU and also their worldMatrices and store these directly in structArrays in storageBuffers, there is a need to be able to use shadow maps in your own shaders.

I tried to use sampler_comparison but I get an error message. I have a modified example from the examples in which you can see what the shadow texture looks like. If I integrate the sampler into the shader, the shader no longer works.

https://codepen.io/Spiri0/pen/xbxreyr

Insert line 129 under line 137

Solution

Make sampler_comparison available for wgslFn so that you can use textureSampleCompare for shadowMaps in own shaders

Alternatives

I haven't an idea of a good alternative, if there is one.
sampler_comparison only appears in one place in the WGSLNodeBuilder. So I hope that it won't be too much work. I'm not very familiar with this part of the code, otherwise I would try to solve it by myself

Additional context

No response

@Spiri0
Copy link
Contributor Author

Spiri0 commented Mar 11, 2025

I think I see the cause of the problem. But I'm not sure what the best solution would be to solve it without potentially causing cross-effects. The codeline: this.fragmentShader = this._getWGSLFragmentCode( shadersData.fragment ); in the WGSLNodeBuilder build this code.

// global
diagnostic( off, derivative_uniformity );


// structs

struct OutputStruct {
	@location(0) color: vec4<f32>
};
var<private> output : OutputStruct;

// uniforms
@binding( 1 ) @group( 1 ) var nodeUniform3_sampler : sampler_comparison;
@binding( 2 ) @group( 1 ) var nodeUniform3 : texture_depth_2d;
struct objectStruct {
	nodeUniform0 : vec4<f32>,
	nodeUniform1 : f32,
	nodeUniform2 : f32,
	nodeUniform4 : mat3x3<f32>
};
@binding( 0 ) @group( 1 )
var<uniform> object : objectStruct;

// codes
fn main_fragment ( 
        uv: vec2<f32>,
	width: f32,
	height: f32,
	shadowDepthTexture: texture_depth_2d,
	shadowDepthSampler: sampler_comparison ) -> vec4<f32> {
	
      	return vec4( 1, 0, 0, 1.0 );
}

fn sRGBTransferOETF ( color : vec3<f32> ) -> vec3<f32> {

	return mix( ( ( pow( color, vec3<f32>( 0.41666 ) ) * vec3<f32>( 1.055 ) ) - vec3<f32>( 0.055 ) ), ( color * vec3<f32>( 12.92 ) ), vec3<f32>( ( color <= vec3<f32>( 0.0031308 ) ) ) );

}

@fragment
fn main( @location( 3 ) nodeVarying3 : vec2<f32>,
	@builtin( position ) fragCoord : vec4<f32> ) -> OutputStruct {

	// vars
	
	var nodeVar0 : f32;
	var nodeVar1 : vec4<f32>;


	// flow
	// code

	nodeVar0 = textureSample( nodeUniform3, nodeUniform3_sampler, ( object.nodeUniform4 * vec3<f32>( nodeVarying3, 1.0 ) ).xy );
	nodeVar1 = main_fragment( ( ( fragCoord.xy - object.nodeUniform0.xy ) / object.nodeUniform0.zw ), object.nodeUniform1, object.nodeUniform2, nodeUniform3, nodeVar0 );

	// result

	output.color = vec4<f32>( sRGBTransferOETF( nodeVar1.xyz ), nodeVar1.w );

	return output;

}

The line near the end in @Fragment nodeVar0 = textureSample( nodeUniform3, nodeUniform3_sampler, ( object.nodeUniform4 * vec3<f32>( nodeVarying3, 1.0 ) ).xy ); leads to the issue. I wonder why the line is generated automatically. There must be a reason for this, so removing the automatic generation of this line could probably lead to other problems. @sunag I assume you know why the line is generated automatically. If one use sampler_comparison, would an if statement be useful to prevent the automatic generation of the textureSample?

@mrdoob mrdoob added the TSL Three.js Shading Language label Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TSL Three.js Shading Language
Projects
None yet
Development

No branches or pull requests

3 participants