void PS_Light::_update(Particle * p) { Float3 Position = p->Position; if (mParent->IsLocalSpace()) Position.TransformA(mParent->GetParent()->GetWorldTM()); mLight->SetPosition(Position); mLight->SetDirection(p->Direction); mLight->SetDiffuse(Float3(p->Color.r, p->Color.g, p->Color.b)); }
void PS_Mesh::_update(Particle * p) { Float3 Position = p->Position; if (mParent->IsLocalSpace()) Position.TransformA(mParent->GetParent()->GetWorldTM()); mMesh->SetPosition(Position); mMesh->SetOpacity(p->Color.a); mMesh->SetRotationEx(p->Rotation); mMesh->SetScale(p->Size); mMesh->_updateTM(); int blendMode = mParent->GetBlendMode(); for (int i = 0; i < mMesh->GetSubMeshCount(); ++i) { SubMesh * submesh = mMesh->GetSubMesh(i); Material * mtl = submesh->GetMaterial(); mtl->diffuse = Float3(p->Color.r, p->Color.g, p->Color.b); if (mParent->_getTexture() != NULL) { mtl->maps[eMapType::DIFFUSE] = mParent->_getTexture(); } if (mParent->GetShaderEnable()) { mtl->depthMode = eDepthMode::N_LESS_EQUAL; if (blendMode == PS_BlendMode::ADD) { mtl->blendMode = eBlendMode::ADD; } else if (blendMode == PS_BlendMode::ALPHA_BLEND) { mtl->blendMode = eBlendMode::ALPHA_BLEND; } else if (blendMode == PS_BlendMode::COLOR_BLEND) { mtl->blendMode = eBlendMode::COLOR_BLEND; } else { mtl->blendMode = eBlendMode::OPACITY; mtl->depthMode = eDepthMode::LESS_EQUAL; } submesh->SetShaderFX(mParent->_getShaderFX()); submesh->SetRenderCallBack(eRenderCallBack::SHADER, mParent->GetShader().c_ptr()); } } }
void MeshGroup::_genMesh(const Array<Mesh *> & arr, int first, int last, bool hasLightingColor) { MeshSourcePtr source = arr[0]->GetSource(); Mesh * mesh = new Mesh; for (int i = 0; i < source->GetMeshBufferCount(); ++i) { SubMesh * submesh = mesh->NewSubMesh(); VertexBufferPtr srcVB = source->GetMeshBuffer(i)->GetRenderOp()->vertexBuffers[0]; IndexBufferPtr srcIB = source->GetMeshBuffer(i)->GetRenderOp()->indexBuffer; int p_offset = source->GetMeshBuffer(i)->GetRenderOp()->vertexDeclarations[0].GetElementOffset(eVertexSemantic::POSITION); int n_offset = source->GetMeshBuffer(i)->GetRenderOp()->vertexDeclarations[0].GetElementOffset(eVertexSemantic::NORMAL); int stride = srcVB->GetStride(); VertexBufferPtr vb = HWBufferManager::Instance()->NewVertexBuffer(stride, srcVB->GetCount() * (last - first)); const char * v_src = (const char *)srcVB->Lock(eLockFlag::READ); char * v_dest = (char *)vb->Lock(eLockFlag::WRITE); for (int j = first; j < last; ++j) { const Mat4 & worldTM = arr[j]->GetWorldTM(); bool hasScale = arr[j]->GetWorldScale() != Float3(1, 1, 1); for (int k = 0; k < srcVB->GetCount(); ++k) { memcpy(v_dest, v_src, stride); Float3 * position = (Float3 *)(v_dest + p_offset); position->TransformA(worldTM); if (n_offset != -1) { Float3 * normal = (Float3 *)(v_dest + n_offset); normal->TransformN(worldTM); if (hasScale) { normal->Normalize(); } } v_dest += stride; v_src += stride; } } vb->Unlock(); srcVB->Unlock(); IndexBufferPtr ib = HWBufferManager::Instance()->NewIndexBuffer(srcIB->GetCount() * (last - first)); int startVertex = 0; const short * i_src = (const short *)srcIB->Lock(eLockFlag::READ); char * i_dest = (char *)ib->Lock(eLockFlag::WRITE); for (int j = first; j < last; ++j) { for (int k = 0; k < srcIB->GetCount(); ++k) { *i_dest++ = (*i_src++) + startVertex; } startVertex += srcVB->GetCount(); } ib->Unlock(); srcIB->Unlock(); submesh->GetRenderOp()->vertexDeclarations[0] = source->GetMeshBuffer(i)->GetRenderOp()->vertexDeclarations[0]; submesh->GetRenderOp()->vertexBuffers[0] = vb; submesh->GetRenderOp()->indexBuffer = ib; submesh->GetRenderOp()->primCount = ib->GetCount() / 3; submesh->GetRenderOp()->primType = ePrimType::TRIANGLE_LIST; if (hasLightingColor) { int count = submesh->GetRenderOp()->vertexBuffers[0]->GetCount(); submesh->GetRenderOp()->vertexDeclarations[LIGHTING_COLOR_STREAM].AddElement(eVertexSemantic::LIGHTING_COLOR, eVertexType::UBYTE4); submesh->GetRenderOp()->vertexBuffers[LIGHTING_COLOR_STREAM] = HWBufferManager::Instance()->NewVertexBuffer(4, count); Array<Rgba32> lightColors; Rgba32 * data = (Rgba32 *)submesh->GetRenderOp()->vertexBuffers[LIGHTING_COLOR_STREAM]->Lock(eLockFlag::WRITE); for (int j = first; j < last; ++j) { arr[j]->GetLightingColor(lightColors); d_assert (lightColors.Size() > 0); memcpy(data, &lightColors[0], 4 * count); startVertex += count; lightColors.Clear(); } submesh->GetRenderOp()->vertexBuffers[LIGHTING_COLOR_STREAM]->Unlock(); } *submesh->GetMaterial() = *source->GetMeshBuffer(i)->GetMaterial(); submesh->SetMeshShader(source->GetMeshBuffer(i)->GetShader()); } mesh->SetSLMode(hasLightingColor ? eStaticLightingMode::LIGHTING_COLOR : eStaticLightingMode::NONE); mMeshes.PushBack(mesh); }