问题描述
shader_type spatial;
void fragment ()
{
vec2 i_resolution = 1.0 / SCREEN_PIXEL_SIZE ;
...
//fragColor = ...;
COLOR = ...; 'Constants cannot be modified' And this is other problem on spatial shader
}
SCREEN_PIXEL_SIZE在空间着色器上不起作用?如何获得分辨率?
SCREEN_PIXEL_SIZE don't work on spatial shader? how to get the resolution?
推荐答案
您已经注意到, SCREEN_PIXEL_SIZE
在Spatial Shader中不存在(在Canvas Shaders中存在).
As you have noticed SCREEN_PIXEL_SIZE
does not exist in Spatial Shader (it exists in Canvas Shaders).
空间着色器的 SCREEN_PIXEL_SIZE
等效于 1.0/VIEWPORT_SIZE
.也就是说, VIEWPORT_SIZE = 1.0/SCREEN_PIXEL_SIZE
.
The equivalent of SCREEN_PIXEL_SIZE
for Spatial Shaders is 1.0/VIEWPORT_SIZE
. That is, VIEWPORT_SIZE = 1.0/SCREEN_PIXEL_SIZE
.
由于您要使用 1.0/SCREEN_PIXEL_SIZE
,因此可以直接使用 VIEWPORT_SIZE
.它为您提供视口的像素大小(在其中绘制着色器,无论是屏幕,窗口还是纹理).
Since 1.0/SCREEN_PIXEL_SIZE
is what you want, you can use VIEWPORT_SIZE
directly. It gives you size in pixels of the viewport (where the shader is being drawn, be it the screen, a window, or a texture).
此外,如果要执行 FRAGCOORD.xy/VIEWPORT_SIZE
,则可以改用 SCREEN_UV
.那就是 SCREEN_UV = FRAGCOORD.xy/VIEWPORT_SIZE
.如您所料, FRAGCOORD
是片段坐标(以像素为单位),而 SCREEN_UV
为您提供归一化的坐标(0到1).
Furthermore, if you are going to do FRAGCOORD.xy/VIEWPORT_SIZE
, you can use SCREEN_UV
instead. That is SCREEN_UV = FRAGCOORD.xy/VIEWPORT_SIZE
. As you would expect, FRAGCOORD
is the fragment coordinates in pixels, while SCREEN_UV
gives you normalized coordinates (0 to 1).
顺便说一句,如果您不希望材质取决于屏幕上的位置,则可以将 SCREEN_UV
换成 UV
.
是的,我知道 SCREEN_UV
说"screen"而不是视口".某些命名可能会造成混淆.
By the way yes, I know SCREEN_UV
says "screen" and not "viewport". Some naming can be confusing.
在空间着色器中,您不会写入 COLOR
,而是写入 ALBEDO
和 ALPHA
.Godot会通知您是否写入 ALPHA
,并在确定渲染顺序时将其考虑在内(因此,透明材料可能会在不透明材料之后进行渲染).
In your spatial shader you don't write to COLOR
, you write ALBEDO
and ALPHA
instead. Godot will notice if you write to ALPHA
or not, and take that into account for deciding render order (so potentially transparent materials are rendered after opaque ones).
谈到令人困惑的命名...
mat4 ModelToWorld = WORLD_MATRIX; // Common name: Model Matrix
mat4 WorldToModel = inverse(WORLD_MATRIX); // Common name: Inverse Model Matrix
mat4 WorldToCamera = INV_CAMERA_MATRIX; // Common name: View Matrix
mat4 CameraToWorld = CAMERA_MATRIX; // Common name: Inverse View Matrix
mat4 ModelToCamera = MODELVIEW_MATRIX; // Common name: View Model Matrix
mat4 CameraToModel = inverse(MODELVIEW_MATRIX); // Common name: Inverse View Model Matrix
mat4 CameraToClip = PROJECTION_MATRIX // Common name: Projection Matrix
mat4 ClipToCamera = INV_PROJECTION_MATRIX // Common name: Inverse Projection Matrix
请参考空间着色器,内置(包括上面提到的)和渲染模式的列表.
Please refer to Spatial Shaders, for the list of built-ins (including those mentioned above) and render modes.
这篇关于如何在godot空间着色器上获得分辨率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!