微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Swift Playgrounds中使用SCNTechnique和Metal Shaders

我想在SCNView上的 Swift Playgrounds中使用带有Metal着色器的SCNTechnique.因此,我尝试使用以下代码编译着色器:

guard let MetalDevice = sceneView.device else {
    return
}

if let path = Bundle.main.path(forResource: "distortTechnique",ofType: "Metal") {
    do {
        let contents = try String(contentsOf: URL(fileURLWithPath: path))
        do {
            try MetalDevice.makeLibrary(source: contents,options: nil)
        } catch { error
            print(error)
        }
    } catch {
        // contents Could not be loaded
    }
}

文件已加载但我收到以下编译器错误

Error Domain=MTLLibraryErrorDomain Code=3 "Compilation Failed: 

program_source:11:10: Fatal error: 'SceneKit/scn_Metal' file not found
#include <SceneKit/scn_Metal>
         ^
" UserInfo={NSLocalizedDescription=Compilation Failed: 

program_source:11:10: Fatal error: 'SceneKit/scn_Metal' file not found
#include <SceneKit/scn_Metal>
         ^
}

所以似乎无法找到»scn_Metal«库.
知道如何解决这个问题吗?
相同的设置在iOS App项目中运行得非常好.

这是着色器代码

#include <Metal_stdlib>
using namespace Metal;
#include <SceneKit/scn_Metal>

struct custom_vertex_t
{
    float4 position [[attribute(SCNVertexSemanticPosition)]];
};

constexpr sampler s = sampler(coord::normalized,address::repeat,filter::linear);

struct out_vertex_t
{
    float4 position [[position]];
    float2 uv;
    float time;
    float random;
};

vertex out_vertex_t pass_through_vertex(custom_vertex_t in [[stage_in]],constant SCNSceneBuffer& scn_frame [[buffer(0)]])
{
    out_vertex_t out;
    out.position = in.position;
    out.uv = float2((in.position.x + 1.0) * 0.5,(in.position.y + 1.0) * -0.5);
    out.time = scn_frame.time;

    return out;
};

fragment half4 pass_through_fragment(out_vertex_t vert [[stage_in]],texture2d<float,access::sample> colorSampler [[texture(0)]])
{

    float4 fragment_color = colorSampler.sample( s,vert.uv);
    return half4(fragment_color);
};

fragment half4 distort_fragment(out_vertex_t vert [[stage_in]],access::sample> colorSampler [[texture(0)]])
{
    float multiplier = sin(vert.time * 0.5);
    float effecTradius = 0.75;
    float effectAmount = 360. * 2.;
    float effectAngle = multiplier * effectAmount;
    effectAngle = effectAngle * M_PI_F / 180;

    float2 resolution = float2(colorSampler.get_width(),colorSampler.get_height());
    float2 center = float2(0.5,0.5);//iMouse.xy / iResolution.xy;

    float2 uv = vert.position.xy / resolution - center;
    float len = length(uv * float2(resolution.x / resolution.y,1.));
    float angle = atan2(uv.y,uv.x) + effectAngle * smoothstep(effecTradius,0.,len);
    float radius = length(uv);

    float4 fragment_color = colorSampler.sample(s,float2(radius * cos(angle),radius * sin(angle)) + center);

    return half4(fragment_color);

};

附件是操场的图像.

谢谢!

enter image description here

解决方法

我最终通过预编译实际应用程序项目的金属库来使用它,然后将结果default.Metallib与操作中包含的着色器函数一起使用.

这似乎是必要的,因为顶点和碎片的SCNTechnique文档,这些函数必须存在于应用程序的认金属库中.«

有点hacky但是为我的目的工作.

具体步骤:
– 为选定平台创建新的应用项目
– 使用所有定义设置SCNTechnique plist
– 使用着色器代码添加.Metal文件
– 实现项目
– 打开archieved包并将default.Metallib文件复制到您的游乐场资源文件夹中
– 现在只需在SCNView上设置您的技术
– 利润.

再次感谢Oliver的帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