Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

471
Visualizações
RealityKit - Can't pass changing custom uniform parameter to a CustomMaterial shader

I'm trying to pass a changing value to my CustomShader in a RealityKit scene, but I'm not able to get the graphics to reflect the change. I'm able to set an initial value of the material.custom.value, and the graphics do use that, but I'm not able to change it once the scene is displayed. The callback that does the per-frame changing is definitely getting called. Any ideas?

My Swift code:

import SwiftUI
import RealityKit
import Combine

// A class with a per-frame callback to change a custom uniform for the geometry shader
class TestData {
    var material: CustomMaterial? = nil
    var theta = Float(0)
    
    func SetupCallback( _ arView: ARView) {
        _ = arView.scene.subscribe(to: SceneEvents.Update.self) { _ in
            // *** This value does get changed, but it's not reflected in the geometry shader
            self.material!.custom.value = simd_float4( 0.5 * sin( self.theta ), 0, 0, 0 )
//            print("x offset = ", self.material!.custom.value[0])
            self.theta += 0.05
        }
    }
}

struct ContentView : View {
    var body: some View {
        return ARViewContainer().edgesIgnoringSafeArea(.all)
    }
}

struct ARViewContainer: UIViewRepresentable {
    
    let testData = TestData()
    
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)

        // Create a simple box
        let box = MeshResource.generateBox(size: 0.3)
        
        // Create a custom materal with a Geometry shader -
        let mtlLibrary = MTLCreateSystemDefaultDevice()!.makeDefaultLibrary()!
        let geometryShader = CustomMaterial.GeometryModifier(
           named: "moveVertices", in: mtlLibrary
        )
        testData.material = try! CustomMaterial(
            from: SimpleMaterial(color: .orange, isMetallic: false),
            geometryModifier: geometryShader )
        // *** This initial value does make it to the Geometry shader
        testData.material!.custom.value = simd_float4(0.5, 0, 0, 0)
        
        // Create an entity with the box and custom material with geometry modifier
        let entity = ModelEntity(mesh: box, materials: [testData.material!])
        
        // Create an anchor in front of us and add the box to it
        let originAnchor = AnchorEntity(world: simd_float3( 0, -1, -1 ))
        originAnchor.addChild(entity)
        arView.scene.anchors.append(originAnchor)

        // Set up the per-frame callback subscription
        testData.SetupCallback(arView)

        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {}
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

and my GeometryModifier shader

#include <metal_stdlib>
#include <RealityKit/RealityKit.h>
using namespace metal;

[[visible]]
void moveVertices(realitykit::geometry_parameters params)
{
    // Interpret custom_parameter as a position offset
    float4 posOffset = float4( params.uniforms().custom_parameter());
    params.geometry().set_model_position_offset(float3(posOffset));
}
over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda