Simulating refraction in SceneKit

痞子三分冷 提交于 2019-12-04 06:51:05

If you want to use your own vertex and fragment shader instead of SceneKit default shader program, you have too use SCNProgram instead of SCNShaderModifierEntryPoint.

SCNShaderModifierEntryPoints are only allow to modify default shader program of Swift.

let material = SCNMaterial()
            let program:SCNProgram = SCNProgram()
            do {
                program.vertexShader = try String(contentsOfFile: Bundle.main.path(forResource: "fisheye", ofType: "vsh")!, encoding: String.Encoding.utf8)
            } catch let error {
                print("shaderReadingError:\(error)")
            }
            do {
                program.fragmentShader = try String(contentsOfFile: Bundle.main.path(forResource: "fisheye", ofType: "fsh")!, encoding: String.Encoding.utf8)
            } catch let error {
                print("shaderReadingError:\(error)")
            }

    // and also your vertex shader has lack. 
    // You have to add some geometry source and transformation matrix to the vertex shader first with setSemantic method. 

            program.setSemantic(SCNGeometrySource.Semantic.vertex.rawValue, forSymbol: "vPosition", options: nil)
            program.setSemantic(SCNGeometrySource.Semantic.texcoord.rawValue, forSymbol: "uv", options: nil)
            program.setSemantic(SCNModelViewProjectionTransform, forSymbol: "uMVPMatrix", options: nil)





    // and also your fragment shader expect some realtime data like
    // colorSampler, rg, uv2, d, xy, Vertex_UV
    // you have to use handleBinding block to update this values before rendering the object.
            material.handleBinding(ofSymbol: "resolution", handler: { (programId:UInt32, location:UInt32, node:SCNNode?, renderer:SCNRenderer) in


            })


            material.program = program
            yourNode.geometry.firstMaterial = material
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!