From 2e73f631568aef097cdb8c1741936cd30321f189 Mon Sep 17 00:00:00 2001 From: elsanide Date: Tue, 10 Jun 2014 18:15:07 +0900 Subject: [PATCH 01/30] #5506 bug fix when Director->convertToUI() called, returned wrong value. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit kazmath code (under 3.0 version.) //******************************************************** kmVec3* kmVec3TransformCoord(kmVec3* pOut, const kmVec3* pV, const kmMat4* pM) { /* a = (Vx, Vy, Vz, 1) b = (a×M)T Out = 1 ⁄ bw(bx, by, bz) */ kmVec4 v; kmVec4 inV; kmVec4Fill(&inV, pV->x, pV->y, pV->z, 1.0); kmVec4Transform(&v, &inV,pM); pOut->x = v.x / v.w; pOut->y = v.y / v.w; pOut->z = v.z / v.w; return pOut; } //******************************************************** Mat4.h & MathUtil version (3.1 or later) //******************************************************** inline void MathUtil::transformVec4(const float* m, const float* v, float* dst) { // Handle case where v == dst. float x = v[0] * m[0] + v[1] * m[4] + v[2] * m[8] + v[3] * m[12]; float y = v[0] * m[1] + v[1] * m[5] + v[2] * m[9] + v[3] * m[13]; float z = v[0] * m[2] + v[1] * m[6] + v[2] * m[10] + v[3] * m[14]; float w = v[0] * m[3] + v[1] * m[7] + v[2] * m[11] + v[3] * m[15]; dst[0] = x; dst[1] = y; dst[2] = z; dst[3] = w; } //**************************************************** Transforms 3-D vector or an array of 3-D vectors using a given matrix, projecting the result back into w = 1. but, it is not apply w = 1. --- cocos/base/CCDirector.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index 9d1e02f187..ac7519a062 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -1,4 +1,4 @@ -/**************************************************************************** +/**************************************************************************** Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2010-2013 cocos2d-x.org Copyright (c) 2011 Zynga Inc. @@ -763,6 +763,18 @@ Vec2 Director::convertToUI(const Vec2& glPoint) Vec4 glCoord(glPoint.x, glPoint.y, 0.0, 1); transform.transformVector(glCoord, &clipCoord); + /* + BUG-FIX #5506 + + a = (Vx, Vy, Vz, 1) + b = (a×M)T + Out = 1 ⁄ bw(bx, by, bz) + */ + + clipCoord.x = clipCoord.x / clipCoord.w; + clipCoord.y = clipCoord.y / clipCoord.w; + clipCoord.z = clipCoord.z / clipCoord.w; + Size glSize = _openGLView->getDesignResolutionSize(); float factor = 1.0/glCoord.w; return Vec2(glSize.width*(clipCoord.x*0.5 + 0.5) * factor, glSize.height*(-clipCoord.y*0.5 + 0.5) * factor); From 8ba4c7a083426e42122d263799b3d1476d249dc1 Mon Sep 17 00:00:00 2001 From: Sachin Garg Date: Tue, 10 Jun 2014 17:59:15 +0530 Subject: [PATCH 02/30] GLProgram should not abort() if shader compilation fails, returning false will allow app to show custom error message to user (or use other simple shaders if a complicated shader fails to compile on some device). --- cocos/renderer/CCGLProgram.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/cocos/renderer/CCGLProgram.cpp b/cocos/renderer/CCGLProgram.cpp index e198cfa2c4..a95e7e714a 100644 --- a/cocos/renderer/CCGLProgram.cpp +++ b/cocos/renderer/CCGLProgram.cpp @@ -138,9 +138,17 @@ GLProgram::~GLProgram() { CCLOGINFO("%s %d deallocing GLProgram: %p", __FUNCTION__, __LINE__, this); - // there is no need to delete the shaders. They should have been already deleted. - CCASSERT(_vertShader == 0, "Vertex Shaders should have been already deleted"); - CCASSERT(_fragShader == 0, "Fragment Shaders should have been already deleted"); + if (_vertShader) + { + glDeleteShader(_vertShader); + } + + if (_fragShader) + { + glDeleteShader(_fragShader); + } + + _vertShader = _fragShader = 0; if (_program) { @@ -436,7 +444,7 @@ bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source } free(src); - abort(); + return false;; } return (status == GL_TRUE); } From e2c75c1f113002babf0b108a128fc1cda2ccef39 Mon Sep 17 00:00:00 2001 From: vision Date: Wed, 11 Jun 2014 14:54:14 +0800 Subject: [PATCH 03/30] New shader for the blur effect, the old one is ineffecient and difficult to maintain. --- .../Classes/ShaderTest/ShaderTest.cpp | 64 +++------------ .../Classes/ShaderTest/ShaderTest2.cpp | 71 +++------------- .../Resources/Shaders/example_Blur.fsh | 82 ++++++++----------- 3 files changed, 59 insertions(+), 158 deletions(-) diff --git a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp index 9aa84d87e0..07d764601b 100644 --- a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp +++ b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp @@ -421,22 +421,14 @@ class SpriteBlur : public Sprite { public: ~SpriteBlur(); - void setBlurSize(float f); + void setBlurRadius(float radius); bool initWithTexture(Texture2D* texture, const Rect& rect); void initGLProgram(); static SpriteBlur* create(const char *pszFileName); protected: - - int _blurRadius; - Vec2 _pixelSize; - - int _samplingRadius; - //gaussian = cons * exp( (dx*dx + dy*dy) * scale); - float _scale; - float _cons; - float _weightSum; + float _blurRadius; }; SpriteBlur::~SpriteBlur() @@ -472,14 +464,7 @@ bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect) _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); #endif - auto s = getTexture()->getContentSizeInPixels(); - - _pixelSize = Vec2(1/s.width, 1/s.height); - - _samplingRadius = 0; - this->initGLProgram(); - - getGLProgramState()->setUniformVec2("onePixelSize", _pixelSize); + initGLProgram(); return true; } @@ -495,43 +480,16 @@ void SpriteBlur::initGLProgram() auto glProgramState = GLProgramState::getOrCreateWithGLProgram(program); setGLProgramState(glProgramState); + + auto size = getTexture()->getContentSizeInPixels(); + getGLProgramState()->setUniformVec2("resolution", size); + getGLProgramState()->setUniformFloat("blurRadius", _blurRadius); } -void SpriteBlur::setBlurSize(float f) +void SpriteBlur::setBlurRadius(float radius) { - if(_blurRadius == (int)f) - return; - _blurRadius = (int)f; - - _samplingRadius = _blurRadius; - if (_samplingRadius > 10) - { - _samplingRadius = 10; - } - if (_blurRadius > 0) - { - float sigma = _blurRadius / 2.0f; - _scale = -0.5f / (sigma * sigma); - _cons = -1.0f * _scale / 3.141592f; - _weightSum = -_cons; - - float weight; - int squareX; - for(int dx = 0; dx <= _samplingRadius; ++dx) - { - squareX = dx * dx; - weight = _cons * exp(squareX * _scale); - _weightSum += 2.0 * weight; - for (int dy = 1; dy <= _samplingRadius; ++dy) - { - weight = _cons * exp((squareX + dy * dy) * _scale); - _weightSum += 4.0 * weight; - } - } - } - log("_blurRadius:%d",_blurRadius); - - getGLProgramState()->setUniformVec4("gaussianCoefficient", Vec4(_samplingRadius, _scale, _cons, _weightSum)); + _blurRadius = radius; + getGLProgramState()->setUniformFloat("blurRadius", _blurRadius); } // ShaderBlur @@ -597,7 +555,7 @@ bool ShaderBlur::init() void ShaderBlur::sliderAction(Ref* sender, Control::EventType controlEvent) { ControlSlider* slider = (ControlSlider*)sender; - _blurSprite->setBlurSize(slider->getValue()); + _blurSprite->setBlurRadius(slider->getValue()); } // ShaderRetroEffect diff --git a/tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp b/tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp index dfd250e781..f406ae8cd7 100644 --- a/tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp +++ b/tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp @@ -249,80 +249,33 @@ class EffectBlur : public Effect { public: CREATE_FUNC(EffectBlur); - virtual void setTarget(EffectSprite *sprite) override; - - void setGaussian(float value); - void setCustomUniforms(); - void setBlurSize(float f); + void setBlurRadius(float radius); protected: - bool init(float blurSize=3.0); - - int _blurRadius; - Vec2 _pixelSize; - - int _samplingRadius; - float _scale; - float _cons; - float _weightSum; + bool init(float blurRadius = 10.0f); + + float _blurRadius; }; void EffectBlur::setTarget(EffectSprite *sprite) { - Size s = sprite->getTexture()->getContentSizeInPixels(); - _pixelSize = Vec2(1/s.width, 1/s.height); - _glprogramstate->setUniformVec2("onePixelSize", _pixelSize); + Size size = sprite->getTexture()->getContentSizeInPixels(); + _glprogramstate->setUniformVec2("resolution", size); + _glprogramstate->setUniformFloat("blurRadius", _blurRadius); } -bool EffectBlur::init(float blurSize) +bool EffectBlur::init(float blurRadius) { initGLProgramState("Shaders/example_Blur.fsh"); - auto s = Size(100,100); - - _blurRadius = 0; - _pixelSize = Vec2(1/s.width, 1/s.height); - _samplingRadius = 0; - - setBlurSize(blurSize); - - _glprogramstate->setUniformVec2("onePixelSize", _pixelSize); - _glprogramstate->setUniformVec4("gaussianCoefficient", Vec4(_samplingRadius, _scale, _cons, _weightSum)); + _blurRadius = blurRadius; + return true; } -void EffectBlur::setBlurSize(float f) +void EffectBlur::setBlurRadius(float radius) { - if(_blurRadius == (int)f) - return; - _blurRadius = (int)f; - - _samplingRadius = _blurRadius; - if (_samplingRadius > 10) - { - _samplingRadius = 10; - } - if (_blurRadius > 0) - { - float sigma = _blurRadius / 2.0f; - _scale = -0.5f / (sigma * sigma); - _cons = -1.0f * _scale / 3.141592f; - _weightSum = -_cons; - - float weight; - int squareX; - for(int dx = 0; dx <= _samplingRadius; ++dx) - { - squareX = dx * dx; - weight = _cons * exp(squareX * _scale); - _weightSum += 2.0 * weight; - for (int dy = 1; dy <= _samplingRadius; ++dy) - { - weight = _cons * exp((squareX + dy * dy) * _scale); - _weightSum += 4.0 * weight; - } - } - } + _blurRadius = radius; } // Outline diff --git a/tests/cpp-tests/Resources/Shaders/example_Blur.fsh b/tests/cpp-tests/Resources/Shaders/example_Blur.fsh index 169795bc0e..c0f94933cd 100644 --- a/tests/cpp-tests/Resources/Shaders/example_Blur.fsh +++ b/tests/cpp-tests/Resources/Shaders/example_Blur.fsh @@ -1,5 +1,3 @@ -// Shader taken from: http://webglsamples.googlecode.com/hg/electricflower/electricflower.html - #ifdef GL_ES precision mediump float; #endif @@ -7,50 +5,42 @@ precision mediump float; varying vec4 v_fragmentColor; varying vec2 v_texCoord; -uniform vec4 gaussianCoefficient; -uniform vec2 onePixelSize; +uniform vec2 resolution; +uniform float blurRadius; -void main() { - if(gaussianCoefficient.x > 0.0) { - vec4 sum = vec4(0.0); - vec2 offset; - float weight; - float squareX; - - for(float dx = 0.0; dx <= gaussianCoefficient.x; dx += 1.0) { - squareX = dx * dx; - weight = gaussianCoefficient.z * exp(squareX * gaussianCoefficient.y); - - offset.x = -dx * onePixelSize.x; - offset.y = 0.0; - sum += texture2D(CC_Texture0, v_texCoord + offset) * weight; - - offset.x = dx * onePixelSize.x; - sum += texture2D(CC_Texture0, v_texCoord + offset) * weight; - - for(float dy = 1.0; dy <= gaussianCoefficient.x; dy += 1.0) { - weight = gaussianCoefficient.z * exp((squareX + dy * dy) * gaussianCoefficient.y); - - offset.x = -dx * onePixelSize.x; - offset.y = -dy * onePixelSize.y; - sum += texture2D(CC_Texture0, v_texCoord + offset) * weight; - - offset.y = dy * onePixelSize.y; - sum += texture2D(CC_Texture0, v_texCoord + offset) * weight; - - offset.x = dx * onePixelSize.x; - sum += texture2D(CC_Texture0, v_texCoord + offset) * weight; - - offset.y = -dy * onePixelSize.y; - sum += texture2D(CC_Texture0, v_texCoord + offset) * weight; - } - } - sum -= texture2D(CC_Texture0, v_texCoord) * gaussianCoefficient.z; - sum /= gaussianCoefficient.w; - gl_FragColor = sum * v_fragmentColor; - } - else { - gl_FragColor = texture2D(CC_Texture0, v_texCoord) * v_fragmentColor; - } +vec3 blur(vec2); + +void main(void) +{ + vec3 col = blur(v_texCoord); + gl_FragColor = vec4(col, 1.0); +} + +vec3 blur(vec2 p) +{ + if (blurRadius > 0.0) + { + vec3 col = vec3(0); + vec2 unit = 1.0 / resolution.xy; + + float r = blurRadius; + float sampleStep = r / 7.37; + + float count = 0.0; + + for(float x = -r; x < r; x += sampleStep) + { + for(float y = -r; y < r; y += sampleStep) + { + float weight = (r - abs(x)) * (r - abs(y)); + col += texture2D(CC_Texture0, p + vec2(x * unit.x, y * unit.y)).rgb * weight; + count += weight; + } + } + + return col / count; + } + + return texture2D(CC_Texture0, p).rgb; } From 63a68632a5b97a2d2292cff7a70a809564d3d7c0 Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Wed, 11 Jun 2014 21:49:08 +0800 Subject: [PATCH 04/30] fixed calculating height of multi-line string was incorrect on IOS. --- cocos/platform/ios/CCDevice.mm | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/cocos/platform/ios/CCDevice.mm b/cocos/platform/ios/CCDevice.mm index 5eeaf7ccaa..9246327882 100644 --- a/cocos/platform/ios/CCDevice.mm +++ b/cocos/platform/ios/CCDevice.mm @@ -215,26 +215,14 @@ static inline void lazyCheckIOS7() static CGSize _calculateStringSize(NSString *str, id font, CGSize *constrainSize) { - NSArray *listItems = [str componentsSeparatedByString: @"\n"]; - CGSize dim = CGSizeZero; CGSize textRect = CGSizeZero; textRect.width = constrainSize->width > 0 ? constrainSize->width : 0x7fffffff; textRect.height = constrainSize->height > 0 ? constrainSize->height : 0x7fffffff; - for (NSString *s in listItems) - { - CGSize tmp = [s sizeWithFont:font constrainedToSize:textRect]; - - if (tmp.width > dim.width) - { - dim.width = tmp.width; - } - - dim.height += tmp.height; - } - + CGSize dim = [s sizeWithFont:font constrainedToSize:textRect]; + dim.width = ceilf(dim.width); dim.height = ceilf(dim.height); From 7cc38792578df1b49bece114ea73ca8812144d85 Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Wed, 11 Jun 2014 22:03:10 +0800 Subject: [PATCH 05/30] Ensuring release cache texture in timely. --- cocos/2d/CCLabel.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index 39ef02f014..d01925bbd0 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -392,16 +392,12 @@ void Label::setFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = false * if (_reusedLetter == nullptr) { - _reusedLetter = Sprite::createWithTexture(_fontAtlas->getTexture(0)); + _reusedLetter = Sprite::create(); _reusedLetter->setOpacityModifyRGB(_isOpacityModifyRGB); _reusedLetter->retain(); _reusedLetter->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT); - _reusedLetter->setBatchNode(this); - } - else - { - _reusedLetter->setTexture(_fontAtlas->getTexture(0)); } + _reusedLetter->setBatchNode(this); if (_fontAtlas) { From 775832a710e24788e4b1748caf23cd5abc3c006f Mon Sep 17 00:00:00 2001 From: Dale Stammen Date: Wed, 11 Jun 2014 13:18:10 -0700 Subject: [PATCH 06/30] added missing files --- cocos/2d/cocos2d_wp8.vcxproj | 2 ++ cocos/2d/cocos2d_wp8.vcxproj.filters | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/cocos/2d/cocos2d_wp8.vcxproj b/cocos/2d/cocos2d_wp8.vcxproj index af64540683..37a477357f 100644 --- a/cocos/2d/cocos2d_wp8.vcxproj +++ b/cocos/2d/cocos2d_wp8.vcxproj @@ -280,6 +280,7 @@ + @@ -476,6 +477,7 @@ + diff --git a/cocos/2d/cocos2d_wp8.vcxproj.filters b/cocos/2d/cocos2d_wp8.vcxproj.filters index e7f75c3fd6..010792145b 100644 --- a/cocos/2d/cocos2d_wp8.vcxproj.filters +++ b/cocos/2d/cocos2d_wp8.vcxproj.filters @@ -593,6 +593,9 @@ renderer + + base + @@ -1205,6 +1208,9 @@ renderer + + base + From b26853638540ed8ad8dd8e4bc156f51cf6547b37 Mon Sep 17 00:00:00 2001 From: LinWenhai Date: Thu, 12 Jun 2014 09:46:18 +0800 Subject: [PATCH 07/30] fixed typo. --- cocos/platform/ios/CCDevice.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/platform/ios/CCDevice.mm b/cocos/platform/ios/CCDevice.mm index 9246327882..fb14b01f71 100644 --- a/cocos/platform/ios/CCDevice.mm +++ b/cocos/platform/ios/CCDevice.mm @@ -221,7 +221,7 @@ static CGSize _calculateStringSize(NSString *str, id font, CGSize *constrainSize textRect.height = constrainSize->height > 0 ? constrainSize->height : 0x7fffffff; - CGSize dim = [s sizeWithFont:font constrainedToSize:textRect]; + CGSize dim = [str sizeWithFont:font constrainedToSize:textRect]; dim.width = ceilf(dim.width); dim.height = ceilf(dim.height); From 97b578185d2a7b84c33dffd85384fafa89ef6b37 Mon Sep 17 00:00:00 2001 From: vision Date: Thu, 12 Jun 2014 09:49:48 +0800 Subject: [PATCH 08/30] new parameter for setting blur shader effect --- .../Classes/ShaderTest/ShaderTest.cpp | 69 ++++++++++++++----- .../cpp-tests/Classes/ShaderTest/ShaderTest.h | 9 ++- .../Classes/ShaderTest/ShaderTest2.cpp | 13 +++- .../Resources/Shaders/example_Blur.fsh | 39 ++++++++++- 4 files changed, 104 insertions(+), 26 deletions(-) diff --git a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp index 07d764601b..b4150a8383 100644 --- a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp +++ b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp @@ -421,14 +421,16 @@ class SpriteBlur : public Sprite { public: ~SpriteBlur(); - void setBlurRadius(float radius); bool initWithTexture(Texture2D* texture, const Rect& rect); void initGLProgram(); static SpriteBlur* create(const char *pszFileName); + void setBlurRadius(float radius); + void setBlurSampleNum(float num); protected: float _blurRadius; + float _blurSampleNum; }; SpriteBlur::~SpriteBlur() @@ -484,6 +486,7 @@ void SpriteBlur::initGLProgram() auto size = getTexture()->getContentSizeInPixels(); getGLProgramState()->setUniformVec2("resolution", size); getGLProgramState()->setUniformFloat("blurRadius", _blurRadius); + getGLProgramState()->setUniformFloat("sampleNum", 7.0f); } void SpriteBlur::setBlurRadius(float radius) @@ -492,6 +495,12 @@ void SpriteBlur::setBlurRadius(float radius) getGLProgramState()->setUniformFloat("blurRadius", _blurRadius); } +void SpriteBlur::setBlurSampleNum(float num) +{ + _blurSampleNum = num; + getGLProgramState()->setUniformFloat("sampleNum", _blurSampleNum); +} + // ShaderBlur ShaderBlur::ShaderBlur() @@ -509,22 +518,43 @@ std::string ShaderBlur::subtitle() const return "Gaussian blur"; } -ControlSlider* ShaderBlur::createSliderCtl() +void ShaderBlur::createSliderCtls() { auto screenSize = Director::getInstance()->getWinSize(); - ControlSlider *slider = ControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png"); - slider->setAnchorPoint(Vec2(0.5f, 1.0f)); - slider->setMinimumValue(0.0f); // Sets the min value of range - slider->setMaximumValue(25.0f); // Sets the max value of range + { + ControlSlider *slider = ControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png"); + slider->setAnchorPoint(Vec2(0.5f, 1.0f)); + slider->setMinimumValue(0.0f); + slider->setMaximumValue(25.0f); + slider->setScale(0.6f); + slider->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f)); + slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::onRadiusChanged), Control::EventType::VALUE_CHANGED); + slider->setValue(2.0f); + addChild(slider); + _sliderRadiusCtl = slider; + + auto label = Label::createWithTTF("Blur Radius", "fonts/arial.ttf", 12.0f); + addChild(label); + label->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f - 24.0f)); + } - slider->setPosition(Vec2(screenSize.width / 2.0f, screenSize.height / 3.0f)); - - // When the value of the slider will change, the given selector will be call - slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::sliderAction), Control::EventType::VALUE_CHANGED); - slider->setValue(2.0f); - - return slider; + { + ControlSlider *slider = ControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png"); + slider->setAnchorPoint(Vec2(0.5f, 1.0f)); + slider->setMinimumValue(0.0f); + slider->setMaximumValue(11.0f); + slider->setScale(0.6f); + slider->setPosition(Vec2(screenSize.width * 3 / 4.0f, screenSize.height / 3.0f)); + slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::onSampleNumChanged), Control::EventType::VALUE_CHANGED); + slider->setValue(7.0f); + addChild(slider); + _sliderNumCtrl = slider; + + auto label = Label::createWithTTF("Blur Sample Num", "fonts/arial.ttf", 12.0f); + addChild(label); + label->setPosition(Vec2(screenSize.width * 3 / 4.0f, screenSize.height / 3.0f - 24.0f)); + } } @@ -533,9 +563,7 @@ bool ShaderBlur::init() if( ShaderTestDemo::init() ) { _blurSprite = SpriteBlur::create("Images/grossini.png"); - auto sprite = Sprite::create("Images/grossini.png"); - auto s = Director::getInstance()->getWinSize(); _blurSprite->setPosition(Vec2(s.width/3, s.height/2)); sprite->setPosition(Vec2(2*s.width/3, s.height/2)); @@ -543,21 +571,26 @@ bool ShaderBlur::init() addChild(_blurSprite); addChild(sprite); - _sliderCtl = createSliderCtl(); + createSliderCtls(); - addChild(_sliderCtl); return true; } return false; } -void ShaderBlur::sliderAction(Ref* sender, Control::EventType controlEvent) +void ShaderBlur::onRadiusChanged(Ref* sender, Control::EventType) { ControlSlider* slider = (ControlSlider*)sender; _blurSprite->setBlurRadius(slider->getValue()); } +void ShaderBlur::onSampleNumChanged(Ref* sender, Control::EventType) +{ + ControlSlider* slider = (ControlSlider*)sender; + _blurSprite->setBlurSampleNum(slider->getValue()); +} + // ShaderRetroEffect ShaderRetroEffect::ShaderRetroEffect() diff --git a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.h b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.h index 798466dae1..e3788de8bd 100644 --- a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.h +++ b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.h @@ -92,11 +92,14 @@ public: virtual std::string title() const override; virtual std::string subtitle() const override; virtual bool init(); - ControlSlider* createSliderCtl(); - void sliderAction(Ref* sender, Control::EventType controlEvent); + void createSliderCtls(); + void onRadiusChanged(Ref* sender, Control::EventType controlEvent); + void onSampleNumChanged(Ref* sender, Control::EventType controlEvent); + protected: SpriteBlur* _blurSprite; - ControlSlider* _sliderCtl; + ControlSlider* _sliderRadiusCtl; + ControlSlider* _sliderNumCtrl; }; class ShaderRetroEffect : public ShaderTestDemo diff --git a/tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp b/tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp index f406ae8cd7..5c7fa92f11 100644 --- a/tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp +++ b/tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp @@ -251,11 +251,13 @@ public: CREATE_FUNC(EffectBlur); virtual void setTarget(EffectSprite *sprite) override; void setBlurRadius(float radius); + void setBlurSampleNum(float num); protected: - bool init(float blurRadius = 10.0f); + bool init(float blurRadius = 10.0f, float sampleNum = 5.0f); float _blurRadius; + float _blurSampleNum; }; void EffectBlur::setTarget(EffectSprite *sprite) @@ -263,12 +265,14 @@ void EffectBlur::setTarget(EffectSprite *sprite) Size size = sprite->getTexture()->getContentSizeInPixels(); _glprogramstate->setUniformVec2("resolution", size); _glprogramstate->setUniformFloat("blurRadius", _blurRadius); + _glprogramstate->setUniformFloat("sampleNum", _blurSampleNum); } -bool EffectBlur::init(float blurRadius) +bool EffectBlur::init(float blurRadius, float sampleNum) { initGLProgramState("Shaders/example_Blur.fsh"); _blurRadius = blurRadius; + _blurSampleNum = sampleNum; return true; } @@ -278,6 +282,11 @@ void EffectBlur::setBlurRadius(float radius) _blurRadius = radius; } +void EffectBlur::setBlurSampleNum(float num) +{ + _blurSampleNum = num; +} + // Outline class EffectOutline : public Effect { diff --git a/tests/cpp-tests/Resources/Shaders/example_Blur.fsh b/tests/cpp-tests/Resources/Shaders/example_Blur.fsh index c0f94933cd..6082e7f748 100644 --- a/tests/cpp-tests/Resources/Shaders/example_Blur.fsh +++ b/tests/cpp-tests/Resources/Shaders/example_Blur.fsh @@ -7,24 +7,25 @@ varying vec2 v_texCoord; uniform vec2 resolution; uniform float blurRadius; +uniform float sampleNum; vec3 blur(vec2); void main(void) { vec3 col = blur(v_texCoord); - gl_FragColor = vec4(col, 1.0); + gl_FragColor = vec4(col, 1.0) * v_fragmentColor; } vec3 blur(vec2 p) { - if (blurRadius > 0.0) + if (blurRadius > 0.0 && sampleNum > 1.0) { vec3 col = vec3(0); vec2 unit = 1.0 / resolution.xy; float r = blurRadius; - float sampleStep = r / 7.37; + float sampleStep = r / sampleNum; float count = 0.0; @@ -44,3 +45,35 @@ vec3 blur(vec2 p) return texture2D(CC_Texture0, p).rgb; } +//vec3 blur(vec2 p) +//{ +// if (blurRadius > 0.0) +// { +// vec3 col = vec3(0); +// vec2 unit = 1.0 / resolution.xy; +// +// float r = blurRadius; +// float sampleStep = r / 5.4; +// float count = 0.0; +// for (float x = 0.0; x < r; x += sampleStep) +// { +// for (float y = 0.0; y < r; y += sampleStep) +// { +// float weight = (r - x) * (r - y); +// col += texture2D(CC_Texture0, p + vec2(-x * unit.x, y * unit.y)).rgb * weight; +// col += texture2D(CC_Texture0, p + vec2(x * unit.x, -y * unit.y)).rgb * weight; +// col += texture2D(CC_Texture0, p + vec2(x * unit.x, y * unit.y)).rgb * weight; +// col += texture2D(CC_Texture0, p + vec2(-x * unit.x, -y * unit.y)).rgb * weight; +// count += 4.0 * weight; +// } +// } +// +// float centerWeight = 3.0 * r * r; +// count -= centerWeight; +// col -= texture2D(CC_Texture0, p).rgb * centerWeight; +// +// return col / count; +// } +// +// return texture2D(CC_Texture0, p).rgb; +//} From dc4cdc9c0fd8a8277aa95b86eb0af888bb323456 Mon Sep 17 00:00:00 2001 From: vision Date: Thu, 12 Jun 2014 10:19:08 +0800 Subject: [PATCH 09/30] remove the comments --- .../Resources/Shaders/example_Blur.fsh | 33 ------------------- 1 file changed, 33 deletions(-) diff --git a/tests/cpp-tests/Resources/Shaders/example_Blur.fsh b/tests/cpp-tests/Resources/Shaders/example_Blur.fsh index 6082e7f748..74eefdf0fe 100644 --- a/tests/cpp-tests/Resources/Shaders/example_Blur.fsh +++ b/tests/cpp-tests/Resources/Shaders/example_Blur.fsh @@ -44,36 +44,3 @@ vec3 blur(vec2 p) return texture2D(CC_Texture0, p).rgb; } - -//vec3 blur(vec2 p) -//{ -// if (blurRadius > 0.0) -// { -// vec3 col = vec3(0); -// vec2 unit = 1.0 / resolution.xy; -// -// float r = blurRadius; -// float sampleStep = r / 5.4; -// float count = 0.0; -// for (float x = 0.0; x < r; x += sampleStep) -// { -// for (float y = 0.0; y < r; y += sampleStep) -// { -// float weight = (r - x) * (r - y); -// col += texture2D(CC_Texture0, p + vec2(-x * unit.x, y * unit.y)).rgb * weight; -// col += texture2D(CC_Texture0, p + vec2(x * unit.x, -y * unit.y)).rgb * weight; -// col += texture2D(CC_Texture0, p + vec2(x * unit.x, y * unit.y)).rgb * weight; -// col += texture2D(CC_Texture0, p + vec2(-x * unit.x, -y * unit.y)).rgb * weight; -// count += 4.0 * weight; -// } -// } -// -// float centerWeight = 3.0 * r * r; -// count -= centerWeight; -// col -= texture2D(CC_Texture0, p).rgb * centerWeight; -// -// return col / count; -// } -// -// return texture2D(CC_Texture0, p).rgb; -//} From d755b7ffcb7b29ee0e2d31479ca1e85571b9e967 Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Thu, 12 Jun 2014 11:21:42 +0800 Subject: [PATCH 10/30] issue #3341:fix TextureCache::addImageAsync repeatedly generate Image for the same image file. --- cocos/renderer/CCTextureCache.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos/renderer/CCTextureCache.cpp b/cocos/renderer/CCTextureCache.cpp index 16643adeb9..d7db2feaa8 100644 --- a/cocos/renderer/CCTextureCache.cpp +++ b/cocos/renderer/CCTextureCache.cpp @@ -206,11 +206,11 @@ void TextureCache::loadImage() for (; pos < infoSize; pos++) { imageInfo = (*_imageInfoQueue)[pos]; - if(imageInfo->asyncStruct->filename.compare(asyncStruct->filename)) + if(imageInfo->asyncStruct->filename.compare(asyncStruct->filename) == 0) break; } _imageInfoMutex.unlock(); - if(infoSize == 0 || pos < infoSize) + if(infoSize == 0 || pos == infoSize) generateImage = true; } From 87ac4c660dddc5ee35542b4ee0bf22c757203dd0 Mon Sep 17 00:00:00 2001 From: heliclei Date: Thu, 12 Jun 2014 11:30:37 +0800 Subject: [PATCH 11/30] [ci skip] Set github commit status context & description --- tools/jenkins-scripts/job-comment-trigger.py | 2 +- tools/jenkins-scripts/job-trigger.py | 2 +- tools/jenkins-scripts/post-build.py | 5 +++-- tools/jenkins-scripts/pull-request-builder.py | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tools/jenkins-scripts/job-comment-trigger.py b/tools/jenkins-scripts/job-comment-trigger.py index 8ba9d710af..b425040948 100644 --- a/tools/jenkins-scripts/job-comment-trigger.py +++ b/tools/jenkins-scripts/job-comment-trigger.py @@ -79,7 +79,7 @@ def main(): print 'pull request #' + str(pr_num) + ' is '+action+', no build triggered' return(0) - data = {"state":"pending", "target_url":target_url} + data = {"state":"pending", "target_url":target_url, "context":"Jenkins CI", "description":"Wait available build machine..."} access_token = os.environ['GITHUB_ACCESS_TOKEN'] Headers = {"Authorization":"token " + access_token} diff --git a/tools/jenkins-scripts/job-trigger.py b/tools/jenkins-scripts/job-trigger.py index 54e566e94c..fbdcf0c3e1 100755 --- a/tools/jenkins-scripts/job-trigger.py +++ b/tools/jenkins-scripts/job-trigger.py @@ -90,7 +90,7 @@ def main(): print 'skip build for pull request #' + str(pr_num) return(0) - data = {"state":"pending", "target_url":target_url} + data = {"state":"pending", "target_url":target_url, "context":"Jenkins CI", "description":"Waiting available build machine..."} access_token = os.environ['GITHUB_ACCESS_TOKEN'] Headers = {"Authorization":"token " + access_token} diff --git a/tools/jenkins-scripts/post-build.py b/tools/jenkins-scripts/post-build.py index 78f7ddf735..fb99727de8 100644 --- a/tools/jenkins-scripts/post-build.py +++ b/tools/jenkins-scripts/post-build.py @@ -18,7 +18,7 @@ statuses_url = payload['statuses_url'] J = Jenkins(os.environ['JENKINS_URL']) target_url = os.environ['BUILD_URL'] build_number = int(os.environ['BUILD_NUMBER']) -data = {"state":"pending", "target_url":target_url} +data = {"state":"pending", "target_url":target_url, "context":"Jenkins CI", "description":"Build finished!"} access_token = os.environ['GITHUB_ACCESS_TOKEN'] Headers = {"Authorization":"token " + access_token} @@ -26,9 +26,10 @@ result = J[os.environ['JOB_NAME']].get_build(build_number).get_status() if(result == STATUS_SUCCESS): data['state'] = "success" + data['description'] = "Build successfully!" else: data['state'] = "failure" - + data['description'] = "Build failed!" http_proxy = '' if(os.environ.has_key('HTTP_PROXY')): http_proxy = os.environ['HTTP_PROXY'] diff --git a/tools/jenkins-scripts/pull-request-builder.py b/tools/jenkins-scripts/pull-request-builder.py index d506bb0df5..eb36154c49 100755 --- a/tools/jenkins-scripts/pull-request-builder.py +++ b/tools/jenkins-scripts/pull-request-builder.py @@ -104,7 +104,7 @@ def main(): set_description(pr_desc, target_url) - data = {"state":"pending", "target_url":target_url} + data = {"state":"pending", "target_url":target_url, "context":"Jenkins CI", "description":"Build started..."} access_token = os.environ['GITHUB_ACCESS_TOKEN'] Headers = {"Authorization":"token " + access_token} From 70147e9a20b18222dcae4829bbc7bd6365ecd49d Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 12 Jun 2014 11:38:28 +0800 Subject: [PATCH 12/30] [ci skip] --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index 9b745809d9..c17a53bf37 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -27,6 +27,7 @@ cocos2d-x-3.2 ??? [FIX] Schedule: schedulePerFrame() can not be called twice [FIX] SpriteFrameCache: fix memory leak [FIX] Texture2D: use image's pixel format to create texture + [FIX] TextureCache: addImageAsync() may repeatedly generate Image for the same image file [FIX] WP8: will restart if app goes to background, then touches icon to go to foreground [FIX] WP8: will be black if: 1. 3rd pops up a view; 2. go to background; 3. come to foreground [FIX] WP8: project name of new project created by console is wrong From cc683ff00f60afaf1f75708981bc2dfb3f10e10e Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 12 Jun 2014 11:40:41 +0800 Subject: [PATCH 13/30] [ci skip] --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index c17a53bf37..f9a3fc15cb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -16,6 +16,7 @@ cocos2d-x-3.2 ??? [FIX] Image: premultiply alpha when loading png file to resolve black border issue [FIX] Label: label is unsharp if it's created by smaller font [FIX] Label: Label's display may go bonkers if invoking Label::setString() with outline feature enabled + [FIX] Label: don't release cached texture in time [FIX] Lua-binding: compiling error on release mode [FIX] Lua-binding: Add xxtea encrypt support [FIX] Node: setPhysicsBody() can not work correctly if it is added to a Node From 22f445d56f86ff925c71a6f5291e602792422950 Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 12 Jun 2014 14:47:42 +0800 Subject: [PATCH 14/30] [ci skip] --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index f9a3fc15cb..223dd5bb51 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -17,6 +17,7 @@ cocos2d-x-3.2 ??? [FIX] Label: label is unsharp if it's created by smaller font [FIX] Label: Label's display may go bonkers if invoking Label::setString() with outline feature enabled [FIX] Label: don't release cached texture in time + [FIX] Label: calculated height of multi-line string was incorrect on iOS [FIX] Lua-binding: compiling error on release mode [FIX] Lua-binding: Add xxtea encrypt support [FIX] Node: setPhysicsBody() can not work correctly if it is added to a Node From 77fdd124a1497a9801ff6168a084e9dcde23d6fa Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 12 Jun 2014 15:05:31 +0800 Subject: [PATCH 15/30] [ci skip] --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 6beee8bf55..90e96f068e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -841,6 +841,7 @@ Developers: sachingarg05 Re-added orientation change callback in java activity + GLProgram should not abort() if shader compilation fails, returning false is better. dplusic Fixed that cc.pGetAngle may return wrong value From bb5471ca8b8ce625fc68da26065ca3ff60c5b6ee Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 12 Jun 2014 15:07:29 +0800 Subject: [PATCH 16/30] [ci skip] --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index 223dd5bb51..166f24f723 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -11,6 +11,7 @@ cocos2d-x-3.2 ??? [FIX] Android: 3d model will be black when coming from background [FIX] Android: don't trigger EVENT_COME_TO_BACKGROUND event when go to background [FIX] Cocos2dxGLSurfaceView.java: prevent flickering when opening another activity + [FIX] GLProgram: not abort if shader compilation fails, just retuan false. [FIX] GLProgramState: sampler can not be changed [FIX] Image: Set jpeg save quality to 90 [FIX] Image: premultiply alpha when loading png file to resolve black border issue From f6913fc9314773a701bb066b440fa0969dd39578 Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 12 Jun 2014 16:12:03 +0800 Subject: [PATCH 17/30] [ci skip] --- AUTHORS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/AUTHORS b/AUTHORS index 90e96f068e..4ac6ffa9c4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -882,6 +882,9 @@ Developers: zhouxiaoxiaoxujian Added TextField::getStringLength() Add shadow, outline, glow filter support for UIText + + QiuleiWang + Fix the bug that calculated height of multi-line string was incorrect on iOS Retired Core Developers: WenSheng Yang From d1ca81634aa111644ca2ce838bf1c0a5271a32d3 Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 12 Jun 2014 16:18:22 +0800 Subject: [PATCH 18/30] [ci skip] --- AUTHORS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/AUTHORS b/AUTHORS index 4ac6ffa9c4..52009d3146 100644 --- a/AUTHORS +++ b/AUTHORS @@ -885,6 +885,9 @@ Developers: QiuleiWang Fix the bug that calculated height of multi-line string was incorrect on iOS + + Rumist + Fix the bug that the result of Director->convertToUI() is error. Retired Core Developers: WenSheng Yang From 1187cfff6d5bd26316327ccefe168474ced89c07 Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 12 Jun 2014 16:19:05 +0800 Subject: [PATCH 19/30] [ci skip] --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index 166f24f723..b42ad6f8ac 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -11,6 +11,7 @@ cocos2d-x-3.2 ??? [FIX] Android: 3d model will be black when coming from background [FIX] Android: don't trigger EVENT_COME_TO_BACKGROUND event when go to background [FIX] Cocos2dxGLSurfaceView.java: prevent flickering when opening another activity + [FIX] Director: Director->convertToUI() returns wrong value. [FIX] GLProgram: not abort if shader compilation fails, just retuan false. [FIX] GLProgramState: sampler can not be changed [FIX] Image: Set jpeg save quality to 90 From 1b24af95f18a585b6cd1b5d7ca19b834718e3c36 Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 12 Jun 2014 18:23:31 +0800 Subject: [PATCH 20/30] [ci skip] --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index b42ad6f8ac..4d58d944e0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -29,6 +29,7 @@ cocos2d-x-3.2 ??? [FIX] Repeat: will run one more over in rare situations [FIX] Scale9Sprite: support culling [FIX] Schedule: schedulePerFrame() can not be called twice + [FIX] ShaderTest: 7 times performance improved of blur effect [FIX] SpriteFrameCache: fix memory leak [FIX] Texture2D: use image's pixel format to create texture [FIX] TextureCache: addImageAsync() may repeatedly generate Image for the same image file From ae0b850d891105a8ae35e7c70c35722a78b93787 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Thu, 12 Jun 2014 18:32:47 +0800 Subject: [PATCH 21/30] boneblendstate 30% --- cocos/3d/CCMeshSkin.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cocos/3d/CCMeshSkin.h b/cocos/3d/CCMeshSkin.h index 7a610e6135..0fb32d9d82 100644 --- a/cocos/3d/CCMeshSkin.h +++ b/cocos/3d/CCMeshSkin.h @@ -103,6 +103,13 @@ protected: Dirty_Translate = 1, Dirty_Rotation = 2, Dirty_Scale = 4, + }; + struct BoneBlendState + { + Vec3 localTranslate; + Quaternion localRot; + Vec3 localScale; + float weight; }; /** * Constructor. @@ -143,6 +150,8 @@ protected: bool _worldDirty; Mat4 _world; Mat4 _local; + + BoneBlendState _blendState; Vec3 _localTranslate; Quaternion _localRot; Vec3 _localScale; From 09fe7e13b71a618a9b40aa1e06d714193f656bdf Mon Sep 17 00:00:00 2001 From: yangxiao Date: Fri, 13 Jun 2014 09:12:29 +0800 Subject: [PATCH 22/30] blendstate --- cocos/3d/CCMeshSkin.cpp | 11 ++++++++--- cocos/3d/CCMeshSkin.h | 11 +++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cocos/3d/CCMeshSkin.cpp b/cocos/3d/CCMeshSkin.cpp index ae6b6ce738..3962fbd14f 100644 --- a/cocos/3d/CCMeshSkin.cpp +++ b/cocos/3d/CCMeshSkin.cpp @@ -87,7 +87,7 @@ const Mat4& Bone::getWorldMat() /** * Set AnimationValue. set to its transform */ -void Bone::setAnimationValueTranslation(float* value) +void Bone::setAnimationValueTranslation(float* value, float weight) { static const int bytes = 3 * sizeof(float); if (memcmp(&_localTranslate.x, value, bytes) != 0) @@ -96,7 +96,7 @@ void Bone::setAnimationValueTranslation(float* value) _localTranslate.set(value); } } -void Bone::setAnimationValueRotation(float* value) +void Bone::setAnimationValueRotation(float* value, float weight) { static const int bytes = 4 * sizeof(float); if (memcmp(&_localRot.x, value, bytes) != 0) @@ -105,7 +105,7 @@ void Bone::setAnimationValueRotation(float* value) _localRot.set(value); } } -void Bone::setAnimationValueScale(float* value) +void Bone::setAnimationValueScale(float* value, float weight) { static const int bytes = 3 * sizeof(float); if (memcmp(&_localScale.x, value, bytes) != 0) @@ -220,6 +220,11 @@ void Bone::updateLocalMat() _dirtyFlag = 0; } +void Bone::clearBlendState() +{ + _blendStates.clear(); +} + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// static int PALETTE_ROWS = 3; diff --git a/cocos/3d/CCMeshSkin.h b/cocos/3d/CCMeshSkin.h index 0fb32d9d82..cba1e844ae 100644 --- a/cocos/3d/CCMeshSkin.h +++ b/cocos/3d/CCMeshSkin.h @@ -62,9 +62,9 @@ public: /** * Set AnimationValue. set to its transform */ - void setAnimationValueTranslation(float* value); - void setAnimationValueRotation(float* value); - void setAnimationValueScale(float* value); + void setAnimationValueTranslation(float* value, float weight = 1.0f); + void setAnimationValueRotation(float* value, float weight = 1.0f); + void setAnimationValueScale(float* value, float weight = 1.0f); /** * Creates C3DBone. @@ -98,6 +98,7 @@ public: protected: + enum DirtyFlag { Dirty_Translate = 1, @@ -126,6 +127,8 @@ protected: */ void updateLocalMat(); + void clearBlendState(); + std::string _name; /** * The Mat4 representation of the Joint's bind pose. @@ -151,7 +154,7 @@ protected: Mat4 _world; Mat4 _local; - BoneBlendState _blendState; + std::vector _blendStates; Vec3 _localTranslate; Quaternion _localRot; Vec3 _localScale; From f7b8acd6fdc6e614f070a05e344ddf5227a10dee Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 13 Jun 2014 11:59:13 +0800 Subject: [PATCH 23/30] change pointer to std::vector for mesh --- cocos/3d/CCBundle3D.cpp | 10 +++++----- cocos/3d/CCBundle3DData.h | 24 ++++++++++++------------ cocos/3d/CCMesh.cpp | 17 ++++++++--------- cocos/3d/CCMesh.h | 6 +++--- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/cocos/3d/CCBundle3D.cpp b/cocos/3d/CCBundle3D.cpp index cb6ae9562e..2af3a8307e 100644 --- a/cocos/3d/CCBundle3D.cpp +++ b/cocos/3d/CCBundle3D.cpp @@ -60,7 +60,7 @@ bool Bundle3D::loadMeshData(const std::string& id, MeshData* meshdata) { meshdata->resetData(); meshdata->vertexSizeInFloat = 13 * 4; - meshdata->vertex = new float[meshdata->vertexSizeInFloat]; + meshdata->vertex.resize(meshdata->vertexSizeInFloat); //dabing's data // float vert[] = {0.f,50.f,0.f, 0.f,0.f, 0.f,0.f,0.f,0.f, 1.f,0.f,0.f,0.f, // 0.f,0.f,50.f, 1.f,1.f, 0.f,0.f,0.f,0.f, 1.f,0.f,0.f,0.f, @@ -73,18 +73,18 @@ bool Bundle3D::loadMeshData(const std::string& id, MeshData* meshdata) //float vert[] = {0.f,50.f,0.f, 0.f,0.f,50.f, 50.f,0.f,0.f, -50.f,0.f,0.f}; - memcpy(meshdata->vertex, vert, meshdata->vertexSizeInFloat * sizeof(float)); + memcpy(&meshdata->vertex[0], vert, meshdata->vertexSizeInFloat * sizeof(float)); meshdata->numIndex = 4 * 3; //meshdata->numIndex = 3; - meshdata->indices = new unsigned short[meshdata->numIndex]; + meshdata->indices.resize(meshdata->numIndex); unsigned short index[] = {0,1,2, 0,3,1, 0,2,3, 3,2,1}; //unsigned short index[] = {0,3,2}; //unsigned short index[] = {0,1,2}; - memcpy(meshdata->indices, index, meshdata->numIndex * sizeof(unsigned short)); + memcpy(&meshdata->indices[0], index, meshdata->numIndex * sizeof(unsigned short)); meshdata->attribCount = 4; - meshdata->attribs = new MeshVertexAttrib[meshdata->attribCount]; + meshdata->attribs.resize(meshdata->attribCount); meshdata->attribs[0].attribSizeBytes = 3 * sizeof(float); meshdata->attribs[0].size = 3; meshdata->attribs[0].type = GL_FLOAT; diff --git a/cocos/3d/CCBundle3DData.h b/cocos/3d/CCBundle3DData.h index 8a907d3c20..ae2dbe1a1b 100644 --- a/cocos/3d/CCBundle3DData.h +++ b/cocos/3d/CCBundle3DData.h @@ -49,29 +49,26 @@ struct MeshVertexAttrib struct MeshData { - float* vertex; + std::vector vertex; int vertexSizeInFloat; - unsigned short* indices; + std::vector indices; int numIndex; - MeshVertexAttrib* attribs; + std::vector attribs; int attribCount; public: void resetData() { - CC_SAFE_DELETE_ARRAY(vertex); - CC_SAFE_DELETE_ARRAY(indices); - CC_SAFE_DELETE_ARRAY(attribs); + vertex.clear(); + indices.clear(); + attribs.clear(); vertexSizeInFloat = 0; numIndex = 0; attribCount = 0; } MeshData() - : vertex(nullptr) - , vertexSizeInFloat(0) - , indices(nullptr) + : vertexSizeInFloat(0) , numIndex(0) - , attribs(nullptr) , attribCount(0) { } @@ -106,6 +103,7 @@ struct MaterialData struct Animation3DData { +public: struct Vec3Key { Vec3Key() @@ -141,13 +139,15 @@ struct Animation3DData float _time; Quaternion _key; }; - + +public: std::map> _translationKeys; std::map> _rotationKeys; std::map> _scaleKeys; float _totalTime; - + +public: Animation3DData() :_totalTime(0) { diff --git a/cocos/3d/CCMesh.cpp b/cocos/3d/CCMesh.cpp index 467f2c88ef..433476a3eb 100644 --- a/cocos/3d/CCMesh.cpp +++ b/cocos/3d/CCMesh.cpp @@ -118,15 +118,14 @@ bool RenderMeshData::initFrom(const std::vector& positions, return true; } -bool RenderMeshData::initFrom(const float* vertex, int vertexSizeInFloat, unsigned short* indices, int numIndex, const MeshVertexAttrib* attribs, int attribCount) +bool RenderMeshData::initFrom(const std::vector& vertices, int vertexSizeInFloat, const std::vector& indices, int numIndex, const std::vector& attribs, int attribCount) { - _vertexs.assign(vertex, vertex + vertexSizeInFloat); - _indices.assign(indices, indices + numIndex); - _vertexAttribs.assign(attribs, attribs + attribCount); + _vertexs = vertices; + _indices = indices; + _vertexAttribs = attribs; _vertexsizeBytes = calVertexSizeBytes(); - return true; } @@ -168,10 +167,10 @@ Mesh* Mesh::create(const std::vector& positions, const std::vector return nullptr; } -Mesh* Mesh::create(const float* vertex, int vertexSizeInFloat, unsigned short* indices, int numIndex, const MeshVertexAttrib* attribs, int attribCount) +Mesh* Mesh::create(const std::vector &vertices, int vertexSizeInFloat, const std::vector &indices, int numIndex, const std::vector &attribs, int attribCount) { auto mesh = new Mesh(); - if (mesh && mesh->init(vertex, vertexSizeInFloat, indices, numIndex, attribs, attribCount)) + if (mesh && mesh->init(vertices, vertexSizeInFloat, indices, numIndex, attribs, attribCount)) { mesh->autorelease(); return mesh; @@ -190,9 +189,9 @@ bool Mesh::init(const std::vector& positions, const std::vector& n return true; } -bool Mesh::init(const float* vertex, int vertexSizeInFloat, unsigned short* indices, int numIndex, const MeshVertexAttrib* attribs, int attribCount) +bool Mesh::init(const std::vector& vertices, int vertexSizeInFloat, const std::vector& indices, int numIndex, const std::vector& attribs, int attribCount) { - bool bRet = _renderdata.initFrom(vertex, vertexSizeInFloat, indices, numIndex, attribs, attribCount); + bool bRet = _renderdata.initFrom(vertices, vertexSizeInFloat, indices, numIndex, attribs, attribCount); if (!bRet) return false; diff --git a/cocos/3d/CCMesh.h b/cocos/3d/CCMesh.h index 775f1c9118..ca4f5a310f 100644 --- a/cocos/3d/CCMesh.h +++ b/cocos/3d/CCMesh.h @@ -46,7 +46,7 @@ public: } bool hasVertexAttrib(int attrib); bool initFrom(const std::vector& positions, const std::vector& normals, const std::vector& texs, const std::vector& indices); - bool initFrom(const float* vertex, int vertexSizeInFloat, unsigned short* indices, int numIndex, const MeshVertexAttrib* attribs, int attribCount); + bool initFrom(const std::vector& vertices, int vertexSizeInFloat, const std::vector& indices, int numIndex, const std::vector& attribs, int attribCount); protected: @@ -83,7 +83,7 @@ public: //create static Mesh* create(const std::vector& positions, const std::vector& normals, const std::vector& texs, const std::vector& indices); - static Mesh* create(const float* vertex, int vertexSizeInFloat, unsigned short* indices, int numIndex, const MeshVertexAttrib* attribs, int attribCount); + static Mesh* create(const std::vector& vertices, int vertexSizeInFloat, const std::vector& indices, int numIndex, const std::vector& attribs, int attribCount); //get vertex buffer inline GLuint getVertexBuffer() const { return _vertexBuffer; } @@ -110,7 +110,7 @@ protected: virtual ~Mesh(); bool init(const std::vector& positions, const std::vector& normals, const std::vector& texs, const std::vector& indices); - bool init(const float* vertex, int vertexSizeInFloat, unsigned short* indices, int numIndex, const MeshVertexAttrib* attribs, int attribCount); + bool init(const std::vector& vertices, int vertexSizeInFloat, const std::vector& indices, int numIndex, const std::vector& attribs, int attribCount); //build buffer void buildBuffer(); From ff6ef1140d51181930e6236e3fb58ce5dc1e3f1c Mon Sep 17 00:00:00 2001 From: yangxiao Date: Fri, 13 Jun 2014 19:20:19 +0800 Subject: [PATCH 24/30] two animate3d the same time --- cocos/3d/CCAnimate3D.cpp | 17 ++-- cocos/3d/CCAnimate3D.h | 1 + cocos/3d/CCBundle3D.cpp | 4 + cocos/3d/CCMeshSkin.cpp | 170 +++++++++++++++++++++++++++++---------- cocos/3d/CCMeshSkin.h | 28 +++---- 5 files changed, 152 insertions(+), 68 deletions(-) diff --git a/cocos/3d/CCAnimate3D.cpp b/cocos/3d/CCAnimate3D.cpp index 4bf6aafe4e..7b75eca12d 100644 --- a/cocos/3d/CCAnimate3D.cpp +++ b/cocos/3d/CCAnimate3D.cpp @@ -101,7 +101,8 @@ void Animate3D::update(float t) { if (_target) { - float dst[4]; + float transDst[3], rotDst[4], scaleDst[3]; + float* trans = nullptr, *rot = nullptr, *scale = nullptr; if (_playBack) t = 1 - t; @@ -110,19 +111,20 @@ void Animate3D::update(float t) auto curve = it.second; if (curve->translateCurve) { - curve->translateCurve->evaluate(t, dst, Linear); - bone->setAnimationValueTranslation(dst); + curve->translateCurve->evaluate(t, transDst, Linear); + trans = &transDst[0]; } if (curve->rotCurve) { - curve->rotCurve->evaluate(t, dst, QuatSlerp); - bone->setAnimationValueRotation(dst); + curve->rotCurve->evaluate(t, rotDst, QuatSlerp); + rot = &rotDst[0]; } if (curve->scaleCurve) { - curve->scaleCurve->evaluate(t, dst, Linear); - bone->setAnimationValueScale(dst); + curve->scaleCurve->evaluate(t, scaleDst, Linear); + scale = &scaleDst[0]; } + bone->setAnimationValue(trans, rot, scale, _weight); } } @@ -130,6 +132,7 @@ void Animate3D::update(float t) Animate3D::Animate3D() : _speed(1) +, _weight(1.f) , _animation(nullptr) , _playBack(false) { diff --git a/cocos/3d/CCAnimate3D.h b/cocos/3d/CCAnimate3D.h index bd21415130..ebf0361331 100644 --- a/cocos/3d/CCAnimate3D.h +++ b/cocos/3d/CCAnimate3D.h @@ -70,6 +70,7 @@ protected: Animation3D* _animation; float _speed; + float _weight; bool _playBack; std::map _boneCurves; //weak ref }; diff --git a/cocos/3d/CCBundle3D.cpp b/cocos/3d/CCBundle3D.cpp index 2af3a8307e..bc0fd4bd0e 100644 --- a/cocos/3d/CCBundle3D.cpp +++ b/cocos/3d/CCBundle3D.cpp @@ -159,6 +159,10 @@ bool Bundle3D::loadAnimationData(const std::string& id, Animation3DData* animati Quaternion::createFromAxisAngle(Vec3(1.f, 0.f, 0.f), MATH_DEG_TO_RAD(270), &quat); animationdata->_rotationKeys[boneName].push_back(Animation3DData::QuatKey(keytime1[3], quat)); + animationdata->_translationKeys[boneName].push_back(Animation3DData::Vec3Key(keytime1[0], Vec3(0.0f, 0.0f, 0.0f))); + animationdata->_translationKeys[boneName].push_back(Animation3DData::Vec3Key(keytime1[1], Vec3(0.0f, 20.0f, 0.0f))); + animationdata->_translationKeys[boneName].push_back(Animation3DData::Vec3Key(keytime1[2], Vec3(20.0f, 0.0f, 0.0f))); + animationdata->_translationKeys[boneName].push_back(Animation3DData::Vec3Key(keytime1[3], Vec3(0.0f, 0.0f, 20.0f))); return true; } diff --git a/cocos/3d/CCMeshSkin.cpp b/cocos/3d/CCMeshSkin.cpp index 3962fbd14f..3142e456df 100644 --- a/cocos/3d/CCMeshSkin.cpp +++ b/cocos/3d/CCMeshSkin.cpp @@ -84,35 +84,49 @@ const Mat4& Bone::getWorldMat() return _world; } -/** - * Set AnimationValue. set to its transform - */ -void Bone::setAnimationValueTranslation(float* value, float weight) +///** +// * Set AnimationValue. set to its transform +// */ +//void Bone::setAnimationValueTranslation(float* value, float weight) +//{ +// static const int bytes = 3 * sizeof(float); +// if (memcmp(&_localTranslate.x, value, bytes) != 0) +// { +// _dirtyFlag |= Dirty_Translate; +// _localTranslate.set(value); +// } +//} +//void Bone::setAnimationValueRotation(float* value, float weight) +//{ +// static const int bytes = 4 * sizeof(float); +// if (memcmp(&_localRot.x, value, bytes) != 0) +// { +// _dirtyFlag |= Dirty_Rotation; +// _localRot.set(value); +// } +//} +//void Bone::setAnimationValueScale(float* value, float weight) +//{ +// static const int bytes = 3 * sizeof(float); +// if (memcmp(&_localScale.x, value, bytes) != 0) +// { +// _dirtyFlag |= Dirty_Scale; +// _localScale.set(value); +// } +//} + +void Bone::setAnimationValue(float* trans, float* rot, float* scale, float weight) { - static const int bytes = 3 * sizeof(float); - if (memcmp(&_localTranslate.x, value, bytes) != 0) - { - _dirtyFlag |= Dirty_Translate; - _localTranslate.set(value); - } -} -void Bone::setAnimationValueRotation(float* value, float weight) -{ - static const int bytes = 4 * sizeof(float); - if (memcmp(&_localRot.x, value, bytes) != 0) - { - _dirtyFlag |= Dirty_Rotation; - _localRot.set(value); - } -} -void Bone::setAnimationValueScale(float* value, float weight) -{ - static const int bytes = 3 * sizeof(float); - if (memcmp(&_localScale.x, value, bytes) != 0) - { - _dirtyFlag |= Dirty_Scale; - _localScale.set(value); - } + BoneBlendState state; + if (trans) + state.localTranslate.set(trans); + if (rot) + state.localRot.set(rot); + if (scale) + state.localScale.set(scale); + state.weight = weight; + + _blendStates.push_back(state); } /** @@ -199,25 +213,97 @@ Bone::~Bone() void Bone::updateLocalMat() { - if (_dirtyFlag & Dirty_Translate) +// if (_dirtyFlag & Dirty_Translate) +// { +// Mat4::createTranslation(_localTranslate, &_local); +// if (_dirtyFlag & Dirty_Rotation) +// _local.rotate(_localRot); +// if (_dirtyFlag & Dirty_Scale) +// _local.scale(_localScale); +// } +// else if (_dirtyFlag & Dirty_Rotation) +// { +// Mat4::createRotation(_localRot, &_local); +// if (_dirtyFlag & Dirty_Scale) +// _local.scale(_localScale); +// } +// else if (_dirtyFlag & Dirty_Scale) +// { +// Mat4::createScale(_localScale, &_local); +// } +// _dirtyFlag = 0; + + Vec3 translate(0.f, 0.f, 0.f), scale(0.f, 0.f, 0.f); + Quaternion quat(0.f, 0.f, 0.f, 0.f); + if (_blendStates.size()) { - Mat4::createTranslation(_localTranslate, &_local); - if (_dirtyFlag & Dirty_Rotation) - _local.rotate(_localRot); - if (_dirtyFlag & Dirty_Scale) - _local.scale(_localScale); + + + float total = 0.f; + for (auto it: _blendStates) { + total += it.weight; + } + if (total) + { + if (_blendStates.size() == 1) + { + translate = _blendStates[0].localTranslate; + scale = _blendStates[0].localScale; + quat = _blendStates[0].localRot; + } + else + { + float invTotal = 1.f / total; + for (auto it : _blendStates) { + float weight = (it.weight * invTotal); + translate += it.localTranslate * weight; + if (!it.localScale.isZero()) + { + scale.x *= it.localScale.x * weight; + scale.y *= it.localScale.y * weight; + scale.z *= it.localScale.z * weight; + } + if (!it.localRot.isZero()) + { + if (!quat.isZero()) + { + Quaternion& q = _blendStates[0].localRot; + if (q.x * quat.x + q.y * quat.y + q.z * quat.z + q.w * quat.w < 0) + weight = -weight; + } + quat = Quaternion(it.localRot.x * weight + quat.x, it.localRot.y * weight + quat.y, it.localRot.z * weight + quat.z, it.localRot.w * weight + quat.w); + } + } + } + } } - else if (_dirtyFlag & Dirty_Rotation) + + bool hasTrans = !translate.isZero(); + bool hasRot = !quat.isZero(); + bool hasScale = !scale.isZero(); + + if (hasTrans) { - Mat4::createRotation(_localRot, &_local); - if (_dirtyFlag & Dirty_Scale) - _local.scale(_localScale); + Mat4::createTranslation(translate, &_local); + if (hasRot) + _local.rotate(quat); + if (hasScale) + _local.scale(scale); } - else if (_dirtyFlag & Dirty_Scale) + else if (hasRot) { - Mat4::createScale(_localScale, &_local); + Mat4::createRotation(quat, &_local); + if (hasScale) + _local.scale(scale); } - _dirtyFlag = 0; + else if (hasScale) + { + Mat4::createScale(scale, &_local); + } + else + _local.setIdentity(); + + _blendStates.clear(); } void Bone::clearBlendState() @@ -269,7 +355,7 @@ MeshSkin* MeshSkin::create(const std::string& filename, const std::string& name) { auto skin = new MeshSkin(); skin->_bindShape = skindata.bindShape; - skin->setBoneCount(skindata.boneNames.size()); + skin->setBoneCount((int)skindata.boneNames.size()); for (size_t i = 0; i < skindata.boneNames.size(); i++) { auto bone = Bone::create(skindata.boneNames[i]); bone->_bindPose = skindata.inverseBindPoseMatrices[i]; diff --git a/cocos/3d/CCMeshSkin.h b/cocos/3d/CCMeshSkin.h index cba1e844ae..6a607c35d6 100644 --- a/cocos/3d/CCMeshSkin.h +++ b/cocos/3d/CCMeshSkin.h @@ -59,12 +59,14 @@ public: const std::string& getName() const { return _name; } - /** - * Set AnimationValue. set to its transform - */ - void setAnimationValueTranslation(float* value, float weight = 1.0f); - void setAnimationValueRotation(float* value, float weight = 1.0f); - void setAnimationValueScale(float* value, float weight = 1.0f); +// /** +// * Set AnimationValue. set to its transform +// */ +// void setAnimationValueTranslation(float* value, float weight = 1.0f); +// void setAnimationValueRotation(float* value, float weight = 1.0f); +// void setAnimationValueScale(float* value, float weight = 1.0f); + + void setAnimationValue(float* trans, float* rot, float* scale, float weight = 1.0f); /** * Creates C3DBone. @@ -135,16 +137,6 @@ protected: */ Mat4 _bindPose; - // /** - // * Flag used to mark if the Joint's matrix is dirty. - // */ - // bool _jointMatrixDirty; - // - // /** - // * The number of MeshSkin's influencing the Joint. - // */ - // unsigned int _skinCount; - Bone* _parent; Vector _children; @@ -155,9 +147,7 @@ protected: Mat4 _local; std::vector _blendStates; - Vec3 _localTranslate; - Quaternion _localRot; - Vec3 _localScale; + }; ///////////////////////////////////////////////////////////////////////////// From 7b621c5c93f5c843f26c0f9adbcb0d79a127bcd9 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Mon, 16 Jun 2014 00:26:31 +0800 Subject: [PATCH 25/30] aa --- cocos/3d/CCMeshSkin.cpp | 72 +++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/cocos/3d/CCMeshSkin.cpp b/cocos/3d/CCMeshSkin.cpp index 969b58bafb..7bd564d201 100644 --- a/cocos/3d/CCMeshSkin.cpp +++ b/cocos/3d/CCMeshSkin.cpp @@ -65,19 +65,19 @@ void Bone::updateWorldMat() const Mat4& Bone::getWorldMat() { - updateLocalMat(); - - if (_worldDirty) + //if (_worldDirty) { + updateLocalMat(); if (_parent) { _world = _parent->getWorldMat() * _local; } else _world = _local; + + _worldDirty = false; } - _worldDirty = false; return _world; } @@ -123,6 +123,7 @@ void Bone::setAnimationValue(float* trans, float* rot, float* scale, float weigh state.localScale.set(scale); state.weight = weight; + _blendStates.clear(); _blendStates.push_back(state); _localDirty = true; } @@ -213,6 +214,13 @@ Bone::~Bone() void Bone::updateLocalMat() { + if (_blendStates.size() == 0) + return; + + Mat4::createTranslation(_blendStates[0].localTranslate, &_local); + _local.rotate(_blendStates[0].localRot); + return; + if (!_localDirty) return; @@ -220,8 +228,6 @@ void Bone::updateLocalMat() Quaternion quat(0.f, 0.f, 0.f, 0.f); if (_blendStates.size()) { - - float total = 0.f; for (auto it: _blendStates) { total += it.weight; @@ -259,35 +265,37 @@ void Bone::updateLocalMat() } } } + + bool hasTrans = !translate.isZero(); + bool hasRot = !quat.isZero(); + bool hasScale = !scale.isZero(); + + if (hasTrans) + { + Mat4::createTranslation(translate, &_local); + if (hasRot) + _local.rotate(quat); + if (hasScale) + _local.scale(scale); + } + else if (hasRot) + { + Mat4::createRotation(quat, &_local); + if (hasScale) + _local.scale(scale); + } + else if (hasScale) + { + Mat4::createScale(scale, &_local); + } + else + _local.setIdentity(); + + _blendStates.clear(); + _localDirty = false; } - bool hasTrans = !translate.isZero(); - bool hasRot = !quat.isZero(); - bool hasScale = !scale.isZero(); - if (hasTrans) - { - Mat4::createTranslation(translate, &_local); - if (hasRot) - _local.rotate(quat); - if (hasScale) - _local.scale(scale); - } - else if (hasRot) - { - Mat4::createRotation(quat, &_local); - if (hasScale) - _local.scale(scale); - } - else if (hasScale) - { - Mat4::createScale(scale, &_local); - } - else - _local.setIdentity(); - - _blendStates.clear(); - _localDirty = false; } void Bone::clearBlendState() From 58a6adca02e959b084f6b867211e3a32cf09ddc4 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Mon, 16 Jun 2014 15:45:58 +0800 Subject: [PATCH 26/30] construct BoneBlendState --- cocos/3d/CCMeshSkin.cpp | 4 ---- cocos/3d/CCMeshSkin.h | 8 ++++++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cocos/3d/CCMeshSkin.cpp b/cocos/3d/CCMeshSkin.cpp index 7bd564d201..97ec4da497 100644 --- a/cocos/3d/CCMeshSkin.cpp +++ b/cocos/3d/CCMeshSkin.cpp @@ -217,10 +217,6 @@ void Bone::updateLocalMat() if (_blendStates.size() == 0) return; - Mat4::createTranslation(_blendStates[0].localTranslate, &_local); - _local.rotate(_blendStates[0].localRot); - return; - if (!_localDirty) return; diff --git a/cocos/3d/CCMeshSkin.h b/cocos/3d/CCMeshSkin.h index 1c5c34219c..75ad139809 100644 --- a/cocos/3d/CCMeshSkin.h +++ b/cocos/3d/CCMeshSkin.h @@ -107,6 +107,14 @@ protected: Quaternion localRot; Vec3 localScale; float weight; + BoneBlendState() + : localTranslate(0.f, 0.f, 0.f) + , localRot(0.f, 0.f, 0.f, 1.0f) + , localScale(1.f, 1.f, 1.f) + , weight(0.f) + { + + } }; /** * Constructor. From 927d29f3342da231cff045124a098dab5f4b67cf Mon Sep 17 00:00:00 2001 From: yangxiao Date: Mon, 16 Jun 2014 18:21:47 +0800 Subject: [PATCH 27/30] bug on Repeat when diff bigger than duration --- cocos/2d/CCActionInterval.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cocos/2d/CCActionInterval.cpp b/cocos/2d/CCActionInterval.cpp index b408512663..69bf25eae0 100644 --- a/cocos/2d/CCActionInterval.cpp +++ b/cocos/2d/CCActionInterval.cpp @@ -528,6 +528,8 @@ void RepeatForever::step(float dt) if (_innerAction->isDone()) { float diff = _innerAction->getElapsed() - _innerAction->getDuration(); + if (diff > _innerAction->getDuration()) + diff = fmodf(diff, _innerAction->getDuration()); _innerAction->startWithTarget(_target); // to prevent jerk. issue #390, 1247 _innerAction->step(0.0f); From c00cdd0ae306447a453853d15a91dab220377c9b Mon Sep 17 00:00:00 2001 From: yangxiao Date: Mon, 16 Jun 2014 18:22:32 +0800 Subject: [PATCH 28/30] use bone dirtyflag --- cocos/3d/CCAnimationCurve.inl | 2 +- cocos/3d/CCMeshSkin.cpp | 23 +++++++++++++---------- cocos/3d/CCMeshSkin.h | 2 -- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/cocos/3d/CCAnimationCurve.inl b/cocos/3d/CCAnimationCurve.inl index bdcd6284be..db31834ea3 100644 --- a/cocos/3d/CCAnimationCurve.inl +++ b/cocos/3d/CCAnimationCurve.inl @@ -12,7 +12,7 @@ void AnimationCurve::evaluate(float time, float* dst, EvaluateTyp } else if (time >= _keytime[_count - 1]) { - memcpy(dst, &_value[(_count - 1) * floatSize], _componentSizeByte); + memcpy(dst, &_value[(_count - 1) * componentSize], _componentSizeByte); return; } diff --git a/cocos/3d/CCMeshSkin.cpp b/cocos/3d/CCMeshSkin.cpp index 80f8881246..e4b235b23a 100644 --- a/cocos/3d/CCMeshSkin.cpp +++ b/cocos/3d/CCMeshSkin.cpp @@ -121,6 +121,9 @@ void Bone::setAnimationValue(float* trans, float* rot, float* scale, float weigh state.localRot.set(rot); if (scale) state.localScale.set(scale); + + if (_name != "L_side01" && _name != "L_side03" && _name != "L_side02") + CCASSERT(fabs(scale[0] - 1) < 0.001f, ""); state.weight = weight; _blendStates.push_back(state); @@ -224,11 +227,13 @@ void Bone::updateLocalMat() } if (total) { - if (_blendStates.size() == 1) + //if (_blendStates.size() == 1) + if (true) { - translate = _blendStates[0].localTranslate; - scale = _blendStates[0].localScale; - quat = _blendStates[0].localRot; + int cnt = _blendStates.size(); + translate = _blendStates[cnt - 1].localTranslate; + scale = _blendStates[cnt - 1].localScale; + quat = _blendStates[cnt - 1].localRot; } else { @@ -263,13 +268,11 @@ void Bone::updateLocalMat() _blendStates.clear(); _localDirty = false; } + else + { + CCLOG("use cached local"); + } - -} - -void Bone::clearBlendState() -{ - _blendStates.clear(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/cocos/3d/CCMeshSkin.h b/cocos/3d/CCMeshSkin.h index e6295ea0f1..4bdfdaea0b 100644 --- a/cocos/3d/CCMeshSkin.h +++ b/cocos/3d/CCMeshSkin.h @@ -131,8 +131,6 @@ protected: */ void updateLocalMat(); - void clearBlendState(); - std::string _name; /** * The Mat4 representation of the Joint's bind pose. From 0c07b05e1d611d9b6413de121cfc4da9d519ad80 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Mon, 16 Jun 2014 18:36:30 +0800 Subject: [PATCH 29/30] remove unused code --- cocos/3d/CCMeshSkin.cpp | 49 ++++++++--------------------------------- cocos/3d/CCMeshSkin.h | 8 +------ 2 files changed, 10 insertions(+), 47 deletions(-) diff --git a/cocos/3d/CCMeshSkin.cpp b/cocos/3d/CCMeshSkin.cpp index e4b235b23a..5548f95c70 100644 --- a/cocos/3d/CCMeshSkin.cpp +++ b/cocos/3d/CCMeshSkin.cpp @@ -81,37 +81,6 @@ const Mat4& Bone::getWorldMat() return _world; } -///** -// * Set AnimationValue. set to its transform -// */ -//void Bone::setAnimationValueTranslation(float* value, float weight) -//{ -// static const int bytes = 3 * sizeof(float); -// if (memcmp(&_localTranslate.x, value, bytes) != 0) -// { -// _dirtyFlag |= Dirty_Translate; -// _localTranslate.set(value); -// } -//} -//void Bone::setAnimationValueRotation(float* value, float weight) -//{ -// static const int bytes = 4 * sizeof(float); -// if (memcmp(&_localRot.x, value, bytes) != 0) -// { -// _dirtyFlag |= Dirty_Rotation; -// _localRot.set(value); -// } -//} -//void Bone::setAnimationValueScale(float* value, float weight) -//{ -// static const int bytes = 3 * sizeof(float); -// if (memcmp(&_localScale.x, value, bytes) != 0) -// { -// _dirtyFlag |= Dirty_Scale; -// _localScale.set(value); -// } -//} - void Bone::setAnimationValue(float* trans, float* rot, float* scale, float weight) { BoneBlendState state; @@ -122,14 +91,20 @@ void Bone::setAnimationValue(float* trans, float* rot, float* scale, float weigh if (scale) state.localScale.set(scale); - if (_name != "L_side01" && _name != "L_side03" && _name != "L_side02") - CCASSERT(fabs(scale[0] - 1) < 0.001f, ""); state.weight = weight; _blendStates.push_back(state); _localDirty = true; } +void Bone::clearBoneBlendState() +{ + _blendStates.clear(); + for (auto it : _children) { + it->clearBoneBlendState(); + } +} + /** * Creates C3DBone. */ @@ -150,16 +125,10 @@ Bone* Bone::create(const std::string& id) */ void Bone::updateJointMatrix(const Mat4& bindShape, Vec4* matrixPalette) { - - //if (_skinCount > 1 || _jointMatrixDirty) { - //_jointMatrixDirty = false; static Mat4 t; Mat4::multiply(_world, getInverseBindPose(), &t); - //Mat4::multiply(t, bindShape, &t); - //Mat4::multiply(getInverseBindPose(), _world, &t); - - //t.setIdentity(); + matrixPalette[0].set(t.m[0], t.m[4], t.m[8], t.m[12]); matrixPalette[1].set(t.m[1], t.m[5], t.m[9], t.m[13]); matrixPalette[2].set(t.m[2], t.m[6], t.m[10], t.m[14]); diff --git a/cocos/3d/CCMeshSkin.h b/cocos/3d/CCMeshSkin.h index 4bdfdaea0b..ab838c4e8f 100644 --- a/cocos/3d/CCMeshSkin.h +++ b/cocos/3d/CCMeshSkin.h @@ -59,15 +59,9 @@ public: const std::string& getName() const { return _name; } -// /** -// * Set AnimationValue. set to its transform -// */ -// void setAnimationValueTranslation(float* value, float weight = 1.0f); -// void setAnimationValueRotation(float* value, float weight = 1.0f); -// void setAnimationValueScale(float* value, float weight = 1.0f); - void setAnimationValue(float* trans, float* rot, float* scale, float weight = 1.0f); + void clearBoneBlendState(); /** * Creates C3DBone. */ From 2c6f7b63d18294fa2317d3f7c4a42088e9200143 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Mon, 16 Jun 2014 18:46:58 +0800 Subject: [PATCH 30/30] avoid reopen file --- cocos/3d/CCBundle3D.cpp | 23 ++++++++++++++--------- cocos/3d/CCBundle3D.h | 1 + 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/cocos/3d/CCBundle3D.cpp b/cocos/3d/CCBundle3D.cpp index 155817e247..6495b2d0d5 100644 --- a/cocos/3d/CCBundle3D.cpp +++ b/cocos/3d/CCBundle3D.cpp @@ -109,17 +109,22 @@ void Bundle3D::purgeBundle3D() bool Bundle3D::load(const std::string& path) { std::string strFileString = FileUtils::getInstance()->getStringFromFile(path); - ssize_t size = strFileString.length(); - - CC_SAFE_DELETE_ARRAY(_documentBuffer); - _documentBuffer = new char[size + 1]; - memcpy(_documentBuffer, strFileString.c_str(), size); - _documentBuffer[size] = '\0'; - if (document.ParseInsitu<0>(_documentBuffer).HasParseError()) + if (strFileString != _fullPath) { - assert(0); - return false; + ssize_t size = strFileString.length(); + + CC_SAFE_DELETE_ARRAY(_documentBuffer); + _documentBuffer = new char[size + 1]; + memcpy(_documentBuffer, strFileString.c_str(), size); + _documentBuffer[size] = '\0'; + if (document.ParseInsitu<0>(_documentBuffer).HasParseError()) + { + assert(0); + return false; + } + _fullPath = strFileString; } + return true; } diff --git a/cocos/3d/CCBundle3D.h b/cocos/3d/CCBundle3D.h index ba124da7dd..5188873c4b 100644 --- a/cocos/3d/CCBundle3D.h +++ b/cocos/3d/CCBundle3D.h @@ -94,6 +94,7 @@ protected: char* _documentBuffer; rapidjson::Document document; + std::string _fullPath; bool _isBinary; };