小编Che*_*itz的帖子

如何在金属片段着色器中动态获取渲染目标尺寸?

该金属着色器基于此处的教程

http://metalkit.org/2016/10/01/using-metalkit-part-2-3-2.html

它绘制了页面上第三张图像中看到的黄色和蓝色渐变。 在此输入图像描述

我使用此着色器的目标是使用片段/顶点对而不是计算着色器来绘制它。

该着色器的结果通过 MacOS Swift Playground 中的 MTKView 的子类可见。

着色器代码:

#include <metal_stdlib>
using namespace metal;

struct Vertex {
    float4 position [[position]];
    float4 color;
};

vertex Vertex vertex_func(constant Vertex *vertices [[buffer(0)]],
                      uint vid [[vertex_id]]) {
    Vertex in = vertices[vid];
    Vertex out;
    out.position = float4(in.position);
    out.color = in.color;
    return out;
}

fragment float4 fragment_func(Vertex vert [[stage_in]],
                          constant float &timer [[buffer(0)]]) {

    float4 fragColor;

    int width = 400;
    int height = 400;

    float2 resolution = float2(width,height);

    float2 uv = vert.position.xy * …
Run Code Online (Sandbox Code Playgroud)

metal

6
推荐指数
1
解决办法
2879
查看次数

为什么金属着色器渐变作为应用于SceneKit节点的SCNProgram比作为MTKView更轻?

我有一个渐变,由我应用于由平面几何定义的SCNNode的Metal片段着色器生成.

它看起来像这样:

scenekit梯度平面节点

当我使用相同的着色器应用于在Xcode游乐场中渲染的MTKView时,颜色会更暗.是什么导致Scenekit版本中的颜色变浅?

在此输入图像描述

这是Metal shader和GameViewController.

着色器:

#include <metal_stdlib>
using namespace metal;
#include <SceneKit/scn_metal>

struct myPlaneNodeBuffer {
    float4x4 modelTransform;
    float4x4 modelViewTransform;
    float4x4 normalTransform;
    float4x4 modelViewProjectionTransform;
    float2x3 boundingBox;
};

typedef struct {
    float3 position [[ attribute(SCNVertexSemanticPosition) ]];
    float2 texCoords [[ attribute(SCNVertexSemanticTexcoord0) ]];
} VertexInput;

struct SimpleVertexWithUV
{
    float4 position [[position]];
    float2 uv;
};


vertex SimpleVertexWithUV gradientVertex(VertexInput in [[ stage_in ]],
                         constant SCNSceneBuffer& scn_frame [[buffer(0)]],
                         constant myPlaneNodeBuffer& scn_node [[buffer(1)]])
{
    SimpleVertexWithUV vert;

    vert.position = scn_node.modelViewProjectionTransform * float4(in.position, 1.0);

    int width = abs(scn_node.boundingBox[0].x) + …
Run Code Online (Sandbox Code Playgroud)

scenekit metal

5
推荐指数
1
解决办法
769
查看次数

标签 统计

metal ×2

scenekit ×1