From fb312a761c86297836b5b9fb2d90ef1e90b9e9b6 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 17 Dec 2015 16:53:38 +0800 Subject: [PATCH] update for 3D object render, revert old changes --- cocos/3d/CCMesh.cpp | 44 ++---------------- cocos/3d/CCMesh.h | 18 ++------ cocos/base/CCDirector.cpp | 3 -- cocos/renderer/CCTexture2D.h | 4 -- cocos/renderer/CCTextureCache.cpp | 8 +--- cocos/renderer/CCTextureCache.h | 5 -- extensions/Particle3D/CCParticle3DRender.cpp | 17 +++++++ extensions/Particle3D/CCParticle3DRender.h | 5 ++ extensions/Particle3D/CCParticleSystem3D.h | 4 ++ .../Particle3D/PU/CCPUBillboardChain.cpp | 16 ++----- extensions/Particle3D/PU/CCPURender.cpp | 46 ++++++++----------- extensions/Particle3D/PU/CCPURender.h | 5 +- extensions/cocos-ext.h | 1 + .../Classes/Sprite3DTest/Sprite3DTest.cpp | 35 +++++++++++++- .../Classes/Sprite3DTest/Sprite3DTest.h | 4 ++ 15 files changed, 98 insertions(+), 117 deletions(-) diff --git a/cocos/3d/CCMesh.cpp b/cocos/3d/CCMesh.cpp index 4a9a347abb..3ed8cae596 100644 --- a/cocos/3d/CCMesh.cpp +++ b/cocos/3d/CCMesh.cpp @@ -133,7 +133,6 @@ Mesh::Mesh() , _blendDirty(true) , _force2DQueue(false) , _texFile("") -, _enableCheckTexture(false) { } @@ -282,7 +281,7 @@ void Mesh::setTexture(Texture2D* tex) setTexture(tex, NTextureData::Usage::Diffuse); } -void Mesh::setTexture(Texture2D* tex, NTextureData::Usage usage) +void Mesh::setTexture(Texture2D* tex, NTextureData::Usage usage, bool cacheFileName) { // Texture must be saved for future use // it doesn't matter if the material is already set or not @@ -307,7 +306,8 @@ void Mesh::setTexture(Texture2D* tex, NTextureData::Usage usage) } bindMeshCommand(); - _texFile = tex->getPath(); + if (cacheFileName) + _texFile = tex->getPath(); } else if (usage == NTextureData::Usage::Normal) // currently only diffuse and normal are supported { @@ -382,9 +382,6 @@ void Mesh::draw(Renderer* renderer, float globalZOrder, const Mat4& transform, u if (isTransparent) flags |= Node::FLAGS_RENDER_AS_3D; - if (_enableCheckTexture) - this->checkTexture(); - _meshCommand.init(globalZ, _material, getVertexBuffer(), @@ -706,39 +703,4 @@ GLuint Mesh::getIndexBuffer() const { return _meshIndexData->getIndexBuffer()->getVBO(); } - -void Mesh::checkTexture() -{ - Texture2D* cacheTex = nullptr; - auto& texture = _textures[NTextureData::Usage::Diffuse]; - if (Director::getInstance()->getTextureCache()->isDirty()) - { - cacheTex = Director::getInstance()->getTextureCache()->getTextureForKey(_texFile); - if (cacheTex == nullptr) - { - cacheTex = getDummyTexture(); - } - } - else if (texture != nullptr && !texture->isValid()) - { - cacheTex = getDummyTexture(); - } - - if (cacheTex != nullptr && texture != cacheTex) - { - CC_SAFE_RETAIN(cacheTex); - CC_SAFE_RELEASE(texture); - texture = cacheTex; - - if (_material) { - auto technique = _material->_currentTechnique; - for (auto& pass : technique->_passes) - { - pass->setTexture(texture); - } - } - bindMeshCommand(); - } -} - NS_CC_END diff --git a/cocos/3d/CCMesh.h b/cocos/3d/CCMesh.h index c9ccd792b9..f8019011e6 100644 --- a/cocos/3d/CCMesh.h +++ b/cocos/3d/CCMesh.h @@ -109,8 +109,9 @@ public: * set texture * @param tex texture to be set * @param usage Usage of this texture + * @param whether refresh the cache file name */ - void setTexture(Texture2D* tex, NTextureData::Usage usage); + void setTexture(Texture2D* tex, NTextureData::Usage usage,bool cacheFileName = true); /** * set texture * @param texPath texture path @@ -224,19 +225,7 @@ public: */ void setForce2DQueue(bool force2D) { _force2DQueue = force2D; } - /** - * check texture - */ - void checkTexture(); - - /** - * set enable check texture, check texture each frame if enable is true. It is false by default - */ - void setEnableCheckTexture(bool enableCheckTexture) { _enableCheckTexture = enableCheckTexture; } - /** - * check texture each frame? - */ - bool enableCheckTexture() const { return _enableCheckTexture; } + std::string getTextureFileName(){ return _texFile; } CC_CONSTRUCTOR_ACCESS: @@ -280,7 +269,6 @@ protected: std::vector _spotLightUniformRangeInverseValues; std::string _texFile; - bool _enableCheckTexture; }; // end of 3d group diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index 1494486bf1..a6d612677a 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -328,9 +328,6 @@ void Director::drawScene() { calculateMPF(); } - - if (_textureCache != nullptr) - _textureCache->setDirty(false); } void Director::calculateDeltaTime() diff --git a/cocos/renderer/CCTexture2D.h b/cocos/renderer/CCTexture2D.h index ced2f2e596..c88dbe7fe7 100644 --- a/cocos/renderer/CCTexture2D.h +++ b/cocos/renderer/CCTexture2D.h @@ -406,10 +406,6 @@ public: /** Get a shader program from the texture.*/ GLProgram* getGLProgram() const; - /** Set if the texture is valid, when it been set as false, it will display as default "file missing texture" */ - void setValid(bool valid) { _valid = valid; } - - bool isValid() const { return _valid; } std::string getPath()const { return _filePath; } public: diff --git a/cocos/renderer/CCTextureCache.cpp b/cocos/renderer/CCTextureCache.cpp index bfbf59cfd2..960c76b4a8 100644 --- a/cocos/renderer/CCTextureCache.cpp +++ b/cocos/renderer/CCTextureCache.cpp @@ -59,7 +59,6 @@ TextureCache::TextureCache() : _loadingThread(nullptr) , _needQuit(false) , _asyncRefCount(0) -, _dirty(false) { } @@ -496,8 +495,7 @@ void TextureCache::removeTexture(Texture2D* texture) for( auto it=_textures.cbegin(); it!=_textures.cend(); /* nothing */ ) { if( it->second == texture ) { - texture->setValid(false); - texture->autorelease(); + it->second->release(); _textures.erase(it++); break; } else @@ -516,8 +514,7 @@ void TextureCache::removeTextureForKey(const std::string &textureKeyName) } if( it != _textures.end() ) { - it->second->setValid(false); - (it->second)->autorelease(); + it->second->release(); _textures.erase(it); } } @@ -626,7 +623,6 @@ void TextureCache::renameTextureWithKey(const std::string srcName, const std::st tex->initWithImage(image); _textures.insert(std::make_pair(fullpath, tex)); _textures.erase(it); - this->setDirty(true); } CC_SAFE_DELETE(image); } diff --git a/cocos/renderer/CCTextureCache.h b/cocos/renderer/CCTextureCache.h index d592a632de..0a5ee7586d 100644 --- a/cocos/renderer/CCTextureCache.h +++ b/cocos/renderer/CCTextureCache.h @@ -204,9 +204,6 @@ public: */ const std::string getTextureFilePath(Texture2D* texture)const; - void setDirty(bool dirty) { _dirty = dirty; } - bool isDirty() const { return _dirty; } - /** Reload texture from a new file. * This function is mainly for editor, won't suggest use it in game for performance reason. * @@ -242,8 +239,6 @@ protected: int _asyncRefCount; std::unordered_map _textures; - - bool _dirty; }; #if CC_ENABLE_CACHE_TEXTURE_DATA diff --git a/extensions/Particle3D/CCParticle3DRender.cpp b/extensions/Particle3D/CCParticle3DRender.cpp index 14ca60de3f..6e6cb19ff8 100755 --- a/extensions/Particle3D/CCParticle3DRender.cpp +++ b/extensions/Particle3D/CCParticle3DRender.cpp @@ -43,6 +43,7 @@ Particle3DQuadRender::Particle3DQuadRender() , _glProgramState(nullptr) , _indexBuffer(nullptr) , _vertexBuffer(nullptr) +, _texFile("") { } @@ -60,6 +61,7 @@ Particle3DQuadRender* Particle3DQuadRender::create(const std::string& texFile) auto ret = new (std::nothrow)Particle3DQuadRender(); if (ret && ret->initQuadRender(texFile)) { + ret->_texFile = texFile; ret->autorelease(); } else @@ -187,6 +189,8 @@ bool Particle3DQuadRender::initQuadRender( const std::string& texFile ) _texture = tex; glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_TEXTURE); } + else + _texture = nullptr; } auto glProgramState = GLProgramState::create(glProgram); glProgramState->retain(); @@ -213,6 +217,11 @@ bool Particle3DQuadRender::initQuadRender( const std::string& texFile ) return true; } +void Particle3DQuadRender::reset() +{ + this->initQuadRender(_texFile); +} + ////////////////////////////////////////////////////////////////////////////// Particle3DModelRender::Particle3DModelRender() : _spriteSize(Vec3::ONE) @@ -284,6 +293,14 @@ void Particle3DModelRender::render(Renderer* renderer, const Mat4 &transform, Pa } } +void Particle3DModelRender::reset() +{ + for (auto iter : _spriteList){ + iter->release(); + } + _spriteList.clear(); +} + // MARK: Particle3DRender Particle3DRender::Particle3DRender() diff --git a/extensions/Particle3D/CCParticle3DRender.h b/extensions/Particle3D/CCParticle3DRender.h index e2cf465222..2616071c0b 100644 --- a/extensions/Particle3D/CCParticle3DRender.h +++ b/extensions/Particle3D/CCParticle3DRender.h @@ -73,6 +73,8 @@ public: void copyAttributesTo (Particle3DRender *render); + virtual void reset(){} + CC_CONSTRUCTOR_ACCESS: Particle3DRender(); virtual ~Particle3DRender(); @@ -94,6 +96,7 @@ public: virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override; + virtual void reset()override; CC_CONSTRUCTOR_ACCESS: Particle3DQuadRender(); virtual ~Particle3DQuadRender(); @@ -118,6 +121,7 @@ protected: std::vector _posuvcolors; //vertex data std::vector _indexData; //index data + std::string _texFile; }; // particle render for Sprite3D @@ -128,6 +132,7 @@ public: virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override; + virtual void reset()override; CC_CONSTRUCTOR_ACCESS: Particle3DModelRender(); virtual ~Particle3DModelRender(); diff --git a/extensions/Particle3D/CCParticleSystem3D.h b/extensions/Particle3D/CCParticleSystem3D.h index ab577bb34f..fd5881cc75 100644 --- a/extensions/Particle3D/CCParticleSystem3D.h +++ b/extensions/Particle3D/CCParticleSystem3D.h @@ -204,6 +204,10 @@ public: * set particle render, can set your own particle render */ void setRender(Particle3DRender* render); + /** + * return particle render + */ + Particle3DRender* getRender(){ return _render; } /** * add particle affector */ diff --git a/extensions/Particle3D/PU/CCPUBillboardChain.cpp b/extensions/Particle3D/PU/CCPUBillboardChain.cpp index c023d108fc..2df9248e17 100644 --- a/extensions/Particle3D/PU/CCPUBillboardChain.cpp +++ b/extensions/Particle3D/PU/CCPUBillboardChain.cpp @@ -734,21 +734,15 @@ void PUBillboardChain::setBlendFunc(const BlendFunc& blendFunc) GLuint PUBillboardChain::getTextureName() { - if (Director::getInstance()->getTextureCache()->isDirty()) - { - if (Director::getInstance()->getTextureCache()->getTextureForKey(_texFile) == nullptr) - { - _texture = nullptr; - this->init(""); - } - else - this->init(_texFile); - } - else if (_texture != nullptr && !_texture->isValid()) + if (Director::getInstance()->getTextureCache()->getTextureForKey(_texFile) == nullptr) { _texture = nullptr; this->init(""); } + else if (_texture == nullptr) + { + this->init(_texFile); + } if (_texture == nullptr) return 0; diff --git a/extensions/Particle3D/PU/CCPURender.cpp b/extensions/Particle3D/PU/CCPURender.cpp index 6f7b2e8413..5d23e298bb 100644 --- a/extensions/Particle3D/PU/CCPURender.cpp +++ b/extensions/Particle3D/PU/CCPURender.cpp @@ -244,7 +244,7 @@ void PUParticle3DQuadRender::render(Renderer* renderer, const Mat4 &transform, P _stateBlock->setBlendFunc(particleSystem->getBlendFunc()); - GLuint texId = this->getTextureName(); + GLuint texId = (_texture ? _texture->getName() : 0); _meshCommand->init(0, texId, _glProgramState, @@ -525,6 +525,14 @@ PUParticle3DModelRender* PUParticle3DModelRender::clone() return mr; } +void PUParticle3DModelRender::reset() +{ + for (auto iter : _spriteList){ + iter->release(); + } + _spriteList.clear(); +} + PUParticle3DEntityRender::PUParticle3DEntityRender() : _meshCommand(nullptr) @@ -564,6 +572,8 @@ bool PUParticle3DEntityRender::initRender( const std::string &texFile ) _texture = tex; glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_TEXTURE); } + else + _texture = nullptr; } auto glProgramState = GLProgramState::create(glProgram); glProgramState->retain(); @@ -587,35 +597,16 @@ bool PUParticle3DEntityRender::initRender( const std::string &texFile ) return true; } -GLuint PUParticle3DEntityRender::getTextureName() -{ - if (Director::getInstance()->getTextureCache()->isDirty()) - { - if (Director::getInstance()->getTextureCache()->getTextureForKey(_texFile) == nullptr) - { - _texture = nullptr; - this->initRender(""); - } - else - this->initRender(_texFile); - } - else if (_texture != nullptr && !_texture->isValid()) - { - _texture = nullptr; - this->initRender(""); - } - - if (_texture == nullptr) - return 0; - - return _texture->getName(); -} - void PUParticle3DEntityRender::copyAttributesTo(PUParticle3DEntityRender *render) { PURender::copyAttributesTo(render); } +void PUParticle3DEntityRender::reset() +{ + this->initRender(_texFile); +} + PUParticle3DBoxRender::PUParticle3DBoxRender() { autoRotate = false; @@ -725,7 +716,7 @@ void PUParticle3DBoxRender::render( Renderer* renderer, const Mat4 &transform, P _vertexBuffer->updateVertices(&_vertices[0], vertexindex/* * sizeof(_posuvcolors[0])*/, 0); _indexBuffer->updateIndices(&_indices[0], index/* * sizeof(unsigned short)*/, 0); - GLuint texId = this->getTextureName(); + GLuint texId = (_texture ? _texture->getName() : 0); _stateBlock->setBlendFunc(_particleSystem->getBlendFunc()); _meshCommand->init(0, texId, @@ -889,8 +880,7 @@ void PUSphereRender::render( Renderer* renderer, const Mat4 &transform, Particle _vertexBuffer->updateVertices(&_vertices[0], vertexindex/* * sizeof(_posuvcolors[0])*/, 0); _indexBuffer->updateIndices(&_indices[0], index/* * sizeof(unsigned short)*/, 0); - GLuint texId = this->getTextureName(); - + GLuint texId = (_texture ? _texture->getName() : 0); _stateBlock->setBlendFunc(particleSystem->getBlendFunc()); _meshCommand->init( 0, diff --git a/extensions/Particle3D/PU/CCPURender.h b/extensions/Particle3D/PU/CCPURender.h index baf976c2b3..43232119a8 100644 --- a/extensions/Particle3D/PU/CCPURender.h +++ b/extensions/Particle3D/PU/CCPURender.h @@ -65,7 +65,7 @@ class CC_DLL PUParticle3DEntityRender : public PURender { public: void copyAttributesTo(PUParticle3DEntityRender *render); - + virtual void reset()override; CC_CONSTRUCTOR_ACCESS: PUParticle3DEntityRender(); virtual ~PUParticle3DEntityRender(); @@ -73,8 +73,6 @@ CC_CONSTRUCTOR_ACCESS: protected: bool initRender(const std::string &texFile); - GLuint getTextureName(); - protected: struct VertexInfo @@ -189,6 +187,7 @@ public: virtual PUParticle3DModelRender* clone() override; void copyAttributesTo(PUParticle3DModelRender *render); + virtual void reset()override; CC_CONSTRUCTOR_ACCESS: PUParticle3DModelRender(); virtual ~PUParticle3DModelRender(); diff --git a/extensions/cocos-ext.h b/extensions/cocos-ext.h index 226fc5dd31..9d491a1070 100644 --- a/extensions/cocos-ext.h +++ b/extensions/cocos-ext.h @@ -21,6 +21,7 @@ #include "ExtensionDeprecated.h" // Particle System, include Particle Universe Particle System +#include "Particle3D/CCParticle3DRender.h" #include "Particle3D/CCParticleSystem3D.h" #include "Particle3D/PU/CCPUParticleSystem3D.h" diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 5c6da55cce..bae4787039 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -2602,6 +2602,7 @@ Sprite3DPropertyTest::Sprite3DPropertyTest() _sprite->setPosition(20.f, 0.f); _sprite->setRotation3D(Vec3(0, 180, 0)); _meshTex = _sprite->getMesh()->getTexture(); + _texFile = _meshTex->getPath(); addChild(_sprite); setCameraMask(2); @@ -2616,11 +2617,14 @@ Sprite3DPropertyTest::Sprite3DPropertyTest() auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(Sprite3DPropertyTest::printMeshName, this)); auto label2 = Label::createWithTTF(ttfConfig, "Remove Used Texture"); auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DPropertyTest::removeUsedTexture, this)); + auto label3 = Label::createWithTTF(ttfConfig, "Reset"); + auto item3 = MenuItemLabel::create(label3, CC_CALLBACK_1(Sprite3DPropertyTest::resetTexture, this)); item1->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 4)); item2->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 5)); + item3->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 6)); - auto pMenu1 = Menu::create(item1, item2, nullptr); + auto pMenu1 = Menu::create(item1, item2, item3,nullptr); pMenu1->setPosition(Vec2(0, 0)); this->addChild(pMenu1, 10); @@ -2654,5 +2658,34 @@ void Sprite3DPropertyTest::removeUsedTexture(cocos2d::Ref* sender) if (_meshTex != nullptr) { TextureCache::getInstance()->removeTexture(_meshTex); + this->refreshSpriteRender(); + } +} + +void Sprite3DPropertyTest::resetTexture(cocos2d::Ref* sender) +{ + if (_meshTex != nullptr) + { + _meshTex = TextureCache::getInstance()->addImage(_texFile); + this->refreshSpriteRender(); + } +} + +void Sprite3DPropertyTest::refreshSpriteRender() +{ + Vector meshes = _sprite->getMeshes(); + for (Mesh* mesh : meshes) + { + std::string file = mesh->getTextureFileName(); + Texture2D* cacheTex = Director::getInstance()->getTextureCache()->getTextureForKey(file); + if (cacheTex == nullptr) + { + unsigned char data[] = { 255, 0, 0, 255 };//1*1 red picture + Image * image = new (std::nothrow) Image(); + image->initWithRawData(data, sizeof(data), 1, 1, sizeof(unsigned char)); + cacheTex = Director::getInstance()->getTextureCache()->addImage(image, "/dummyTexture"); + image->release(); + } + mesh->setTexture(cacheTex, cocos2d::NTextureData::Usage::Diffuse, false); } } \ No newline at end of file diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h index 301b9f9356..bbfd47652c 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h @@ -606,9 +606,13 @@ public: void printMeshName(cocos2d::Ref* sender); void removeUsedTexture(cocos2d::Ref* sender); + void resetTexture(cocos2d::Ref* sender); + + void refreshSpriteRender(); protected: cocos2d::Sprite3D* _sprite; cocos2d::Texture2D* _meshTex; + std::string _texFile; }; #endif