From 6b86b638ec49728b2ea5fd897ef45efa411a673b Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 22 Apr 2016 10:15:20 +0800 Subject: [PATCH] add distortion sprite --- cocos2d/core/components/CCSpriteDistortion.js | 91 +++++++++++++++++++ cocos2d/core/components/index.js | 1 + cocos2d/core/sprites/CCScale9Sprite.js | 5 +- .../sprites/CCScale9SpriteWebGLRenderCmd.js | 59 +++++++++++- 4 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 cocos2d/core/components/CCSpriteDistortion.js diff --git a/cocos2d/core/components/CCSpriteDistortion.js b/cocos2d/core/components/CCSpriteDistortion.js new file mode 100644 index 0000000000..4af490fef1 --- /dev/null +++ b/cocos2d/core/components/CCSpriteDistortion.js @@ -0,0 +1,91 @@ +/**************************************************************************** + Copyright (c) 2013-2016 Chukong Technologies Inc. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Chukong Aipu reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +/** + * !#en A distortion used to change the rendering of simple sprite.If will take effect after sprite component is added. + * !#zh 扭曲效果组件,用于改变SIMPLE类型sprite的渲染,只有当sprite组件已经添加后,才能起作用. + * @class SpriteDistortion + * @extends Component + * @example + * // Create a new node and add sprite components. + * var node = new cc.Node("New Sprite"); + * var sprite = node.addComponent(cc.Sprite); + * var spriteDistortion = node.addComponent(cc.SpriteDistortion); + * node.parent = this.node; + */ + +var SpriteDistortion = cc.Class({ + name: 'cc.SpriteDistortion', extends: require('./CCComponent'), + editor: CC_EDITOR && { + menu: 'i18n:MAIN_MENU.component.renderers/SpriteDistortion', + executeInEditMode: true, + }, + + ctor: function() { + this._distortionOffset = cc.v2(0.0,0.0); + this._spriteSGNode = null; + }, + + properties: { + /** + * !#en Change the UV offset for distortion rendering. + * !#zh 在渲染时改变UV的整体偏移. + * @property offset + * @type {Vec2} + * @example + * distortion.offset = new cc.Vec2(0.5, 0.3);; + */ + offset: { + get: function() { + return this._distortionOffset; + }, + set:function(value) { + this._distortionOffset.x = value.x; + this._distortionOffset.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; + } + }, + + onDisable: function () { + if(this._spriteSGNode) { + this._spriteSGNode.setState(cc.Scale9Sprite.state.NORMAL); + } + + this._spriteSGNode = null; + }, + +}); + +cc.SpriteDistortion = module.exports = SpriteDistortion; diff --git a/cocos2d/core/components/index.js b/cocos2d/core/components/index.js index 027a97c615..e246995f45 100644 --- a/cocos2d/core/components/index.js +++ b/cocos2d/core/components/index.js @@ -43,4 +43,5 @@ module.exports = [ require('./CCScrollView'), require('./CCLayout'), require('./CCEditBox'), + require('./CCSpriteDistortion'), ]; diff --git a/cocos2d/core/sprites/CCScale9Sprite.js b/cocos2d/core/sprites/CCScale9Sprite.js index 1fd624cb4f..c50122d1d6 100644 --- a/cocos2d/core/sprites/CCScale9Sprite.js +++ b/cocos2d/core/sprites/CCScale9Sprite.js @@ -874,7 +874,8 @@ cc.Scale9Sprite = _ccsg.Node.extend({ //normalized filled start and range _fillStart: 0, _fillRange: Math.PI * 2, - + _distortionOffset: null, + ctor: function (textureOrSpriteFrame) { _ccsg.Node.prototype.ctor.call(this); this._renderCmd.setState(this._brightState); @@ -1249,7 +1250,7 @@ cc.defineGetterSetter(_p, "insetBottom", _p.getInsetBottom, _p.setInsetBottom); _p = null; -cc.Scale9Sprite.state = {NORMAL: 0, GRAY: 1}; +cc.Scale9Sprite.state = {NORMAL: 0, GRAY: 1, DISTORTION: 2}; /** * Enum for sprite type diff --git a/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js b/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js index 6d71fb7836..d7c482bb47 100644 --- a/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js +++ b/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js @@ -64,7 +64,10 @@ 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); + } ccgl.blendFunc(node._blendFunc.src, node._blendFunc.dst); //optimize performance for javascript ccgl.bindTexture2DN(0, locTexture); // = cc.gl.bindTexture2D(locTexture); @@ -139,6 +142,8 @@ if (_ccsg.Node.WebGLRenderCmd) { node.setShaderProgram(cc.shaderCache.programForKey(cc.macro.SHADER_POSITION_TEXTURECOLOR)); } else if (state === cc.Scale9Sprite.state.GRAY) { node.setShaderProgram(cc.Scale9Sprite.WebGLRenderCmd._getGrayShaderProgram()); + } else if (state === cc.Scale9Sprite.state.DISTORTION) { + node.setShaderProgram(cc.Scale9Sprite.WebGLRenderCmd._getDistortionProgram()); } }; @@ -170,4 +175,56 @@ if (_ccsg.Node.WebGLRenderCmd) { + " gl_FragColor.xyz = vec3(0.2126*c.r + 0.7152*c.g + 0.0722*c.b); \n" +" gl_FragColor.w = c.w ; \n" + "}"; + + cc.Scale9Sprite.WebGLRenderCmd._distortionProgram = null; + cc.Scale9Sprite.WebGLRenderCmd._getDistortionProgram = function(){ + var shader = cc.Scale9Sprite.WebGLRenderCmd._distortionProgram; + if(shader) + return shader; + + shader = new cc.GLProgram(); + shader.initWithVertexShaderByteArray(cc.PresetShaders.POSITION_TEXTURE_COLOR_VERT, distortionSpriteShader.fShader); + shader.addAttribute(cc.macro.ATTRIBUTE_NAME_POSITION, cc.macro.VERTEX_ATTRIB_POSITION); + shader.addAttribute(cc.macro.ATTRIBUTE_NAME_COLOR, cc.macro.VERTEX_ATTRIB_COLOR); + shader.addAttribute(cc.macro.ATTRIBUTE_NAME_TEX_COORD, cc.macro.VERTEX_ATTRIB_TEX_COORDS); + shader.link(); + shader.updateUniforms(); + + cc.Scale9Sprite.WebGLRenderCmd._distortionProgram = shader; + cc.Scale9Sprite.WebGLRenderCmd._distortionLocation = shader.getUniformLocationForName('u_offset'); + return shader; + }; + + var distortionSpriteShader = { + }; + distortionSpriteShader.shaderKey = 'cc.Sprite.Shader.Distortion'; + distortionSpriteShader.fShader = "precision lowp float;\n" + + "varying vec4 v_fragmentColor; \n" + + "varying vec2 v_texCoord; \n" + + "uniform vec2 u_offset; \n" + + "const float PI = 3.14159265359;\n" + + "void main() \n" + + "{ \n" + + "float halfPI = 0.5 * PI;\n" + + "float maxFactor = sin(halfPI);\n" + + "vec2 uv = v_texCoord;\n" + + "vec2 xy = 2.0 * uv.xy - 1.0;\n" + + "float d = length(xy);\n" + + "if (d < (2.0-maxFactor)) {\n" + + "d = length(xy * maxFactor);\n" + + "float z = sqrt(1.0 - d * d);\n" + + " float r = atan(d, z) / PI;\n" + + "float phi = atan(xy.y, xy.x);\n" + + "uv.x = r * cos(phi) + 0.5;\n" + + "uv.y = r * sin(phi) + 0.5;\n" + + "} 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 = fract(uv); \n" + + "gl_FragColor = v_fragmentColor * texture2D(CC_Texture0, uv);\n" + + "}"; }