From 68ca51085f07cb6b9d26f384f02242d55a76c7a9 Mon Sep 17 00:00:00 2001 From: Johnny Wu Date: Mon, 25 Apr 2016 10:20:11 +0800 Subject: [PATCH 1/2] add distortion scale --- cocos2d/core/components/CCSpriteDistortion.js | 23 +++++++++++++++++++ cocos2d/core/sprites/CCScale9Sprite.js | 1 + .../sprites/CCScale9SpriteWebGLRenderCmd.js | 19 +++++++++------ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/cocos2d/core/components/CCSpriteDistortion.js b/cocos2d/core/components/CCSpriteDistortion.js index 19b5344151..7497d303c6 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._distortionScale = cc.v2(1.0,1.0); this._spriteSGNode = null; }, @@ -70,12 +71,34 @@ var SpriteDistortion = cc.Class({ }, + properties: { + /** + * !#en Change the UV offset for distortion rendering. + * !#zh 在渲染时改变UV的整体偏移. + * @property offset + * @type {Vec2} + * @example + * distortion.scale = new cc.Vec2(0.5, 0.3);; + */ + scale: { + get: function() { + return this._distortionScale; + }, + set:function(value) { + this._distortionScale.x = value.x; + this._distortionScale.y = value.y; + } + }, + + }, + onEnable: function () { var sprite = this.node.getComponent('cc.Sprite'); var sgNode = this._spriteSGNode = sprite && sprite._sgNode; if(this._spriteSGNode) { sgNode.setState(cc.Scale9Sprite.state.DISTORTION); sgNode._distortionOffset = this._distortionOffset; + sgNode._distortionScale = this._distortionScale; } }, diff --git a/cocos2d/core/sprites/CCScale9Sprite.js b/cocos2d/core/sprites/CCScale9Sprite.js index c50122d1d6..b007685c11 100644 --- a/cocos2d/core/sprites/CCScale9Sprite.js +++ b/cocos2d/core/sprites/CCScale9Sprite.js @@ -875,6 +875,7 @@ cc.Scale9Sprite = _ccsg.Node.extend({ _fillStart: 0, _fillRange: Math.PI * 2, _distortionOffset: null, + _distortionScale: null, ctor: function (textureOrSpriteFrame) { _ccsg.Node.prototype.ctor.call(this); diff --git a/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js b/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js index d7c482bb47..ba3a3af780 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._distortionScale, + this._node._distortionScale.x, this._node._distortionScale.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._distortionScale = shader.getUniformLocationForName('u_offset_scale'); 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_scale; \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_scale + u_offset;\n" + "uv = fract(uv); \n" + "gl_FragColor = v_fragmentColor * texture2D(CC_Texture0, uv);\n" + "}"; From 41ca344fe1b2cfa3463f191eaba98e650bcad3ab Mon Sep 17 00:00:00 2001 From: Johnny Wu Date: Mon, 25 Apr 2016 10:30:01 +0800 Subject: [PATCH 2/2] fix properties define syntax error --- cocos2d/core/components/CCSpriteDistortion.js | 24 ++++++++----------- cocos2d/core/sprites/CCScale9Sprite.js | 4 ++-- .../sprites/CCScale9SpriteWebGLRenderCmd.js | 10 ++++---- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/cocos2d/core/components/CCSpriteDistortion.js b/cocos2d/core/components/CCSpriteDistortion.js index 7497d303c6..3e81563f5d 100644 --- a/cocos2d/core/components/CCSpriteDistortion.js +++ b/cocos2d/core/components/CCSpriteDistortion.js @@ -46,7 +46,7 @@ var SpriteDistortion = cc.Class({ ctor: function() { this._distortionOffset = cc.v2(0.0,0.0); - this._distortionScale = cc.v2(1.0,1.0); + this._distortionTiling = cc.v2(1.0,1.0); this._spriteSGNode = null; }, @@ -69,27 +69,23 @@ var SpriteDistortion = cc.Class({ } }, - }, - - properties: { /** - * !#en Change the UV offset for distortion rendering. - * !#zh 在渲染时改变UV的整体偏移. - * @property offset + * !#en Change the UV scale for distortion rendering. + * !#zh 在渲染时改变UV的寻址系数 + * @property tiling * @type {Vec2} * @example - * distortion.scale = new cc.Vec2(0.5, 0.3);; + * distortion.tiling = new cc.Vec2(0.5, 0.3);; */ - scale: { + tiling: { get: function() { - return this._distortionScale; + return this._distortionTiling; }, set:function(value) { - this._distortionScale.x = value.x; - this._distortionScale.y = value.y; + this._distortionTiling.x = value.x; + this._distortionTiling.y = value.y; } }, - }, onEnable: function () { @@ -98,7 +94,7 @@ var SpriteDistortion = cc.Class({ if(this._spriteSGNode) { sgNode.setState(cc.Scale9Sprite.state.DISTORTION); sgNode._distortionOffset = this._distortionOffset; - sgNode._distortionScale = this._distortionScale; + sgNode._distortionTiling = this._distortionTiling; } }, diff --git a/cocos2d/core/sprites/CCScale9Sprite.js b/cocos2d/core/sprites/CCScale9Sprite.js index b007685c11..255ccaa790 100644 --- a/cocos2d/core/sprites/CCScale9Sprite.js +++ b/cocos2d/core/sprites/CCScale9Sprite.js @@ -875,8 +875,8 @@ cc.Scale9Sprite = _ccsg.Node.extend({ _fillStart: 0, _fillRange: Math.PI * 2, _distortionOffset: null, - _distortionScale: 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 ba3a3af780..6d63790753 100644 --- a/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js +++ b/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js @@ -70,8 +70,8 @@ if (_ccsg.Node.WebGLRenderCmd) { this._node._distortionOffset.x, this._node._distortionOffset.y ); this._shaderProgram.setUniformLocationWith2f( - cc.Scale9Sprite.WebGLRenderCmd._distortionScale, - this._node._distortionScale.x, this._node._distortionScale.y + cc.Scale9Sprite.WebGLRenderCmd._distortionTiling, + this._node._distortionTiling.x, this._node._distortionTiling.y ); } ccgl.blendFunc(node._blendFunc.src, node._blendFunc.dst); @@ -198,7 +198,7 @@ if (_ccsg.Node.WebGLRenderCmd) { cc.Scale9Sprite.WebGLRenderCmd._distortionProgram = shader; cc.Scale9Sprite.WebGLRenderCmd._distortionOffset = shader.getUniformLocationForName('u_offset'); - cc.Scale9Sprite.WebGLRenderCmd._distortionScale = shader.getUniformLocationForName('u_offset_scale'); + cc.Scale9Sprite.WebGLRenderCmd._distortionTiling = shader.getUniformLocationForName('u_offset_tiling'); return shader; }; @@ -209,7 +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_scale; \n" + + "uniform vec2 u_offset_tiling; \n" + "const float PI = 3.14159265359;\n" + "void main() \n" + "{ \n" @@ -228,7 +228,7 @@ if (_ccsg.Node.WebGLRenderCmd) { + "} else {\n" + "discard;\n" + "}\n" - + "uv = uv * u_offset_scale + u_offset;\n" + + "uv = uv * u_offset_tiling + u_offset;\n" + "uv = fract(uv); \n" + "gl_FragColor = v_fragmentColor * texture2D(CC_Texture0, uv);\n" + "}";