You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在raylib中为绘制调用应用Shader:无需Texture2D的实现方案

在Raylib中为绘制调用应用Shader效果(无需转为Texture2D)

问题描述

我想在Raylib中为若干绘制调用应用Shader效果,但没找到不需要Texture2D的实现方式。请问能不能把下面的drawBoard函数重构为Texture2D形式?如果可行该怎么操作?

void drawBoard(Color lineColor, Color boardColor){
  ClearBackground(Color(boardColor));

  DrawRectangleV( (Vector2){294, 100}, (Vector2){7, 600}, Color(lineColor));
  DrawRectangleV( (Vector2){494, 100}, (Vector2){7, 600}, Color(lineColor));

  DrawRectangle(100, 294, 600, 7, Color(lineColor));
  DrawRectangle(100, 494, 600, 7, Color(lineColor));
}

解决方案

不需要将绘制内容转为Texture2D,直接使用Raylib内置的BeginShaderMode()和EndShaderMode()函数,就能为指定范围内的绘制调用应用Shader效果。

修改后的示例代码如下:

// 先加载你的Shader(替换为实际的 shader 文件路径)
Shader boardShader = LoadShader("vertex.glsl", "fragment.glsl");

void drawBoard(Color lineColor, Color boardColor){
  ClearBackground(boardColor);

  // 开启Shader模式,后续绘制会应用该Shader
  BeginShaderMode(boardShader);
  
  DrawRectangleV( (Vector2){294, 100}, (Vector2){7, 600}, lineColor);
  DrawRectangleV( (Vector2){494, 100}, (Vector2){7, 600}, lineColor);

  DrawRectangle(100, 294, 600, 7, lineColor);
  DrawRectangle(100, 494, 600, 7, lineColor);
  
  // 关闭Shader模式,恢复默认渲染状态
  EndShaderMode();
}

关键说明

  • BeginShaderMode(shader):启动指定Shader的作用域,之后所有的图形绘制都会应用该Shader的效果
  • EndShaderMode():结束Shader作用域,后续绘制回到Raylib的默认渲染管线
  • 这种方式无需额外渲染到Texture2D,流程更简洁,性能也更高效

内容的提问来源于stack exchange,提问作者Gabesz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 09:33:20