大家好,

我是游戏开发的新手(使用GameMaker不到一周),一直在制作一个简单的平台跳跃游戏。我想在游戏中加入对话功能,于是创建了一个名为“dummy”的NPC,当玩家距离它小于32像素并按下F键时,会触发对话。

除了添加对话外,我还想给NPC添加一个着色器,为其生成动态轮廓,当玩家进入交互距离时轮廓颜色会变化,从黑色轮廓变为白色。我参考了多年前Sara Spaulding的轮廓着色器教程,奇迹般地完全正常工作了。但现在我需要给着色器添加颜色变化的功能。我尝试自己实现但搞糊涂了,于是去别处寻求帮助,但找不到相关主题的资料,至少找不到能与我的代码兼容的内容。

以下是代码:

sh_outline 片段着色器

// 简单的直通片段着色器
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform float pixelH;
uniform float pixelW;

void main()
{
// 计算一个像素的大小
vec2 offsetx;
offsetx.x = pixelW;
vec2 offsety;
offsety.y = pixelH;

vec4 texColour = texture2D(gm_BaseTexture, v_vTexcoord);

// 计算像素在精灵上的位置
float alpha = texColour.a;
float original_alpha = alpha;

alpha += ceil(texture2D(gm_BaseTexture, v_vTexcoord + offsetx)).a;
alpha += ceil(texture2D(gm_BaseTexture, v_vTexcoord - offsetx)).a;
alpha += ceil(texture2D(gm_BaseTexture, v_vTexcoord + offsety)).a;
alpha += ceil(texture2D(gm_BaseTexture, v_vTexcoord - offsety)).a;

if (alpha > 1.0) alpha = 1.0; // 固定alpha
if (original_alpha != alpha) {
gl_FragColor = vec4(0.1529,0.1529,0.1529,1.0);
}
else {
gl_FragColor = texColour;
}
gl_FragColor.a = alpha;
}

obj_dummy 创建事件

//轮廓着色器
uPixelH = shader_get_uniform(sh_outline,"pixelH");
uPixelW = shader_get_uniform(sh_outline,"pixelW");
texelW = texture_get_texel_width(sprite_get_texture(sprite_index,0));
texelH = texture_get_texel_height(sprite_get_texture(sprite_index,0));

//默认对话
text = [
"默认文本",
]

obj_dummy 绘制事件

//轮廓着色器
shader_set(sh_outline);
shader_set_uniform_f(uPixelW,texelW);
shader_set_uniform_f(uPixelH,texelH);
draw_self();
shader_reset();

obj_player 步进事件

//对话启动
if (keyboard_check_pressed(ord("F"))) {
if (currently_talking == noone){
var check_x = x + (32 * moveDir);
var who_is_here = instance_place(check_x,y,obj_dummy);
if who_is_here != noone {
audio_play_sound(sfx_interact,1,false);
current_text = (who_is_here.text[0]);
currently_talking = who_is_here;
current_text_index = 0;
current_text_line_number = 0;
instance_create_depth(x,y,1,obj_pauser);
}
} else {
if (current_text_line_number < array_length(currently_talking.text) - 1) {
audio_play_sound(sfx_interact,1,false);
current_text_line_number++;
current_text = currently_talking.text[current_text_line_number];
current_text_index = 0;
} else {
currently_talking = noone;
instance_destroy(obj_pauser); 
}
}
}

基本上,代码检测F键是否被按下,然后检测玩家在按键时是否靠近NPC,如果是则启动对话。我在想应该把按键检测和who_is_here的if语句调换一下,让who_is_here持续被检测,当玩家检测到前方有NPC时,向dummy发送一条消息,告诉着色器将轮廓颜色改为白色。只是这一步我卡住了。

非常感谢任何帮助。绝对不想用AI来获取编程建议。

谢谢!