首页 文章

如何在 SCNScene/SCNView 中连续动画 SCNProgram 着色器?

提问于
浏览
1

在 MacOS 应用程序中,我有一个由 SCNProgram 定义的模式,映射到 SCNPlane。

它看起来像这样:

在此输入图像描述

着色器应该使三角形行移动,就像在这个视频中一样。该视频是在 MTKview 内部运行的同一着色器的屏幕抓取。

动画纹理

在此着色器的 SceneKit 版本中,着色器仅在我单击视图中的平面时动画。

如何让 SceneKit 视图(或 Scene?)一直为着色器设置动画?这个应用程序再次在 MacOS 上。我试过设置

self.gameView!.isPlaying = true

但这似乎没有解决问题

这是金属着色器:

#include <metal_stdlib>
using namespace metal;
#include <SceneKit/scn_metal>

struct myPlaneNodeBuffer {
    float4x4 modelTransform;
    float4x4 modelViewTransform;
    float4x4 normalTransform;
    float4x4 modelViewProjectionTransform;
    float2x3 boundingBox;
};

typedef struct {
    float3 position [[ attribute(SCNVertexSemanticPosition) ]];
    float2 texCoords [[ attribute(SCNVertexSemanticTexcoord0) ]];
} VertexInput;

static float rand(float2 uv)
{
    return fract(sin(dot(uv, float2(12.9898, 78.233))) * 43758.5453);
}

static float2 uv2tri(float2 uv)
{
    float sx = uv.x - uv.y / 2;
    float sxf = fract(sx);
    float offs = step(fract(1 - uv.y), sxf);
    return float2(floor(sx) * 2 + sxf + offs, uv.y);
}

struct SimpleVertexWithUV
{
    float4 position [[position]];
    float2 uv;
};

vertex SimpleVertexWithUV trianglequiltVertex(VertexInput in [[ stage_in ]],
                                     constant SCNSceneBuffer& scn_frame [[buffer(0)]],
                                     constant myPlaneNodeBuffer& scn_node [[buffer(1)]])
{
    SimpleVertexWithUV vert;
    vert.position = scn_node.modelViewProjectionTransform * float4(in.position, 1.0);
    vert.uv = in.texCoords;
    return vert;
}

fragment float4 trianglequiltFragment(SimpleVertexWithUV in [[stage_in]],
                             constant SCNSceneBuffer& scn_frame [[buffer(0)]],
                             constant myPlaneNodeBuffer& scn_node [[buffer(1)]])
{
    float4 fragColor;
    float2 uv = in.uv*10;
    float timer = scn_frame.time;
    uv.y += timer;

    float t = timer * 0.8;
    float tc = floor(t);
    float tp = smoothstep(0, 0.8, fract(t));

    float2 r1 = float2(floor(uv.y), tc);
    float2 r2 = float2(floor(uv.y), tc + 1);
    float offs = mix(rand(r1), rand(r2), tp);

    uv.x += offs * 8;

    float2 p = uv2tri(uv);
    float ph = rand(floor(p)) * 6.3 + p.y * 0.2;
    float c = abs(sin(ph + timer));

    fragColor = float4(c, c, c, 1);
    return(fragColor);

}

这是视图控制器:

import SceneKit
import QuartzCore

class GameViewController: NSViewController {

    @IBOutlet weak var gameView: GameView!

    override func awakeFromNib(){
        super.awakeFromNib()

    // create a new scene
        let scene = SCNScene()

        Swift.print(gameView.colorPixelFormat.rawValue)

    // create and add a camera to the scene
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        scene.rootNode.addChildNode(cameraNode)

    // place the camera
        cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)

    // turn off default lighting
        self.gameView!.autoenablesDefaultLighting = false

    // set the scene to the view
        self.gameView!.scene = scene

    // allows the user to manipulate the camera
        self.gameView!.allowsCameraControl = true

    // show statistics such as fps and timing information
        self.gameView!.showsStatistics = true

    // configure the view
        self.gameView!.backgroundColor = NSColor.black

    // play it always?
        self.gameView!.isPlaying = true

        var geometry:SCNGeometry

        geometry = SCNPlane(width:10, height:10)

        let geometryNode = SCNNode(geometry: geometry)

        let program = SCNProgram()
        program.fragmentFunctionName = "trianglequiltFragment"
        program.vertexFunctionName = "trianglequiltVertex"

        let gradientMaterial = SCNMaterial()
        gradientMaterial.program = program
        gradientMaterial.specular.contents = NSColor.black
        gradientMaterial.locksAmbientWithDiffuse = true
        geometry.materials = [gradientMaterial]
        geometryNode.geometry?.firstMaterial?.lightingModel = .constant

        scene.rootNode.addChildNode(geometryNode)

    }

}

1 回答

  • 1

    尝试设置:

    gameView?.rendersContinuously = true

    (你不需要那些额外的'自我.的 either.)

相关问题