From 137d180dc9a0150edfe1b8bb1eb408e4ceca249c Mon Sep 17 00:00:00 2001 From: yangxiao Date: Mon, 7 Sep 2015 13:59:08 +0800 Subject: [PATCH] fix material clone --- cocos/renderer/CCMaterial.cpp | 32 ++++---------------------------- cocos/renderer/CCMaterial.h | 21 +-------------------- cocos/renderer/CCRenderState.h | 6 ++++++ 3 files changed, 11 insertions(+), 48 deletions(-) diff --git a/cocos/renderer/CCMaterial.cpp b/cocos/renderer/CCMaterial.cpp index 48e0c8faff..9ff10ac6b4 100644 --- a/cocos/renderer/CCMaterial.cpp +++ b/cocos/renderer/CCMaterial.cpp @@ -45,9 +45,6 @@ NS_CC_BEGIN -Material* Material::_diffuseSkinnedMaterial = nullptr; -Material* Material::_diffuseMaterial = nullptr; - // Helpers declaration static const char* getOptionalString(Properties* properties, const char* key, const char* defaultValue); static bool isValidUniform(const char* name); @@ -428,7 +425,6 @@ Material::Material() : _name("") , _target(nullptr) , _currentTechnique(nullptr) -, _materialType(Material::MaterialType::CUSTOM) { } @@ -446,13 +442,16 @@ Material* Material::clone() const for (const auto& technique: _techniques) { auto t = technique->clone(); + t->setParent(material); + for (ssize_t i = 0; i < t->getPassCount(); i++) { + t->getPassByIndex(i)->setParent(t); + } material->_techniques.pushBack(t); } // current technique auto name = _currentTechnique->getName(); material->_currentTechnique = material->getTechniqueByName(name); - material->_materialType = _materialType; material->autorelease(); } @@ -502,29 +501,6 @@ ssize_t Material::getTechniqueCount() const return _techniques.size(); } -Material* Material::createDiffuseMaterial(bool skinned) -{ - if (skinned) - { - return _diffuseSkinnedMaterial->clone(); - } - - return _diffuseMaterial->clone(); -} - -void Material::loadDefaultMaterial(Material::MaterialType type) -{ - if (_diffuseMaterial == nullptr) - { - auto glProgramState = GLProgramState::create(<#cocos2d::GLProgram *glprogram#>) - _diffuseMaterial = Material::createWithGLStateProgram(); - } - if (_diffuseSkinnedMaterial == nullptr) - { - _diffuseSkinnedMaterial = Material::createWithGLStateProgram(); - } -} - // Helpers implementation static bool isValidUniform(const char* name) { diff --git a/cocos/renderer/CCMaterial.h b/cocos/renderer/CCMaterial.h index 3181427c9c..4bde1f6af1 100644 --- a/cocos/renderer/CCMaterial.h +++ b/cocos/renderer/CCMaterial.h @@ -63,14 +63,6 @@ class CC_DLL Material : public RenderState public: - enum class MaterialType - { - DIFFUSE, - NORMAL, - LIGHTMAP, - CUSTOM, - }; - /** * Creates a Material using the data from the Properties object defined at the specified URL, * where the URL is of the format ".#//.../" @@ -128,12 +120,7 @@ public: void setTechnique(const std::string& techniqueName); /** returns a clone (deep-copy) of the material */ - Material* clone() const; - - /** create default diffuse material */ - static Material* createDiffuseMaterial(bool skinned); - - MaterialType getMaterialType() const { return _materialType; } + virtual Material* clone() const; protected: Material(); @@ -151,8 +138,6 @@ protected: bool parseSampler(GLProgramState* glProgramState, Properties* properties); bool parseUniform(GLProgramState* programState, Properties* properties, const char* uniformName); bool parseRenderState(RenderState* renderState, Properties* properties); - - static void loadDefaultMaterial(MaterialType type); // material name std::string _name; @@ -166,10 +151,6 @@ protected: // weak reference Node* _target; - MaterialType _materialType; - static Material* _diffuseSkinnedMaterial; - static Material* _diffuseMaterial; -// static Material* }; NS_CC_END diff --git a/cocos/renderer/CCRenderState.h b/cocos/renderer/CCRenderState.h index c26cd3bc48..c30b3710ab 100644 --- a/cocos/renderer/CCRenderState.h +++ b/cocos/renderer/CCRenderState.h @@ -84,6 +84,12 @@ public: * Returns the topmost RenderState in the hierarchy below the given RenderState. */ RenderState* getTopmost(RenderState* below); + + /** + * Set parent RenderState + * @param parent Parent RenderState + */ + void setParent(RenderState* parent) { _parent = parent; } enum Blend {