简体   繁体   中英

ShaderToy with SpriteKit?

Can someone provide some direction on how to use code from the shadertoy site in sprite kit? This works with other shaders in an .fsh file but I can't seem to get any code from shadertoy to work. Im using the .sks file to add my SpriteNode to the scene. I apply the Custom Shader To the SpriteNode using the SKNode Inspector in interface builder.

I have managed to get a few shaders from the shadertoy web-site to work with Sprite Kit.

Firstly, in a shader where you see:

void mainImage( out vec4 fragColor, in vec2 fragCoord )

change that to:

void main ()

Often, you will need to change the names of these variables:

iGlobalTime to u_time

fragColor to gl_FragColor

fragCoord to gl_FragCoord

iResolution to u_sprite_size

But in iOS9, u_sprite_size is broken. I have been following a solution suggested elsewhere by creating my own uniform & calling it c_sprite_size and setting the value in the sks file in the inspector.

A list of the SpriteKit uniforms is here:

SKShader Class Reference

Global variables don't seem to work that well in the fsh files; I had to in a few cases change the code to pass the variables values around as a function parameter; I am quite new to shader programming; there might be something else to get this working with how the variables are declared as uniform & varying etc.

I have experienced a few shaders not working on the simulator or in the Sprite Kit scene editor but will work on the device. I have been using an iPhone 6s with iOS 9.2.

Lastly, it is worth noting when you run the app on an iDevice, shader compilation errors will show up in the debug console. Eg


Jet: Error Domain=MTLLibraryErrorDomain Code=3 "Compilation failed: 

program_source:6:1: error: unknown type name 'precision' precision mediump float; ^ program_source:6:19: error: expected unqualified-id precision mediump float; ^ program_source:8:1: error: unknown type name 'varying' varying vec2 position; ^ program_source:8:13: error: expected ';' after top level declarator varying vec2 position; ^

I was able to get some shaders fixed this way. 😊

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM