我使用的着色器依赖于游戏中瓷砖的位置。我还没有找到任何关于使用SKShader对象的属性变量,所以我去更新统一变量。但似乎着色器不会与变量通信,特别是一旦它们的值被更新和更改。我正在努力制作一个基本的灯光效果,但我根本无法从着色器中获得任何效果。有帮助吗?我的着色器和Objective C类的代码如下所示。
uniform float midX, midY;
uniform float posX;
uniform float posY;
void main()
{
vec4 temp = SKDefaultShading(); // get the default shading
float lightRad = 200.0; // Light radius
float dist = distance(vec2(posX, posY), vec2(midX, midY)); // location of the light on the screen
vec4 color = vec4(1.0, 0, 0.0, (float)(dist / lightRad)); // creates an alpha gradient for the light. (falloff)
if (dist < lightRad) // only applies the light color if the distance from the light to the tile is smaller than the radius of the light
{
gl_FragColor = temp * color; // applies the color
}
else // otherwise, do nothing
{
gl_FragColor = temp;
}
}
- (void) loadShaders
{
SKUniform* posX = [SKUniform uniformWithName:@"posX" float: 0.0f]; // adds the x position (with a placeholder value)
SKUniform* posY = [SKUniform uniformWithName:@"posY" float: 0.0f]; // adds the y position (with a placeholder value)
[_shader addUniform:posX];
[_shader addUniform:posY];
}
-(void)update:(CFTimeInterval)currentTime
{
for (int i = 0; i < _array.count; i++) // Loop through all tiles
{
float x = ((i % 100) - 13.5f) * 15.0f; // Calculate x pos of the tile
float y = ((1 - (i / 100)) + 6.5f) * 15.0f; // Calculate y pos of the tile
SKUniform* uniX = [[_tMap getShader] uniformNamed:@"posX"]; // get the uniform with the name posX
uniX.floatValue = x; // set the value of that uniform
SKUniform* uniY = [[_tMap getShader] uniformNamed:@"posY"]; // get the uniform with the name posY
uniY.floatValue = y; // set the value of that uniform
}
}
我对精灵套件相当新,而且我也是GLSL的新手。