c# - 如何从 Unity 3d Shaderlab 中的像素着色器返回纹理?

标签 c# unity-game-engine shader pixel-shader

如何创建一个简单的像素颜色着色器,它采用纹理,应用诸如 mask 之类的东西:

half4 color = tex2D(_Texture0, i.uv.xy);
if(distance(color, mask) > _CutOff)
{
    return color;
}
else
{
return static_color;
}

并返回一个纹理,该纹理可以从 C# 代码传递到下一个着色器,其方式类似于 mats[1].SetTexture("_MainTex", mats[0].GetTexture("_MainTex"));

最佳答案

But... you might not want to do a shader to only modify a texture.

为什么不呢?这是一种常见的做法。

查看Graphics.Blit。它基本上绘制了一个应用了 Material (包括着色器)的四边形。因此,您可以使用着色器来修改纹理。但纹理必须是RenderTexture。

它会是这样的:

var mat = new Material(Shader.Find("My Shader"));
var output = new RenderTexture(...);
Graphics.Blit(sourceTexture, output, mat);

在这种情况下,sourceTexture 将绑定(bind)到My Shader 的 _MainTex。

关于c# - 如何从 Unity 3d Shaderlab 中的像素着色器返回纹理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13147652/

相关文章:

C++ 库中的 C# 枚举

c# - 如何通过 Web API 向所有客户端广播消息

c# - unity IOException : Sharing violation on path for output_log. txt,如何读取正在使用的文件?

ios - Unity3D中如何组织资源的加载和卸载?

OpenGL 2.0 与 GL_ARB_vertex_program

c# - Automapper - 有条件地添加到列表

c# - 确定哪个文本框触发了文本更改事件

unity-game-engine - 当 Unity 没有输入焦点时捕获 Unity 中的按键

three.js - 使用 Three.js 在 3D 模型之间转换顶点

ios - 增加 SKShader 中的统一值