首页 > 解决方案 > 将 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;
}

标签: swiftopengl-esarkitmetalfragment-shader

解决方案


推荐阅读