gpt4 book ai didi

macos - 将 Metal texture2d_array 传递给 SceneKit 着色器修改器

转载 作者:行者123 更新时间:2023-12-03 09:23:32 28 4
gpt4 key购买 nike

我想为 SCNMaterial(使用 SCNShaderModifierEntryPointSurface)创建着色器修改器,并将 Metal texture2d_array 作为自定义输入传递。为此,我尝试使用 SCNMaterialProperty 类型的键值编码设置参数。来自 SCNMaterialProperty 属性 contents 的 SceneKit 手册:

您可以使用以下任何类型为此属性设置一个值:

  • ...
  • 纹理(SKTexture、MDLTexture、MTLTexture 或 GLKTextureInfo)
  • ...

所以,我构建了MTLTexture:

  • 创建类型为MTLTextureType2DArrayMTLTextureDescriptor
  • 使用 MTLDevice 的 newTextureWithDescriptor 创建 MTLTexture:
  • 使用 [texture replaceRegion:mipmapLevel:withBytes:bytesPerRow:];
  • 用数据填充纹理

然后我创建 SCNMaterialProperty,将我创建的 MTLTexture 分配给它的 contents 并将其传递给 Material :

[material setValue:aTexture forKey:@"tex"];

然后我附上这个着色器修改器:

#pragma arguments
texture2d_array tex;

#pragma body
// do sampling from 1st slice
constexpr sampler smp(coord::normalized, address::repeat, filter::nearest);
_surface.diffuse = tex.sample(smp, _surface.diffuseTexcoord, 0);

然后我得到:

exception

回溯:

enter image description here

我还尝试使用 [material setValue:aTexture forKey:@"tex"]; 直接传递纹理,但随后出现错误:

/Library/Caches/com.apple.xbs/Sources/Metal/Metal-56.6/ToolsLayers/Debug/MTLDebugRenderCommandEncoder.mm:1600: 
failed assertion `textureType mismatch at texture binding at index 8 for tex[0].'

此外,我尝试在 SCNMaterialProperty 内容中传递 MTLTexture2D,但失败并出现 C3DImageGetImage 类型异常。直接通过 [material setValue:forKey] 传递 MTLTexture2D 会导致一切为白色 - 采样的纹理错误。

在分析“捕获 GPU 帧”输出时,我发现在绘制调用期间绑定(bind)了错误的纹理。

也许有人设法将 texture2d_array 传递给着色器修改器?有可能吗?我可以使用 Metal 命令缓冲区手动绑定(bind),但如何在每种 Material 的基础上访问它?

谢谢大家

最佳答案

        #include <metal_stdlib>
#include <metal_texture>
using namespace metal;
#pragma arguments
texture2d<float> mask;

#pragma body
constexpr sampler quadSampler(address::clamp_to_edge, filter::linear);

_surface.diffuse = mask.sample(quadSampler, _surface.diffuseTexcoord);

然后在swift方面

let mask = SCNMaterialProperty(contents: MDLTexture(named: "gameFrame.png")!)
doorFrame.geometry?.firstMaterial?.setValue(mask, forKey: "mask")

关于macos - 将 Metal texture2d_array 传递给 SceneKit 着色器修改器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37348476/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com