|
| 1 | +// |
| 2 | +// CCEffectDistanceField.m |
| 3 | +// cocos2d-ios |
| 4 | +// |
| 5 | +// Created by Oleg Osin on 8/8/14. |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +#import "CCEffectDistanceField.h" |
| 10 | + |
| 11 | +#import "CCEffectDistanceField.h" |
| 12 | +#import "CCEffect_Private.h" |
| 13 | +#import "CCRenderer.h" |
| 14 | +#import "CCTexture.h" |
| 15 | + |
| 16 | +@implementation CCEffectDistanceField |
| 17 | + |
| 18 | +-(id)init |
| 19 | +{ |
| 20 | + return [self initWithGlowColor:[CCColor blackColor]]; |
| 21 | +} |
| 22 | + |
| 23 | +-(id)initWithGlowColor:(CCColor*)glowColor |
| 24 | +{ |
| 25 | + NSArray *uniforms = @[ |
| 26 | + [CCEffectUniform uniform:@"vec4" name:@"u_glowColor" value:[NSValue valueWithGLKVector4:glowColor.glkVector4]], |
| 27 | + [CCEffectUniform uniform:@"vec4" name:@"u_fillColor" value:[NSValue valueWithGLKVector4:[CCColor blackColor].glkVector4]], |
| 28 | + [CCEffectUniform uniform:@"vec4" name:@"u_outlineColor" value:[NSValue valueWithGLKVector4:[CCColor redColor].glkVector4]], |
| 29 | + [CCEffectUniform uniform:@"float" name:@"u_outline" value:[NSNumber numberWithFloat:1.0f]], |
| 30 | + [CCEffectUniform uniform:@"float" name:@"u_glow" value:[NSNumber numberWithFloat:1.0f]], |
| 31 | + [CCEffectUniform uniform:@"vec2" name:@"u_glowOffset" value:[NSValue valueWithGLKVector2:GLKVector2Make(0.0, 0.0)]], |
| 32 | + [CCEffectUniform uniform:@"vec2" name:@"u_outlineOuterWidth" value:[NSValue valueWithGLKVector2:GLKVector2Make(0.5, 1.0)]], |
| 33 | + [CCEffectUniform uniform:@"vec2" name:@"u_outlineInnerWidth" value:[NSValue valueWithGLKVector2:GLKVector2Make(0.4, 0.42)]], |
| 34 | + ]; |
| 35 | + |
| 36 | + if((self = [super initWithFragmentUniforms:uniforms vertexUniforms:nil varyings:nil])) |
| 37 | + { |
| 38 | + _outlineInnerWidth = 0.08; |
| 39 | + _outlineOuterWidth = 0.08; |
| 40 | + _glow = YES; |
| 41 | + _outline = YES; |
| 42 | + _glowOffset = GLKVector2Make(0.0f, 0.0f); |
| 43 | + _glowColor = glowColor; |
| 44 | + _fillColor = [CCColor blackColor]; |
| 45 | + _outlineColor = [CCColor redColor]; |
| 46 | + |
| 47 | + self.debugName = @"CCEffectDistanceField"; |
| 48 | + } |
| 49 | + return self; |
| 50 | +} |
| 51 | + |
| 52 | ++(id)effectWithGlowColor:(CCColor*)glowColor |
| 53 | +{ |
| 54 | + return [[self alloc] initWithGlowColor:glowColor]; |
| 55 | +} |
| 56 | + |
| 57 | +-(void)buildFragmentFunctions |
| 58 | +{ |
| 59 | + self.fragmentFunctions = [[NSMutableArray alloc] init]; |
| 60 | + |
| 61 | + NSString* effectBody = CC_GLSL( |
| 62 | + vec4 outputColor = u_fillColor; |
| 63 | + outputColor.a = 1.0; |
| 64 | + float distAlphaMask = texture2D(cc_MainTexture, cc_FragTexCoord1).r; |
| 65 | + |
| 66 | + // 0.5 == center(edge), < 0.5 == outside, > 0.5 == inside |
| 67 | + float min0 = u_outlineOuterWidth.x; |
| 68 | + float max0 = u_outlineOuterWidth.y; |
| 69 | + float min1 = u_outlineInnerWidth.x; |
| 70 | + float max1 = u_outlineInnerWidth.y; |
| 71 | + if(u_outline == 1.0 && distAlphaMask >= min0 && distAlphaMask <= max1)//outline |
| 72 | + { |
| 73 | + float oFactor = 1.0; |
| 74 | + if(distAlphaMask <= min1) |
| 75 | + { |
| 76 | + oFactor = smoothstep(min0, min1, distAlphaMask); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + oFactor = smoothstep(max1, max0, distAlphaMask); |
| 81 | + } |
| 82 | + |
| 83 | + outputColor = mix(outputColor, u_outlineColor, oFactor); |
| 84 | + } |
| 85 | + |
| 86 | + float center = 0.5; |
| 87 | + float transition = fwidth(distAlphaMask) * 1.0; |
| 88 | + |
| 89 | + float min = center - transition; |
| 90 | + float max = center + transition; |
| 91 | + |
| 92 | + // soft edges |
| 93 | + outputColor.a *= smoothstep(min, max, distAlphaMask); |
| 94 | + |
| 95 | + // glow |
| 96 | + if(u_glow == 1.0) |
| 97 | + { |
| 98 | + vec4 glowTexel = texture2D(cc_MainTexture, cc_FragTexCoord1 - u_glowOffset); |
| 99 | +// min -= 0.2; |
| 100 | +// max += 0.2; |
| 101 | + |
| 102 | + min = 0.3; |
| 103 | + max = 0.5; |
| 104 | + |
| 105 | + vec4 glowc = u_glowColor * smoothstep(min, max, glowTexel.r); |
| 106 | + |
| 107 | + outputColor = mix(glowc, outputColor, outputColor.a); |
| 108 | + } |
| 109 | + |
| 110 | + return outputColor; |
| 111 | + |
| 112 | + ); |
| 113 | + |
| 114 | + CCEffectFunction* fragmentFunction = [[CCEffectFunction alloc] initWithName:@"outerGlowEffect" |
| 115 | + body:effectBody inputs:nil returnType:@"vec4"]; |
| 116 | + [self.fragmentFunctions addObject:fragmentFunction]; |
| 117 | +} |
| 118 | + |
| 119 | +-(void)buildRenderPasses |
| 120 | +{ |
| 121 | + __weak CCEffectDistanceField *weakSelf = self; |
| 122 | + |
| 123 | + CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; |
| 124 | + pass0.debugLabel = @"CCEffectDistanceField pass 0"; |
| 125 | + pass0.shader = self.shader; |
| 126 | + pass0.blendMode = [CCBlendMode premultipliedAlphaMode]; |
| 127 | + pass0.beginBlocks = @[[^(CCEffectRenderPass *pass, CCTexture *previousPassTexture) { |
| 128 | + |
| 129 | + pass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture; |
| 130 | + pass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture; |
| 131 | + |
| 132 | + pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_glowColor"]] = [NSValue valueWithGLKVector4:weakSelf.glowColor.glkVector4]; |
| 133 | + pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_fillColor"]] = [NSValue valueWithGLKVector4:weakSelf.fillColor.glkVector4]; |
| 134 | + pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_outlineColor"]] = [NSValue valueWithGLKVector4:weakSelf.outlineColor.glkVector4]; |
| 135 | + |
| 136 | + // 0.5 == center(edge), < 0.5 == outside, > 0.5 == inside |
| 137 | + float innerMin = 0.5; |
| 138 | + float innerMax = (0.5 * _outlineInnerWidth) + innerMin; |
| 139 | + pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_outlineInnerWidth"]] = [NSValue valueWithGLKVector2:GLKVector2Make(innerMin, innerMax)]; |
| 140 | + |
| 141 | + float outerMin = (0.5 * (1.0 - _outlineOuterWidth)); |
| 142 | + float outerMax = 0.5; |
| 143 | + pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_outlineOuterWidth"]] = [NSValue valueWithGLKVector2:GLKVector2Make(outerMin, outerMax)]; |
| 144 | + |
| 145 | + pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_outline"]] = _outline ? [NSNumber numberWithFloat:1.0f] : [NSNumber numberWithFloat:0.0f]; |
| 146 | + pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_glow"]] = _glow ? [NSNumber numberWithFloat:1.0f] : [NSNumber numberWithFloat:0.0f]; |
| 147 | + |
| 148 | + GLKVector2 offset = GLKVector2Make(weakSelf.glowOffset.x / previousPassTexture.contentSize.width, weakSelf.glowOffset.y / previousPassTexture.contentSize.height); |
| 149 | + pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_glowOffset"]] = [NSValue valueWithGLKVector2:offset]; |
| 150 | + |
| 151 | + } copy]]; |
| 152 | + |
| 153 | + self.renderPasses = @[pass0]; |
| 154 | +} |
| 155 | + |
| 156 | +-(void)setOutlineInnerWidth:(float)outlineInnerWidth |
| 157 | +{ |
| 158 | + _outlineInnerWidth = clampf(_outlineInnerWidth, 0.0f, 1.0f); |
| 159 | +} |
| 160 | + |
| 161 | +-(void)setOutlineOuterWidth:(float)outlineOuterWidth |
| 162 | +{ |
| 163 | + _outlineOuterWidth = clampf(_outlineOuterWidth, 0.0f, 1.0f); |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | +@end |
0 commit comments