ios - Draw an SKShapeNode line color-gradient from one color to another -
i'm writing app ipad , ios 10. i'm using objective-c , spritekit.
i need draw line starts in 1 color , ends in color (eg. red green).
this objective-c code:
cgpoint points[2]; points[0] = cgpointmake(100, 100); points[1] = cgpointmake(160, 110); skshapenode *line1=[skshapenode shapenodewithpoints:points count:2]; line1.linewidth = 2.0; line1.zposition = -1; line1.name=@"line"; skshader *myshader=[skshader shaderwithfilenamed:@"shader1.fsh"]; line1.strokeshader=myshader; [self addchild:line1];
and shader:
void main(){ float length = u_path_length; float distance = v_path_distance; gl_fragcolor = vec4(1.0*(1-(distance/length)), 1.0*(distance/length), 0., 1.); }
when run app error (related metal): failed assertion 'missing buffer binding @ index 1 u_path_length[0].'
it looks u_path_length , v_path_distance don't work in ios 10.
can solve problem? or can suggest different solution problem?
if add prefersopengl key bool value of yes in info.plist file, error (related metal) disappears. of course means app not use metal. suppose bug in ios 10.
i faced problem: v_path_distance works expect u_path_length doesn't. u_path_length = 0.
to solve problem had modify code.
this objective-c code:
cgpoint points[2]; points[0] = cgpointmake(100, 100); points[1] = cgpointmake(160, 110); skshapenode *line1=[skshapenode shapenodewithpoints:points count:2]; line1.linewidth = 2.0; line1.zposition = -1; line1.name=@"line"; skshader *myshader=[skshader shaderwithfilenamed:@"shader1.fsh"]; float my_length=hypotf(points[1].x-points[0].x, points[1].y-points[0].y); myshader.uniforms=@[[skuniform uniformwithname:@"u_my_length" float:my_length]]; line1.strokeshader=myshader; [self addchild:line1];
and shader:
void main(){ gl_fragcolor=vec4(1.0*(1.0-(v_path_distance/u_my_length)),1.0*(v_path_distance/u_my_length),0.,1.0); }
Comments
Post a Comment