axmol/extensions/Particle3D/PU/CCPUMaterialTranslator.cpp

434 lines
16 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (C) 2013 Henry van Merode. All rights reserved.
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
https://axis-project.github.io/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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.
****************************************************************************/
#include "CCPUMaterialTranslator.h"
#include "extensions/Particle3D/PU/CCPUMaterialManager.h"
#include "renderer/backend/Types.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
enum MaterialToken
{
TOKEN_MAT_TECHNIQUE,
TOKEN_MAT_PASS,
2021-12-25 10:04:45 +08:00
// pass
2019-11-23 20:27:39 +08:00
TOKEN_MAT_LIGHTING,
TOKEN_MAT_AMIBIENT,
TOKEN_MAT_DIFFUSE,
TOKEN_MAT_SPECULAR,
TOKEN_MAT_EMISSIVE,
TOKEN_MAT_BLEND,
TOKEN_MAT_DEPTH_CHECK,
TOKEN_MAT_DEPTH_WRITE,
TOKEN_MAT_TEXTURE_UNIT,
2021-12-25 10:04:45 +08:00
// texture unit
2019-11-23 20:27:39 +08:00
TOKEN_MAT_TEXTURE_FILE,
TOKEN_MAT_TEXTURE_WRAP,
2021-12-25 10:04:45 +08:00
// status
2019-11-23 20:27:39 +08:00
TOKEN_MAT_ON,
TOKEN_MAT_OFF,
TOKEN_MAT_BLEND_ADD,
TOKEN_MAT_BLEND_ALPHA,
TOKEN_MAT_BLEND_COLOR,
TOKEN_MAT_BLEND_MODULATE,
TOKEN_MAT_BLEND_REPLACE,
TOKEN_MAT_BLEND_SRC_COLOR,
TOKEN_MAT_BLEND_ONE,
TOKEN_MAT_BLEND_ZERO,
TOKEN_MAT_BLEND_DEST_COLOR,
TOKEN_MAT_TEXTURE_REPEAT,
TOKEN_MAT_TEXTURE_CLAMP,
TOKEN_MAT_TEXTURE_MIRROR,
};
static const std::string matToken[] = {
"technique",
"pass",
2021-12-25 10:04:45 +08:00
// pass
2019-11-23 20:27:39 +08:00
"lighting",
"ambient",
"diffuse",
"specular",
"emissive",
"scene_blend",
"depth_check",
"depth_write",
"texture_unit",
2021-12-25 10:04:45 +08:00
// texture unit
2019-11-23 20:27:39 +08:00
"texture",
"tex_address_mode",
2021-12-25 10:04:45 +08:00
// status
2019-11-23 20:27:39 +08:00
"on",
"off",
"add",
"alpha_blend",
"colour_blend",
"modulate",
"replace",
"src_colour",
"one",
"zero",
"dest_colour",
"repeat",
"clamp",
"mirror",
};
2021-12-25 10:04:45 +08:00
CCPUMaterialTranslator::CCPUMaterialTranslator() //: mTechnique(0)
{}
CCPUMaterialTranslator::~CCPUMaterialTranslator() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void CCPUMaterialTranslator::translate(PUScriptCompiler* compiler, PUAbstractNode* node)
2019-11-23 20:27:39 +08:00
{
PUObjectAbstractNode* obj = reinterpret_cast<PUObjectAbstractNode*>(node);
2021-12-25 10:04:45 +08:00
// PUObjectAbstractNode* parent = obj->parent ? reinterpret_cast<PUObjectAbstractNode*>(obj->parent) : 0;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_material = new PUMaterial();
2019-11-23 20:27:39 +08:00
_material->fileName = obj->file;
2021-12-25 10:04:45 +08:00
_material->name = obj->name;
2019-11-23 20:27:39 +08:00
_material->autorelease();
_ms->addMaterial(_material);
obj->context = _material;
2021-12-25 10:04:45 +08:00
for (PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if ((*i)->type == ANT_OBJECT)
2019-11-23 20:27:39 +08:00
{
PUObjectAbstractNode* child = reinterpret_cast<PUObjectAbstractNode*>(*i);
2021-12-25 10:04:45 +08:00
if (child->cls == matToken[TOKEN_MAT_TECHNIQUE])
2019-11-23 20:27:39 +08:00
{
PUMaterialTechniqueTranslator materialTechniqueTranslator;
materialTechniqueTranslator.translate(compiler, *i);
}
}
}
}
2021-12-25 10:04:45 +08:00
void CCPUMaterialTranslator::setMaterialSystem(PUMaterialCache* ms)
2019-11-23 20:27:39 +08:00
{
_ms = ms;
}
2021-12-25 10:04:45 +08:00
void PUMaterialTechniqueTranslator::translate(PUScriptCompiler* compiler, PUAbstractNode* node)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
PUObjectAbstractNode* obj = reinterpret_cast<PUObjectAbstractNode*>(node);
2019-11-23 20:27:39 +08:00
PUObjectAbstractNode* parent = obj->parent ? reinterpret_cast<PUObjectAbstractNode*>(obj->parent) : 0;
if (parent)
obj->context = parent->context;
2021-12-25 10:04:45 +08:00
for (PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if ((*i)->type == ANT_OBJECT)
2019-11-23 20:27:39 +08:00
{
PUObjectAbstractNode* child = reinterpret_cast<PUObjectAbstractNode*>(*i);
2021-12-25 10:04:45 +08:00
if (child->cls == matToken[TOKEN_MAT_PASS])
2019-11-23 20:27:39 +08:00
{
PUMaterialPassTranslator materialPassTranslator;
materialPassTranslator.translate(compiler, *i);
}
}
}
}
2021-12-25 10:04:45 +08:00
void PUMaterialPassTranslator::translate(PUScriptCompiler* compiler, PUAbstractNode* node)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
PUObjectAbstractNode* obj = reinterpret_cast<PUObjectAbstractNode*>(node);
2019-11-23 20:27:39 +08:00
PUObjectAbstractNode* parent = obj->parent ? reinterpret_cast<PUObjectAbstractNode*>(obj->parent) : 0;
if (parent)
obj->context = parent->context;
2021-12-25 10:04:45 +08:00
PUMaterial* material = static_cast<PUMaterial*>(obj->context);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
for (PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if ((*i)->type == ANT_PROPERTY)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
PUPropertyAbstractNode* prop = reinterpret_cast<PUPropertyAbstractNode*>((*i));
2019-11-23 20:27:39 +08:00
if (prop->name == matToken[TOKEN_MAT_LIGHTING])
{
if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_LIGHTING], VAL_STRING))
{
std::string val;
2021-12-25 10:04:45 +08:00
if (getString(*prop->values.front(), &val))
2019-11-23 20:27:39 +08:00
{
if (val == matToken[TOKEN_MAT_ON])
{
material->isEnabledLight = true;
}
else if (val == matToken[TOKEN_MAT_OFF])
{
material->isEnabledLight = false;
}
}
}
}
else if (prop->name == matToken[TOKEN_MAT_AMIBIENT])
{
if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_AMIBIENT], VAL_VECTOR4))
{
Vec4 val;
2021-12-25 10:04:45 +08:00
if (getVector4(prop->values.begin(), prop->values.end(), &val))
2019-11-23 20:27:39 +08:00
{
material->ambientColor = val;
}
}
}
else if (prop->name == matToken[TOKEN_MAT_DIFFUSE])
{
if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_AMIBIENT], VAL_VECTOR4))
{
Vec4 val;
2021-12-25 10:04:45 +08:00
if (getVector4(prop->values.begin(), prop->values.end(), &val))
2019-11-23 20:27:39 +08:00
{
material->diffuseColor = val;
}
}
}
else if (prop->name == matToken[TOKEN_MAT_SPECULAR])
{
2021-12-25 10:04:45 +08:00
PUAbstractNodeList::const_iterator it = prop->values.begin();
2019-11-23 20:27:39 +08:00
PUAbstractNodeList::const_iterator end = prop->values.end();
2021-12-25 10:04:45 +08:00
unsigned int n = 0;
2019-11-23 20:27:39 +08:00
Vec4 color;
float shininess = 0.0f;
2021-12-25 10:04:45 +08:00
while (it != end)
2019-11-23 20:27:39 +08:00
{
float v = 0;
2021-12-25 10:04:45 +08:00
if (getFloat(**it, &v))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
switch (n)
2019-11-23 20:27:39 +08:00
{
case 0:
color.x = v;
break;
case 1:
color.y = v;
break;
case 2:
color.z = v;
break;
case 3:
color.w = v;
break;
case 4:
shininess = v;
break;
}
}
++n;
++it;
}
material->specularColor = color;
2021-12-25 10:04:45 +08:00
material->shininess = shininess;
2019-11-23 20:27:39 +08:00
}
else if (prop->name == matToken[TOKEN_MAT_EMISSIVE])
{
if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_AMIBIENT], VAL_VECTOR4))
{
Vec4 val;
2021-12-25 10:04:45 +08:00
if (getVector4(prop->values.begin(), prop->values.end(), &val))
2019-11-23 20:27:39 +08:00
{
material->emissiveColor = val;
}
}
}
else if (prop->name == matToken[TOKEN_MAT_BLEND])
{
2021-12-25 10:04:45 +08:00
// if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_BLEND], VAL_STRING))
2019-11-23 20:27:39 +08:00
if (!prop->values.empty())
{
std::string val;
2021-12-25 10:04:45 +08:00
if (getString(*prop->values.front(), &val))
2019-11-23 20:27:39 +08:00
{
if (val == matToken[TOKEN_MAT_BLEND_ADD])
{
material->blendFunc.src = backend::BlendFactor::ONE;
2021-12-25 10:04:45 +08:00
material->blendFunc.dst = backend::BlendFactor::ONE;
2019-11-23 20:27:39 +08:00
}
else if (val == matToken[TOKEN_MAT_BLEND_ALPHA])
{
material->blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
}
else if (val == matToken[TOKEN_MAT_BLEND_COLOR])
{
material->blendFunc.src = backend::BlendFactor::SRC_COLOR;
2021-12-25 10:04:45 +08:00
material->blendFunc.dst = backend::BlendFactor::ONE_MINUS_SRC_COLOR;
2019-11-23 20:27:39 +08:00
}
else if (val == matToken[TOKEN_MAT_BLEND_MODULATE])
{
material->blendFunc.src = backend::BlendFactor::DST_COLOR;
2021-12-25 10:04:45 +08:00
material->blendFunc.dst = backend::BlendFactor::ZERO;
2019-11-23 20:27:39 +08:00
}
else if (val == matToken[TOKEN_MAT_BLEND_REPLACE])
{
material->blendFunc.src = backend::BlendFactor::ONE;
2021-12-25 10:04:45 +08:00
material->blendFunc.dst = backend::BlendFactor::ZERO;
2019-11-23 20:27:39 +08:00
}
else if (val == matToken[TOKEN_MAT_BLEND_SRC_COLOR])
{
material->blendFunc.src = backend::BlendFactor::SRC_COLOR;
}
else if (val == matToken[TOKEN_MAT_BLEND_DEST_COLOR])
{
material->blendFunc.src = backend::BlendFactor::DST_COLOR;
}
}
if (prop->values.size() == 2)
{
if (getString(*prop->values.back(), &val))
{
if (val == matToken[TOKEN_MAT_BLEND_ONE])
{
material->blendFunc.dst = backend::BlendFactor::ONE;
}
else if (val == matToken[TOKEN_MAT_BLEND_ZERO])
{
material->blendFunc.dst = backend::BlendFactor::ZERO;
}
else if (val == matToken[TOKEN_MAT_BLEND_SRC_COLOR])
{
material->blendFunc.dst = backend::BlendFactor::SRC_COLOR;
}
else if (val == matToken[TOKEN_MAT_BLEND_DEST_COLOR])
{
material->blendFunc.dst = backend::BlendFactor::DST_COLOR;
}
}
}
}
}
else if (prop->name == matToken[TOKEN_MAT_DEPTH_CHECK])
{
if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_DEPTH_CHECK], VAL_STRING))
{
std::string val;
2021-12-25 10:04:45 +08:00
if (getString(*prop->values.front(), &val))
2019-11-23 20:27:39 +08:00
{
if (val == matToken[TOKEN_MAT_ON])
{
material->depthTest = true;
}
else if (val == matToken[TOKEN_MAT_OFF])
{
material->depthTest = false;
}
}
}
}
else if (prop->name == matToken[TOKEN_MAT_DEPTH_WRITE])
{
if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_DEPTH_WRITE], VAL_STRING))
{
std::string val;
2021-12-25 10:04:45 +08:00
if (getString(*prop->values.front(), &val))
2019-11-23 20:27:39 +08:00
{
if (val == matToken[TOKEN_MAT_ON])
{
material->depthWrite = true;
}
else if (val == matToken[TOKEN_MAT_OFF])
{
material->depthWrite = false;
}
}
}
}
}
else if ((*i)->type == ANT_OBJECT)
{
PUObjectAbstractNode* child = reinterpret_cast<PUObjectAbstractNode*>(*i);
2021-12-25 10:04:45 +08:00
if (child->cls == matToken[TOKEN_MAT_TEXTURE_UNIT])
2019-11-23 20:27:39 +08:00
{
PUMaterialTextureUnitTranslator materialTextureUnitTranslator;
materialTextureUnitTranslator.translate(compiler, *i);
}
}
}
}
2021-12-25 10:04:45 +08:00
void PUMaterialTextureUnitTranslator::translate(PUScriptCompiler* compiler, PUAbstractNode* node)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
PUObjectAbstractNode* obj = reinterpret_cast<PUObjectAbstractNode*>(node);
2019-11-23 20:27:39 +08:00
PUObjectAbstractNode* parent = obj->parent ? reinterpret_cast<PUObjectAbstractNode*>(obj->parent) : 0;
2021-12-25 10:04:45 +08:00
PUMaterial* material = static_cast<PUMaterial*>(parent->context);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
for (PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if ((*i)->type == ANT_PROPERTY)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
PUPropertyAbstractNode* prop = reinterpret_cast<PUPropertyAbstractNode*>((*i));
2019-11-23 20:27:39 +08:00
if (prop->name == matToken[TOKEN_MAT_TEXTURE_FILE])
{
2021-12-25 10:04:45 +08:00
// if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_TEXTURE_FILE], VAL_STRING))
2019-11-23 20:27:39 +08:00
if (!prop->values.empty())
{
std::string val;
2021-12-25 10:04:45 +08:00
if (getString(*prop->values.front(), &val))
2019-11-23 20:27:39 +08:00
{
material->textureFile = val;
}
}
}
else if (prop->name == matToken[TOKEN_MAT_TEXTURE_WRAP])
{
if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_TEXTURE_WRAP], VAL_STRING))
{
std::string val;
2021-12-25 10:04:45 +08:00
if (getString(*prop->values.front(), &val))
2019-11-23 20:27:39 +08:00
{
if (val == matToken[TOKEN_MAT_TEXTURE_CLAMP])
{
material->wrapMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
}
else if (val == matToken[TOKEN_MAT_TEXTURE_REPEAT])
{
material->wrapMode = backend::SamplerAddressMode::REPEAT;
}
else if (val == matToken[TOKEN_MAT_TEXTURE_MIRROR])
{
2021-12-25 10:04:45 +08:00
material->wrapMode = backend::SamplerAddressMode::MIRROR_REPEAT; // GL_MIRROR_CLAMP_EXT;
2019-11-23 20:27:39 +08:00
}
}
}
}
}
}
}
NS_AX_END