首页 > 解决方案 > 金属着色器:将颜色编码为 4 字节 INTEGER 而不是 8 Bytes FLOAT

问题描述

我需要将在INTEGER(不是浮点数)中编码为 4 字节 RGBA 通道的颜色发送到我的金属着色器,但我不知道金属着色器是否可以处理存储在 INTEGER 中的颜色。实际上我在着色器中将它翻译成 float4 (甚至不知道使用 half4 代替是否更好)颜色,但我不知道这是否是一个好方法:

struct VertexIn {
  packed_float3 pos;
  packed_uchar4 color;
};

struct VertexOut {
  float4 pos [[position]];
  float4 color;
};

vertex VertexOut vertexShader(const device VertexIn *vertexArray [[buffer(0)]],
                              const unsigned int vid [[vertex_id]]){

  VertexIn in = vertexArray[vid];
  VertexOut out;
  out.color = float4(float(in.color[2])/255,float(in.color[1])/255,float(in.color[0])/255,float(in.color[3])/255);
  out.pos = float4(in.pos.x, in.pos.y, in.pos.z, 1);

  return out;

}


fragment float4 fragmentShader(VertexOut interpolated [[stage_in]]){
    return interpolated.color;
}

标签: iosobjective-cshadermetalmetalkit

解决方案


推荐阅读