Merge pull request #14526 from super626/v3

3D Normal Map Support
This commit is contained in:
XiaoYang 2015-12-02 14:22:20 +08:00
commit 867664197e
26 changed files with 730 additions and 79 deletions

View File

@ -2102,6 +2102,14 @@ unsigned int Bundle3D::parseGLProgramAttribute(const std::string& str)
{
return GLProgram::VERTEX_ATTRIB_BLEND_INDEX;
}
else if (str == "VERTEX_ATTRIB_TANGENT")
{
return GLProgram::VERTEX_ATTRIB_TANGENT;
}
else if (str == "VERTEX_ATTRIB_BINORMAL")
{
return GLProgram::VERTEX_ATTRIB_BINORMAL;
}
else
{
CCASSERT(false, "Wrong Attribute type");

View File

@ -46,6 +46,22 @@ NS_CC_BEGIN
// Helpers
//sampler uniform names, only diffuse and normal texture are supported for now
std::string s_uniformSamplerName[] =
{
"",//NTextureData::Usage::Unknown,
"",//NTextureData::Usage::None
"",//NTextureData::Usage::Diffuse
"",//NTextureData::Usage::Emissive
"",//NTextureData::Usage::Ambient
"",//NTextureData::Usage::Specular
"",//NTextureData::Usage::Shininess
"u_normalTex",//NTextureData::Usage::Normal
"",//NTextureData::Usage::Bump
"",//NTextureData::Usage::Transparency
"",//NTextureData::Usage::Reflection
};
static const char *s_dirLightUniformColorName = "u_DirLightSourceColor";
static const char *s_dirLightUniformDirName = "u_DirLightSourceDirection";
@ -106,8 +122,7 @@ static Texture2D * getDummyTexture()
Mesh::Mesh()
: _texture(nullptr)
, _skin(nullptr)
: _skin(nullptr)
, _visible(true)
, _isTransparent(false)
, _meshIndexData(nullptr)
@ -118,12 +133,15 @@ Mesh::Mesh()
, _blendDirty(true)
, _force2DQueue(false)
, _texFile("")
, _enableCheckTexture(false)
{
}
Mesh::~Mesh()
{
CC_SAFE_RELEASE(_texture);
for (auto &tex : _textures){
CC_SAFE_RELEASE(tex.second);
}
CC_SAFE_RELEASE(_skin);
CC_SAFE_RELEASE(_meshIndexData);
CC_SAFE_RELEASE(_material);
@ -256,10 +274,15 @@ void Mesh::setTexture(const std::string& texPath)
{
_texFile = texPath;
auto tex = Director::getInstance()->getTextureCache()->addImage(texPath);
setTexture(tex);
setTexture(tex, NTextureData::Usage::Diffuse);
}
void Mesh::setTexture(Texture2D* tex)
{
setTexture(tex, NTextureData::Usage::Diffuse);
}
void Mesh::setTexture(Texture2D* tex, NTextureData::Usage usage)
{
// Texture must be saved for future use
// it doesn't matter if the material is already set or not
@ -267,32 +290,51 @@ void Mesh::setTexture(Texture2D* tex)
if (tex == nullptr)
tex = getDummyTexture();
if (tex != _texture)
{
CC_SAFE_RETAIN(tex);
CC_SAFE_RELEASE(_texture);
_texture = tex;
CC_SAFE_RETAIN(tex);
CC_SAFE_RELEASE(_textures[usage]);
_textures[usage] = tex;
if (usage == NTextureData::Usage::Diffuse){
if (_material) {
auto technique = _material->_currentTechnique;
for(auto& pass: technique->_passes)
{
// FIXME: Ideally it should use glProgramState->setUniformTexture()
// and set CC_Texture0 that way. But trying to it, will trigger
// another bug
pass->setTexture(tex);
}
}
bindMeshCommand();
_texFile = tex->getPath();
}
if (_material) {
auto technique = _material->_currentTechnique;
for(auto& pass: technique->_passes)
{
// FIXME: Ideally it should use glProgramState->setUniformTexture()
// and set CC_Texture0 that way. But trying to it, will trigger
// another bug
pass->setTexture(tex);
else if (usage == NTextureData::Usage::Normal) // currently only diffuse and normal are supported
{
if (_material){
auto technique = _material->_currentTechnique;
for(auto& pass: technique->_passes)
{
pass->getGLProgramState()->setUniformTexture(s_uniformSamplerName[(int)usage], tex);
}
}
}
}
bindMeshCommand();
_texFile = _texture->getPath();
void Mesh::setTexture(const std::string& texPath, NTextureData::Usage usage)
{
auto tex = Director::getInstance()->getTextureCache()->addImage(texPath);
setTexture(tex, usage);
}
Texture2D* Mesh::getTexture() const
{
return _texture;
return _textures.at(NTextureData::Usage::Diffuse);
}
Texture2D* Mesh::getTexture(NTextureData::Usage usage)
{
return _textures[usage];
}
void Mesh::setMaterial(Material* material)
@ -315,8 +357,9 @@ void Mesh::setMaterial(Material* material)
}
}
// Was the texture set before teh GLProgramState ? Set it
if (_texture)
setTexture(_texture);
for(auto& tex : _textures)
setTexture(tex.second, tex.first);
if (_blendDirty)
setBlendFunc(_blend);
@ -339,6 +382,7 @@ 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,
@ -666,30 +710,31 @@ GLuint Mesh::getIndexBuffer() const
void Mesh::checkTexture()
{
Texture2D* cacheTex = nullptr;
if (TextureCache::getInstance()->isDirty())
auto& texture = _textures[NTextureData::Usage::Diffuse];
if (Director::getInstance()->getTextureCache()->isDirty())
{
cacheTex = TextureCache::getInstance()->getTextureForKey(_texFile);
cacheTex = Director::getInstance()->getTextureCache()->getTextureForKey(_texFile);
if (cacheTex == nullptr)
{
cacheTex = getDummyTexture();
}
}
else if (_texture != nullptr && !_texture->isValid())
else if (texture != nullptr && !texture->isValid())
{
cacheTex = getDummyTexture();
}
if (cacheTex != nullptr && _texture != cacheTex)
if (cacheTex != nullptr && texture != cacheTex)
{
CC_SAFE_RETAIN(cacheTex);
CC_SAFE_RELEASE(_texture);
_texture = cacheTex;
CC_SAFE_RELEASE(texture);
texture = cacheTex;
if (_material) {
auto technique = _material->_currentTechnique;
for (auto& pass : technique->_passes)
{
pass->setTexture(_texture);
pass->setTexture(texture);
}
}
bindMeshCommand();

View File

@ -26,6 +26,7 @@
#define __CCMESH_H__
#include <string>
#include <map>
#include "3d/CCBundle3DData.h"
#include "3d/CCAABB.h"
@ -94,10 +95,39 @@ public:
/**get per vertex size in bytes*/
int getVertexSizeInBytes() const;
/**texture getter and setter*/
/**
* set texture (diffuse), which is responsible for the main apearence. It is also means main textrue, you can also call setTexture(texPath, NTextureData::Usage::Diffuse)
* @param texPath texture path
*/
void setTexture(const std::string& texPath);
/**
* set texture (diffuse), which is responsible for the main apearence. It is also means main textrue, you can also call setTexture(texPath, NTextureData::Usage::Diffuse)
* @param tex texture to be set
*/
void setTexture(Texture2D* tex);
/**
* set texture
* @param tex texture to be set
* @param usage Usage of this texture
*/
void setTexture(Texture2D* tex, NTextureData::Usage usage);
/**
* set texture
* @param texPath texture path
* @param usage Usage of this texture
*/
void setTexture(const std::string& texPath, NTextureData::Usage usage);
/**
* Get texture (diffuse), which is responsible for the main apearence. It is also means main textrue, you can also call getTexture(NTextureData::Usage::Diffuse)
* @return Texture used, return the texture of first mesh if multiple meshes exist
*/
Texture2D* getTexture() const;
/**
* Get textrue
* @param usage Usage of returned texture
* @return The texture of this usage, return the textrue of first mesh if multiple meshes exist
*/
Texture2D* getTexture(NTextureData::Usage usage);
/**visible getter and setter*/
void setVisible(bool visible);
@ -194,7 +224,19 @@ public:
*/
void setForce2DQueue(bool force2D) { _force2DQueue = force2D; }
/**
* check texture
*/
void checkTexture();
/**
* set enable check texture, check texture each frame if eanble is true. It is false by default
*/
void setEnableCheckTexture(bool enableCheckTexture) { _enableCheckTexture = enableCheckTexture; }
/**
* check texture each frame?
*/
bool enableCheckTexture() const { return _enableCheckTexture; }
CC_CONSTRUCTOR_ACCESS:
@ -206,7 +248,7 @@ protected:
void setLightUniforms(Pass* pass, Scene* scene, const Vec4& color, unsigned int lightmask);
void bindMeshCommand();
Texture2D* _texture; //texture that submesh is using
std::map<NTextureData::Usage, Texture2D*> _textures; //textures that submesh is using
MeshSkin* _skin; //skin
bool _visible; // is the submesh visible
bool _isTransparent; // is this mesh transparent, it is a property of material in fact
@ -238,11 +280,15 @@ protected:
std::vector<float> _spotLightUniformRangeInverseValues;
std::string _texFile;
bool _enableCheckTexture;
};
// end of 3d group
/// @}
/// @cond
extern std::string CC_DLL s_uniformSamplerName[];//uniform sampler names array
/// @endcond
NS_CC_END

View File

@ -390,6 +390,21 @@ Sprite3D* Sprite3D::createSprite3DNode(NodeData* nodedata,ModelData* modeldata,c
mesh->_isTransparent = (materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr);
}
}
textureData = materialData->getTextureData(NTextureData::Usage::Normal);
if (textureData)
{
auto tex = Director::getInstance()->getTextureCache()->addImage(textureData->filename);
if(tex)
{
Texture2D::TexParams texParams;
texParams.minFilter = GL_LINEAR;
texParams.magFilter = GL_LINEAR;
texParams.wrapS = textureData->wrapS;
texParams.wrapT = textureData->wrapT;
tex->setTexParameters(texParams);
}
mesh->setTexture(tex, NTextureData::Usage::Normal);
}
}
}
@ -477,7 +492,6 @@ void Sprite3D::genMaterial(bool useLight)
for (auto& mesh: _meshes)
{
auto material = materials[mesh->getMeshIndexData()->getMeshVertexData()];
//keep original state block if exist
auto oldmaterial = mesh->getMaterial();
if (oldmaterial)
@ -540,6 +554,21 @@ void Sprite3D::createNode(NodeData* nodedata, Node* root, const MaterialDatas& m
mesh->_isTransparent = (materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr);
}
}
textureData = materialData->getTextureData(NTextureData::Usage::Normal);
if (textureData)
{
auto tex = Director::getInstance()->getTextureCache()->addImage(textureData->filename);
if (tex)
{
Texture2D::TexParams texParams;
texParams.minFilter = GL_LINEAR;
texParams.magFilter = GL_LINEAR;
texParams.wrapS = textureData->wrapS;
texParams.wrapT = textureData->wrapT;
tex->setTexParameters(texParams);
}
mesh->setTexture(tex, NTextureData::Usage::Normal);
}
}
}
@ -968,10 +997,17 @@ static Sprite3DMaterial* getSprite3DMaterialForAttribs(MeshVertexData* meshVerte
bool hasSkin = meshVertexData->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_BLEND_INDEX)
&& meshVertexData->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_BLEND_WEIGHT);
bool hasNormal = meshVertexData->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_NORMAL);
bool hasTangentSpace = meshVertexData->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_TANGENT)
&& meshVertexData->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_BINORMAL);
Sprite3DMaterial::MaterialType type;
if(textured)
{
type = hasNormal && usesLight ? Sprite3DMaterial::MaterialType::DIFFUSE : Sprite3DMaterial::MaterialType::UNLIT;
if (hasTangentSpace){
type = hasNormal && usesLight ? Sprite3DMaterial::MaterialType::BUMPED_DIFFUSE : Sprite3DMaterial::MaterialType::UNLIT;
}
else{
type = hasNormal && usesLight ? Sprite3DMaterial::MaterialType::DIFFUSE : Sprite3DMaterial::MaterialType::UNLIT;
}
}
else
{

View File

@ -80,7 +80,7 @@ public:
static void createAsync(const std::string &modelPath, const std::string &texturePath, const std::function<void(Sprite3D*, void*)>& callback, void* callbackparam);
/**set texture, set the first if multiple textures exist*/
/**set diffuse texture, set the first if multiple textures exist*/
void setTexture(const std::string& texFile);
void setTexture(Texture2D* texture);

View File

@ -23,6 +23,7 @@
****************************************************************************/
#include "3d/CCSprite3DMaterial.h"
#include "3d/CCMesh.h"
#include "platform/CCFileUtils.h"
#include "renderer/CCTexture2D.h"
#include "renderer/CCGLProgram.h"
@ -100,6 +101,22 @@ void Sprite3DMaterial::createBuiltInMaterial()
{
_diffuseNoTexMaterial->_type = Sprite3DMaterial::MaterialType::DIFFUSE_NOTEX;
}
glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_POSITION_BUMPEDNORMAL_TEXTURE);
glprogramstate = GLProgramState::create(glProgram);
_bumpedDiffuseMaterial = new (std::nothrow) Sprite3DMaterial();
if (_bumpedDiffuseMaterial && _bumpedDiffuseMaterial->initWithGLProgramState(glprogramstate))
{
_bumpedDiffuseMaterial->_type = Sprite3DMaterial::MaterialType::BUMPED_DIFFUSE;
}
glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_BUMPEDNORMAL_TEXTURE);
glprogramstate = GLProgramState::create(glProgram);
_bumpedDiffuseMaterialSkin = new (std::nothrow) Sprite3DMaterial();
if (_bumpedDiffuseMaterialSkin && _bumpedDiffuseMaterialSkin->initWithGLProgramState(glprogramstate))
{
_bumpedDiffuseMaterialSkin->_type = Sprite3DMaterial::MaterialType::BUMPED_DIFFUSE;
}
}
void Sprite3DMaterial::releaseBuiltInMaterial()
@ -182,7 +199,7 @@ Sprite3DMaterial* Sprite3DMaterial::createBuiltInMaterial(MaterialType type, boo
break;
case Sprite3DMaterial::MaterialType::BUMPED_DIFFUSE:
CCASSERT(0, "not implement");
material = skinned ? _bumpedDiffuseMaterialSkin : _bumpedDiffuseMaterial;
break;
default:
@ -231,6 +248,14 @@ Sprite3DMaterial* Sprite3DMaterial::createWithGLStateProgram(GLProgramState* pro
return nullptr;
}
void Sprite3DMaterial::setTexture(Texture2D* tex, NTextureData::Usage usage)
{
const auto& passes = getTechnique()->getPasses();
for (auto& pass : passes) {
pass->getGLProgramState()->setUniformTexture(s_uniformSamplerName[(int)usage], tex);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
Sprite3DMaterialCache::Sprite3DMaterialCache()

View File

@ -29,6 +29,7 @@
#include <unordered_map>
#include "base/ccTypes.h"
#include "renderer/CCMaterial.h"
#include "3d/CCBundle3DData.h"
NS_CC_BEGIN
@ -90,6 +91,8 @@ public:
*/
static Sprite3DMaterial* createWithGLStateProgram(GLProgramState* programState);
void setTexture(Texture2D* tex, NTextureData::Usage usage);
/**
* Create all build in materials
*/

View File

@ -85,6 +85,8 @@ const char* GLProgram::SHADER_3D_SKINPOSITION_TEXTURE = "Shader3DSkinPositionTex
const char* GLProgram::SHADER_3D_POSITION_NORMAL = "Shader3DPositionNormal";
const char* GLProgram::SHADER_3D_POSITION_NORMAL_TEXTURE = "Shader3DPositionNormalTexture";
const char* GLProgram::SHADER_3D_SKINPOSITION_NORMAL_TEXTURE = "Shader3DSkinPositionNormalTexture";
const char* GLProgram::SHADER_3D_POSITION_BUMPEDNORMAL_TEXTURE = "Shader3DPositionBumpedNormalTexture";
const char* GLProgram::SHADER_3D_SKINPOSITION_BUMPEDNORMAL_TEXTURE = "Shader3DSkinPositionBumpedNormalTexture";
const char* GLProgram::SHADER_3D_PARTICLE_COLOR = "Shader3DParticleColor";
const char* GLProgram::SHADER_3D_PARTICLE_TEXTURE = "Shader3DParticleTexture";
const char* GLProgram::SHADER_3D_SKYBOX = "Shader3DSkybox";
@ -118,6 +120,10 @@ const char* GLProgram::ATTRIBUTE_NAME_TEX_COORD3 = "a_texCoord3";
const char* GLProgram::ATTRIBUTE_NAME_NORMAL = "a_normal";
const char* GLProgram::ATTRIBUTE_NAME_BLEND_WEIGHT = "a_blendWeight";
const char* GLProgram::ATTRIBUTE_NAME_BLEND_INDEX = "a_blendIndex";
const char* GLProgram::ATTRIBUTE_NAME_TANGENT = "a_tangent";
const char* GLProgram::ATTRIBUTE_NAME_BINORMAL = "a_binormal";
static const char * COCOS2D_SHADER_UNIFORMS =
"uniform mat4 CC_PMatrix;\n"
@ -175,7 +181,6 @@ GLProgram* GLProgram::createWithFilenames(const std::string& vShaderFilename, co
return nullptr;
}
GLProgram::GLProgram()
: _program(0)
, _vertShader(0)

View File

@ -110,6 +110,10 @@ public:
VERTEX_ATTRIB_BLEND_WEIGHT,
/**Index 8 will be used as Blend index.*/
VERTEX_ATTRIB_BLEND_INDEX,
/**Index 9 will be used as tangent.*/
VERTEX_ATTRIB_TANGENT,
/**Index 10 will be used as Binormal.*/
VERTEX_ATTRIB_BINORMAL,
VERTEX_ATTRIB_MAX,
// backward compatibility
@ -211,6 +215,15 @@ public:
*/
static const char* SHADER_3D_SKINPOSITION_NORMAL_TEXTURE;
/**
Built in shader used for 3D, support Position, Bumped Normal, Texture vertex attribute, used in lighting. with color specified by a uniform.
*/
static const char* SHADER_3D_POSITION_BUMPEDNORMAL_TEXTURE;
/**
Built in shader used for 3D, support Position(skeletal animation by hardware skin), Bumped Normal, Texture vertex attribute,
used in lighting. with color specified by a uniform.
*/
static const char* SHADER_3D_SKINPOSITION_BUMPEDNORMAL_TEXTURE;
/**
Built in shader for particles, support Position and Texture, with a color specified by a uniform.
*/
static const char* SHADER_3D_PARTICLE_TEXTURE;
@ -297,6 +310,10 @@ public:
static const char* ATTRIBUTE_NAME_BLEND_WEIGHT;
/**Attribute blend index.*/
static const char* ATTRIBUTE_NAME_BLEND_INDEX;
/**Attribute blend tangent.*/
static const char* ATTRIBUTE_NAME_TANGENT;
/**Attribute blend binormal.*/
static const char* ATTRIBUTE_NAME_BINORMAL;
/**
end of Built Attribute names
@}

View File

@ -61,6 +61,8 @@ enum {
kShaderType_3DPositionNormal,
kShaderType_3DPositionNormalTex,
kShaderType_3DSkinPositionNormalTex,
kShaderType_3DPositionBumpedNormalTex,
kShaderType_3DSkinPositionBumpedNormalTex,
kShaderType_3DParticleTex,
kShaderType_3DParticleColor,
kShaderType_3DSkyBox,
@ -247,6 +249,14 @@ void GLProgramCache::loadDefaultGLPrograms()
loadDefaultGLProgram(p, kShaderType_3DSkinPositionNormalTex);
_programs.insert(std::make_pair(GLProgram::SHADER_3D_SKINPOSITION_NORMAL_TEXTURE, p));
p = new GLProgram();
loadDefaultGLProgram(p, kShaderType_3DPositionBumpedNormalTex);
_programs.insert(std::make_pair(GLProgram::SHADER_3D_POSITION_BUMPEDNORMAL_TEXTURE, p));
p = new GLProgram();
loadDefaultGLProgram(p, kShaderType_3DSkinPositionBumpedNormalTex);
_programs.insert(std::make_pair(GLProgram::SHADER_3D_SKINPOSITION_BUMPEDNORMAL_TEXTURE, p));
p = new GLProgram();
loadDefaultGLProgram(p, kShaderType_3DParticleColor);
_programs.insert(std::make_pair(GLProgram::SHADER_3D_PARTICLE_COLOR, p));
@ -384,6 +394,14 @@ void GLProgramCache::reloadDefaultGLPrograms()
p->reset();
loadDefaultGLProgram(p, kShaderType_3DSkinPositionNormalTex);
p = getGLProgram(GLProgram::SHADER_3D_POSITION_BUMPEDNORMAL_TEXTURE);
p->reset();
loadDefaultGLProgram(p, kShaderType_3DPositionBumpedNormalTex);
p = getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_BUMPEDNORMAL_TEXTURE);
p->reset();
loadDefaultGLProgram(p, kShaderType_3DSkinPositionBumpedNormalTex);
p = getGLProgram(GLProgram::SHADER_3D_PARTICLE_TEXTURE);
p->reset();
loadDefaultGLProgram(p, kShaderType_3DParticleTex);
@ -418,6 +436,14 @@ void GLProgramCache::reloadDefaultGLProgramsRelativeToLights()
p = getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_NORMAL_TEXTURE);
p->reset();
loadDefaultGLProgram(p, kShaderType_3DSkinPositionNormalTex);
p = getGLProgram(GLProgram::SHADER_3D_POSITION_BUMPEDNORMAL_TEXTURE);
p->reset();
loadDefaultGLProgram(p, kShaderType_3DPositionBumpedNormalTex);
p = getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_BUMPEDNORMAL_TEXTURE);
p->reset();
loadDefaultGLProgram(p, kShaderType_3DSkinPositionBumpedNormalTex);
}
void GLProgramCache::loadDefaultGLProgram(GLProgram *p, int type)
@ -503,6 +529,20 @@ void GLProgramCache::loadDefaultGLProgram(GLProgram *p, int type)
p->initWithByteArrays((def + std::string(cc3D_SkinPositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormalTex_frag)).c_str());
}
break;
case kShaderType_3DPositionBumpedNormalTex:
{
std::string def = getShaderMacrosForLight();
std::string normalMapDef = "\n#define USE_NORMAL_MAPPING 1 \n";
p->initWithByteArrays((def + normalMapDef + std::string(cc3D_PositionNormalTex_vert)).c_str(), (def + normalMapDef + std::string(cc3D_ColorNormalTex_frag)).c_str());
}
break;
case kShaderType_3DSkinPositionBumpedNormalTex:
{
std::string def = getShaderMacrosForLight();
std::string normalMapDef = "\n#define USE_NORMAL_MAPPING 1 \n";
p->initWithByteArrays((def + normalMapDef + std::string(cc3D_SkinPositionNormalTex_vert)).c_str(), (def + normalMapDef + std::string(cc3D_ColorNormalTex_frag)).c_str());
}
break;
case kShaderType_3DParticleTex:
{
p->initWithByteArrays(cc3D_Particle_vert, cc3D_Particle_tex_frag);

View File

@ -436,6 +436,7 @@ Texture2D::Texture2D()
, _shaderProgram(nullptr)
, _antialiasEnabled(true)
, _ninePatchInfo(nullptr)
, _valid(true)
{
}

View File

@ -37,7 +37,9 @@ std::string s_attributeNames[] = {
GLProgram::ATTRIBUTE_NAME_TEX_COORD3,
GLProgram::ATTRIBUTE_NAME_NORMAL,
GLProgram::ATTRIBUTE_NAME_BLEND_WEIGHT,
GLProgram::ATTRIBUTE_NAME_BLEND_INDEX
GLProgram::ATTRIBUTE_NAME_BLEND_INDEX,
GLProgram::ATTRIBUTE_NAME_TANGENT,
GLProgram::ATTRIBUTE_NAME_BINORMAL
};
static GLuint __maxVertexAttribs = 0;

View File

@ -20,32 +20,59 @@ uniform vec3 u_AmbientLightSourceColor;
\n#ifdef GL_ES\n
varying mediump vec2 TextureCoordOut;
\n#ifdef USE_NORMAL_MAPPING\n
\n#if MAX_DIRECTIONAL_LIGHT_NUM\n
varying mediump vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM];
\n#endif\n
\n#endif\n
\n#if MAX_POINT_LIGHT_NUM\n
varying mediump vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if MAX_SPOT_LIGHT_NUM\n
varying mediump vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#ifdef USE_NORMAL_MAPPING\n
varying mediump vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#endif\n
\n#endif\n
\n#ifndef USE_NORMAL_MAPPING\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
varying mediump vec3 v_normal;
\n#endif\n
\n#endif\n
\n#else\n
varying vec2 TextureCoordOut;
\n#ifdef USE_NORMAL_MAPPING\n
\n#if MAX_DIRECTIONAL_LIGHT_NUM\n
varying vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM];
\n#endif\n
\n#endif\n
\n#if MAX_POINT_LIGHT_NUM\n
varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if MAX_SPOT_LIGHT_NUM\n
varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#ifdef USE_NORMAL_MAPPING\n
varying vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#endif\n
\n#endif\n
\n#ifndef USE_NORMAL_MAPPING\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
varying vec3 v_normal;
\n#endif\n
\n#endif\n
\n#endif\n
uniform vec4 u_color;
\n#ifdef USE_NORMAL_MAPPING\n
uniform sampler2D u_normalTex;
\n#endif\n
vec3 computeLighting(vec3 normalVector, vec3 lightDirection, vec3 lightColor, float attenuation)
{
@ -57,8 +84,15 @@ vec3 computeLighting(vec3 normalVector, vec3 lightDirection, vec3 lightColor, fl
void main(void)
{
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
vec3 normal = normalize(v_normal);
\n#ifdef USE_NORMAL_MAPPING\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
vec3 normal = normalize(2.0 * texture2D(u_normalTex, TextureCoordOut).xyz - 1.0);
\n#endif\n
\n#else\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
vec3 normal = normalize(v_normal);
\n#endif\n
\n#endif\n
vec4 combinedColor = vec4(u_AmbientLightSourceColor, 1.0);
@ -67,7 +101,11 @@ void main(void)
\n#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i)
{
\n#ifdef USE_NORMAL_MAPPING\n
vec3 lightDirection = normalize(v_dirLightDirection[i] * 2.0);
\n#else\n
vec3 lightDirection = normalize(u_DirLightSourceDirection[i] * 2.0);
\n#endif\n
combinedColor.xyz += computeLighting(normal, -lightDirection, u_DirLightSourceColor[i], 1.0);
}
\n#endif\n
@ -91,7 +129,11 @@ void main(void)
float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection[i]);
\n#ifdef USE_NORMAL_MAPPING\n
vec3 spotLightDirection = normalize(v_spotLightDirection[i] * 2.0);
\n#else\n
vec3 spotLightDirection = normalize(u_SpotLightSourceDirection[i] * 2.0);
\n#endif\n
// "-lightDirection" is used because light direction points in opposite direction to spot direction.
float spotCurrentAngleCos = dot(spotLightDirection, -vertexToSpotLightDirection);

View File

@ -1,47 +1,110 @@
const char* cc3D_PositionNormalTex_vert = STRINGIFY(
\n#ifdef USE_NORMAL_MAPPING\n
\n#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)\n
uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM];
\n#endif\n
\n#endif\n
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
uniform vec3 u_PointLightSourcePosition[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
uniform vec3 u_SpotLightSourcePosition[MAX_SPOT_LIGHT_NUM];
\n#ifdef USE_NORMAL_MAPPING\n
uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM];
\n#endif\n
\n#endif\n
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec3 a_normal;
\n#ifdef USE_NORMAL_MAPPING\n
attribute vec3 a_tangent;
attribute vec3 a_binormal;
\n#endif\n
varying vec2 TextureCoordOut;
\n#ifdef USE_NORMAL_MAPPING\n
\n#if MAX_DIRECTIONAL_LIGHT_NUM\n
varying vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM];
\n#endif\n
\n#endif\n
\n#if MAX_POINT_LIGHT_NUM\n
varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if MAX_SPOT_LIGHT_NUM\n
varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#ifdef USE_NORMAL_MAPPING\n
varying vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#endif\n
\n#endif\n
\n#ifndef USE_NORMAL_MAPPING\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
varying vec3 v_normal;
\n#endif\n
\n#endif\n
void main(void)
{
vec4 ePosition = CC_MVMatrix * a_position;
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
}
\n#endif\n
\n#ifdef USE_NORMAL_MAPPING\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
vec3 eTangent = normalize(CC_NormalMatrix * a_tangent);
vec3 eBinormal = normalize(CC_NormalMatrix * a_binormal);
vec3 eNormal = normalize(CC_NormalMatrix * a_normal);
\n#endif\n
\n#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i)
{
v_dirLightDirection[i].x = dot(eTangent, u_DirLightSourceDirection[i]);
v_dirLightDirection[i].y = dot(eBinormal, u_DirLightSourceDirection[i]);
v_dirLightDirection[i].z = dot(eNormal, u_DirLightSourceDirection[i]);
}
\n#endif\n
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz;
}
\n#endif\n
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
vec3 pointLightDir = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
v_vertexToPointLightDirection[i].x = dot(eTangent, pointLightDir);
v_vertexToPointLightDirection[i].y = dot(eBinormal, pointLightDir);
v_vertexToPointLightDirection[i].z = dot(eNormal, pointLightDir);
}
\n#endif\n
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
vec3 spotLightDir = u_SpotLightSourcePosition[i] - ePosition.xyz;
v_vertexToSpotLightDirection[i].x = dot(eTangent, spotLightDir);
v_vertexToSpotLightDirection[i].y = dot(eBinormal, spotLightDir);
v_vertexToSpotLightDirection[i].z = dot(eNormal, spotLightDir);
v_spotLightDirection[i].x = dot(eTangent, u_SpotLightSourceDirection[i]);
v_spotLightDirection[i].y = dot(eBinormal, u_SpotLightSourceDirection[i]);
v_spotLightDirection[i].z = dot(eNormal, u_SpotLightSourceDirection[i]);
}
\n#endif\n
\n#else\n
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
}
\n#endif\n
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz;
}
\n#endif\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
v_normal = CC_NormalMatrix * a_normal;
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
v_normal = CC_NormalMatrix * a_normal;
\n#endif\n
\n#endif\n
TextureCoordOut = a_texCoord;
@ -52,11 +115,19 @@ void main(void)
const char* cc3D_SkinPositionNormalTex_vert = STRINGIFY(
\n#ifdef USE_NORMAL_MAPPING\n
\n#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)\n
uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM];
\n#endif\n
\n#endif\n
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
uniform vec3 u_PointLightSourcePosition[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
uniform vec3 u_SpotLightSourcePosition[MAX_SPOT_LIGHT_NUM];
\n#ifdef USE_NORMAL_MAPPING\n
uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM];
\n#endif\n
\n#endif\n
attribute vec3 a_position;
@ -67,6 +138,10 @@ attribute vec4 a_blendIndex;
attribute vec2 a_texCoord;
attribute vec3 a_normal;
\n#ifdef USE_NORMAL_MAPPING\n
attribute vec3 a_tangent;
attribute vec3 a_binormal;
\n#endif\n
const int SKINNING_JOINT_COUNT = 60;
// Uniforms
@ -75,15 +150,28 @@ uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
// Varyings
varying vec2 TextureCoordOut;
\n#ifdef USE_NORMAL_MAPPING\n
\n#if MAX_DIRECTIONAL_LIGHT_NUM\n
varying vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM];
\n#endif\n
\n#endif\n
\n#if MAX_POINT_LIGHT_NUM\n
varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if MAX_SPOT_LIGHT_NUM\n
varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#ifdef USE_NORMAL_MAPPING\n
varying vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#endif\n
\n#endif\n
varying vec3 v_normal;
void getPositionAndNormal(out vec4 position, out vec3 normal)
\n#ifndef USE_NORMAL_MAPPING\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
varying vec3 v_normal;
\n#endif\n
\n#endif\n
void getPositionAndNormal(out vec4 position, out vec3 normal, out vec3 tangent, out vec3 binormal)
{
float blendWeight = a_blendWeight[0];
@ -131,6 +219,16 @@ void getPositionAndNormal(out vec4 position, out vec3 normal)
normal.x = dot(n, matrixPalette1);
normal.y = dot(n, matrixPalette2);
normal.z = dot(n, matrixPalette3);
\n#ifdef USE_NORMAL_MAPPING\n
vec4 t = vec4(a_tangent, 0.0);
tangent.x = dot(t, matrixPalette1);
tangent.y = dot(t, matrixPalette2);
tangent.z = dot(t, matrixPalette3);
vec4 b = vec4(a_binormal, 0.0);
binormal.x = dot(b, matrixPalette1);
binormal.y = dot(b, matrixPalette2);
binormal.z = dot(b, matrixPalette3);
\n#endif\n
\n#endif\n
}
@ -138,25 +236,68 @@ void main()
{
vec4 position;
vec3 normal;
getPositionAndNormal(position, normal);
vec3 tangent;
vec3 binormal;
getPositionAndNormal(position, normal, tangent, binormal);
vec4 ePosition = CC_MVMatrix * position;
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz- ePosition.xyz;
}
\n#endif\n
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz;
}
\n#endif\n
\n#ifdef USE_NORMAL_MAPPING\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
vec3 eTangent = normalize(CC_NormalMatrix * tangent);
vec3 eBinormal = normalize(CC_NormalMatrix * binormal);
vec3 eNormal = normalize(CC_NormalMatrix * normal);
\n#endif\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
v_normal = CC_NormalMatrix * normal;
\n#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i)
{
v_dirLightDirection[i].x = dot(eTangent, u_DirLightSourceDirection[i]);
v_dirLightDirection[i].y = dot(eBinormal, u_DirLightSourceDirection[i]);
v_dirLightDirection[i].z = dot(eNormal, u_DirLightSourceDirection[i]);
}
\n#endif\n
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
vec3 pointLightDir = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
v_vertexToPointLightDirection[i].x = dot(eTangent, pointLightDir);
v_vertexToPointLightDirection[i].y = dot(eBinormal, pointLightDir);
v_vertexToPointLightDirection[i].z = dot(eNormal, pointLightDir);
}
\n#endif\n
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
vec3 spotLightDir = u_SpotLightSourcePosition[i] - ePosition.xyz;
v_vertexToSpotLightDirection[i].x = dot(eTangent, spotLightDir);
v_vertexToSpotLightDirection[i].y = dot(eBinormal, spotLightDir);
v_vertexToSpotLightDirection[i].z = dot(eNormal, spotLightDir);
v_spotLightDirection[i].x = dot(eTangent, u_SpotLightSourceDirection[i]);
v_spotLightDirection[i].y = dot(eBinormal, u_SpotLightSourceDirection[i]);
v_spotLightDirection[i].z = dot(eNormal, u_SpotLightSourceDirection[i]);
}
\n#endif\n
\n#else\n
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz- ePosition.xyz;
}
\n#endif\n
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz;
}
\n#endif\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
v_normal = CC_NormalMatrix * normal;
\n#endif\n
\n#endif\n
TextureCoordOut = a_texCoord;

2
plugin

@ -1 +1 @@
Subproject commit 4b68a78781f57c3cda70fcebb2a9066914d28efb
Subproject commit ad539b56c5354c1083f479d4c8d275ae8c7fdfa7

View File

@ -68,6 +68,7 @@ Sprite3DTests::Sprite3DTests()
ADD_TEST_CASE(Sprite3DVertexColorTest);
ADD_TEST_CASE(MotionStreak3DTest);
ADD_TEST_CASE(Sprite3DPropertyTest);
ADD_TEST_CASE(Sprite3DNormalMappingTest);
};
//------------------------------------------------------------------
@ -2514,6 +2515,77 @@ void MotionStreak3DTest::update(float delta)
_streak->setSweepAxis(Vec3(cosf(angle), 0, sinf(angle)));
}
Sprite3DNormalMappingTest::Sprite3DNormalMappingTest()
{
auto s = Director::getInstance()->getWinSize();
{
auto sprite = Sprite3D::create("Sprite3DTest/sphere.c3b");
sprite->setPosition(Vec2(-30, 0));
sprite->setRotation3D(Vec3(90.0f, 0.0f, 0.0f));
sprite->setScale(2.0);
sprite->setCameraMask(2);
sprite->setTexture("Sprite3DTest/brickwork-texture.jpg");
addChild(sprite);
}
int maxAttributes;
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxAttributes);
CCASSERT(maxAttributes > 8, "attributes supported must be greater than 8");
if (maxAttributes > 8)
{
auto sprite = Sprite3D::create("Sprite3DTest/sphere_bumped.c3b");
sprite->setPosition(Vec2(30, 0));
sprite->setRotation3D(Vec3(90.0f, 0.0f, 0.0f));
sprite->setScale(20.0);
sprite->setCameraMask(2);
sprite->setTexture("Sprite3DTest/brickwork-texture.jpg");
addChild(sprite);
}
float radius = 100.0;
PointLight* light = PointLight::create(Vec3(0.0, 0.0, 0.0), Color3B(255, 255, 255), 1000);
light->runAction(RepeatForever::create(Sequence::create(CallFuncN::create([radius](Node *node){
static float angle = 0.0;
static bool reverseDir = false;
node->setPosition3D(Vec3(radius * cos(angle), 0.0f, radius * sin(angle)));
if (reverseDir){
angle -= 0.01;
if (angle < 0.0)
reverseDir = false;
}
else{
angle += 0.01;
if (3.14159 < angle)
reverseDir = true;
}
}), nullptr)));
//setup camera
auto camera = Camera::createPerspective(60.0, s.width / s.height, 1.0f, 1000.f);
camera->setCameraFlag(CameraFlag::USER1);
camera->setPosition3D(Vec3(0.f, 0.f, 100.f));
camera->lookAt(Vec3(0.f, 0.f, 0.f));
addChild(camera);
addChild(light);
}
Sprite3DNormalMappingTest::~Sprite3DNormalMappingTest()
{
}
std::string Sprite3DNormalMappingTest::title() const
{
return "NormalMapping Test";
}
std::string Sprite3DNormalMappingTest::subtitle() const
{
return "";
}
Sprite3DPropertyTest::Sprite3DPropertyTest()
{
auto s = Director::getInstance()->getWinSize();

View File

@ -584,6 +584,17 @@ protected:
cocos2d::MotionStreak3D* _streak;
};
class Sprite3DNormalMappingTest : public Sprite3DTestDemo
{
public:
CREATE_FUNC(Sprite3DNormalMappingTest);
Sprite3DNormalMappingTest();
virtual std::string title() const override;
virtual std::string subtitle() const override;
virtual ~Sprite3DNormalMappingTest();
};
class Sprite3DPropertyTest : public Sprite3DTestDemo
{
public:

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

65
tests/js-tests/src/Sprite3DTest/Sprite3DTest.js Normal file → Executable file
View File

@ -1610,6 +1610,70 @@ var Sprite3DCubeMapTest = Sprite3DTestDemo.extend({
}
}
});
var Sprite3DNormalMappingTest = Sprite3DTestDemo.extend({
_title:"NormalMapping Test",
_subtitle:"",
_angle:0.0,
_reverseDir:false,
_radius:100,
_light:null,
_TAG_GROSSINI:10,
ctor:function(){
this._super();
var visibleSize = cc.director.getVisibleSize();
var sphere1 = new jsb.Sprite3D("Sprite3DTest/sphere.c3b");
sphere1.setPosition3D(cc.math.vec3(-30, 0, 0));
sphere1.setRotation3D(cc.math.vec3(90, 0, 0));
sphere1.setScale(2);
sphere1.setCameraMask(2);
sphere1.setTexture("Sprite3DTest/brickwork-texture.jpg");
this.addChild(sphere1);
var sphere2 = new jsb.Sprite3D("Sprite3DTest/sphere_bumped.c3b");
sphere2.setPosition3D(cc.math.vec3(30, 0, 0));
sphere2.setRotation3D(cc.math.vec3(90, 0, 0));
sphere2.setScale(20);
sphere2.setCameraMask(2);
sphere2.setTexture("Sprite3DTest/brickwork-texture.jpg");
this.addChild(sphere2);
_light = new jsb.PointLight(cc.math.vec3(0, 0, 0), cc.color(255, 255, 255), 1000);
var camera = new cc.Camera(cc.Camera.Mode.PERSPECTIVE, 60, visibleSize.width/visibleSize.height, 1, 1000);
camera.setCameraFlag(cc.CameraFlag.USER1);
camera.setPosition3D(cc.math.vec3(0, 0, 100));
camera.lookAt(cc.math.vec3(0,0,0));
this.addChild(camera);
this.addChild(_light, 1, this._TAG_GROSSINI);
var angleReverseDir = cc.callFunc(function(){
if(this._reverseDir){
this._angle -= 0.01;
if(this._angle < 0.0)
{
this._reverseDir = false;
}
}
else{
this._angle += 0.01;
if(3.14159 < this._angle)
{
this._reverseDir = true;
}
}
this.getChildByTag(this._TAG_GROSSINI).setPosition3D(cc.math.vec3(this._radius * Math.cos(this._angle), 0, this._radius * Math.sin(this._angle)));
}, this);
var seq1 = cc.sequence(angleReverseDir);
_light.runAction(cc.repeatForever(seq1));
}
});
//
// Flow control
//
@ -1640,6 +1704,7 @@ if (cc.sys.os !== cc.sys.OS_WP8 || cc.sys.os !== cc.sys.OS_WINRT) {
Sprite3DUVAnimationTest,
Sprite3DFakeShadowTest,
Sprite3DBasicToonShaderTest,
Sprite3DNormalMappingTest
]);
}

View File

@ -1208,6 +1208,93 @@ function Sprite3DCubeMapTest:addNewSpriteWithCoords(pos)
end
end
----------------------------------------
----Sprite3DNormalMappingTest
----------------------------------------
local Sprite3DNormalMappingTest = class("Sprite3DNormalMappingTest", function ()
local layer = cc.Layer:create()
Helper.initWithLayer(layer)
return layer
end)
function Sprite3DNormalMappingTest:ctor()
-- body
self:init()
end
function Sprite3DNormalMappingTest:init()
Helper.titleLabel:setString(self:title())
Helper.subtitleLabel:setString(self:subtitle())
self:registerScriptHandler(function (event)
if event == "enter" then
self:onEnter()
elseif event == "exit" then
self:onExit()
end
end)
end
function Sprite3DNormalMappingTest:title()
return "Testing Normal Mapping"
end
function Sprite3DNormalMappingTest:subtitle()
return ""
end
function Sprite3DNormalMappingTest:onEnter()
local sprite3d = cc.Sprite3D:create("Sprite3DTest/sphere.c3b")
sprite3d:setScale(2.0)
sprite3d:setPosition(cc.p(-30,0))
sprite3d:setRotation3D(cc.vec3(90.0, 0.0, 0.0))
sprite3d:setTexture("Sprite3DTest/brickwork-texture.jpg")
sprite3d:setCameraMask(2)
self:addChild(sprite3d)
local sprite3dBumped = cc.Sprite3D:create("Sprite3DTest/sphere_bumped.c3b")
sprite3dBumped:setScale(20.0)
sprite3dBumped:setPosition(cc.p(30,0))
sprite3dBumped:setRotation3D(cc.vec3(90.0, 0.0, 0.0))
sprite3dBumped:setCameraMask(2)
self:addChild(sprite3dBumped)
local radius = 100.0
local angle = 0.0
local reverseDir = false
local light = cc.PointLight:create(cc.vec3(0.0, 0.0, 0.0), cc.c3b(255, 255, 255), 1000.0)
local function lightUpdate()
light:setPosition3D(cc.vec3(radius * math.cos(angle), 0.0, radius * math.sin(angle)))
if reverseDir == true then
angle = angle - 0.01
if angle < 0.0 then
reverseDir = false
end
else
angle = angle + 0.01
if 3.14159 < angle then
reverseDir = true
end
end
end
local seq = cc.Sequence:create(cc.CallFunc:create(lightUpdate))
light:runAction(cc.RepeatForever:create(seq))
self:addChild(light)
local visibleSize = cc.Director:getInstance():getVisibleSize()
local camera = cc.Camera:createPerspective(60, visibleSize.width / visibleSize.height, 10, 1000)
camera:setPosition3D(cc.vec3(0.0, 0.0, 100.0))
camera:lookAt(cc.vec3(0.0, 0.0, 0.0))
camera:setCameraFlag(cc.CameraFlag.USER1)
self:addChild(camera)
end
function Sprite3DNormalMappingTest:onExit()
end
function Sprite3DTest()
local scene = cc.Scene:create()
@ -1223,6 +1310,7 @@ function Sprite3DTest()
Sprite3DMirrorTest.create,
AsyncLoadSprite3DTest.create,
Sprite3DCubeMapTest.create,
Sprite3DNormalMappingTest.create,
}
scene:addChild(Sprite3DBasicTest.create())

View File

@ -132,6 +132,7 @@ skip = Node::[^setPosition$ setGLServerState description getUserObject .*UserDat
Mesh::[*],
Animation3DCache::[*],
Sprite3DMaterialCache::[*],
Sprite3DMaterial::[*],
Sprite3DCache::[*],
Bone3D::[*],
Device::[getTextureDataForText],

View File

@ -41,13 +41,14 @@ classes_need_extend = Sprite3D
skip = Skeleton3D::[create],
Sprite3D::[getAABB getMeshArrayByName createAsync],
Mesh::[create getMeshCommand getAABB getDefaultGLProgram getMeshVertexAttribute draw],
Mesh::[create getMeshCommand getAABB getDefaultGLProgram getMeshVertexAttribute draw setTexture getTexture],
Sprite3DCache::[addSprite3DData getSpriteData],
Animation3D::[getBoneCurves],
Animate3D::[getKeyFrameUserInfo],
TextureCube::[setTexParameters],
Terrain::[getAABB getQuadTree create getHeightData],
Bundle3D::[getTrianglesList calculateAABB]
Bundle3D::[getTrianglesList calculateAABB],
Sprite3DMaterial::[setTexture]
rename_functions =

View File

@ -132,6 +132,7 @@ skip = Node::[setGLServerState description getUserObject .*UserData getGLServerS
Mesh::[*],
Animation3DCache::[*],
Sprite3DMaterialCache::[*],
Sprite3DMaterial::[*],
Sprite3DCache::[*],
Bone3D::[*],
Device::[getTextureDataForText],

View File

@ -35,7 +35,7 @@ classes = Animate3D Sprite3D Animation3D Skeleton3D ^Mesh$ AttachNode BillBoard
# will apply to all class names. This is a convenience wildcard to be able to skip similar named
# functions from all classes.
skip = Mesh::[create getAABB getVertexBuffer hasVertexAttrib getSkin getMeshIndexData getGLProgramState getPrimitiveType getIndexCount getIndexFormat getIndexBuffer getMeshCommand getDefaultGLProgram],
skip = Mesh::[create getAABB getVertexBuffer hasVertexAttrib getSkin getMeshIndexData getGLProgramState getPrimitiveType getIndexCount getIndexFormat getIndexBuffer getMeshCommand getDefaultGLProgram getTexture setTexture],
Sprite3D::[getSkin getAABB getMeshArrayByName createAsync init initWithFile initFrom loadFromCache loadFromFile visit genGLProgramState createNode createAttachSprite3DNode createSprite3DNode getMeshIndexData addMesh onAABBDirty afterAsyncLoad],
Skeleton3D::[create],
Animation3D::[getBoneCurveByName getBoneCurves],
@ -43,10 +43,11 @@ skip = Mesh::[create getAABB getVertexBuffer hasVertexAttrib getSkin getMeshInde
BillBoard::[draw],
Sprite3DCache::[addSprite3DData getSpriteData],
Terrain::[lookForIndicesLODSkrit lookForIndicesLOD insertIndicesLOD insertIndicesLODSkirt getIntersectionPoint getAABB getQuadTree create ^getHeight$],
Bundle3D::[calculateAABB loadMeshDatas getTrianglesList loadObj]
Bundle3D::[calculateAABB loadMeshDatas getTrianglesList loadObj],
Sprite3DMaterial::[setTexture]
rename_functions =
rename_functions =
rename_classes =