Skip to content

Commit 6833331

Browse files
author
Thayer J Andrews
committed
CCEffectGlass - Add shininess property.
It functions identically to the property of the same name in CCEffectReflection.
1 parent 0f2ae93 commit 6833331

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

cocos2d-ui-tests/tests/CCEffectsTest.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ -(void)setupGlassEffectTest
4141

4242

4343
CCSpriteFrame *normalMap = [CCSpriteFrame frameWithImageNamed:@"Images/ShinyBallNormals.png"];
44-
CCEffectGlass *glass = [[CCEffectGlass alloc] initWithRefraction:1.0f refractionEnvironment:refractEnvironment reflectionEnvironment:reflectEnvironment];
44+
CCEffectGlass *glass = [[CCEffectGlass alloc] initWithShininess:1.0f refraction:1.0f refractionEnvironment:refractEnvironment reflectionEnvironment:reflectEnvironment];
4545
glass.fresnelBias = 0.1f;
4646
glass.fresnelPower = 2.0f;
4747
glass.refraction = 0.75f;
@@ -515,7 +515,7 @@ -(void)setupStackTest
515515
[CCEffectPixellate effectWithBlockSize:8.0f],
516516
[CCEffectSaturation effectWithSaturation:-1.0f],
517517
[CCEffectHue effectWithHue:90.0f],
518-
[CCEffectGlass effectWithRefraction:0.75f refractionEnvironment:refractEnvironment reflectionEnvironment:reflectEnvironment],
518+
[CCEffectGlass effectWithShininess:1.0f refraction:0.75f refractionEnvironment:refractEnvironment reflectionEnvironment:reflectEnvironment],
519519
[CCEffectRefraction effectWithRefraction:0.75f environment:refractEnvironment],
520520
[CCEffectReflection effectWithShininess:1.0f fresnelBias:0.1f fresnelPower:2.0f environment:reflectEnvironment],
521521
];
@@ -570,7 +570,7 @@ -(void)setupPerformanceTest
570570
[CCEffectPixellate effectWithBlockSize:4.0f],
571571
[CCEffectSaturation effectWithSaturation:-1.0f],
572572
[CCEffectHue effectWithHue:90.0f],
573-
[CCEffectGlass effectWithRefraction:0.5f refractionEnvironment:refractEnvironment reflectionEnvironment:reflectEnvironment],
573+
[CCEffectGlass effectWithShininess:1.0f refraction:0.5f refractionEnvironment:refractEnvironment reflectionEnvironment:reflectEnvironment],
574574
[CCEffectRefraction effectWithRefraction:0.5f environment:refractEnvironment],
575575
[CCEffectReflection effectWithShininess:1.0f fresnelBias:0.1f fresnelPower:4.0f environment:reflectEnvironment],
576576
];

cocos2d/CCEffectGlass.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
*/
2929
@property (nonatomic) float refraction;
3030

31+
/** The overall shininess of the attached sprite. This value is in the range [0..1] and it controls
32+
* how much of the reflected environment contributes to the final color of the affected pixels.
33+
*/
34+
@property (nonatomic) float shininess;
35+
3136
/** The bias term in the fresnel reflectance equation:
3237
* reflectance = max(0.0, fresnelBias + (1 - fresnelBias) * pow((1 - nDotV), fresnelPower))
3338
* This value is in the range [0..1] and it controls the constant (view angle independent) contribution
@@ -75,25 +80,27 @@
7580
/**
7681
* Initializes a CCEffectGlass object with the supplied parameters and a nil normal map.
7782
*
83+
* @param shininess The overall shininess.
7884
* @param refraction The refraction strength.
7985
* @param refractionEnvironment The environment image that will be refracted by the affected node.
8086
* @param reflectionEnvironment The environment image that will be reflected by the affected node.
8187
*
8288
* @return The CCEffectGlass object.
8389
*/
84-
-(id)initWithRefraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment;
90+
-(id)initWithShininess:(float)shininess refraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment;
8591

8692
/**
8793
* Initializes a CCEffectGlass object with the supplied parameters.
8894
*
95+
* @param shininess The overall shininess.
8996
* @param refraction The refraction strength.
9097
* @param refractionEnvironment The environment image that will be refracted by the affected node.
9198
* @param reflectionEnvironment The environment image that will be reflected by the affected node.
9299
* @param normalMap The normal map of the affected node. This can also be specified as a property of the affected sprite.
93100
*
94101
* @return The CCEffectGlass object.
95102
*/
96-
-(id)initWithRefraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment normalMap:(CCSpriteFrame *)normalMap;
103+
-(id)initWithShininess:(float)shininess refraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment normalMap:(CCSpriteFrame *)normalMap;
97104

98105

99106
/// -----------------------------------------------------------------------
@@ -104,24 +111,26 @@
104111
/**
105112
* Creates a CCEffectGlass object with the supplied parameters and a nil normal map.
106113
*
114+
* @param shininess The overall shininess.
107115
* @param refraction The refraction strength.
108116
* @param refractionEnvironment The environment image that will be refracted by the affected node.
109117
* @param reflectionEnvironment The environment image that will be reflected by the affected node.
110118
*
111119
* @return The CCEffectGlass object.
112120
*/
113-
+(id)effectWithRefraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment;
121+
+(id)effectWithShininess:(float)shininess refraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment;
114122

