@@ -50,28 +50,7 @@ @implementation CCEffectBloom {
50
50
NSUInteger _numberOfOptimizedOffsets;
51
51
GLfloat _sigma;
52
52
BOOL _shaderDirty;
53
- }
54
-
55
- - (void )setBlurRadiusAndDependents : (NSUInteger )blurRadius
56
- {
57
- blurRadius = MIN (blurRadius, GAUSSIANBLUR_OPTMIZIED_RADIUS_MAX);
58
- _blurRadius = blurRadius;
59
- _sigma = blurRadius / 2 ;
60
- if (_sigma == 0.0 )
61
- _sigma = 1 .0f ;
62
-
63
- _numberOfOptimizedOffsets = MIN (blurRadius / 2 + (blurRadius % 2 ), GAUSSIANBLUR_OPTMIZIED_RADIUS_MAX);
64
- }
65
-
66
- -(void )setBlurRadius : (NSUInteger )blurRadius
67
- {
68
- [self setBlurRadiusAndDependents: blurRadius];
69
-
70
- // The shader is constructed dynamically based on the blur radius
71
- // so mark it dirty and make sure this propagates up to any containing
72
- // effect stacks.
73
- _shaderDirty = YES ;
74
- [self .owningStack passesDidChange: self ];
53
+ float _transformedIntensity;
75
54
}
76
55
77
56
-(id )init
@@ -87,13 +66,12 @@ -(id)init
87
66
88
67
-(id )initWithPixelBlurRadius : (NSUInteger )blurRadius intensity : (float )intensity luminanceThreshold : (float )luminanceThreshold
89
68
{
90
- _intensity = clampf (intensity, 0 .0f , 1 .0f );
91
- _intensity = 1 .0f - _intensity;
92
-
93
- _luminanceThreshold = clampf (luminanceThreshold, 0 .0f , 1 .0f );
94
-
95
- [self setBlurRadiusAndDependents: blurRadius];
69
+ self.intensity = intensity;
70
+ self.luminanceThreshold = luminanceThreshold;
96
71
72
+ [self setBlurRadiusAndDependents: blurRadius];
73
+
74
+ CCEffectUniform* u_intensity = [CCEffectUniform uniform: @" float" name: @" u_intensity" value: [NSNumber numberWithFloat: _transformedIntensity]];
97
75
CCEffectUniform* u_luminanceThreshold = [CCEffectUniform uniform: @" float" name: @" u_luminanceThreshold" value: [NSNumber numberWithFloat: _luminanceThreshold]];
98
76
CCEffectUniform* u_enableGlowMap = [CCEffectUniform uniform: @" float" name: @" u_enableGlowMap" value: [NSNumber numberWithFloat: 0 .0f ]];
99
77
CCEffectUniform* u_blurDirection = [CCEffectUniform uniform: @" vec2" name: @" u_blurDirection"
@@ -102,7 +80,7 @@ -(id)initWithPixelBlurRadius:(NSUInteger)blurRadius intensity:(float)intensity l
102
80
unsigned long count = (unsigned long )(1 + (_numberOfOptimizedOffsets * 2 ));
103
81
CCEffectVarying* v_blurCoords = [CCEffectVarying varying: @" vec2" name: @" v_blurCoordinates" count: count];
104
82
105
- if (self = [super initWithFragmentUniforms: @[u_enableGlowMap, u_luminanceThreshold]
83
+ if (self = [super initWithFragmentUniforms: @[u_enableGlowMap, u_luminanceThreshold, u_intensity ]
106
84
vertexUniforms: @[u_blurDirection]
107
85
varying: @[v_blurCoords]])
108
86
{
@@ -120,6 +98,39 @@ +(id)effectWithPixelBlurRadius:(NSUInteger)blurRadius intensity:(float)intensity
120
98
return [[self alloc ] initWithPixelBlurRadius: blurRadius intensity: intensity luminanceThreshold: luminanceThreshold];
121
99
}
122
100
101
+ -(void )setLuminanceThreshold : (float )luminanceThreshold
102
+ {
103
+ _luminanceThreshold = clampf (luminanceThreshold, 0 .0f , 1 .0f );
104
+ }
105
+
106
+ -(void )setIntensity : (float )intensity
107
+ {
108
+ _intensity = clampf (intensity, 0 .0f , 1 .0f );
109
+ _transformedIntensity = 1 .0f - _intensity;
110
+ }
111
+
112
+ -(void )setBlurRadius : (NSUInteger )blurRadius
113
+ {
114
+ [self setBlurRadiusAndDependents: blurRadius];
115
+
116
+ // The shader is constructed dynamically based on the blur radius
117
+ // so mark it dirty and make sure this propagates up to any containing
118
+ // effect stacks.
119
+ _shaderDirty = YES ;
120
+ [self .owningStack passesDidChange: self ];
121
+ }
122
+
123
+ - (void )setBlurRadiusAndDependents : (NSUInteger )blurRadius
124
+ {
125
+ blurRadius = MIN (blurRadius, GAUSSIANBLUR_OPTMIZIED_RADIUS_MAX);
126
+ _blurRadius = blurRadius;
127
+ _sigma = blurRadius / 2 ;
128
+ if (_sigma == 0.0 )
129
+ _sigma = 1 .0f ;
130
+
131
+ _numberOfOptimizedOffsets = MIN (blurRadius / 2 + (blurRadius % 2 ), GAUSSIANBLUR_OPTMIZIED_RADIUS_MAX);
132
+ }
133
+
123
134
-(void )buildFragmentFunctions
124
135
{
125
136
self.fragmentFunctions = [[NSMutableArray alloc ] init ];
@@ -202,8 +213,9 @@ -(void)buildFragmentFunctions
202
213
203
214
204
215
// Choose one?
216
+ // TODO: try using min(src, dst) to create a gloomEffect
205
217
NSString * addativeBlending = @" src + dst" ;
206
- NSString * screenBlending = [ NSString stringWithFormat: @" (src + dst) - ((src * dst) * %f ) " , _intensity] ;
218
+ NSString * screenBlending = @" (src + dst) - ((src * dst) * u_intensity) " ;
207
219
208
220
[shaderString appendFormat: @" \
209
221
return %@ ;\n " , screenBlending];
@@ -300,6 +312,8 @@ -(void)buildRenderPasses
300
312
pass.shaderUniforms [CCShaderUniformMainTexture] = previousPassTexture;
301
313
pass.shaderUniforms [CCShaderUniformPreviousPassTexture] = previousPassTexture;
302
314
pass.shaderUniforms [self .uniformTranslationTable[@" u_enableGlowMap" ]] = [NSNumber numberWithFloat: 0 .0f ];
315
+ pass.shaderUniforms [self .uniformTranslationTable[@" u_luminanceThreshold" ]] = [NSNumber numberWithFloat: _luminanceThreshold];
316
+ pass.shaderUniforms [self .uniformTranslationTable[@" u_intensity" ]] = [NSNumber numberWithFloat: _transformedIntensity];
303
317
304
318
GLKVector2 dur = GLKVector2Make (1.0 / (previousPassTexture.pixelWidth / previousPassTexture.contentScale ), 0.0 );
305
319
pass.shaderUniforms [self .uniformTranslationTable[@" u_blurDirection" ]] = [NSValue valueWithGLKVector2: dur];
@@ -313,6 +327,8 @@ -(void)buildRenderPasses
313
327
314
328
pass.shaderUniforms [CCShaderUniformPreviousPassTexture] = previousPassTexture;
315
329
pass.shaderUniforms [self .uniformTranslationTable[@" u_enableGlowMap" ]] = [NSNumber numberWithFloat: 0 .0f ];
330
+ pass.shaderUniforms [self .uniformTranslationTable[@" u_luminanceThreshold" ]] = [NSNumber numberWithFloat: _luminanceThreshold];
331
+ pass.shaderUniforms [self .uniformTranslationTable[@" u_intensity" ]] = [NSNumber numberWithFloat: _transformedIntensity];
316
332
317
333
GLKVector2 dur = GLKVector2Make (0.0 , 1.0 / (previousPassTexture.pixelHeight / previousPassTexture.contentScale ));
318
334
pass.shaderUniforms [self .uniformTranslationTable[@" u_blurDirection" ]] = [NSValue valueWithGLKVector2: dur];
@@ -325,6 +341,9 @@ -(void)buildRenderPasses
325
341
pass2.beginBlocks = @[[^(CCEffectRenderPass *pass, CCTexture *previousPassTexture){
326
342
pass.shaderUniforms [CCShaderUniformPreviousPassTexture] = previousPassTexture;
327
343
pass.shaderUniforms [self .uniformTranslationTable[@" u_enableGlowMap" ]] = [NSNumber numberWithFloat: 1 .0f ];
344
+ pass.shaderUniforms [self .uniformTranslationTable[@" u_luminanceThreshold" ]] = [NSNumber numberWithFloat: _luminanceThreshold];
345
+ pass.shaderUniforms [self .uniformTranslationTable[@" u_intensity" ]] = [NSNumber numberWithFloat: _transformedIntensity];
346
+
328
347
} copy]];
329
348
330
349
self.renderPasses = @[pass0, pass1, pass2];
@@ -340,6 +359,10 @@ - (CCEffectPrepareStatus)prepareForRendering
340
359
CCEffectPrepareStatus result = CCEffectPrepareNothingToDo;
341
360
if (_shaderDirty)
342
361
{
362
+ unsigned long count = (unsigned long )(1 + (_numberOfOptimizedOffsets * 2 ));
363
+ CCEffectVarying* v_blurCoords = [CCEffectVarying varying: @" vec2" name: @" v_blurCoordinates" count: count];
364
+ [self setVarying: @[v_blurCoords]];
365
+
343
366
[self buildFragmentFunctions ];
344
367
[self buildVertexFunctions ];
345
368
[self buildEffectShader ];
0 commit comments