如何在SceneKit中使用着色器添加透明度? [英] How to add transparency with a shader in SceneKit?

查看:502
本文介绍了如何在SceneKit中使用着色器添加透明度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从图像中获得透明效果,现在我只是用环面测试,但着色器似乎不适用于alpha。从我对这个主题的理解(在Scenekit中使用混合函数)和这个wiki链接关于透明度:( http://en.wikibooks.org/wiki/GLSL_Programming/GLUT/在SceneKit中,透明度), GLBlendFunc pragma透明度替换。

I would like to have a transparency effect from an image, for now I just test with a torus, but the shader does not seem to work with alpha. From what I understood from this thread (Using Blending Functions in Scenekit) and this wiki link about transparency : (http://en.wikibooks.org/wiki/GLSL_Programming/GLUT/Transparency), GLBlendFunc is replaced by pragma transparency in SceneKit.

你知道这个代码有什么问题吗?

Would you know what is wrong with this code?

我用SceneKit创建了一个新项目,我更换了一个环形船的网格。

I created a new project with SceneKit, and I changed the ship mesh for a torus.

编辑:

我正在尝试使用飞机,但下图中没有出现飞机,我得到的图像下面有红色和褐色的框。

EDIT :
I am trying with a plane, but the image below does not appear inside the plane, instead I get the image with the red and brownish boxes below.

我的图像有alpha:

My image with alpha :

结果(带alpha的图像应该替换褐色):

The result (the image with alpha should replace the brownish color) :

let plane = SCNPlane(width: 2, height: 2)
var texture = SKTexture(imageNamed:"small")
texture.filteringMode = SKTextureFilteringMode.Nearest
plane.firstMaterial?.diffuse.contents = texture
let ship = SCNNode(geometry: plane) //SCNTorus(ringRadius: 1, pipeRadius: 0.5)
ship.position = SCNVector3(x: 0, y: 0, z: 15)
scene.rootNode.addChildNode(ship)

let myscale : CGFloat = 10
let box = SCNBox(width: myscale, height: myscale, length: myscale, chamferRadius: 0)
box.firstMaterial?.diffuse.contents = UIColor.redColor()
let theBox = SCNNode(geometry: box)
theBox.position = SCNVector3(x: 0, y: 0, z: 5)
scene.rootNode.addChildNode(theBox)


let scnView = self.view as SCNView
scnView.scene = scene
scnView.backgroundColor = UIColor.blackColor()


var shaders = NSMutableDictionary()
shaders[SCNShaderModifierEntryPointFragment] = String(contentsOfFile: NSBundle.mainBundle().pathForResource("test", ofType: "shader")!, encoding: NSUTF8StringEncoding, error: nil)
var material = SCNMaterial()
material.shaderModifiers = shaders
ship.geometry?.materials = [material]

着色器:

#pragma transparent
#pragma body
_output.color.rgba = vec4(0.0, 0.2, 0.0, 0.2);


推荐答案

SceneKit使用预乘alpha( r g b 字段应乘以所需的 a ):

SceneKit uses premultiplied alpha (r, g and b fields should be multiplied by the desired a) :

vec4(0.0, 0.2, 0.0, 0.2); // `vec4(0.0, 1.0, 0.0, 1.0) * alpha` with alpha = 0.2

这篇关于如何在SceneKit中使用着色器添加透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