GMS2 绘图着色器仅适用于 DrawGUI

GMS2 Drawing Shaders Only Working in DrawGUI

所以我对着色器很陌生,我正在使用来自 GMS2 市场的免费着色器> 无论如何,我 运行 遇到了这个着色器的问题,我想我太新了着色器了解这一点。 着色器的代码是:

shd_mosaic.vsh:

attribute vec3 in_Position;
attribute vec4 in_Colour;
attribute vec2 in_TextureCoord;

varying vec2 v_texcoord;

void main()
{
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4(in_Position, 1.0);
    v_texcoord = in_TextureCoord;
}

shd_mosaic.fsh:

varying vec2 v_texcoord;

uniform float time;
uniform vec2 mouse_pos;
uniform vec2 resolution;
uniform float pixel_amount;

void main()
{ 
    vec2 res = vec2(1.0, resolution.x/resolution.y);
    vec2 size = vec2(res.x/pixel_amount, res.y/pixel_amount);
    vec2 uv = v_texcoord - mod(v_texcoord,size);
    gl_FragColor = texture2D( gm_BaseTexture, uv );
}

然后我还有管理着色器的对象,obj_controller_mosaic:

创建代码:

draw_set_color(c_white);

uni_time = shader_get_uniform(shd_mosaic,"time");
var_time_var = 0;

uni_mouse_pos = shader_get_uniform(shd_mosaic,"mouse_pos");
var_mouse_pos_x = mouse_x - camera_get_view_x(view_get_camera(view_current));
var_mouse_pos_y = mouse_y - camera_get_view_y(view_get_camera(view_current));

uni_resolution = shader_get_uniform(shd_mosaic,"resolution");
var_resolution_x = room_width //camera_get_view_width(view_get_camera(view_current));
var_resolution_y = room_height //camera_get_view_height(view_get_camera(view_current));

uni_pixel_amount = shader_get_uniform(shd_mosaic, "pixel_amount");
var_pixel_amount = 700.0;

shader_enabled = true;
full_screen_effect = true;

surf = surface_create(camera_get_view_width(view_get_camera(view_current)), camera_get_view_height(view_get_camera(view_current)));
view_set_surface_id(view_get_camera(view_current), surf);

DrawGUI代码:

var_time_var+=0.04;

var_resolution_x = camera_get_view_width(view_get_camera(view_current));
var_resolution_y = camera_get_view_height(view_get_camera(view_current)); 

if shader_enabled shader_set(shd_mosaic);
    shader_set_uniform_f(uni_time, var_time_var);
    shader_set_uniform_f(uni_mouse_pos, var_mouse_pos_x, var_mouse_pos_y);
    shader_set_uniform_f(uni_resolution, var_resolution_x, var_resolution_y);
    shader_set_uniform_f(uni_pixel_amount, var_pixel_amount);
    if full_screen_effect draw_surface(surf, 0, 0);
shader_reset();

好的,现在由于游戏中其他对象的一些问题,我希望 DrawGUI 代码实际上只是在 Draw 事件中。问题是,当我将这段代码复制到 Draw 事件上时,我只是得到一个黑屏(当代码在 DrawGUI 事件中时它工作正常)。我可以做些什么来使这段代码在 Draw 事件而不是 DrawGUI 中工作吗?预先感谢您的回答!

view_set_surface_id 使视图渲染到表面。因此,它不会自动绘制到屏幕上 - 您必须自己绘制,就像在 Draw GUI 中所做的那样。

如果您迫切需要在普通绘图事件中绘制视图,您将需要第二个视图并根据 view_current 在绘图事件中进行筛选。