How to create an animated texture in SceneKit? “fallback on default program” error

泄露秘密 提交于 2019-12-11 02:54:35

问题


I would like to use an animated texture in SceneKit, I found this Using shader modifiers to animate texture in SceneKit leads to jittery textures over time , but I have got an error with the same code :

C3DResourceManagerMakeProgramResident failed to compile program - fallback on default program

I tried to use another file for the shader (New file > Empty > called animated.shader), and use this code instead, but I have got the same error :

let fireImage = UIImage(contentsOfFile: "art.scnassets/torch.png")
myMaterial = SCNMaterial()
myMaterial.diffuse.contents = fireImage
myMaterial.diffuse.wrapS = SCNWrapMode.Repeat
myMaterial.diffuse.wrapT = SCNWrapMode.Repeat

myMaterial.shaderModifiers = [ SCNShaderModifierEntryPointGeometry : "uniform float u_time; _geometry.texcoords[0] = vec2((_geometry.texcoords[0].x+floor(u_time*30.0))/24.0, (_geometry.texcoords[0].y+floor(u_time*30.0/24.0))/1.0);" ]
plane.materials = [myMaterial]

//OR :

let url = NSBundle.mainBundle().pathForResource("animated", ofType: "shader")
let str = String(contentsOfFile: url!, encoding: NSUTF8StringEncoding , error: nil)
let nstr = NSString(string: str!)
myMaterial.shaderModifiers = [ SCNShaderModifierEntryPointFragment: nstr ]
plane.materials = [myMaterial]

I tried with a simple shader that gave Apple in the documentation, to be sure, and it works. So the problem comes from the code I am using.

I found the second code here (wiki unity) : link, I changed it a little bit to use glsl instead of unityscript :

uniform float u_time;
float numTileX = 24;
float numTileY = 1;
float fps = 30;

float indexA = u_time * fps;
float index = index % (numTileX * numTileY);
float uIndex = index % numTileX;
float vIndex = index / numTileY;

_geometry.texcoords[0] = vec2( (_geometry.texcoords[0].x + uIndex) , (_geometry.texcoords[0].y + vIndex) );

EDIT: Here are the errors :

The errors :

SceneKit: error, failed to link program: ERROR: 0:66: '%' does not operate on 'int' and 'int'
ERROR: 0:67: Use of undeclared identifier 'index'
ERROR: 0:68: Use of undeclared identifier 'index'
ERROR: 0:69: Use of undeclared identifier '_geometry'

New code :

int numTileX = 24;
int numTileY = 1;

float fps = 30.0;
float indexA = u_time * fps;
int indexI = int(indexA);
int total = int(numTileX * numTileY);

int index = indexI % total;
float uIndex = index % numTileX;
float vIndex = index / numTileY;

_geometry.texcoords[0] = vec2( (_geometry.texcoords[0].x + uIndex) , (_geometry.texcoords[0].y + vIndex) );

Would you know how to solve this?

Thanks


回答1:


Without knowing more about the shader compilation error it's hard to tell if something more is wrong with that shader code, but the error that you are getting means that the shader program fails to compile.

Just by looking at the shader code, I think I see two errors:

  1. Scene Kit already declares the uniform float u_time; for the current time in seconds, that is available at all entry points. Further, the u_, a_, and v_ prefixes are reserved by Scene Kit and should not be used for custom variables in shader modifiers.

  2. GLSL is very picky about type conversion. Assigning an integer type to a float variable does not lead to casting (as it would in C), but is instead a type error.

This means that, for example:

float numTileX = 24;

Is a type error and should be redefined as:

float numTileX = 24.0;

The same thing applies to arithmetic that mixes integer and floating types.



来源:https://stackoverflow.com/questions/29462745/how-to-create-an-animated-texture-in-scenekit-fallback-on-default-program-err

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!