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
TSL: Transitioning default GLSL Matrices to TSL #28070
Comments
I found a manual way to do this, but it's pretty complicated and not practical for the average user. The problem is the modelMatrix because it is usually different for one material but different meshes. Threejs seems to create its own material instance internally in webgl for each mesh in order to be able to use the individual modelMatrix. |
@sunag, is there anything I can do to help here? |
In case of using an object with default parameters, could it help? import { modelViewMatrix, cameraProjectionMatrix, ... }
const defaultParameters = { modelViewMatrix, projectionMatrix: cameraProjectionMatrix, ... };
material.vertexNode = someNodeFunction( { otherParameter, ... defaultParameters } ); |
I tested your suggestion, but I had to create a new export in the ModelNode with modelMatrix analogous to modelViewMatrix because there was no modelMatrix node. I've already thought about how I can make the simplest example possible, because the code that I want to port from webgl to webgpu is very large. The nice thing is that up to this point everything works exactly the same with webgpu. From this point onwards the differentiation occurs. I have the two test shaders here as an illustration. With exactly the glsl code the representation is correct. With the wgsl code below in the webgpu variant, the code runs but unfortunately the transformations are not correct.
I tried to trace the path of the matrices through the material inheritance to the RawShader in three.module.js. My thought was to try to reproduce exactly that for tsl and wgsl shader, because the necessary information is somewhere in the MeshBasicNodeMaterial as well as in the RawShaderMaterial. The MeshBasicNodeMaterial also uses the individual modelMatrices for all objects because otherwise the individual meshes would not be initialized correctly. But fortunately that is the case. And I find that all very reassuring. But without access to the individual modelMatrices in the shader I cannot implement the transformation. This is part of a floating point origin transform that I use to set the camera as the origin. |
This should happen, otherwise the examples wouldn't work correctly. I believe it is related to the nodes used as input. A basic MVP should be: import { cameraProjectionMatrix, modelViewMatrix, positionLocal } from 'three/nodes'
material.vertexNode = cameraProjectionMatrix.mul( modelViewMatrix ).mul( positionLocal ); |
Ah, I think I understand. I also passed the modelViewMatrix to the shader and then did the usual "cameraProjectionMatrix * modelViewMatrix * vec4f(position, 1.0)" in the shader. Then my naive thought to insert this here in ModelNodes.js and adding modelMatrix on line 93 in Nodes.js was not that bad at all.
With the viewMatrix from the camera, I get the same result. So now I know a lot more thanks to your explanation and a lot of it fits together. But why the transformation still doesn't work in contrast to my analog code with my RawShader is still a mystery to me at the moment. I must have overlooked something with the modelMatrix, because the modelMatrixes are not updated correctly. If instead of my self made modelMatrix node I simply use a mesh.matrixWorld from one of the meshes and thus update the uniform at every interval then my origin transformation works wonderfully. All meshes move smoothly. The transformation is correct, but of course the mesh positions are not correct because I use the same mesh matrix for each mesh. I now suspect that my simple attempt to add a modelMatrix node is incomplete. |
For world matrix the node |
Yes, that works 😄 The fact that with the modelWorldMatrix looks exactly right is due to my coordinate transformation mechanism. It would therefore make sense to include the modelMatrix in ModelNode.js. I can test that then. A viewMatrix node would round off the range. Personally, I'm fine with simply accessing the camera's matrixWorldInverse, but with a viewMatrix node you would have pretty much all the matrices together. And with it all possibilities |
The viewMatrix is called |
I'll rename the issue to "Add modelMatrix to ModelNode.js" Your answer in the forum to my post-processing question helped me a lot. I noticed something interesting in your example that you recommended to me that could perhaps elegantly close an older issue, but I still have to take a closer look at it. |
Nice! Why |
Yes, local matrix, that's the only one missing from the ModelNode.js range. But since only a few users work with matrices, this is nothing urgent |
I think |
Comparisons between GLSL and TSL default properties.
@Spiri0 I'm going to close the topic as resolved and rename the title, if you think it's necessary you can open another one about |
I was just thinking about one of my previous apps where I often use the local matrix, but that was always only on the javascript side in the 3js code and never in the shader. |
Description
In webGL you only need to declare the standard matrices in a RawShader, but you don't have to initialize or update them by yourself using the uniforms. Threejs does this automatically. I have two simple shaders here as an example in webGL and webGPU. The four standard matrices can be seen in both.
For the wgslFn and tslFn you have to initialize and update these four matrices yourself. It would be nice if this would also happen automatically like in a RawShader, because the effort can be considerable if you have to do it yourself with a more complex app
Solution
The solution is to adopt the functionality of threejs for the rawShaders from webGL into the wgslFn and tslFn.
Alternatives
At the moment this can be done through manual initialization and updates via the material parameters of a MeshBasicNodeMaterial.
Additional context
No response
The text was updated successfully, but these errors were encountered: