2

How can one use additive blending in SceneKit?

I'd be okay if I have to use a shader but I hope there's an easier way, as even SpriteKit supports blend modes.

EDIT: As this was marked as a duplicate: I'm not sure if the solution to the other question is really talking about additive blending. I haven't got it to work yet, so I'm asking if somebody is actually able to get additive blending to work.

EDIT 2: I actually tried the "solution"in the other thead now and it doesn't work. My shapes (SCNShape) get solid pink then and there's no additive blending at all.

EDIT 3: After trying and fiddling more, I got it somewhat to work, but it's not a proper additive blend (yet?). The colors get definitely brighter but they don't behave exactly like addtive blending. If anybody has an addition to this / knows how to make it function properly, I'd be glad.

-(void) additiveTest {
{
SCNNode *node1 = [self createCircleWithRadius: 50.0 atPos:SCNVector3Make(100, 100, 0) withColor: [UIColor colorWithRed:0.5 green:0.0 blue:0.0 alpha: 0.5]];
[_baseNode addChildNode: node1];


SCNNode *node2 = [self createCircleWithRadius: 50.0 atPos:SCNVector3Make(125, 100, -1) withColor: [UIColor colorWithRed:0.5 green: 0.0 blue: 0.0 alpha: 0.5]];
[_baseNode addChildNode: node2];


NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[SCNShaderModifierEntryPointFragment] = @"#pragma transparent\n#pragma body\n_output.color.a = 0.0;";
//    node2.geometry.firstMaterial.shaderModifiers = dict;
node1.geometry.firstMaterial.shaderModifiers = dict;
}

{

    SCNNode *node1 = [self createCircleWithRadius: 50.0 atPos:SCNVector3Make(100, 300, 0) withColor: [UIColor colorWithRed:0.5 green:0.0 blue:0.0 alpha: 0.5]];
    [_baseNode addChildNode: node1];

    SCNNode *node2 = [self createCircleWithRadius: 50.0 atPos:SCNVector3Make(125, 300, -1) withColor: [UIColor colorWithRed:0.5 green: 0.0 blue: 0.0 alpha: 0.5]];
    [_baseNode addChildNode: node2];


}
}

This is the result, in the lower "row", the left side of the circle is lighter than I'd expect it to be (as the background is black), or am I wrong?

enter image description here

Max
  • 2,699
  • 2
  • 27
  • 50
  • As I mentioned in my answer to that other question, the shader I provided isn't really a full solution to additive blending - it's just the closest work around I've found, since SceneKit doesn't seem to support setting the blend mode at all. – jportway Apr 06 '15 at 19:20
  • The shader works by setting the alpha channel of the fragment being drawn to zero, so that because SceneKit uses GL_ONE, GL_ONE_MINUS_SRC_ALPHA blend mode it will add the colour of the fragment to be rendered to the colour of the existing fragment times 1 - minus the alpha (which is set to zero by the shader) - so in effect it will add the two fragments together. However, in the process you lose the actual alpha value of your fragment.If you set the alpha of your geometry to 1 do you get a more reasonable looking result? – jportway Apr 06 '15 at 19:21

0 Answers0