2015-01-09 5 views
5

Non c'è davvero alcun modo per definire SKLabelNode? Voglio dire oltre a cambiare il carattere e il colore. So che posso fare qualcosa come aggiungere un'ombra esterna creando un secondo SKLabelNode dietro al primo (già disposto l'hacky) .. Creare immagini per il mio font sarebbe terribile anche per ovvi motivi. Sembra strano che non ci sia nulla che tu possa fare per uscire dal noioso testo piatto.Non c'è davvero alcun modo per definire SKLabelNode?

Voglio dire .. anche il conteggio dei nodi spritekit ha uno stile gradiente molto bello ... come si fa ?!

enter image description here

bene ecco che sto cercando di ricreare un effetto sfumato. Ho provato ogni combinazione di modalità di fusione e combinazioni multiple di colore e colorBlendFactor.

alfa

alpha

moltiplicare

enter image description here

aggiungere

enter image description here

Ecco il codice

let testNode = SKLabelNode(text: "hey there") 
testNode.fontSize = 30 
testNode.color = SKColor.blueColor() 
testNode.colorBlendFactor = 1 
testNode.fontName = "Helvetica-Bold" 
testNode.blendMode = SKBlendMode.Multiply 
testNode.colorBlendFactor = 0.6 
testNode.position = CGPoint(x: self.size.width/2, y: self.size.height/2) 

self.addChild(testNode) 
+0

E i valori giusti per 'fontColor',' color' e 'colorBlendFactor'? anche 'blendMode' potrebbe essere d'aiuto. – rmaddy

+0

Immagino che la mia domanda sia, potresti ricreare qualcosa come il font nodecount che ho mostrato sopra usando spritekit – hamobi

+0

Prova quello che ho appena suggerito. Guarda cosa ottieni – rmaddy

risposta

2

Non esiste un metodo o proprietà di SKLabelNode che vi permetterà di raggiungere lo stile di sfumatura. Come notato nel link nei commenti, SpriteKit ha una classe privata SKBitmapFont, ma questo non ti aiuterà.

è possibile rotolare la vostra soluzione o provare glifo progettista: https://71squared.com/glyphdesigner

3

è abbastanza facile con uno shader. Aggiungere un file di testo al progetto denominato "TheShader.fsh":

void main() 
{ 
    float yPos = gl_FragCoord.y - labelBase; 
    float gradient = yPos/u_sprite_size.y; // ranges from 0 at base to 1 at top 

    vec4 color = SKDefaultShading(); // the current label color 
    color = vec4(gradient + color.r, gradient + color.g, gradient + color.b, color.a); 
    color.rgb *= color.a; // set background to alpha 0 

    gl_FragColor = color; 
} 

NOW Crea uno SKEffectNode in SKScene, impostare lo shader per "TheShader", e aggiungere la vostra etichetta come un figlio del nodo effetto. L'etichetta verrà disegnata utilizzando un gradiente verticale che va dal carattere fontColor in basso al bianco in alto.

override func didMoveToView(view: SKView) 
{ 
    makeGradientLabel("318 nodes 60.0 fps", labelPosition: CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame))) 
} 

func makeGradientLabel(labelText: String, labelPosition: CGPoint) 
{ 
    let myShader = SKShader(fileNamed: "TheShader") 
    let labelBase = SKUniform(name: "labelBase", float: Float(labelPosition.y)) 
    myShader.addUniform(labelBase) 

    let effectNode = SKEffectNode() 
    effectNode.shader = myShader 
    effectNode.shouldEnableEffects = true 
    addChild(effectNode) 

    let theLabel = SKLabelNode(fontNamed: "Courier") 
    theLabel.fontSize = 36 
    theLabel.fontColor = SKColor.blueColor() 
    theLabel.text = labelText 
    theLabel.position = labelPosition 

    effectNode.addChild(theLabel) 
} 

Se si sposta l'etichetta sarà necessario aggiornare l'uniforme labelBase così lo shader saprà dove si trova:

labelBase.floatValue = Float(theLabel.position.y) 
+0

L'ho provato ma l'etichetta è solo blu per me. L'ho riscritto per ObjC, ma spero di non aver commesso errori. – Jonny

+0

Ricevo un messaggio di errore nella console: ** errore: uso dell'identificatore non dichiarato 'SKDefaultShading' ** – Jonny

+0

Oups c'è un argomento correlato su questa stessa questione: http://stackoverflow.com/questions/31492404/spritekit-shader- crash-ios-9-skdefaultshading-gl-fragcoord – Jonny

1

Dopo un sacco di tentativi ed errori, ho ottenuto questo lavoro:

{ 
     const CGFloat LABELFONTSIZE = 100; 
     SKColor* fontcolor = [SKColor redColor]; 

     UIFont* font = [AppDelegate fontOfType:Fonttype_main size:LABELFONTSIZE]; // LABELFONTSIZE not used here but whatever. 
     SKLabelNode* label = [SKLabelNode labelNodeWithFontNamed:font.fontName]; 
     label.fontColor = fontcolor; 
     label.verticalAlignmentMode = SKLabelVerticalAlignmentModeTop; 
     self.labelScore = label; 
     //label.zPosition = Z_HUD; 
     label.fontSize = LABELFONTSIZE; 

     self.score = 0; 

     const CGPoint LABELPOSITION = CGPointMake(self.size.width * 0.5, self.size.height); 
     label.position = LABELPOSITION; 

#define USESHADER 
#ifdef USESHADER 
     SKShader* myShader = [SKShader shaderWithFileNamed:@"TheShader"]; 
     NSAssert(myShader, @"myShader was nil!"); 

     SKEffectNode* effectNode = [SKEffectNode node]; 
     effectNode.zPosition = Z_HUD; 
     effectNode.shader = myShader; 
     effectNode.shouldEnableEffects = YES; 
     [self addChild:effectNode]; 

     [effectNode addChild:label]; 
#else 
     label.zPosition = Z_HUD; 
     [self addChild:label]; 
#endif 

     [label runAction:[SKAction repeatActionForever:[SKAction sequence:@[ 
                      [SKAction moveByX:0 y:-300 duration:3.0], 
                      [SKAction moveByX:0 y:300 duration:3.0], 
                      ]]]]; 
    } 

TheShader.fsh:

void main() 
{ 
    float gradient = v_tex_coord.y; 
    //vec4 color = SKDefaultShading(); // the current label color THIS DOES NOT WORK ON IOS 9.2 AND EARLIERISH (MAYBE IT DOES ON IOS 8) WORKS ONLY ON IOS 9.3 BETA AT THIS MOMENT! 
    vec4 color = texture2D(u_texture, v_tex_coord); 
    color = vec4(color.r + gradient, color.g + gradient, color.b + gradient, color.a); 
    color.rgb *= color.a; // set background to alpha 0 
    gl_FragColor = color; 
} 

Si basa sulla risposta di Loengard, ma non usa uniformi. Alcune delle cose all'interno dello shader ha/non funziona su un certo numero di iOS 9 versioni, tra cui l'ultimo iOS 9.3 beta fin d'ora (20.160.115). Questo codice sopra funziona bene, non interessa nemmeno la posizione dell'etichetta sullo schermo; Animare l'etichetta su/giù per confermare questo.

Problemi correlati