使用SceneKit时是否可以创建自定义剪裁平面?既然你可以提供自己的着色器,我唯一需要做的就是以某种方式启用GL_CLIP_DISTANCE0。这是可能的还是我必须坚持使用较低级别的库?
编辑:
我尝试使用渲染器委托启用GL状态GL_CLIP_DISTANCE0而不起作用。以下是相关的代码部分:
class GameViewController: NSViewController, SCNSceneRendererDelegate {
@IBOutlet weak var gameView: GameView!
override func awakeFromNib() {
let attrs: [Int] = [
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 24,
NSOpenGLPFAOpenGLProfile,
NSOpenGLProfileVersion3_2Core,
0
]
let pixelFormat: NSOpenGLPixelFormat = NSOpenGLPixelFormat(attributes: UnsafePointer<NSOpenGLPixelFormatAttribute>(attrs))
gameView.pixelFormat = pixelFormat
let scene = SCNScene()
let obj = SCNSphere(radius: 1.0)
obj.shaderModifiers = [SCNShaderModifierEntryPointGeometry: "gl_CLipDistance[0] = 0.0;"]
let objNode = SCNNode(geometry: obj)
scene.rootNode.addChildNode(objNode)
// ... create camera and lights ...
// set renderer delegate
self.gameView.delegate = self
// set the scene to the view
self.gameView!.scene = scene
self.gameView!.backgroundColor = NSColor.blackColor()
}
func renderer(aRenderer: SCNSceneRenderer, updateAtTime time: NSTimeInterval) {
glEnable(GLenum(GL_CLIP_DISTANCE0))
}
}
无论我尝试什么,我都会收到以下错误:
SceneKit: error, 2: uniform mat4 u_normalTransform;
SceneKit: error, 3: uniform mat4 u_projectionTransform;
SceneKit: error, 4: uniform mat4 u_modelViewTransform;
SceneKit: error, 5: // In model space, must stay in it
SceneKit: error, 6: struct SCNShaderGeometry
SceneKit: error, 7: {
SceneKit: error, 8: vec4 position;
SceneKit: error, 9: vec3 normal;
SceneKit: error, 10: vec4 tangent;
SceneKit: error, 11: vec4 color;
SceneKit: error, 12: vec2 texcoords[8]; // MAX_UV
SceneKit: error, 13: } _geometry;
SceneKit: error, 14: struct SCNShaderSurface
SceneKit: error, 15: {
SceneKit: error, 16: vec3 view; // in view space
SceneKit: error, 17: vec3 position; // in view space
SceneKit: error, 18: vec3 normal; // in view space
SceneKit: error, 19: vec3 tangent; // in view space
SceneKit: error, 20: vec3 bitangent; // in view space
SceneKit: error, 21: float shininess;
SceneKit: error, 22: float fresnel;
SceneKit: error, 23: vec3 _normalTS; // UNDOCUMENTED in tangent space
SceneKit: error, 24: vec4 diffuse;
SceneKit: error, 25: } _surface;
SceneKit: error, 26: // Attributes
SceneKit: error, 27: attribute vec4 a_position;
SceneKit: error, 28: attribute vec3 a_normal;
SceneKit: error, 29: // Varyings
SceneKit: error, 30: varying vec3 v_position;
SceneKit: error, 31: varying vec3 v_normal;
SceneKit: error, 32: void main(void)
SceneKit: error, 33: {
SceneKit: error, 34: _geometry.position = a_position;
SceneKit: error, 35: _geometry.normal = a_normal;
SceneKit: error, 36: gl_CLipDistance[0] = 0.0;
SceneKit: error, 37: // Transform the geometry elements in view space
SceneKit: error, 38: _surface.position = (u_modelViewTransform * _geometry.position).xyz;
SceneKit: error, 39: _surface.normal = normalize(mat3(u_normalTransform) * _geometry.normal);
SceneKit: error, 40: //if USE_VIEW is 2 we may also need to set _surface.view. todo: make USE_VIEW a mask
SceneKit: error, 41: v_position = _surface.position;
SceneKit: error, 42: v_normal = _surface.normal;
SceneKit: error, 43:
SceneKit: error, 44: // this means that the geometry are still in model space
SceneKit: error, 45: gl_Position = u_projectionTransform * vec4(_surface.position, 1.);
SceneKit: error, 46: }
SceneKit: error, 47:
SceneKit: error, failed to link program: ERROR: 0:36: Use of undeclared identifier 'gl_CLipDistance'
我做错了什么,或者是在场景中使用的OpenGL版本中没有“gl_ClipDistance”?
答案 0 :(得分:1)
如果要在SceneKit渲染场景之前启用某个GL状态,请使用scene renderer delegate。在renderer:willRenderScene:atTime:
方法中,您可以glEnable
自定义着色器所依赖的任何状态。
答案 1 :(得分:1)
在SceneKit中集成自定义OpenGL代码的三种方法之一:
(1)node.renderDelegate但这仅适用于指定的节点 (2)附在材料上的SCN程序 (3)你自己的渲染器
您在(1)或(3)中启用的状态适用于(2)中的任何程序,因此在这种情况下选择一个或另一个取决于自定义剪辑是否应该应用于特定节点(使用选项1 )或所有节点(使用选项3)。
对于(3),请参阅https://developer.apple.com/reference/scenekit/scnscenerendererdelegate,其中包含SCNSceneRendererDelegate中的所有委托方法以及对SCNNodeRendererDelegate和SCNShadable的引用。