Dynamic skins and materials swap
by game4Rest · in Torque 3D Professional · 06/10/2009 (5:42 am) · 28 replies
I'm porting dynamic skins and materials swap resource for TGEA to T3D. By this time, the porting has been successful except one thing. As you can see in below video, when the camera looks at the player in certain direction, the color changes to a white tone.
Here goes the changed codes. Commented with HJ: is the code that I made change.
I guess it has something to do with the lighting. But I have no idea where and how I fix it. Please someone give me any kind of advice to solve this.
Here goes the changed codes. Commented with HJ: is the code that I made change.
//HJ: dynamic skin and material swap
CustomMaterial & CustomMaterial::operator =(const CustomMaterial &material) {
Parent::operator =(material);
// custom material copy
for (S32 i = 0; i < MAX_TEX_PER_PASS; i++) {
mTexFilename[i] = material.mTexFilename[i];
mFlags[i] = material.mFlags[i];
}
if (NULL == material.mFallback) {
mFallback = NULL;
} else{
*mFallback = *material.mFallback;
}
//HJ: These are changed in T3D
/*if (NULL == material.mDynamicLightingMaterial) {
mDynamicLightingMaterial = NULL;
} else{
mDynamicLightingMaterial = material.mDynamicLightingMaterial;
}
if (NULL == material.mDynamicLightingMaskMaterial) {
mDynamicLightingMaskMaterial = NULL;
} else{
mDynamicLightingMaskMaterial = material.mDynamicLightingMaskMaterial;
} */
//End:
mVersion = material.mVersion;
mRefract = material.mRefract;
mMaxTex = material.mMaxTex;
ShaderData* sd = static_cast<ShaderData*>(Sim::findObject(material.mShaderDataName));
if ( sd ) {
mShaderData = sd;
}
mShaderDataName = material.mShaderDataName;
return *this;
}//HJ: dynamic skin and material swap
Material & Material::operator =(const Material &material)
{
// tedious, but want to avoid copying simobject data, which a straight memcopy would do.
// if you do that you get the simobject ID and then it complains when you try to register it.
for (S32 i = 0; i < MAX_STAGES; i++) {
mBaseTexFilename[i] = material.mBaseTexFilename[i];
mDetailTexFilename[i] = material.mDetailTexFilename[i]; //HJ: mDetailFilename>>>mDetailTexFilename
mBumpTexFilename[i] = material.mBumpTexFilename[i]; //HJ: mBumpFileName>>>mBumpTexFilename
mEnvTexFilename[i] = material.mEnvTexFilename[i]; //HJ: mEnvFileName>>>mEnvTexFilename
mStages[i] = material.mStages[i];
mDiffuse[i] = material.mDiffuse[i];
mSpecular[i] = material.mSpecular[i];
mSpecularPower[i] = material.mSpecularPower[i];
mPixelSpecular[i] = material.mPixelSpecular[i];
mVertexSpecular[i] = material.mVertexSpecular[i];
mExposure[i] = material.mExposure[i];
mAnimFlags[i] = material.mAnimFlags[i];
mScrollDir[i] = material.mScrollDir[i];
mScrollSpeed[i] = material.mScrollSpeed[i];
mScrollOffset[i] = material.mScrollOffset[i];
mRotSpeed[i] = material.mRotSpeed[i];
mRotPivotOffset[i] = material.mRotPivotOffset[i];
mRotPos[i] = material.mRotPos[i];
mWavePos[i] = material.mWavePos[i];
mWaveFreq[i] = material.mWaveFreq[i];
mWaveAmp[i] = material.mWaveAmp[i];
mWaveType[i] = material.mWaveType[i];
mSeqFramePerSec[i] = material.mSeqFramePerSec[i];
mSeqSegSize[i] = material.mSeqSegSize[i];
mGlow[i] = material.mGlow[i];
mEmissive[i] = material.mEmissive[i];
mColorMultiply[i] = material.mColorMultiply[i];
}
mDoubleSided = material.isDoubleSided();
mCubemapName = material.mCubemapName;
mCubemapData = material.mCubemapData;
mDynamicCubemap = material.mDynamicCubemap;
mTranslucent = material.isTranslucent();
mTranslucentBlendOp = material.mTranslucentBlendOp;
mTranslucentZWrite = material.mTranslucentZWrite;
mAlphaTest = material.mAlphaTest;
mAlphaRef = material.mAlphaRef;
mPlanarReflection = material.mPlanarReflection;
mMapTo = material.mMapTo;
mIsIFL = material.isIFL();
mPath = material.getPath();
return *this;
}bool TSMaterialList::setMaterial(U32 index, Material* newMat)
{
if (index < 0 || index > mMaterials.size()) {
return false;
}
if ( newMat ) {
if (mMatInstList[index]->getMaterial() == newMat) {
return true;
}
// dump the old mat instance
if (mMatInstList[index]) {
delete mMatInstList[index];
}
mMatInstList[index] = NULL;
// change texture
CustomMaterial* cust = dynamic_cast<CustomMaterial*>(newMat);
String texPath;
if (cust) {
texPath = cust->mTexFilename[0];
} else{
texPath = newMat->mBaseTexFilename[0];
}
GFXTexHandle tex = GFXTexHandle(texPath, &GFXDefaultStaticDiffuseProfile, avar("%s() - NA (line %d)", __FUNCTION__, __LINE__));
if (!tex.isValid()) {
return false;
}
// change texture
mMaterials[index] = tex;
BaseMatInstance *matInst = newMat->createMatInstance();
mMatInstList[index] = matInst;
// GFX2_RENDER_MERGE
//HJ: changed from original to work with T3D
MaterialFeatureData fd = MATMGR->getDefaultFeatureData(); // GFXMaterialFeatureData >>>> MaterialFeatureData, MaterialManager::get() >>>MATMGR
fd.features[MFT_RTLighting] = true; //
MaterialFeaturesHandle features = fd.codify(); //MaterialFeatureData::MaterialFeaturesHandle >>>MaterialFeaturesHandle
//GFXVertexPNT *tsVertex = NULL;
matInst->init(features, features, getGFXVertexFormat<GFXVertexPNT>() );
//end:
}
return true;
}I guess it has something to do with the lighting. But I have no idea where and how I fix it. Please someone give me any kind of advice to solve this.
About the author
Recent Threads
#2
06/15/2009 (2:55 am)
If anyone is watching this thread and want to know the answer, I found out that my code change has no problem. It was just matter of setting materials.
#3
06/15/2009 (3:07 am)
Thanks for clearing that up! Good to hear you got it working!
#4
06/15/2009 (3:57 am)
Nice work.
#5
and leave the initmateriallist work to tsshapeinstance
like this
06/15/2009 (5:39 am)
try commenting line 35 - 42and leave the initmateriallist work to tsshapeinstance
like this
void TSShapeInstance::SetDynamicSkin(DynamicSkinData* data)
{
....
pMatList->setMaterial(foundMatInstIndex,newMat);
initMaterialList(); // add this line
}
#7
I have looked at the changes that you made and I understand most of what you have done. I am however having trouble understanding this part
If you can explain this piece of code, I'd really appreciate it. (incidentally I am having trouble compiling this portion, for example, the object MaterialFeaturesHandle does not exist, and the line fd.features[MFT_RTLighting] = true; is also returning an error)
Thanks in advance.
07/27/2009 (2:16 pm)
Hi game4Rest,I have looked at the changes that you made and I understand most of what you have done. I am however having trouble understanding this part
// GFX2_RENDER_MERGE //HJ: changed from original to work with T3D MaterialFeatureData fd = MATMGR->getDefaultFeatureData(); // GFXMaterialFeatureData >>>> MaterialFeatureData, MaterialManager::get() >>>MATMGR fd.features[MFT_RTLighting] = true; // MaterialFeaturesHandle features = fd.codify(); //MaterialFeatureData::MaterialFeaturesHandle >>>MaterialFeaturesHandle //GFXVertexPNT *tsVertex = NULL; matInst->init(features, features, getGFXVertexFormat<GFXVertexPNT>() ); //end:
If you can explain this piece of code, I'd really appreciate it. (incidentally I am having trouble compiling this portion, for example, the object MaterialFeaturesHandle does not exist, and the line fd.features[MFT_RTLighting] = true; is also returning an error)
Thanks in advance.
#8
I tested above my codes in T3D beta 3 version. I found there have been made many changes with material codes in T3D beta 4 version again. Which version do you have?
I don't have background details of the code you mentioned. But I guess the errors you have have something to do with it.
07/27/2009 (9:54 pm)
@Samaursa,I tested above my codes in T3D beta 3 version. I found there have been made many changes with material codes in T3D beta 4 version again. Which version do you have?
I don't have background details of the code you mentioned. But I guess the errors you have have something to do with it.
#9
I'm busy getting it to work in B4, so to fix the
fd.features[MFT_RTLighting] error,
add
the MaterialFeaturesHandle, I'm busy right now trying to figure out.
07/28/2009 (1:53 am)
@Samaursa,I'm busy getting it to work in B4, so to fix the
fd.features[MFT_RTLighting] error,
add
#include "materials/materialFeatureTypes.h"to the begining of that file.
the MaterialFeaturesHandle, I'm busy right now trying to figure out.
#10
change
remove
remove
put this
below this
remember, this is in beta 4.
07/28/2009 (2:33 am)
ok,change
MaterialFeaturesHandle features = fd.codifyto
const FeatureSet &features = fd.codify();
remove
fd.features[MFT_RTLighting] = true;
remove
mVertexSpecular[i] = material.mVertexSpecular[i];
put this
protected :
Vector<DynamicSkinData> mDynamicSkins;
void BuildDynamicSkins();
void ClearDynamicSkins();below this
bool ResetDynamicSkin(const char* skinTag, const char* texture, const char* matName);
remember, this is in beta 4.
#11
Thanks again.
07/28/2009 (7:04 am)
Thank you for your help deepscratch. The changes work so far. When you are able to get the resource work with B4, can you please put your changes here? Currently I am trying to get the next few parts working (I started getting into the engine source a week ago so my knowledge about Torque Engine is limited). Thanks again.
#12
07/28/2009 (2:47 pm)
Way to go dude. This would be a great resource to build into the out-of-box T3D FPS sample.
#14
07/28/2009 (4:56 pm)
To avoid that big long copy method, you may be able to do something like this (haven't tried this so you may have to mess with it):Material & Material::operator =(const Material &material)
{
dMemcpy(reinterpret_cast<U8 *>(this) + sizeof(SimObject), reinterpret_cast<U8 *>(&material) + sizeof(SimObject), sizeof(Material) - sizeof(SimObject));
return *this;
}Check the values in the debugger to make sure they are correct...do not assume this code works, as I said I haven't tried it ;)
#15
07/29/2009 (12:13 pm)
I have been looking into T3D materials, and I found that there are four diffuse slots for each material. Diffuse[0], Diffuse[1] Diffuse[2] etc. Diffuse[0] is used for the base texture and any textures added to the other diffuse slots are 'blended' onto the main texture. My questions is (since I haven't seen/experienced previous torque versions such as TGEA), is this new to T3D? If so, isn't it much simpler to change the diffuse[1], [2] etc. to change the texture and to mimic the efficiency of the resource, create those materials (either beforehand or on the fly) and assign them to whichever object is currently using that combination.
#16
is this working for you in beta 5?
I have the tgea resource in, with the t3d changes, builds fine,
but in game, on giving the commands, I get the echo's that it is doing its job, but the skins dont change.
any ideas?
09/11/2009 (9:00 pm)
@game4Rest,is this working for you in beta 5?
I have the tgea resource in, with the t3d changes, builds fine,
but in game, on giving the commands, I get the echo's that it is doing its job, but the skins dont change.
any ideas?
#17
I haven't tried it in beta 5 yet. I'm going to try it this weekend and come back to you with result whether it works or not.
09/12/2009 (2:43 am)
@deepscratchI haven't tried it in beta 5 yet. I'm going to try it this weekend and come back to you with result whether it works or not.
#18
09/15/2009 (9:50 am)
So many things are changed in beta 5. I need more time.
#19
Will you be release you changes to the public through a resource?
09/15/2009 (4:17 pm)
@game4Rest,Will you be release you changes to the public through a resource?
#20
Can you let us know what changes you've made in Beta 5 to make the build work?
@Tek0,
When it works, I will.
09/16/2009 (5:40 am)
@deepsratch,Can you let us know what changes you've made in Beta 5 to make the build work?
@Tek0,
When it works, I will.
Torque Owner game4Rest
colyd studio