Skip to content

Commit 0f2ae93

Browse files
author
Thayer J Andrews
committed
CCEffectReflection - Add shininess property
This is an overall control of the affected sprite's shininess that works something like this: finalColor = baseColor + reflectedColor * shininess * fresnelReflectance Set it to zero and you see none of the reflected environment. Set it to one and the effect is completely controlled by the fresnel term.
1 parent 67b4067 commit 0f2ae93

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

cocos2d-ui-tests/tests/CCEffectsTest.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ -(void)setupReflectEffectTest
8181
[self.contentNode addChild:environment];
8282

8383
CCSpriteFrame *normalMap = [CCSpriteFrame frameWithImageNamed:@"Images/ShinyBallNormals.png"];
84-
CCEffectReflection *reflection = [[CCEffectReflection alloc] initWithEnvironment:environment];
84+
CCEffectReflection *reflection = [[CCEffectReflection alloc] initWithShininess:0.4f environment:environment];
8585
reflection.fresnelBias = 0.0f;
8686
reflection.fresnelPower = 0.0f;
8787

@@ -517,7 +517,7 @@ -(void)setupStackTest
517517
[CCEffectHue effectWithHue:90.0f],
518518
[CCEffectGlass effectWithRefraction:0.75f refractionEnvironment:refractEnvironment reflectionEnvironment:reflectEnvironment],
519519
[CCEffectRefraction effectWithRefraction:0.75f environment:refractEnvironment],
520-
[CCEffectReflection effectWithFresnelBias:0.1f fresnelPower:2.0f environment:reflectEnvironment],
520+
[CCEffectReflection effectWithShininess:1.0f fresnelBias:0.1f fresnelPower:2.0f environment:reflectEnvironment],
521521
];
522522

523523

@@ -572,7 +572,7 @@ -(void)setupPerformanceTest
572572
[CCEffectHue effectWithHue:90.0f],
573573
[CCEffectGlass effectWithRefraction:0.5f refractionEnvironment:refractEnvironment reflectionEnvironment:reflectEnvironment],
574574
[CCEffectRefraction effectWithRefraction:0.5f environment:refractEnvironment],
575-
[CCEffectReflection effectWithFresnelBias:0.1f fresnelPower:4.0f environment:reflectEnvironment],
575+
[CCEffectReflection effectWithShininess:1.0f fresnelBias:0.1f fresnelPower:4.0f environment:reflectEnvironment],
576576
];
577577
CCEffect *selectedEffect = allEffects[8];
578578

cocos2d/CCEffectReflection.h

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
*/
3333
@property (nonatomic) CCSpriteFrame *normalMap;
3434

35+
/** The overall shininess of the attached sprite. This value is in the range [0..1] and it controls
36+
* how much of the reflected environment contributes to the final color of the affected pixels.
37+
*/
38+
@property (nonatomic) float shininess;
39+
3540
/** The bias term in the fresnel reflectance equation:
3641
* reflectance = max(0.0, fresnelBias + (1 - fresnelBias) * pow((1 - nDotV), fresnelPower))
3742
* This value is in the range [0..1] and it controls the constant (view angle independent) contribution
@@ -63,45 +68,49 @@
6368
* Initializes a CCEffectReflection object with the specified environment and normal map and the following default parameters:
6469
* fresnelBias = 1.0, fresnelPower = 0.0, normalMap = nil
6570
*
71+
* @param shininess The overall shininess of the effect.
6672
* @param environment The environment image that will be reflected by the affected node.
6773
*
6874
* @return The CCEffectReflection object.
6975
*/
70-
-(id)initWithEnvironment:(CCSprite *)environment;
76+
-(id)initWithShininess:(float)shininess environment:(CCSprite *)environment;
7177

7278
/**
7379
* Initializes a CCEffectReflection object with the specified environment and normal map and the following default parameters:
7480
* fresnelBias = 1.0, fresnelPower = 0.0
7581
*
82+
* @param shininess The overall shininess of the effect.
7683
* @param environment The environment image that will be reflected by the affected node.
7784
* @param normalMap The normal map of the affected node. This can also be specified as a property of the affected sprite.
7885
*
7986
* @return The CCEffectReflection object.
8087
*/
81-
-(id)initWithEnvironment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap;
88+
-(id)initWithShininess:(float)shininess environment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap;
8289

