由于是第一次做coco3d方面的开发,技术资料比较少,很多情况下都是自己开引擎的源码来揣摩如何使用,因此出现很多差错是在所难免的。
今天刚解决了一个困扰好几天的单个模型设置多个shader的问题。在开发的过程中并没有多mesh shader渲染的例子,设置shader的例子都是通过sprite3d设置glprogram
然后绑定纹理,这样就导致了子mesh用的都是一个纹理,显示上出现了纹理重复的问题。
之后通过阅读引擎源码终于发现遍历sprite3d的子mesh可以单独设置shader,然后就用上了这个方法,但是用了之后发现,纹理还是会出现重复的现象,这导致了我怀疑是不是引擎没有设计好导致一个sprite3d只能设置一个shader,多个mesh共用。不停的查阅资料,baidu搜索,始终没找到解决方案。最后的大招就是请教了行业牛人了,cocos3d专门写教程的达人火云洞红孩,我想他一定能解决这个问题了吧。在qq上咨询了大牛,demo源码,3d模型也传给了他,终于在他有空的时候给我来了一句,这个问题不就是sprite3d子模型shader渲染么,没那么复杂,你的shader代码写的太乱了,你重新写一个最简单最干净的分别绑定不同的纹理看看。
我就照他说的做了,果然发现绑定不同的纹理是可以的,是我写的测试shader代码太乱了,然后c++代码中绑定的纹理本身重复了、。
上代码
void HelloWorld::Sprite3DLightMapTest()
{
//the assets are from the OpenVR demo
//get the visible size.
Size visibleSize = Director::getInstance()->getVisibleSize();
_camera = Camera::createPerspective(60,visibleSize.width / visibleSize.height,0.1f,200);
_camera->setCameraFlag(CameraFlag::USER1);
//_camera->setPosition3D(Vec3(0,25,15));
_camera->setPosition3D(Vec3(0,15,25));
//_camera->setRotation3D(Vec3(-35,0));
//auto LightMapScene = Sprite3D::create("Sprite3DTest/LightMapScene.c3b"); huangjinkai.c3b
auto LightMapScene = Sprite3D::create("longwangkaijia.c3b");
//auto LightMapScene = Sprite3D::create("longwanghutui.c3b");
auto sp = Sprite3D::create("longwanghutui.c3b");
//sp->setScale(0.1f);
//LightMapScene->getAttachNode("Bip01 Pelvis")->addChild(sp);
auto count = LightMapScene->getMeshCount();
//创建波光贴图。
auto textrue2 = Director::getInstance()->getTextureCache()->addImage("upper_03_m.jpg");
//创建细的波光贴图。
auto textrue21 = Director::getInstance()->getTextureCache()->addImage("caustics.png");
auto textrue22 = Director::getInstance()->getTextureCache()->addImage("caustics.png");
auto texture0 = Director::getInstance()->getTextureCache()->addImage("upper_03_huangjinkai.jpg");
Texture2D::TexParams tRepeatParams;
tRepeatParams.magFilter = GL_LINEAR_MIPMAP_LINEAR;
tRepeatParams.minFilter = GL_LINEAR;
tRepeatParams.wrapS = GL_REPEAT;
tRepeatParams.wrapT = GL_REPEAT;
textrue2->setTexParameters(tRepeatParams);
textrue21->setTexParameters(tRepeatParams);
textrue22->setTexParameters(tRepeatParams);
for (ssize_t i = 0; i < LightMapScene->getMeshCount(); i++) {
auto mesh = LightMapScene->getMeshByIndex(static_cast<int>(i));
std::string strName=mesh->getName();
auto texture = mesh->getTexture();
char szBuf[100],szBuf1[100];
sprintf(szBuf,"UVAnimation.vsh",i + 1);
sprintf(szBuf1,"UVAnimation.fsh",i + 1);
auto glprogram = GLProgram::createWithFilenames(szBuf,szBuf1);
// 由Shader文件创建这个Shader
auto glprogramstate = GLProgramState::create(glprogram);
sprintf(szBuf,"u_texture1",i + 1);
if (i == 0)
{
glprogramstate->setUniformTexture(szBuf,texture);
mesh->setTexture(texture);
}
else
{
glprogramstate->setUniformTexture(szBuf,texture);
}
glprogramstate->setUniformTexture("u_lightTexture",textrue21);
glprogramstate->setUniformTexture("u_lightTexture21",textrue21);
glprogramstate->setUniformTexture("u_lightTexture22",textrue22);
Vec4 tLightColor(1.0,1.0,1.0);
glprogramstate->setUniformVec4("v_LightColor",tLightColor);
mesh->setGLProgramState(glprogramstate);
long offset = 0;
auto attributeCount = mesh->getMeshVertexAttribCount();
for (auto k = 0; k < attributeCount; k++) {
auto meshattribute = mesh->getMeshVertexAttribute(k);
std::string strName = s_attributeNames[meshattribute.vertexAttrib];
mesh->getGLProgramState()->setVertexAttribPointer(strName,
meshattribute.size,
meshattribute.type,
GL_FALSE,
mesh->getVertexSizeInBytes(),
(GLvoid*)offset);
offset += meshattribute.attribSizeBytes;
}
}
//return;
auto textrue3 = Director::getInstance()->getTextureCache()->addImage("upper_03_huangjinkai.jpg");
auto _rend = RenderTexture::create(128,128,Texture2D::PixelFormat::RGBA8888);
auto sprite = Sprite::createWithTexture(textrue3);
sprite->setPosition(64,64);
sprite->setFlippedY(true);
_rend->beginWithClear(1.0,0.5);
sprite->visit();
_rend->end();
_rend->saveToFile("test.png",Image::Format::PNG);
auto texture4 = _rend->getSprite()->getTexture();
//LightMapScene->setTexture(texture4);
LightMapScene->setScale(0.1f);
//LightMapScene->setRotation3D(Vec3(0,360,0));
addChild(LightMapScene);
//return;
addChild(_camera);
setCameraMask(2);
//add a point light
//auto light = PointLight::create(Vec3(35,75,-20.5),Color3B(255,255,255),150);
auto light = PointLight::create(Vec3(0,15),150);
auto _directionalLight = DirectionLight::create(Vec3(0,-1.0f),255));
auto _spotLight = SpotLight::create(Vec3(0,Vec3(0.0f,15.0f,25.0f),0),0.0,0.5,10000.0f);
//addChild(_spotLight);
//addChild(_directionalLight);
//light->setRotation3D(Vec3(-35,0));
Vec3 vpos=LightMapScene->getPosition3D();
//addChild(light);
//set the ambient light
auto ambient = AmbientLight::create(Color3B(255,255));
//addChild(ambient);
}
总结:碰到这种从来没有弄过的新技术问题,一定要写最小的demo来做测试验证,不要写的太乱太杂。实在无法解决问题的终极方案就是请教行业专家。
这其中要掌握推断的技巧,还有缺乏api文档demo直接看工程源码的能力