//----------------------------------------------------------------------------
void FramesMesh::_Cal(const std::string &texPackFilename)
{
	const TexPack &texPack = PX2_RM.GetTexPack(texPackFilename);
	if (!texPack.IsValid()) return;

	std::string outPath;
	std::string outBaseFilename;
	StringHelp::SplitFilename(texPackFilename, outPath, outBaseFilename);

	std::string outBaseName;
	std::string outExt;
	StringHelp::SplitBaseFilename(outBaseFilename, outBaseName, outExt);

	VertexFormat *vf = PX2_GR.GetVertexFormat(GraphicsRoot::VFT_PCT1);
	SetVertexFormat(vf);

	mNumAllFrames = (int)texPack.Elements.size();
	mNumFramesPerDir = mNumAllFrames / mNumDir;
	if (0 == mNumFramesPerDir)
	{
		//assertion(false, "no frames");
		//return;
	}

	VBIBObj &obj = VBIBManager::GetSingleton().GetVBID(texPackFilename);
	if (obj.IsValued)
	{
		SetVertexBuffer(obj.mVB);
		SetIndexBuffer(obj.mIB);
	}
	else
	{
		int numVertex = mNumAllFrames * 4;
		int numIndex = mNumAllFrames * 6;

		int frameIndex = 0;
		VertexBuffer *vb = new0 VertexBuffer(numVertex, vf->GetStride());
		VertexBufferAccessor vba(vf, vb);
		for (int i = 0; i < mNumDir; i++)
		{
			for (int j = 0; j < mNumFramesPerDir; j++)
			{
				std::string eleName = outBaseName + "_" + StringHelp::IntToString(i * 45) + "_" + StringHelp::IntToString(j + 1);

				const TexPackElement &ele = PX2_RM.GetTexPackElement(texPackFilename, eleName);

				float xPlusPer = 0.0f;
				float widthPer = 0.0f;
				float zPlusPer = 0.0f;
				float heightPer = 0.0f;
				if (0 != ele.OW)
				{
					xPlusPer = (float)ele.OX / (float)ele.OW;
					widthPer = (float)ele.W / (float)ele.OW;
				}
				if (0 != ele.OH)
				{
					zPlusPer = 1.0f - (float)(ele.OY + ele.H) / (float)ele.OH;
					heightPer = (float)ele.H / (float)ele.OH;
				}

				float width = mSize;
				float height = mSize;

				float xPos = 0.0f - width * 0.5f;
				float zPos = 0.0f - height * 0.5f;
				xPos += xPlusPer * width;
				zPos += zPlusPer * height;

				width *= widthPer;
				height *= heightPer;

				Float3 p0 = Float3(xPos, zPos, 0.0f);
				Float3 p1 = Float3(xPos + width, zPos, 0.0f);
				Float3 p2 = Float3(xPos, zPos + height, 0.0f);
				Float3 p3 = Float3(xPos + width, zPos + height, 0.0f);

				float uBegin = (float)ele.X / (float)ele.TexWidth;
				float uEnd = (float)(ele.X + ele.W) / (float)ele.TexWidth;
				float vBegin = (float)(ele.TexHeight - ele.Y - ele.H) / (float)ele.TexHeight;
				float vEnd = (float)(ele.TexHeight - ele.Y) / (float)ele.TexHeight;

				vba.Position<Float3>(frameIndex * 4 + 0) = p0;
				vba.Position<Float3>(frameIndex * 4 + 1) = p1;
				vba.Position<Float3>(frameIndex * 4 + 2) = p2;
				vba.Position<Float3>(frameIndex * 4 + 3) = p3;

				vba.TCoord<Float2>(0, frameIndex * 4 + 0) = Float2(uBegin, vBegin);
				vba.TCoord<Float2>(0, frameIndex * 4 + 1) = Float2(uEnd, vBegin);
				vba.TCoord<Float2>(0, frameIndex * 4 + 2) = Float2(uBegin, vEnd);
				vba.TCoord<Float2>(0, frameIndex * 4 + 3) = Float2(uEnd, vEnd);

				frameIndex++;
			}
		}

		IndexBuffer *ib = new0 IndexBuffer(numIndex, 2);
		for (int i = 0; i < mNumAllFrames; i++)
		{
			unsigned short *indices = (unsigned short*)ib->GetData();
			indices[i * 6 + 0] = (uint16_t)(i * 4 + 0);
			indices[i * 6 + 1] = (uint16_t)(i * 4 + 1);
			indices[i * 6 + 2] = (uint16_t)(i * 4 + 2);
			indices[i * 6 + 3] = (uint16_t)(i * 4 + 1);
			indices[i * 6 + 4] = (uint16_t)(i * 4 + 3);
			indices[i * 6 + 5] = (uint16_t)(i * 4 + 2);
		}

		SetVertexBuffer(vb);
		SetIndexBuffer(ib);

		VBIBManager::GetSingleton().AddVBIB(texPackFilename, vb, ib);
	}

	Texture2D *tex = (Texture2D*)PX2_RM.BlockLoad(texPack.ImageFullPath);

	MaterialInstance *mi = new0 MaterialInstance(
		"Data/engine_mtls/tex2d/tex2d.px2obj", "tex2d", false);
	mi->SetPixelTexture(0, "SampleBase", tex);
	SetMaterialInstance(mi);

	mi->GetMaterial()->GetCullProperty(0, 0)->Enabled = false;

	mIsNeedReCal = false;
}
//-----------------------------------------------------------------------------
void ProjTreeItem::SetTreeLevel(ProjTreeLevel level, bool isShowHelpNode)
{
	mTreeLevel = level;
	mIsShowHelpNode = isShowHelpNode;

	Project *proj = DynamicCast<Project>(mObject);
	Scene *scene = DynamicCast<Scene>(mObject);
	if (proj || scene)
		return;

	Actor *actor = DynamicCast<Actor>(mObject);
	Movable *mov = DynamicCast<Movable>(mObject);
	Node *node = DynamicCast<Node>(mObject);
	Renderable *renderable = DynamicCast<Renderable>(mObject);
	EffectableController *effectableCtrl = DynamicCast<EffectableController>(mObject);
	Material *mtl = DynamicCast<Material>(mObject);
	MaterialTechnique *mtlTechnique = DynamicCast<MaterialTechnique>(mObject);
	MaterialPass *mtlPass = DynamicCast<MaterialPass>(mObject);

	if (!mObject)
	{ // IT_CATALOG
		for (int i = 0; i < (int)mChildItems.size(); i++)
		{
			ProjTreeItem *item = mChildItems[i];
			if (item) item->SetTreeLevel(level, isShowHelpNode);
		}
		return;
	}
	else
	{
		ClearChildren();
	}

	bool addCtrl = false;
	bool addModule = false;
	bool addNode = false;
	bool addMaterial = false;

	if (PTL_GENERAL == mTreeLevel)
	{
	}
	else if (PTL_CHILDREN == mTreeLevel)
	{
		if (!scene && !proj && node)
		{
			addNode = true;
		}
	}
	else if (PTL_CONTROLS == mTreeLevel)
	{
		if (!scene && !proj && mov)
		{
			addCtrl = true;
		}

		if (!scene && !proj && node)
		{
			addNode = true;
		}

		if (effectableCtrl)
		{
			addModule = true;
		}
	}
	else if (PTL_MATERIALS == mTreeLevel)
	{
		if (!scene && !proj && mov)
		{
			addCtrl = true;
		}

		if (!scene && !proj && node)
		{
			addNode = true;
		}

		if (renderable)
		{
			addMaterial = true;
		}
	}
	else if (PTL_DETAIL == mTreeLevel)
	{
		if (!scene && !proj && mov)
		{
			addCtrl = true;
		}

		if (!scene && !proj && node)
		{
			addNode = true;
		}

		if (renderable)
		{
			addMaterial = true;
		}
	}

	if (addCtrl)
	{
		int numCtrls = mov->GetNumControllers();
		for (int i = 0; i < numCtrls; i++)
		{
			Controller *ctrl = mov->GetController(i);

			AddChild(ctrl, mIconID, mTreeLevel, mIsShowHelpNode);
		}
	}

	if (addMaterial)
	{
		MaterialInstance *mtlInst = renderable->GetMaterialInstance();
		Material *mtl = mtlInst->GetMaterial();
		AddChild(mtl, mIconID, mTreeLevel, mIsShowHelpNode);
	}
	if (mtl)
	{
		int numTechniques = mtl->GetNumTechniques();
		for (int i = 0; i < numTechniques; i++)
		{
			MaterialTechnique *mtlTechnique = mtl->GetTechnique(i);
			AddChild(mtlTechnique, mIconID, mTreeLevel, isShowHelpNode);
		}
	}
	if (mtlTechnique)
	{
		int numPasses = mtlTechnique->GetNumPasses();
		for (int i = 0; i < numPasses; i++)
		{
			MaterialPass *mtlPass = mtlTechnique->GetPass(i);
			AddChild(mtlPass, mIconID, mTreeLevel, isShowHelpNode);
		}
	}
	if (mtlPass)
	{
		AddChild(mtlPass->GetVertexShader(), mIconID, mTreeLevel, isShowHelpNode);
		AddChild(mtlPass->GetPixelShader(), mIconID, mTreeLevel, isShowHelpNode);

		AddChild(mtlPass->GetAlphaProperty(), mIconID, mTreeLevel, isShowHelpNode);
		AddChild(mtlPass->GetCullProperty(), mIconID, mTreeLevel, isShowHelpNode);
		AddChild(mtlPass->GetDepthProperty(), mIconID, mTreeLevel, isShowHelpNode);
		AddChild(mtlPass->GetOffsetProperty(), mIconID, mTreeLevel, isShowHelpNode);
		AddChild(mtlPass->GetStencilProperty(), mIconID, mTreeLevel, isShowHelpNode);
		AddChild(mtlPass->GetWireProperty(), mIconID, mTreeLevel, isShowHelpNode);
	}

	if (addModule)
	{
		int numModules = effectableCtrl->GetNumModules();
		for (int i = 0; i < numModules; i++)
		{
			EffectModule *module = effectableCtrl->GetModule(i);
			AddChild(module, mIconID, mTreeLevel, mIsShowHelpNode);
		}
	}

	if (addNode)
	{
		int numChildren = node->GetNumChildren();
		for (int i = 0; i < numChildren; i++)
		{
			Movable *child = node->GetChild(i);
			if (child)
			{
				bool ingore = false;
				if (!isShowHelpNode && ("HelpNode" == child->GetName()))
					ingore = true;

				if (!ingore)
				{
					AddChild(child, mIconID, mTreeLevel, mIsShowHelpNode);
				}
			}
		}
	}
}