8390
/**
8491
* Initializes a CCEffectReflection object with the specified parameters and a nil normal map.
8592
*
93+
* @param shininess The overall shininess of the effect.
8694
* @param bias The bias term in the fresnel reflectance equation.
8795
* @param power The power term in the fresnel reflectance equation.
8896
* @param environment The environment image that will be reflected by the affected node.
8997
*
9098
* @return The CCEffectReflection object.
9199
*/
92-
-(id)initWithFresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment;
100+
-(id)initWithShininess:(float)shininess fresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment;
93101

94102
/**
95103
* Initializes a CCEffectReflection object with the specified parameters.
96104
*
105+
* @param shininess The overall shininess of the effect.
97106
* @param bias The bias term in the fresnel reflectance equation.
98107
* @param power The power term in the fresnel reflectance equation.
99108
* @param environment The environment image that will be reflected by the affected node.
100109
* @param normalMap The normal map of the affected node. This can also be specified as a property of the affected sprite.
101110
*
102111
* @return The CCEffectReflection object.
103112
*/
104-
-(id)initWithFresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap;
113+
-(id)initWithShininess:(float)shininess fresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap;
105114

106115

107116
/// -----------------------------------------------------------------------
@@ -112,44 +121,48 @@
112121
* Creates a CCEffectReflection object with the specified environment and the following default parameters:
113122
* fresnelBias = 1.0, fresnelPower = 0.0, normalMap = nil
114123
*
124+
* @param shininess The overall shininess of the effect.
115125
* @param environment The environment image that will be reflected by the affected node.
116126
*
117127
* @return The CCEffectReflection object.
118128
*/
119-
+(id)effectWithEnvironment:(CCSprite *)environment;
129+
+(id)effectWithShininess:(float)shininess environment:(CCSprite *)environment;
120130

121131
/**
122132
* Creates a CCEffectReflection object with the specified environment and normal map and the following default parameters:
123133
* fresnelBias = 1.0, fresnelPower = 0.0
124134
*
135+
* @param shininess The overall shininess of the effect.
125136
* @param environment The environment image that will be reflected by the affected node.
126137
* @param normalMap The normal map of the affected node. This can also be specified as a property of the affected sprite.
127138
*
128139
* @return The CCEffectReflection object.
129140
*/
130-
+(id)effectWithEnvironment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap;
141+
+(id)effectWithShininess:(float)shininess environment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap;
131142

132143
/**
133144
* Creates a CCEffectReflection object with the specified parameters and nil normal map.
134145
*
146+
* @param shininess The overall shininess of the effect.
135147
* @param bias The bias term in the fresnel reflectance equation.
136148
* @param power The power term in the fresnel reflectance equation.
137149
* @param environment The environment image that will be reflected by the affected node.
138150
*
139151
* @return The CCEffectReflection object.
140152
*/
141-
+(id)effectWithFresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment;
153+
+(id)effectWithShininess:(float)shininess fresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment;
142154

143155
/**
144156
* Creates a CCEffectReflection object with the specified parameters.
145157
*
158+
* @param shininess The overall shininess of the effect.
146159
* @param bias The bias term in the fresnel reflectance equation.
147160
* @param power The power term in the fresnel reflectance equation.
148161
* @param environment The environment image that will be reflected by the affected node.
149162
* @param normalMap The normal map of the affected node. This can also be specified as a property of the affected sprite.
150163
*
151164
* @return The CCEffectReflection object.
152165
*/
153-
+(id)effectWithFresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap;
166+
+(id)effectWithShininess:(float)shininess fresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap;
154167

155168
@end

cocos2d/CCEffectReflection.m

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,28 @@ @implementation CCEffectReflection
3030

3131
-(id)init
3232
{
33-
return [self initWithEnvironment:nil];
33+
return [self initWithShininess:1.0f environment:nil];
3434
}
3535

36-
-(id)initWithEnvironment:(CCSprite *)environment
36+
-(id)initWithShininess:(float)shininess environment:(CCSprite *)environment
3737
{
38-
return [self initWithEnvironment:environment normalMap:nil];
38+
return [self initWithShininess:shininess environment:environment normalMap:nil];
3939
}
4040

41-
-(id)initWithEnvironment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap
41+
-(id)initWithShininess:(float)shininess environment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap
4242
{
43-
return [self initWithFresnelBias:1.0f fresnelPower:0.0f environment:environment normalMap:normalMap];
43+
return [self initWithShininess:shininess fresnelBias:1.0f fresnelPower:0.0f environment:environment normalMap:normalMap];
4444
}
4545

