Who knows what means unity shader buildin variable " unity_CameraToWorld" , how convert to three.js uniform…
I asked Chat GPT. Here’s its answer. Hope this helps
The unity_CameraToWorld
variable in Unity shaders. This variable represents the 4x4 world-to-camera matrix, which transforms points from world space to camera space. It can be used in shaders to perform various operations related to camera-space calculations.
In Three.js shaders, there isn’t a direct equivalent to Unity’s unity_CameraToWorld
variable because Three.js shaders are typically written in GLSL (OpenGL Shading Language), and the variable names and conventions are different.
In Three.js, you can use the modelViewMatrix
and projectionMatrix
uniforms provided by the built-in shader material to perform transformations. These matrices are often used to transform vertices from object space to camera space and then to screen space.
Here’s a simplified example of how you might use these matrices in a Three.js vertex shader:
uniform mat4 modelViewMatrix; // Model-View matrix
uniform mat4 projectionMatrix; // Projection matrix
attribute vec3 position;
void main() {
// Transform vertex to camera space
vec4 cameraSpacePosition = modelViewMatrix * vec4(position, 1.0);
// Transform vertex to clip space
gl_Position = projectionMatrix * cameraSpacePosition;
}
In this example, modelViewMatrix
represents the transformation from object space to camera space, and projectionMatrix
represents the projection transformation. You can then use these matrices to perform the necessary transformations in your shaders.
Since it’s ChatGPT, a small note - keep in mind you shouldn’t define those yourself. These (and some other) uniforms are defined automatically in shaders by three (except in RawShaderMaterial) - docs.
Since it’s ChatGPT, a small note
More than that - the ChatGPT result doesn’t give a straight answer and the one that can be inferred from it is wrong. Unity’s CameraToWorld matrix does not include the projection matrix transformation. I highly encourage people to not post generative AI answers without at least notionally validating them. Ultimately I don’t think pasting ChatGPT answers has a place in a forum where people come to ask other people questions.
That said there’s some nuance to the answer. A more comprehensive write up on the unity_CameraToWorld matrix is available here. The bottom line is that without some more context and information about what you’re trying to transform it can be hard to provide an exact answer on what you need. But from the link above the closest thing to Unity’s unity_WorldToCamera
is three.js’ viewMatrix
(. And given that unity_CameraToWorld
is the inverse you can compute that in a three.js shader using inverse( viewMatix )
or you could pass the inverse matrix yourself. Do note that the inverse
function is only available in WebGL2 if you for some reason are still using WebGL 1.
But again I think a more complete example of what’s being implemented would be best for understanding what’s needed. Porting a shader from what engine to another can be complicated and doing it correctly requires knowledge of a lot of engine state / context.
Thank you sir, I find in three.js “viewMatrix” is camera’s matrixWorldInverse, inverse again, so the “unity_CameraToWorld” is the camera’s matrixWorld?I have try this matrix many times, but it not give me the correct result。。。 I am transplanting Unity’s atmospheric scattering demo into the three. js project and have found many issues…
I think I have converted 85% of the code, but there are still some results that are not ideal. Overall, they are running. If you are interested in further modifications, I can send you the code for Unity and Three,I think atmospheric scattering is very useful for improving the rendering quality of three. js。
this does look pretty cool! If you’re able to post the source code on GitHub or similar and share it here, I or others on this forum may be interested to take a look and help out
On the subject of chatGPT:
Here is a screenshot of a discussion I had with chatGPT. The discussion was in German, so I’m translating what you see here.
Vector a: (1, 0.3, 0)
Vector b: (0, 0.2, 1)
I asked chatGPT about the cross product. I don’t need chatGPT for something so simple, but my expectation of an AI is that it can calculate something as banal as a cross product without any problems.
I had already pointed out to chatGPT that it had made mistakes and chatGPT apologized but then repeated the error and made it even worse wrong
Look at the y component:
-ax*bz = 1 but chatGPT allways make -1 * 0 = 0
After I repeatedly pointing out to chatGPT that it is calculating incorrectly, it not only repeats the error for the y component but also suddenly turns the 1 * 0.2 into - 0.2 in the z component
Then I got tired of it because chatGPT just didn’t want to understand it. It takes me about 30 seconds to calculate that by myself. ChatGPT is perhaps suitable for literary topics that exist millions of times on the web where it’s not about precision but about art and philosophy, but when it comes to mathematical issues or modern technologies then chatGPT quickly becomes overwhelmed and says crap.
I would be happy if chatGPT comments were at least checked for accuracy by the poster before they were posted. Anyone can ask chatGPT questions, but misleading chatGPT answers do not help, they only confuse those seeking help and cause damage because they lead people down the wrong path from the start.
I mention this here because you had already posted a general chatGPT answer that didn’t help the person searching at all, because chatGPT has absolutely no idea about the three.js node system and doesn’t even begin to know how to convert a glsl shader into a wgsl code for the node system. In the three.js node system, the node system takes over the laborious handling of all the bindings and locations for wgsl, which is a huge relief, but chat GPT knows nothing about all of this.
I don’t want to curb your enthusiasm for chatGPT, but please check before you post anything from chatGPT whether it is actually correct and works.
Thank you sir, I find in three.js “viewMatrix” is camera’s matrixWorldInverse, inverse again, so the “unity_CameraToWorld” is the camera’s matrixWorld?I have try this matrix many times, but it not give me the correct result。。。 I am transplanting Unity’s atmospheric scattering demo into the three. js project and have found many issues…
Yes this is correct. You can see that the viewMatrix uniform is set to the camera’s matrix world inverse here. However keep in mind that it’s still important to know what frame the data you’re working with is in. Unity’s camera forward vector is +Z direction while Three.js’ is -Z. I wouldn’t expect it to be possible to just directly replace these matrices with each other depending on the prior calculations.
I think I have converted 85% of the code, but there are still some results that are not ideal. Overall, they are running. If you are interested in further modifications, I can send you the code for Unity and Three,I think atmospheric scattering is very useful for improving the rendering quality of three. js。
Can you share them in this forum so others can take a look?
You get what you pay for. The free 3.5 version is not worth using for anything technical including programming. Version 4 Turbo is much better.
Given that the original question was bridging two domains, I was curious.
I will only post when I know the answer is pretty close or explains it better than I could.
I think there’s value is improving this evolving technology. We learn through mistakes. It has been tremendously helpful in both my personal and work programming projects, so I see its value.
I agree, it can confidently mislead you or go back to repeating earlier mistake. If you don’t give it feedback, it will never learn to correct them.
We’ve all adopted technology when it was immature and persevered. Give it a chance.
In the spirit of the original ChatGPT answer,
“I don’t know, but I can at least give a different perspective”
Here is a less ambitious approach to the original question.
It uses a HDR and a slightly yellow Fog
link : SBCODE Editor
Sorry, I don’t have a Minecraft landscape to demo with.
sir, this is unity demo, GitHub - AKGWSB/RealTimeAtmosphere: real-time atmosphere rendering in Unity URP, if you can solve some unity shader convert problem, i think it easy to convert into three.js
sir, this is unity demo, GitHub - AKGWSB/RealTimeAtmosphere: real-time atmosphere rendering in Unity URP, if you can solve some unity shader convert problem, i think it easy to convert into three.js…
I don’t have the bandwidth to convert the shader for you or dig through a large project. If you’d like to show the shader code you’re working on and the shader file you’re trying to convert from and where you’re having an issue I may be able to take a look.
ok, thank you