Skip to content

sneha-belkhale/MetalShaderTest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MetalShaderTest

I thought I would share a very simple example of how to pass in custom uniforms to a metal shader in Swift / SceneKit, since for some reason there is absolutely no coded example of how to do this.

Main Steps

[1] define a struct for you uniforms

struct Uniforms {
    float3 uColor;
};

[2] instantiate them

let color = float3(1,1,0);
var uniforms = Uniforms(uColor:color)

[3] attach to a material

let data = NSData(bytes: &uniforms, length: MemoryLayout<Uniforms>.size)
geo.firstMaterial?.setValue(data, forKey: "uniforms")

[4] use in shader!

fragment float4 flexFragment(SimpleVertex in [[stage_in]],
                          constant Uniforms& uniforms [[buffer(2)]]
                          )
{
    return float4(0.2*uniforms.uColor, 1.0);
}

to update uniforms, simply update the Uniform struct and repeat step [4] see full project to understand more about how it all fits together, but this is the essence.

About

an actual coded example of using a custom shader with scenekit and metal because for some reason this doesnt exist!!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published