我(拼命地)试图在SCNScene中使用视频作为纹理,使用一些奇特的着色器修饰符。我想为那部分使用SCNProgram。我刚从here获取了一个:
#include <metal_stdlib>
using namespace metal;
#include <SceneKit/scn_metal>
struct MyNodeBuffer {
float4x4 modelTransform;
float4x4 modelViewTransform;
float4x4 normalTransform;
float4x4 modelViewProjectionTransform;
};
typedef struct {
float3 position [[ attribute(SCNVertexSemanticPosition) ]];
float2 texCoords [[ attribute(SCNVertexSemanticTexcoord0) ]];
} MyVertexInput;
struct SimpleVertex
{
float4 position [[position]];
float2 texCoords;
};
vertex SimpleVertex myVertex(MyVertexInput in [[ stage_in ]],
constant SCNSceneBuffer& scn_frame [[buffer(0)]],
constant MyNodeBuffer& scn_node [[buffer(1)]])
{
SimpleVertex vert;
vert.position = scn_node.modelViewProjectionTransform * float4(in.position, 1.0);
vert.texCoords = in.texCoords;
return vert;
}
fragment half4 myFragment(SimpleVertex in [[stage_in]],
texture2d<float, access::sample> diffuseTexture [[texture(0)]])
{
constexpr sampler sampler2d(coord::normalized, filter::linear, address::repeat);
float4 color = diffuseTexture.sample(sampler2d, in.texCoords);
return half4(color);
}
只是在几何体上显示纹理。我现在正在创建一个SceneKit场景,添加一个使用SCNProgram的简单立方体,创建一个带有简单方形的SpriteKit场景,并将此SpriteKit场景作为纹理传递给我的立方体。它工作正常:
class ViewController: UIViewController {
@IBOutlet weak var scnView: SCNView!
let fragmentModifier = try! String(contentsOfFile: Bundle.main.path(forResource: "fragment", ofType: "shader")!)
var videoLayer: AVPlayerLayer!
var videoPlayer: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
setupVideo()
// Do any additional setup after loading the view, typically from a nib.
scnView.showsStatistics = true
scnView.allowsCameraControl = true
let scnScene = SCNScene()
scnView.scene = scnScene
// setup SceneKit scene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 0, y: 0, z: 25)
scnScene.rootNode.addChildNode(cameraNode)
let cubeNode = SCNNode()
cubeNode.geometry = SCNBox(width: 5, height: 5, length: 5, chamferRadius: 0)
scnScene.rootNode.addChildNode(cubeNode)
// setup SpriteKit Scene
let skScene = SKScene()
skScene.backgroundColor = UIColor.black
skScene.size = CGSize(width: 100, height: 100)
// a simple green square
let skNode = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 20, height: 20))
skNode.fillColor = UIColor.green
skNode.position = CGPoint(x: 5, y: 5)
// a node to hold a video
let skVideoNode = SKVideoNode(avPlayer: videoPlayer)
skVideoNode.size = CGSize(width: 100, height: 100)
skVideoNode.position = CGPoint(x: 5, y: 5)
skScene.addChild(skNode)
// skScene.addChild(skVideoNode)
let material = cubeNode.geometry!.firstMaterial!
createProgram(for: material)
let textureProperty = SCNMaterialProperty(contents: skScene)
material.setValue(textureProperty, forKey: "diffuseTexture")
}
func setupVideo() {
let item = AVPlayerItem(url: URL(fileURLWithPath: Bundle.main.path(forResource: "test", ofType: "mp4")!))
let player = AVPlayer(playerItem: item)
let layer = AVPlayerLayer(player: player)
videoLayer = layer
videoPlayer = player
player.play()
}
func createProgram(for material: SCNMaterial) {
let program = SCNProgram()
program.fragmentFunctionName = "myFragment"
program.vertexFunctionName = "myVertex"
material.program = program
}
}
现在,如果我用sprite kit场景中的视频节点替换方块,即我现在这样做:
// skScene.addChild(skNode)
skScene.addChild(skVideoNode)
输出只是纯黑色。视频确实正在播放并输出声音。如果这是相关的,请访问iOS 10.0.1。