我是新来的金属(和一般的着色器)。我一直在尝试创建一个ARSCNView应用程序,使用MSL规范和SCNTechnique文档应用着色器。
我想要一个着色管道,这将允许我改变相机馈送视图(sceneView.scene.background),但允许所有的SCNNodes与未改变的相机饲料,他们重叠。我的计划是:
我的计划
1)第一次将我的第一个样式化着色器应用于场景(它只是调整了颜色/luma,我用DRAW_QUAD测试了它,它工作得很好)到DRAW_SCENE,并使用excludeCategoryMask排除所有节点。
着色器应该包含某种统一的数据结构,它包含场景中的所有其他节点(目前是空的,因为节点尚未呈现),并使用它们的屏幕空间坐标在节点重叠的颜色过滤器中裁剪适当大小/位置的形状。
2)第二次传递呈现我的其余节点(球体等)使用DRAW_NODE使用标准/默认SCNKit着色器。
问题
我不知道这是否是一种有效的方法,因为我的应用程序几乎立即与com.apple.scenekit.scnview-renderer (15): EXC_BAD_ACCESS (code=1, address=0xeb52be860)崩溃。当我从DRAW_QUAD切换到DRAW_SCENE时,着色器会崩溃。我想我漏掉了深度缓冲什么的。
#include <metal_stdlib>
using namespace metal;
#include <SceneKit/scn_metal>
struct VertexInput_0 {
float4 position [[ attribute(SCNVertexSemanticPosition) ]];
float2 texcoord [[ attribute(SCNVertexSemanticTexcoord0) ]];
};
struct VertexOutput_0 {
float4 position [[position]];
float2 texcoord;
};
// metalVertexShader
vertex VertexOutput_0 scene_filter_vertex_0(VertexInput_0 in [[stage_in]])
{
VertexOutput_0 out;
out.position = in.position;
out.texcoord = float2((in.position.x + 1.0) * 0.5 , (in.position.y + 1.0) * -0.5);
return out;
}
// metalFragmentShader
fragment half4 scene_filter_fragment_0(VertexOutput_0 vert [[stage_in]],
texture2d<half, access::sample> scene [[texture(0)]])
{
constexpr sampler samp = sampler(coord::normalized, address::repeat, filter::nearest);
half4 color = scene.sample(samp, vert.texcoord);
constexpr half3 weights = half3(2, 0.7152, 0.0722);
color.rgb = half3(dot(color.rgb, weights));
return color;
}
// Doesn't reach here, crashes before second pass below
struct VertexInput_1 {
float4 position [[ attribute(SCNVertexSemanticPosition) ]];
float2 texcoord [[ attribute(SCNVertexSemanticTexcoord0) ]];
};
struct VertexOutput_1 {
float4 position [[position]];
float2 texcoord;
};
// metalVertexShader
vertex VertexOutput_1 scene_filter_vertex_1(VertexInput_1 in [[stage_in]])
{
VertexOutput_1 out;
out.position = in.position;
out.texcoord = float2((in.position.x + 1.0) * 0.5 , (in.position.y + 1.0) * -0.5);
return out;
}
// metalFragmentShader
fragment half4 scene_filter_fragment_1(VertexOutput_1 vert [[stage_in]],
texture2d<half, access::sample> scene [[texture(0)]])
{
constexpr sampler samp = sampler(coord::normalized, address::repeat, filter::nearest);
half4 color = scene.sample(samp, vert.texcoord);
return color;
}shaders.metal
<plist version="1.0">
<dict>
<key>sequence</key>
<array>
<string>filter_0</string>
<string>filter_1</string>
</array>
<key>passes</key>
<dict>
<key>filter_1</key>
<dict>
<key>outputs</key>
<dict>
<key>color</key>
<string>COLOR</string>
</dict>
<key>inputs</key>
<dict>
<key>scene</key>
<string>COLOR</string>
</dict>
<key>draw</key>
<string>DRAW_QUAD</string>
<key>metalVertexShader</key>
<string>scene_filter_vertex_2</string>
<key>metalFragmentShader</key>
<string>scene_filter_fragment_2</string>
</dict>
<key>filter_0</key>
<dict>
<key>metalFragmentShader</key>
<string>scene_filter_fragment_0</string>
<key>metalVertexShader</key>
<string>scene_filter_vertex_0</string>
<key>excludeCategoryMask</key>
<integer>2</integer>
<key>outputs</key>
<dict>
<key>color</key>
<string>COLOR</string>
</dict>
<key>inputs</key>
<dict>
<key>scene</key>
<string>COLOR</string>
</dict>
<key>draw</key>
<string>DRAW_SCENE</string>
</dict>
</dict>
</dict>
</plist>为什么这会崩溃?我是不是遗漏了一个参数?
发布于 2020-04-06 10:43:49
可能是scene_filter_vertex_2和scene_filter_fragment_2需要重命名为scene_filter_vertex_1和scene_filter_fragment_1
https://stackoverflow.com/questions/57412659
复制相似问题