46-
-(id)initWithFresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment
46+
-(id)initWithShininess:(float)shininess fresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment
4747
{
48-
return [self initWithFresnelBias:bias fresnelPower:power environment:environment normalMap:nil];
48+
return [self initWithShininess:shininess fresnelBias:bias fresnelPower:power environment:environment normalMap:nil];
4949
}
5050

51-
-(id)initWithFresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap
51+
-(id)initWithShininess:(float)shininess fresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap
5252
{
5353
NSArray *uniforms = @[
54+
[CCEffectUniform uniform:@"float" name:@"u_shininess" value:[NSNumber numberWithFloat:1.0f]],
5455
[CCEffectUniform uniform:@"float" name:@"u_fresnelBias" value:[NSNumber numberWithFloat:1.0f]],
5556
[CCEffectUniform uniform:@"float" name:@"u_fresnelPower" value:[NSNumber numberWithFloat:0.0f]],
5657
[CCEffectUniform uniform:@"sampler2D" name:@"u_envMap" value:(NSValue*)[CCTexture none]],
@@ -63,6 +64,7 @@ -(id)initWithFresnelBias:(float)bias fresnelPower:(float)power environment:(CCSp
6364
{
6465
_environment = environment;
6566
_normalMap = normalMap;
67+
_shininess = shininess;
6668
_fresnelBias = bias;
6769
_fresnelPower = power;
6870

@@ -71,24 +73,24 @@ -(id)initWithFresnelBias:(float)bias fresnelPower:(float)power environment:(CCSp
7173
return self;
7274
}
7375

74-
+(id)effectWithEnvironment:(CCSprite *)environment
76+
+(id)effectWithShininess:(float)shininess environment:(CCSprite *)environment
7577
{
76-
return [[self alloc] initWithEnvironment:environment];
78+
return [[self alloc] initWithShininess:shininess environment:environment];
7779
}
7880

79-
+(id)effectWithEnvironment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap
81+
+(id)effectWithShininess:(float)shininess environment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap
8082
{
81-
return [[self alloc] initWithEnvironment:environment normalMap:normalMap];
83+
return [[self alloc] initWithShininess:shininess environment:environment normalMap:normalMap];
8284
}
8385

84-
+(id)effectWithFresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment
86+
+(id)effectWithShininess:(float)shininess fresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment
8587
{
86-
return [[self alloc] initWithFresnelBias:bias fresnelPower:power environment:environment];
88+
return [[self alloc] initWithShininess:shininess fresnelBias:bias fresnelPower:power environment:environment];
8789
}
8890

89-
+(id)effectWithFresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap
91+
+(id)effectWithShininess:(float)shininess fresnelBias:(float)bias fresnelPower:(float)power environment:(CCSprite *)environment normalMap:(CCSpriteFrame *)normalMap
9092
{
91-
return [[self alloc] initWithFresnelBias:bias fresnelPower:power environment:environment normalMap:normalMap];
93+
return [[self alloc] initWithShininess:shininess fresnelBias:bias fresnelPower:power environment:environment normalMap:normalMap];
9294
}
9395

9496
-(void)buildFragmentFunctions
@@ -130,7 +132,7 @@ -(void)buildFragmentFunctions
130132
// If the refracted texture coordinates are within the bounds of the environment map
131133
// blend the primary color with the refracted environment. Multiplying by the normal
132134
// map alpha also allows the effect to be disabled for specific pixels.
133-
primaryColor += normalMap.a * fresnel * texture2D(u_envMap, reflectTexCoords);
135+
primaryColor += normalMap.a * fresnel * u_shininess * texture2D(u_envMap, reflectTexCoords);
134136
return primaryColor;
135137
);
136138

@@ -162,6 +164,7 @@ -(void)buildRenderPasses
162164
pass.verts = verts;
163165
}
164166

167+
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_shininess"]] = [NSNumber numberWithFloat:weakSelf.shininess];
165168
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_fresnelBias"]] = [NSNumber numberWithFloat:weakSelf.fresnelBias];
166169
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_fresnelPower"]] = [NSNumber numberWithFloat:weakSelf.fresnelPower];
167170

0 commit comments

Comments
 (0)