修改SKShader的SKUniform

时间:2015-03-10 04:15:31

标签: macos cocoa sprite-kit glsl

我正在尝试使用Sprite Kit制作一个简单的分形渲染器。使用“Shader3.fsh”计算分形。到目前为止它工作正常,但我现在希望能够点击窗口并将分形重新定位到点击区域。我计划这样做的方法是基于鼠标点击计算翻译矩阵,然后将其作为SKUniform交给着色器。在着色器中,然后我将vec2(v_tex_coord.x,v_tex_coord.y, 1.0)乘以此平移矩阵。如何将这个新矩阵放入着色器?此外,我希望能够使用触控板上的“捏合缩放”手势进行放大,但是 - (void)magnifyWithEvent:(NSEvent *)event 没有被召唤。 这是相关代码

`

#import "GameScene.h"
#import <GLKit/GLKit.h>

@implementation GameScene
{
    GLKMatrix3 transMatrix;
}
@synthesize myShader, scalingMatrixUniform, transMatrixUniform;

-(void)didMoveToView:(SKView *)view {

    /* Here we set up the matrix to give us the appropriate window */
    GLKMatrix3 scalingMatrix = GLKMatrix3Make(3.0, 0.0, 0.0,    //reduces the scale by a third
                                              0.0, 3.0, 0.0,
                                              0.0, 0.0, 3.0);
    transMatrix = GLKMatrix3Make(1.0, 0.0, 0.0,
                                 0.0, 1.0, 0.0,
                                 0.0, 0.0, 1.0);


    scalingMatrixUniform = [SKUniform uniformWithName:@"scalingMatrix" floatMatrix3:scalingMatrix];
    transMatrixUniform = [SKUniform uniformWithName:@"transMatrix" floatMatrix3:transMatrix];
    myShader = [SKShader shaderWithFileNamed:@"Shader3.fsh"];

    //We then hand off the transformation matrix to the shader as uniform data
    [myShader addUniform: scalingMatrixUniform];
    [myShader addUniform: transMatrixUniform];

    //we create a rectangle sprite which wille be shaded with our custom shader
    SKSpriteNode *mySprite = [SKSpriteNode spriteNodeWithColor:[NSColor redColor]
                                                          size:CGSizeMake(self.frame.size.width, self.frame.size.height)];
    mySprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

    //We hand off the shader to the sprite
    mySprite.shader = myShader;

    //We add the shaded sprite to the view
    [self addChild:mySprite];
}

-(void)mouseDown:(NSEvent *)theEvent {

    CGFloat mouseX = [theEvent locationInNode:self].x;
    CGFloat mouseY = [theEvent locationInNode:self].y;
    mouseX = mouseX / [self size].width;
    mouseY = mouseY / [self size].height;
    transMatrix = GLKMatrix3Make(1.0, 0.0, -mouseX,
                                 0.0, 1.0, -mouseY,
                                 0.0, 0.0, 1.0);
}


-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}

@end

`

0 个答案:

没有答案