问题描述
我正在尝试将 GLKVector4
传递给应将其作为 vec4
接收的着色器.我正在使用片段着色器修饰符:
I'm trying to pass a GLKVector4
to a shader that should receive it as a vec4
. I'm using a fragment shader modifier:
material.shaderModifiers = [ SCNShaderModifierEntryPoint.fragment: shaderModifier ]
其中 shaderModifier
是:
// color changes
uniform float colorModifier;
uniform vec4 colorOffset;
vec4 color = _output.color;
color = color + colorOffset;
color = color + vec4(0.0, colorModifier, 0.0, 0.0);
_output.color = color;
(我只是添加了一个颜色偏移)我试过了:
(I'm simply adding a color offset) I've tried:
material.setValue(GLKVector4(v: (250.0, 0.0, 0.0, 0.0)), "colorOffset")
这不起作用(没有添加偏移量,着色器使用默认值(0, 0, 0, 0)).如果我用 SCNVector4
which doesn't work (no offset is added and the shader uses the default value that is (0, 0, 0, 0)). Same happens if I replace GLKVector4
by SCNVector4
按照这个我也试过:
let points: [float2] = [float2(250.0), float2(0.0), float2(0.0), float2(0.0)]
material.setValue(NSData(bytes: points, length: points.count * sizeof(float2)), "colorOffset")
但是,我可以通过以下方式轻松地将浮点值传递给统一的 colorModifier
:
However, I can pass a float value to the uniform colorModifier
easily by doing:
material.setValue(250.0, forKey: "colorModifier")
这将增加绿色通道作为例外
and that will increase the green channel as excepted
推荐答案
所以你必须使用 NSValue,它对 SCNVector4 有一个方便的初始化,所以:
So you have to use NSValue, that has a convenience initialization for SCNVector4, so:
let v = SCNVector4(x: 250.0, y: 0.0, z: 0.0, w: 0.0)
material.setValue(NSValue(scnVector4: v), "colorOffset")
如果 SceneKit 可以直接处理它自己的类型就太好了...
It'd be too good if SceneKit could handle it's own types directly...
这篇关于SceneKit 将统一向量传递给着色器修改器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!