@@ -235,8 +235,8 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
235
235
static CCUniformSetter
236
236
SetFloat (NSString *name, GLint location)
237
237
{
238
- return ^(CCRenderer *renderer, NSNumber *value ){
239
- value = value ?: @ 0 ;
238
+ return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms ){
239
+ NSNumber * value = shaderUniforms[name] ?: globalShaderUniforms[name] ?: @( 0.0 ) ;
240
240
NSCAssert ([value isKindOfClass: [NSNumber class ]], @" Shader uniform '%@ ' value must be wrapped in a NSNumber." , name);
241
241
242
242
glUniform1f (location, value.floatValue );
@@ -246,8 +246,29 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
246
246
static CCUniformSetter
247
247
SetVec2 (NSString *name, GLint location)
248
248
{
249
- return ^(CCRenderer *renderer, NSValue *value){
250
- value = value ?: [NSValue valueWithGLKVector2: GLKVector2Make (0 .0f , 0 .0f )];
249
+ NSString *textureName = nil ;
250
+ bool pixelSize = [name hasSuffix: @" PixelSize" ];
251
+ if (pixelSize){
252
+ textureName = [name substringToIndex: name.length - @" PixelSize" .length];
253
+ } else if ([name hasSuffix: @" Size" ]){
254
+ textureName = [name substringToIndex: name.length - @" Size" .length];
255
+ }
256
+
257
+ return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms){
258
+ NSValue *value = shaderUniforms[name] ?: globalShaderUniforms[name];
259
+
260
+ // Fall back on looking up the actual texture size if the name matches a texture.
261
+ if (value == nil && textureName){
262
+ CCTexture *texture = shaderUniforms[textureName] ?: globalShaderUniforms[textureName];
263
+ GLKVector2 sizeInPixels = GLKVector2Make (texture.pixelWidth , texture.pixelHeight );
264
+
265
+ GLKVector2 size = GLKVector2MultiplyScalar (sizeInPixels, pixelSize ? 1.0 : 1.0 /texture.contentScale );
266
+ value = [NSValue valueWithGLKVector2: size];
267
+ }
268
+
269
+ // Finally fall back on 0.
270
+ if (value == nil ) value = [NSValue valueWithGLKVector2: GLKVector2Make (0 .0f , 0 .0f )];
271
+
251
272
NSCAssert ([value isKindOfClass: [NSValue class ]], @" Shader uniform '%@ ' value must be wrapped in a NSValue." , name);
252
273
253
274
if (strcmp (value.objCType , @encode (GLKVector2)) == 0 ){
@@ -268,8 +289,8 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
268
289
static CCUniformSetter
269
290
SetVec3 (NSString *name, GLint location)
270
291
{
271
- return ^(CCRenderer *renderer, NSValue *value ){
272
- value = value ?: [NSValue valueWithGLKVector3: GLKVector3Make (0 .0f , 0 .0f , 0 .0f )];
292
+ return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms ){
293
+ NSValue * value = shaderUniforms[name] ?: globalShaderUniforms[name] ?: [NSValue valueWithGLKVector3: GLKVector3Make (0 .0f , 0 .0f , 0 .0f )];
273
294
NSCAssert ([value isKindOfClass: [NSValue class ]], @" Shader uniform '%@ ' value must be wrapped in a NSValue." , name);
274
295
NSCAssert (strcmp(value.objCType, @encode (GLKVector3)) == 0, @"Shader uniformm 'vec3 %@' value must be passed using [NSValue valueWithGLKVector3:]", name);
275
296
@@ -281,8 +302,8 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
281
302
static CCUniformSetter
282
303
SetVec4 (NSString *name, GLint location)
283
304
{
284
- return ^(CCRenderer *renderer, id value ){
285
- value = value ?: [NSValue valueWithGLKVector4: GLKVector4Make (0 .0f , 0 .0f , 0 .0f , 1 .0f )];
305
+ return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms ){
306
+ NSValue * value = shaderUniforms[name] ?: globalShaderUniforms[name] ?: [NSValue valueWithGLKVector4: GLKVector4Make (0 .0f , 0 .0f , 0 .0f , 1 .0f )];
286
307
287
308
if ([value isKindOfClass: [NSValue class ]]){
288
309
NSCAssert (strcmp([(NSValue *)value objCType ], @encode (GLKVector4)) == 0, @"Shader uniformm 'vec4 %@' value must be passed using [NSValue valueWithGLKVector4:].", name);
@@ -301,8 +322,8 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
301
322
static CCUniformSetter
302
323
SetMat4 (NSString *name, GLint location)
303
324
{
304
- return ^(CCRenderer *renderer, NSValue *value ){
305
- value = value ?: [NSValue valueWithGLKMatrix4: GLKMatrix4Identity];
325
+ return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms ){
326
+ NSValue * value = shaderUniforms[name] ?: globalShaderUniforms[name] ?: [NSValue valueWithGLKMatrix4: GLKMatrix4Identity];
306
327
NSCAssert ([value isKindOfClass: [NSValue class ]], @" Shader uniform '%@ ' value must be wrapped in a NSValue." , name);
307
328
NSCAssert (strcmp(value.objCType, @encode (GLKMatrix4)) == 0, @"Shader uniformm 'mat4 %@' value must be passed using [NSValue valueWithGLKMatrix4:]", name);
308
329
@@ -344,8 +365,8 @@ -(NSDictionary *)uniformSettersForProgram:(GLuint)program
344
365
case GL_FLOAT_MAT4: uniformSetters[name] = SetMat4 (name, location); break ;
345
366
case GL_SAMPLER_2D: {
346
367
// Sampler setters are handled a differently since the real work is binding the texture and not setting the uniform value.
347
- uniformSetters[name] = ^(CCRenderer *renderer, CCTexture *texture ){
348
- texture = texture ?: [CCTexture none ];
368
+ uniformSetters[name] = ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms ){
369
+ CCTexture * texture = shaderUniforms[name] ?: globalShaderUniforms[name] ?: [CCTexture none ];
349
370
NSAssert ([texture isKindOfClass: [CCTexture class ]], @" Shader uniform '%@ ' value must be a CCTexture object." , name);
350
371
351
372
// Bind the texture to the texture unit for the uniform.
0 commit comments