Game Development Community

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.

//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.



Page«First 1 2 Next»
#21
09/17/2009 (5:59 pm)
Okay, great! Thanks
#22
09/17/2009 (6:23 pm)
// GFX2_RENDER_MERGE 
		//HJ: changed from original to work with T3D
		MaterialFeatureData fd = MATMGR->getDefaultFeatures();   // GFXMaterialFeatureData >>>> MaterialFeatureData, MaterialManager::get() >>>MATMGR
		//fd.features[MFT_RTLighting] = true;                        //  
		const FeatureSet &features = fd.codify();   //MaterialFeatureData::MaterialFeaturesHandle >>>MaterialFeaturesHandle
		//GFXVertexPNT *tsVertex = NULL;   
		matInst->init(features, getGFXVertexFormat<GFXVertexPNT>() );  
		//end:

I THINK thats all
#23
09/20/2009 (11:18 am)
Even though I had no problem with the build, nothing happens with the relevant commands after it.
So, at first, I'm going to look closely into the MaterialFeatureData to find out if it is the reason.
#24
09/20/2009 (2:33 pm)
thats exactly the issue I have...
#25
09/28/2009 (12:59 am)
I was able to spend a little time on this yesterday. I tried to change the default material setting in another way. But no luck.
I think I can spare my time on this this evening. Hope I can post some good result soon.
#26
10/06/2009 (12:02 pm)
I have been porting it to T3D 1.0, no difference with beta 5 for this resource.
At this moment, I see the skins are changed, but only when I disregard the assertFatal message by force. It is
AssertFatal(pd.size >= size, "Not enough room in the buffer for this data!");
in GenericConstBufferLayout::set(). I'm having a difficulty in finding the answer to avoid this.

The only thing that I made change in 1.0 is the last part of TSMaterialList::setMaterial(U32 index, Material* newMat). It is this.
// GFX2_RENDER_MERGE    
		//HJ: changed from original to work with T3D   
		//MaterialFeatureData fd = MaterialFeatureData(MATMGR->getDefaultFeatures());     
		//fd.features[MFT_RTLighting] = true;				//beta 5: commented out by deepscratch's solution
		FeatureSet features = MATMGR->getDefaultFeatures(); 
		//GFXVertexPNT *tsVertex = NULL;
		features.addFeature( MFT_VertTransform );
	        features.addFeature( MFT_DiffuseMap );
		features.addFeature( MFT_OverlayMap );
		features.addFeature( MFT_DetailMap );
		features.addFeature( MFT_DiffuseColor );
		features.addFeature( MFT_ColorMultiply );
		features.addFeature( MFT_AlphaTest );
		features.addFeature( MFT_IsTranslucent );
		mMatInstList[index]->init(features, getGFXVertexFormat<GFXVertexPNT>() );     
		//end:

I'm going to spend a little more time on this. But if someone has the answer for this and let us know, it would be highly appreciated.
#27
10/29/2009 (2:32 pm)
@game4Rest - I'm a little confused as to how to get this in. Its a bit difficult to follow the thread and see what the code should actually look like with regard to the code in your initial post. Add to that we are now in version 1.0.1. Maybe you could edit your initial post to show the most recent version you are referring to as of the post immediately above this one?

Anyway, in lieu of that:
To catch up to the changes you express in this post should I first download the original resource then make changes as expressed in your initial post on this thread?

My goal is to catch up to were you are and figure out what the fix is for the current problem.


Thanks in advance.
#28
10/30/2009 (11:35 pm)
@Everyone,

I apologize for being late to come out with the solution. As I don't have enough time to concentrate on this, and I don't have good knowledge about shader which seems bring this problem either, it hasn't been easy to find the answer.

So, as request, I started a new thread here.
Page«First 1 2 Next»