Quantcast
Channel: Cocos Forums - Latest topics
Viewing all articles
Browse latest Browse all 17083

Sprite position issue when using setShaderProgram

$
0
0

@supersuraccoon1 wrote:

I'm using the latest cocos2d-x v3.9 (JSBinding).

And here is a code (add a sprite and attach with a simple gray shader):

this.winSize = cc.director.getWinSize();

var sprite = new cc.Sprite(res.png_building_3);
sprite.setPosition(this.winSize.width / 2, this.winSize.height / 2);

var shaderProgram = new cc.GLProgram();
shaderProgram.init("GrayScaleShader.vsh", "GrayScaleShader.fsh");
shaderProgram.addAttribute(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION);
shaderProgram.addAttribute(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS);
shaderProgram.link();
shaderProgram.updateUniforms();

sprite.setShaderProgram(shaderProgram);
this.addChild(sprite);

And here is the shader vsh:

attribute vec4 a_position;
attribute vec2 a_texCoord;

#ifdef GL_ES
varying mediump vec2 v_texCoord;
#else
varying vec2 v_texCoord;
#endif

void main()
{
    gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position;
	v_texCoord = a_texCoord;
}

and fsh:

#ifdef GL_ES
precision lowp float;
#endif

varying vec2 v_texCoord;

void main(void) {
	vec4 normalColor = texture2D(CC_Texture0, v_texCoord).rgba;
	float grayColor = dot(normalColor.rgb, vec3(0.299, 0.587, 0.114));
	gl_FragColor = vec4(grayColor, grayColor, grayColor, normalColor.a);
}

This code works fine in Browser, I can see a gray sprite in the center of the screen.

But in Mac and iOS (JSBinding) the sprite is gray but the position of the sprite is in the right top of the screen (not in the center where it should be).

Not sure what is going wrong here, any advice will be appreciated, thanks smile

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 17083

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>