Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SceneKit pass uniform vector to shader modifiers

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 ]

where shaderModifier is:

// 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")

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

Following this I've also tried:

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")

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

like image 505
Guig Avatar asked Oct 30 '22 20:10

Guig


1 Answers

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")

It'd be too good if SceneKit could handle it's own types directly...

like image 125
Guig Avatar answered Nov 15 '22 07:11

Guig