swift - 将 OpenGL ES 2.0 着色器代码转换为 Metal(用于 ARKit 场景)
问题描述
过去,我们使用 Scenekit 类中使用的 OpenGL ES 2.0 着色器语言编写代码。但是 OpenGL 不再可用于 ARkit,它已被弃用。因此,我们必须为 Metal 重写这些着色器代码,这些代码是为 OpenGL ES 2.0 编写的。
但是金属库对我来说很新,我很难理解它。
下面是我们用 OpenGL ES 2.0 语言编写的简单着色器代码之一。如果你帮我翻译这个,它基本上可以帮助我理解它的逻辑,这样我就可以翻译其他的了。
在此先感谢您的帮助。
precision highp float;
varying vec2 textureCoordinate;
varying vec2 textureCoordinate2;
uniform sampler2D inputImageTexture;
uniform sampler2D inputImageTexture2;
void main() {
vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
vec4 base = texture2D(inputImageTexture2, textureCoordinate2);
vec2 colorCoords = vec2((1.0 + textureCoordinate.x) * 0.5,textureCoordinate.y);
vec2 alphaCoords = vec2(textureCoordinate.x * 0.5,textureCoordinate.y);
vec4 color = texture2D(inputImageTexture, colorCoords);
float alpha = texture2D(inputImageTexture, alphaCoords).r;
vec4 overlay = vec4(color.rgb, alpha);
vec4 c2 = base;
vec4 c1 = overlay;
vec4 outputColor;
// outputColor.r = c1.r + c2.r * c2.a * (1.0 - c1.a);
// outputColor.g = c1.g + c2.g * c2.a * (1.0 - c1.a);
// outputColor.b = c1.b + c2.b * c2.a * (1.0 - c1.a);
// outputColor.a = c1.a + c2.a * (1.0 - c1.a);
float a = c1.a + c2.a * (1.0 - c1.a);
float alphaDivisor = a + step(a, 0.0); // Protect against a divide-by-zero blacking out things in the output
outputColor.r = (c1.r * c1.a + c2.r * c2.a * (1.0 - c1.a))/alphaDivisor;
outputColor.g = (c1.g * c1.a + c2.g * c2.a * (1.0 - c1.a))/alphaDivisor;
outputColor.b = (c1.b * c1.a + c2.b * c2.a * (1.0 - c1.a))/alphaDivisor;
outputColor.a = a;
gl_FragColor = outputColor;
}
解决方案
推荐阅读
- python - selenium WebDriverException:消息:“geckodriver”可执行文件需要在 PATH 和
- hyperledger-fabric - 运行 byfn 时出错:无法支持链代码安装,rpc 错误代码不可用
- javascript - 是否可以在 JS/TS 中解构类的公共属性?
- python - Matplotlib 散点图,具有从红色到蓝色的不同颜色
- android - 将 Android 服务作为后台和前台服务
- php - 仅针对某些列进行查询时防止涉及 Laravel 访问器
- python-3.x - 如何检查列表中是否存在日期以更改列的值?
- javascript - 如何使可重用导航栏的文本内容包含在所有页面中以反映
- python - 无法在 VScode 中选择 python 解释器
- angular - [parameter]="value" 和 parameter="value" 之间的角度差异