import {ShaderNode, MeshBasicNodeMaterial, texture, color, vec2, vec3, vec4, wgslFn, tslFn, uv, attribute, uniform, storage, positionWorld, normalLocal, normalWorld } from './three-defs.js';
I’m trying to change vertex positions with wgslFn (WebGPU-shading-language-function). With tslFn I manage that but with wgsl I have a situation that I don’t understand yet. Here is my test code.
const textureLoader = new THREE.TextureLoader();
const vertexTexture = textureLoader.load( '../resources/textures/displacement.jpg' );
this.dt = 0.01;
const WGSLPosition = wgslFn(`
fn WGSLPosition(
vTex: texture_2d<f32>,
vTex_sampler: sampler,
uv: vec2<f32>,
time: f32,
position: vec3<f32>
) -> vec4<f32> {
return vec4<f32>( position * 1.5, 1.0 );
}
`);
this.wgslShaderParams = {
vTex: texture( vertexTexture ),
vTex_sampler: texture( vertexTexture ),
uv: uv(),
time: uniform(0),
position: attribute('position'),
}
const material = new MeshBasicNodeMaterial();
material.colorNode = WGSLPosition(this.wgslShaderParams);
//When I include material.positionNode I see nothing but the code is running
//material.positionNode = WGSLPosition(this.wgslShaderParams);
const geometry = new THREE.BoxGeometry();
this.cube = new THREE.Mesh(geometry, material);
this.scene.add(this.cube);
With material.positionNode commented out, I see this:
The camera is at (0, 0, 5) the cube has an edge length of 1. So I see exactly what I want to see. The wgslFn works and I see the vertex positions with the colorNode in color depending on their positions. So reading the attributes works.
If I now set the wgslFn to the positionNode, I expect to see the same thing with a cube that is 1.5 times larger. But as soon as I include the material.positionNode I see nothing but the code is running.
The example is very simple but I can’t figure out where my thinking is wrong.
Does anyone recognize my thinking error?