115123
/**
116124
* Creates a CCEffectGlass object with the supplied parameters.
117125
*
126+
* @param shininess The overall shininess.
118127
* @param refraction The refraction strength.
119128
* @param refractionEnvironment The environment image that will be refracted by the affected node.
120129
* @param reflectionEnvironment The environment image that will be reflected by the affected node.
121130
* @param normalMap The normal map of the affected node. This can also be specified as a property of the affected sprite.
122131
*
123132
* @return The CCEffectGlass object.
124133
*/
125-
+(id)effectWithRefraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment normalMap:(CCSpriteFrame *)normalMap;
134+
+(id)effectWithShininess:(float)shininess refraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment normalMap:(CCSpriteFrame *)normalMap;
126135

127136
@end

cocos2d/CCEffectGlass.m

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ @implementation CCEffectGlass
2929

3030
-(id)init
3131
{
32-
return [self initWithRefraction:1.0f refractionEnvironment:nil reflectionEnvironment:nil normalMap:nil];
32+
return [self initWithShininess:1.0f refraction:1.0f refractionEnvironment:nil reflectionEnvironment:nil normalMap:nil];
3333
}
3434

35-
-(id)initWithRefraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment
35+
-(id)initWithShininess:(float)shininess refraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment
3636
{
37-
return [self initWithRefraction:refraction refractionEnvironment:refractionEnvironment reflectionEnvironment:reflectionEnvironment normalMap:nil];
37+
return [self initWithShininess:shininess refraction:refraction refractionEnvironment:refractionEnvironment reflectionEnvironment:reflectionEnvironment normalMap:nil];
3838
}
3939

40-
-(id)initWithRefraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment normalMap:(CCSpriteFrame *)normalMap
40+
-(id)initWithShininess:(float)shininess refraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment normalMap:(CCSpriteFrame *)normalMap
4141
{
4242
NSArray *uniforms = @[
4343
[CCEffectUniform uniform:@"float" name:@"u_refraction" value:[NSNumber numberWithFloat:1.0f]],
4444

45+
[CCEffectUniform uniform:@"float" name:@"u_shininess" value:[NSNumber numberWithFloat:1.0f]],
4546
[CCEffectUniform uniform:@"float" name:@"u_fresnelBias" value:[NSNumber numberWithFloat:0.0f]],
4647
[CCEffectUniform uniform:@"float" name:@"u_fresnelPower" value:[NSNumber numberWithFloat:0.0f]],
4748

@@ -60,6 +61,7 @@ -(id)initWithRefraction:(float)refraction refractionEnvironment:(CCSprite *)refr
6061
if((self = [super initWithFragmentUniforms:uniforms vertexUniforms:nil varying:nil]))
6162
{
6263
_refraction = refraction;
64+
_shininess = shininess;
6365
_fresnelBias = 0.1f;
6466
_fresnelPower = 2.0f;
6567
_conditionedRefraction = CCEffectUtilsConditionRefraction(refraction);
@@ -71,14 +73,15 @@ -(id)initWithRefraction:(float)refraction refractionEnvironment:(CCSprite *)refr
7173
}
7274
return self;
7375
}
74-
+(id)effectWithRefraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment
76+
77+
+(id)effectWithShininess:(float)shininess refraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment
7578
{
76-
return [[self alloc] initWithRefraction:refraction refractionEnvironment:refractionEnvironment reflectionEnvironment:reflectionEnvironment];
79+
return [[self alloc] initWithShininess:shininess refraction:refraction refractionEnvironment:refractionEnvironment reflectionEnvironment:reflectionEnvironment];
7780
}
7881

79-
+(id)effectWithRefraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment normalMap:(CCSpriteFrame *)normalMap
82+
+(id)effectWithShininess:(float)shininess refraction:(float)refraction refractionEnvironment:(CCSprite *)refractionEnvironment reflectionEnvironment:(CCSprite *)reflectionEnvironment normalMap:(CCSpriteFrame *)normalMap
8083
{
81-
return [[self alloc] initWithRefraction:refraction refractionEnvironment:refractionEnvironment reflectionEnvironment:reflectionEnvironment normalMap:normalMap];
84+
return [[self alloc] initWithShininess:shininess refraction:refraction refractionEnvironment:refractionEnvironment reflectionEnvironment:reflectionEnvironment normalMap:normalMap];
8285
}
8386

8487
-(void)buildFragmentFunctions
@@ -147,7 +150,7 @@ -(void)buildFragmentFunctions
147150
// Add the reflected color modulated by the fresnel term. Multiplying by the normal
148151
// map alpha also allows the effect to be disabled for specific pixels.
149152
float fresnel = max(u_fresnelBias + (1.0 - u_fresnelBias) * pow((1.0 - nDotV), u_fresnelPower), 0.0);
150-
vec4 reflection = normalMap.a * fresnel * texture2D(u_reflectEnvMap, reflectTexCoords);
153+
vec4 reflection = normalMap.a * fresnel * u_shininess * texture2D(u_reflectEnvMap, reflectTexCoords);
151154

152155
return primaryColor + refraction + reflection;
153156
);
@@ -182,6 +185,7 @@ -(void)buildRenderPasses
182185

183186
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_refraction"]] = [NSNumber numberWithFloat:weakSelf.conditionedRefraction];
184187

188+
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_shininess"]] = [NSNumber numberWithFloat:weakSelf.shininess];
185189
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_fresnelBias"]] = [NSNumber numberWithFloat:weakSelf.fresnelBias];
186190
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_fresnelPower"]] = [NSNumber numberWithFloat:weakSelf.fresnelPower];
187191

0 commit comments

Comments
 (0)