2015-05-06 04:07:32 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Ideas taken from:
|
|
|
|
- GamePlay3D: http://gameplay3d.org/
|
|
|
|
- OGRE3D: http://www.ogre3d.org/
|
|
|
|
- Qt3D: http://qt-project.org/
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "renderer/CCMaterial.h"
|
|
|
|
#include "renderer/CCTechnique.h"
|
|
|
|
#include "renderer/CCPass.h"
|
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
#include "renderer/CCTexture2D.h"
|
|
|
|
#include "renderer/CCGLProgram.h"
|
|
|
|
#include "renderer/CCGLProgramState.h"
|
2015-05-12 12:31:33 +08:00
|
|
|
#include "base/CCProperties.h"
|
2015-05-06 04:07:32 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
|
|
|
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2015-05-06 04:07:32 +08:00
|
|
|
#define strcasecmp _stricmp
|
|
|
|
#endif
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
NS_CC_BEGIN
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
// Helpers declaration
|
2015-05-12 12:31:33 +08:00
|
|
|
static const char* getOptionalString(Properties* properties, const char* key, const char* defaultValue);
|
2015-05-06 04:07:32 +08:00
|
|
|
static bool isValidUniform(const char* name);
|
|
|
|
|
|
|
|
Material* Material::createWithFilename(const std::string& filepath)
|
|
|
|
{
|
|
|
|
auto validfilename = FileUtils::getInstance()->fullPathForFilename(filepath);
|
|
|
|
if (validfilename.size() > 0) {
|
|
|
|
auto mat = new (std::nothrow) Material();
|
|
|
|
if (mat && mat->initWithFile(validfilename))
|
|
|
|
{
|
|
|
|
mat->autorelease();
|
|
|
|
return mat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
Material* Material::createWithProperties(Properties* materialProperties)
|
|
|
|
{
|
|
|
|
auto mat = new (std::nothrow) Material();
|
|
|
|
if (mat && mat->initWithProperties(materialProperties))
|
|
|
|
{
|
|
|
|
mat->autorelease();
|
|
|
|
return mat;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
Material* Material::createWithGLStateProgram(GLProgramState* programState)
|
|
|
|
{
|
|
|
|
CCASSERT(programState, "Invalid GL Program State");
|
|
|
|
|
|
|
|
auto mat = new (std::nothrow) Material();
|
|
|
|
if (mat && mat->initWithGLProgramState(programState))
|
|
|
|
{
|
|
|
|
mat->autorelease();
|
|
|
|
return mat;
|
|
|
|
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Material::initWithGLProgramState(cocos2d::GLProgramState *state)
|
|
|
|
{
|
|
|
|
auto technique = Technique::createWithGLProgramState(this, state);
|
|
|
|
if (technique) {
|
|
|
|
_techniques.pushBack(technique);
|
|
|
|
|
|
|
|
// weak pointer
|
|
|
|
_currentTechnique = technique;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Material::initWithFile(const std::string& validfilename)
|
|
|
|
{
|
2015-05-15 08:24:27 +08:00
|
|
|
// Warning: properties is not a "Ref" object, must be manually deleted
|
2015-05-20 04:51:52 +08:00
|
|
|
Properties* properties = Properties::createNonRefCounted(validfilename);
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
// get the first material
|
|
|
|
parseProperties((strlen(properties->getNamespace()) > 0) ? properties : properties->getNextNamespace());
|
2015-05-15 08:24:27 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(properties);
|
2015-05-06 04:07:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
bool Material::initWithProperties(Properties* materialProperties)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
return parseProperties(materialProperties);
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
void Material::setTarget(cocos2d::Node *target)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
_target = target;
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
bool Material::parseProperties(Properties* materialProperties)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
setName(materialProperties->getId());
|
|
|
|
auto space = materialProperties->getNextNamespace();
|
|
|
|
while (space)
|
|
|
|
{
|
|
|
|
const char* name = space->getNamespace();
|
2015-05-14 05:12:37 +08:00
|
|
|
if (strcmp(name, "technique") == 0)
|
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
parseTechnique(space);
|
|
|
|
}
|
2015-05-14 05:12:37 +08:00
|
|
|
else if (strcmp(name, "renderState") == 0)
|
|
|
|
{
|
|
|
|
parseRenderState(this, space);
|
|
|
|
}
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
space = materialProperties->getNextNamespace();
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
bool Material::parseTechnique(Properties* techniqueProperties)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
auto technique = Technique::create(this);
|
|
|
|
_techniques.pushBack(technique);
|
|
|
|
|
|
|
|
// first one is the default one
|
|
|
|
if (!_currentTechnique)
|
|
|
|
_currentTechnique = technique;
|
|
|
|
|
|
|
|
// name
|
2015-05-12 12:31:33 +08:00
|
|
|
technique->setName(techniqueProperties->getId());
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
// passes
|
2015-05-12 12:31:33 +08:00
|
|
|
auto space = techniqueProperties->getNextNamespace();
|
|
|
|
while (space)
|
|
|
|
{
|
|
|
|
const char* name = space->getNamespace();
|
2015-05-14 05:12:37 +08:00
|
|
|
if (strcmp(name, "pass") == 0)
|
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
parsePass(technique, space);
|
|
|
|
}
|
2015-05-14 05:12:37 +08:00
|
|
|
else if (strcmp(name, "renderState") == 0)
|
|
|
|
{
|
|
|
|
parseRenderState(this, space);
|
|
|
|
}
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
space = techniqueProperties->getNextNamespace();
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
bool Material::parsePass(Technique* technique, Properties* passProperties)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
auto pass = Pass::create(technique);
|
|
|
|
technique->addPass(pass);
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
// Pass can have 3 different namespaces:
|
|
|
|
// - one or more "sampler"
|
|
|
|
// - one "renderState"
|
|
|
|
// - one "shader"
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
auto space = passProperties->getNextNamespace();
|
|
|
|
while (space)
|
|
|
|
{
|
|
|
|
const char* name = space->getNamespace();
|
2015-06-06 05:53:30 +08:00
|
|
|
if (strcmp(name, "shader") == 0)
|
2015-05-12 12:31:33 +08:00
|
|
|
parseShader(pass, space);
|
|
|
|
else if (strcmp(name, "renderState") == 0)
|
|
|
|
parseRenderState(pass, space);
|
|
|
|
else {
|
|
|
|
CCASSERT(false, "Invalid namespace");
|
|
|
|
return false;
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
space = passProperties->getNextNamespace();
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
// cocos2d-x doesn't support Samplers yet. But will be added soon
|
2015-06-06 05:53:30 +08:00
|
|
|
bool Material::parseSampler(GLProgramState* glProgramState, Properties* samplerProperties)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2015-06-06 05:53:30 +08:00
|
|
|
CCASSERT(samplerProperties->getId(), "Sampler must have an id. The id is the uniform name");
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
// required
|
2015-06-06 05:53:30 +08:00
|
|
|
auto filename = samplerProperties->getString("path");
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
auto texture = Director::getInstance()->getTextureCache()->addImage(filename);
|
|
|
|
if (!texture) {
|
|
|
|
CCLOG("Invalid filepath");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// optionals
|
|
|
|
|
|
|
|
{
|
|
|
|
Texture2D::TexParams texParams;
|
|
|
|
|
|
|
|
// mipmap
|
|
|
|
bool usemipmap = false;
|
2015-06-06 05:53:30 +08:00
|
|
|
const char* mipmap = getOptionalString(samplerProperties, "mipmap", "false");
|
2015-05-06 04:07:32 +08:00
|
|
|
if (mipmap && strcasecmp(mipmap, "true")==0) {
|
|
|
|
texture->generateMipmap();
|
|
|
|
usemipmap = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// valid options: REPEAT, CLAMP
|
2015-06-06 05:53:30 +08:00
|
|
|
const char* wrapS = getOptionalString(samplerProperties, "wrapS", "CLAMP_TO_EDGE");
|
2015-05-06 04:07:32 +08:00
|
|
|
if (strcasecmp(wrapS, "REPEAT")==0)
|
|
|
|
texParams.wrapS = GL_REPEAT;
|
|
|
|
else if(strcasecmp(wrapS, "CLAMP_TO_EDGE")==0)
|
|
|
|
texParams.wrapS = GL_CLAMP_TO_EDGE;
|
|
|
|
else
|
|
|
|
CCLOG("Invalid wrapS: %s", wrapS);
|
|
|
|
|
|
|
|
|
|
|
|
// valid options: REPEAT, CLAMP
|
2015-06-06 05:53:30 +08:00
|
|
|
const char* wrapT = getOptionalString(samplerProperties, "wrapT", "CLAMP_TO_EDGE");
|
2015-05-06 04:07:32 +08:00
|
|
|
if (strcasecmp(wrapT, "REPEAT")==0)
|
|
|
|
texParams.wrapT = GL_REPEAT;
|
|
|
|
else if(strcasecmp(wrapT, "CLAMP_TO_EDGE")==0)
|
|
|
|
texParams.wrapT = GL_CLAMP_TO_EDGE;
|
|
|
|
else
|
|
|
|
CCLOG("Invalid wrapT: %s", wrapT);
|
|
|
|
|
|
|
|
|
|
|
|
// valid options: NEAREST, LINEAR, NEAREST_MIPMAP_NEAREST, LINEAR_MIPMAP_NEAREST, NEAREST_MIPMAP_LINEAR, LINEAR_MIPMAP_LINEAR
|
2015-06-06 05:53:30 +08:00
|
|
|
const char* minFilter = getOptionalString(samplerProperties, "minFilter", usemipmap ? "LINEAR_MIPMAP_NEAREST" : "LINEAR");
|
2015-05-06 04:07:32 +08:00
|
|
|
if (strcasecmp(minFilter, "NEAREST")==0)
|
|
|
|
texParams.minFilter = GL_NEAREST;
|
|
|
|
else if(strcasecmp(minFilter, "LINEAR")==0)
|
|
|
|
texParams.minFilter = GL_LINEAR;
|
|
|
|
else if(strcasecmp(minFilter, "NEAREST_MIPMAP_NEAREST")==0)
|
|
|
|
texParams.minFilter = GL_NEAREST_MIPMAP_NEAREST;
|
|
|
|
else if(strcasecmp(minFilter, "LINEAR_MIPMAP_NEAREST")==0)
|
|
|
|
texParams.minFilter = GL_LINEAR_MIPMAP_NEAREST;
|
|
|
|
else if(strcasecmp(minFilter, "NEAREST_MIPMAP_LINEAR")==0)
|
|
|
|
texParams.minFilter = GL_NEAREST_MIPMAP_LINEAR;
|
|
|
|
else if(strcasecmp(minFilter, "LINEAR_MIPMAP_LINEAR")==0)
|
|
|
|
texParams.minFilter = GL_LINEAR_MIPMAP_LINEAR;
|
|
|
|
else
|
|
|
|
CCLOG("Invalid minFilter: %s", minFilter);
|
|
|
|
|
|
|
|
// valid options: NEAREST, LINEAR
|
2015-06-06 05:53:30 +08:00
|
|
|
const char* magFilter = getOptionalString(samplerProperties, "magFilter", "LINEAR");
|
2015-05-06 04:07:32 +08:00
|
|
|
if (strcasecmp(magFilter, "NEAREST")==0)
|
|
|
|
texParams.magFilter = GL_NEAREST;
|
|
|
|
else if(strcasecmp(magFilter, "LINEAR")==0)
|
|
|
|
texParams.magFilter = GL_LINEAR;
|
|
|
|
else
|
|
|
|
CCLOG("Invalid magFilter: %s", magFilter);
|
|
|
|
|
|
|
|
texture->setTexParameters(texParams);
|
|
|
|
}
|
|
|
|
|
2015-06-06 05:53:30 +08:00
|
|
|
glProgramState->setUniformTexture(samplerProperties->getId(), texture);
|
2015-05-06 04:07:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
bool Material::parseShader(Pass* pass, Properties* shaderProperties)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
// vertexShader
|
2015-05-12 12:31:33 +08:00
|
|
|
const char* vertShader = getOptionalString(shaderProperties, "vertexShader", nullptr);
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
// fragmentShader
|
2015-05-12 12:31:33 +08:00
|
|
|
const char* fragShader = getOptionalString(shaderProperties, "fragmentShader", nullptr);
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
// compileTimeDefines
|
2015-05-12 12:31:33 +08:00
|
|
|
const char* compileTimeDefines = getOptionalString(shaderProperties, "defines", "");
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
if (vertShader && fragShader)
|
|
|
|
{
|
|
|
|
auto glProgramState = GLProgramState::getOrCreateWithShaders(vertShader, fragShader, compileTimeDefines);
|
|
|
|
pass->setGLProgramState(glProgramState);
|
|
|
|
|
|
|
|
// Parse uniforms only if the GLProgramState was created
|
2015-05-12 12:31:33 +08:00
|
|
|
auto property = shaderProperties->getNextProperty();
|
|
|
|
while (property)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
if (isValidUniform(property))
|
|
|
|
{
|
|
|
|
parseUniform(glProgramState, shaderProperties, property);
|
|
|
|
}
|
|
|
|
|
|
|
|
property = shaderProperties->getNextProperty();
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
2015-06-06 05:53:30 +08:00
|
|
|
auto space = shaderProperties->getNextNamespace();
|
|
|
|
while (space)
|
|
|
|
{
|
|
|
|
const char* name = space->getNamespace();
|
|
|
|
if (strcmp(name, "sampler") == 0)
|
|
|
|
{
|
|
|
|
parseSampler(glProgramState, space);
|
|
|
|
}
|
|
|
|
space = shaderProperties->getNextNamespace();
|
|
|
|
}
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
bool Material::parseUniform(GLProgramState* programState, Properties* properties, const char* uniformName)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2015-06-03 07:56:59 +08:00
|
|
|
bool ret = true;
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
auto type = properties->getType(uniformName);
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
switch (type) {
|
|
|
|
case Properties::Type::NUMBER:
|
|
|
|
{
|
|
|
|
auto f = properties->getFloat(uniformName);
|
|
|
|
programState->setUniformFloat(uniformName, f);
|
|
|
|
break;
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
case Properties::Type::VECTOR2:
|
|
|
|
{
|
|
|
|
Vec2 v2;
|
|
|
|
properties->getVec2(uniformName, &v2);
|
|
|
|
programState->setUniformVec2(uniformName, v2);
|
|
|
|
break;
|
|
|
|
}
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
case Properties::Type::VECTOR3:
|
|
|
|
{
|
|
|
|
Vec3 v3;
|
|
|
|
properties->getVec3(uniformName, &v3);
|
|
|
|
programState->setUniformVec3(uniformName, v3);
|
|
|
|
break;
|
|
|
|
}
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
case Properties::Type::VECTOR4:
|
|
|
|
{
|
|
|
|
Vec4 v4;
|
|
|
|
properties->getVec4(uniformName, &v4);
|
|
|
|
programState->setUniformVec4(uniformName, v4);
|
|
|
|
break;
|
|
|
|
}
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
case Properties::Type::MATRIX:
|
|
|
|
{
|
|
|
|
Mat4 m4;
|
|
|
|
properties->getMat4(uniformName, &m4);
|
|
|
|
programState->setUniformMat4(uniformName, m4);
|
|
|
|
break;
|
|
|
|
}
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
case Properties::Type::STRING:
|
2015-05-12 12:31:33 +08:00
|
|
|
default:
|
2015-06-03 07:56:59 +08:00
|
|
|
{
|
|
|
|
// Assume this is a parameter auto-binding.
|
|
|
|
programState->setParameterAutoBinding(uniformName, properties->getString());
|
2015-05-12 12:31:33 +08:00
|
|
|
break;
|
2015-06-03 07:56:59 +08:00
|
|
|
}
|
2015-05-12 12:31:33 +08:00
|
|
|
}
|
2015-06-03 07:56:59 +08:00
|
|
|
return ret;
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
|
2015-05-14 05:12:37 +08:00
|
|
|
bool Material::parseRenderState(RenderState* renderState, Properties* properties)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2015-05-14 05:12:37 +08:00
|
|
|
auto state = renderState->getStateBlock();
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
auto property = properties->getNextProperty();
|
|
|
|
while (property)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
// Parse uniforms only if the GLProgramState was created
|
|
|
|
// Render state only can have "strings" or numbers as values. No new namespaces
|
|
|
|
state->setState(property, properties->getString(property));
|
|
|
|
|
|
|
|
property = properties->getNextProperty();
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Material::setName(const std::string&name)
|
|
|
|
{
|
|
|
|
_name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Material::getName() const
|
|
|
|
{
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
|
|
|
Material::Material()
|
|
|
|
: _name("")
|
|
|
|
, _currentTechnique(nullptr)
|
2017-02-04 09:46:32 +08:00
|
|
|
, _target(nullptr)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Material::~Material()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-05-14 08:39:39 +08:00
|
|
|
Material* Material::clone() const
|
|
|
|
{
|
|
|
|
auto material = new (std::nothrow) Material();
|
|
|
|
if (material)
|
|
|
|
{
|
|
|
|
RenderState::cloneInto(material);
|
|
|
|
|
|
|
|
for (const auto& technique: _techniques)
|
|
|
|
{
|
|
|
|
auto t = technique->clone();
|
2015-09-10 09:23:02 +08:00
|
|
|
t->_parent = material;
|
2015-05-14 08:39:39 +08:00
|
|
|
material->_techniques.pushBack(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
// current technique
|
|
|
|
auto name = _currentTechnique->getName();
|
|
|
|
material->_currentTechnique = material->getTechniqueByName(name);
|
|
|
|
|
|
|
|
material->autorelease();
|
|
|
|
}
|
|
|
|
return material;
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
Technique* Material::getTechnique() const
|
|
|
|
{
|
|
|
|
return _currentTechnique;
|
|
|
|
}
|
2015-05-12 12:31:33 +08:00
|
|
|
|
|
|
|
const Vector<Technique*>& Material::getTechniques() const
|
|
|
|
{
|
|
|
|
return _techniques;
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
Technique* Material::getTechniqueByName(const std::string& name)
|
|
|
|
{
|
|
|
|
for(const auto& technique : _techniques) {
|
|
|
|
if (technique->getName().compare(name)==0)
|
|
|
|
return technique;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Technique* Material::getTechniqueByIndex(ssize_t index)
|
|
|
|
{
|
|
|
|
CC_ASSERT(index>=0 && index<_techniques.size() && "Invalid size");
|
|
|
|
|
|
|
|
return _techniques.at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Material::addTechnique(Technique* technique)
|
|
|
|
{
|
|
|
|
_techniques.pushBack(technique);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Material::setTechnique(const std::string& techniqueName)
|
|
|
|
{
|
|
|
|
auto technique = getTechniqueByName(techniqueName);
|
|
|
|
if (technique)
|
|
|
|
_currentTechnique = technique;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Material::getTechniqueCount() const
|
|
|
|
{
|
|
|
|
return _techniques.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helpers implementation
|
|
|
|
static bool isValidUniform(const char* name)
|
|
|
|
{
|
|
|
|
return !(strcmp(name, "defines")==0 ||
|
|
|
|
strcmp(name, "vertexShader")==0 ||
|
|
|
|
strcmp(name, "fragmentShader")==0);
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
static const char* getOptionalString(Properties* properties, const char* key, const char* defaultValue)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
|
|
|
|
const char* ret = properties->getString(key);
|
|
|
|
if (!ret)
|
|
|
|
ret = defaultValue;
|
|
|
|
|
|
|
|
return ret;
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
NS_CC_END
|