diff --git a/cocos2d/core/components/CCSpriteDistortion.js b/cocos2d/core/components/CCSpriteDistortion.js index 19b5344151..3e81563f5d 100644 --- a/cocos2d/core/components/CCSpriteDistortion.js +++ b/cocos2d/core/components/CCSpriteDistortion.js @@ -46,6 +46,7 @@ var SpriteDistortion = cc.Class({ ctor: function() { this._distortionOffset = cc.v2(0.0,0.0); + this._distortionTiling = cc.v2(1.0,1.0); this._spriteSGNode = null; }, @@ -68,6 +69,23 @@ var SpriteDistortion = cc.Class({ } }, + /** + * !#en Change the UV scale for distortion rendering. + * !#zh 在渲染时改变UV的寻址系数 + * @property tiling + * @type {Vec2} + * @example + * distortion.tiling = new cc.Vec2(0.5, 0.3);; + */ + tiling: { + get: function() { + return this._distortionTiling; + }, + set:function(value) { + this._distortionTiling.x = value.x; + this._distortionTiling.y = value.y; + } + }, }, onEnable: function () { @@ -76,6 +94,7 @@ var SpriteDistortion = cc.Class({ if(this._spriteSGNode) { sgNode.setState(cc.Scale9Sprite.state.DISTORTION); sgNode._distortionOffset = this._distortionOffset; + sgNode._distortionTiling = this._distortionTiling; } }, diff --git a/cocos2d/core/sprites/CCScale9Sprite.js b/cocos2d/core/sprites/CCScale9Sprite.js index c50122d1d6..255ccaa790 100644 --- a/cocos2d/core/sprites/CCScale9Sprite.js +++ b/cocos2d/core/sprites/CCScale9Sprite.js @@ -875,7 +875,8 @@ cc.Scale9Sprite = _ccsg.Node.extend({ _fillStart: 0, _fillRange: Math.PI * 2, _distortionOffset: null, - + _distortionTiling: null, + ctor: function (textureOrSpriteFrame) { _ccsg.Node.prototype.ctor.call(this); this._renderCmd.setState(this._brightState); diff --git a/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js b/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js index d7c482bb47..6d63790753 100644 --- a/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js +++ b/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js @@ -65,8 +65,14 @@ if (_ccsg.Node.WebGLRenderCmd) { this._shaderProgram.use(); this._shaderProgram._setUniformForMVPMatrixWithMat4(this._stackMatrix); if(this._shaderProgram === cc.Scale9Sprite.WebGLRenderCmd._distortionProgram && this._node._distortionOffset) { - this._shaderProgram.setUniformLocationWith2f(cc.Scale9Sprite.WebGLRenderCmd._distortionLocation, - this._node._distortionOffset.x, this._node._distortionOffset.y); + this._shaderProgram.setUniformLocationWith2f( + cc.Scale9Sprite.WebGLRenderCmd._distortionOffset, + this._node._distortionOffset.x, this._node._distortionOffset.y + ); + this._shaderProgram.setUniformLocationWith2f( + cc.Scale9Sprite.WebGLRenderCmd._distortionTiling, + this._node._distortionTiling.x, this._node._distortionTiling.y + ); } ccgl.blendFunc(node._blendFunc.src, node._blendFunc.dst); //optimize performance for javascript @@ -191,7 +197,8 @@ if (_ccsg.Node.WebGLRenderCmd) { shader.updateUniforms(); cc.Scale9Sprite.WebGLRenderCmd._distortionProgram = shader; - cc.Scale9Sprite.WebGLRenderCmd._distortionLocation = shader.getUniformLocationForName('u_offset'); + cc.Scale9Sprite.WebGLRenderCmd._distortionOffset = shader.getUniformLocationForName('u_offset'); + cc.Scale9Sprite.WebGLRenderCmd._distortionTiling = shader.getUniformLocationForName('u_offset_tiling'); return shader; }; @@ -202,6 +209,7 @@ if (_ccsg.Node.WebGLRenderCmd) { + "varying vec4 v_fragmentColor; \n" + "varying vec2 v_texCoord; \n" + "uniform vec2 u_offset; \n" + + "uniform vec2 u_offset_tiling; \n" + "const float PI = 3.14159265359;\n" + "void main() \n" + "{ \n" @@ -220,10 +228,7 @@ if (_ccsg.Node.WebGLRenderCmd) { + "} else {\n" + "discard;\n" + "}\n" - + "uv.x /= 0.5;\n" - + "uv.y /= 0.5;\n" - + "uv.x += u_offset.x;\n" - + "uv.y += u_offset.y;\n" + + "uv = uv * u_offset_tiling + u_offset;\n" + "uv = fract(uv); \n" + "gl_FragColor = v_fragmentColor * texture2D(CC_Texture0, uv);\n" + "}";