Jump to content

How to displace UVs in real time?


LoLJester

Recommended Posts

Hello,


I'm trying to create a Tank vehicle with animated UV tracks.

Material animation is not an option as I'm to understand that Materials are not instanced per mesh, but one material applies to all meshes that have this material mapped to them.


Is it "tverts" that I must modify for the UV to displace in real time?

Link to comment
Share on other sites

Hi Azaezel, the video looks pretty good.

I can see what you did, but the code seems to only apply to quads that you create from point A to point B.

How do I apply this to sceneObjects that are already mapped?

What I'm trying to do is modify the positioning of a texture already mapped to the tank tracks.

I see that "tverts" is assigned the UVs of the mesh in assembleMesh() and assembleShape(), but once I modify it, where is it updated?

Am I even on the right path?

Link to comment
Share on other sites

Honestly, what I'd do is hack into something like

https://github.com/GarageGames/Torque3D/blob/2044b2691e1a29fa65d1bdd163f0d834995433ce/Engine/source/materials/processedShaderMaterial.cpp#L887

with

https://github.com/Azaezel/Torque3D/commit/178703f19a601c3ca370514930ef6e39fa405ba7


The per-instance material damage in the vid was an older version that used that mMaterialDamage being passed around to modify the pre-existing detail feature alpha blend, for instance. So in your case...


https://github.com/Azaezel/Torque3D/commit/178703f19a601c3ca370514930ef6e39fa405ba7#diff-ee63be9e1180cd38f5b10d798839b5f1R2604 would be something along the lines of rdata.setMaterialSpeed(getVelocity().len());


https://github.com/Azaezel/Torque3D/commit/178703f19a601c3ca370514930ef6e39fa405ba7#diff-8c164601ef835960f8dd7a4931f78792R161 for you setMaterialSpeed definitions


https://github.com/Azaezel/Torque3D/commit/178703f19a601c3ca370514930ef6e39fa405ba7#diff-5507520a39aa2d8c9d489a38d0d85565R215 + https://github.com/Azaezel/Torque3D/commit/178703f19a601c3ca370514930ef6e39fa405ba7#diff-1edd3e647b9fb79d7c959cd66c077883R37 + https://github.com/Azaezel/Torque3D/commit/178703f19a601c3ca370514930ef6e39fa405ba7#diff-e344db6d2836ae639d06783f2388d090R175 for your glue code


probably will need to alt https://github.com/GarageGames/Torque3D/blob/2044b2691e1a29fa65d1bdd163f0d834995433ce/Engine/source/materials/processedShaderMaterial.cpp#L783 to pass the sgData along so you can do the equivalent of https://github.com/Azaezel/Torque3D/commit/178703f19a601c3ca370514930ef6e39fa405ba7#diff-b7123dc2958b63669f99f0cd9ee534a5R1240 for your offset multiplier. (also probably want to add a flag to materials that conrols whether or not they're multiplied by velocity so you don't end up grinding the rest to a halt)


Edit: further reading: http://wiki.torque3d.org/coder:extending-the-torque3d-material-system probably does a much better job of handling the whys.

Link to comment
Share on other sites

  • 3 weeks later...

The Vert and Pix shaders:

 

void uvAnimFeatureHLSL::processVert( Vector &componentList, 
										 const MaterialFeatureData &fd )
	{
		MultiLine *meta = new MultiLine;
 
		if(fd.features[MFT_UseInstancing])
		{
			// We pass the uvOffset to the pixel shader via
			// another output register.
			//
			// TODO: We should see if we can share this register
			// with some other common instanced data.
			//
			ShaderConnector *conn = dynamic_cast( componentList[C_CONNECTOR] );
			Var *out_UV_Offset = conn->getElement( RT_TEXCOORD );
			out_UV_Offset->setStructName( "OUT" );
			out_UV_Offset->setName( "uvOffset" );
			out_UV_Offset->setType( "float" );
 
			ShaderConnector *vertStruct = dynamic_cast( componentList[C_VERT_STRUCT] );
			Var *inst_UV_Offset = vertStruct->getElement( RT_TEXCOORD, 1 );
			inst_UV_Offset->setStructName( "IN" );
			inst_UV_Offset->setName( "inst_uvOffset" );
			inst_UV_Offset->setType( "float" );
			mInstancingFormat->addElement( "uvOffset", GFXDeclType_Float, inst_UV_Offset->constNum );
 
			meta->addStatement( new GenOp( "   @ = @; // Instancing!\r\n", out_UV_Offset, inst_UV_Offset ) );
		}
 
		getOutTexCoord("texCoord", "float2", true, false, meta, componentList);
 
		output = meta;
	}
 
	//-------------------------------------------------------------------------------------------------------
	void uvAnimFeatureHLSL::processPix( Vector &componentList, 
										const MaterialFeatureData &fd )
	{
		//Sorin D: Get the uvOffset constant.
		Var *uvOffset = NULL;
		if(fd.features[MFT_UseInstancing])
			uvOffset = getInTexCoord( "uvOffset", "float", false, componentList );
		else
		{
			uvOffset = (Var*)LangElement::find( "uvOffset" );
 
			if(!uvOffset)
			{
				uvOffset = new Var();
				uvOffset->setType( "float" );
				uvOffset->setName( "uvOffset" );
				uvOffset->uniform = true;
				uvOffset->constSortPos = cspPotentialPrimitive;  
			}
		}
 
		MultiLine *meta = new MultiLine;
 
		//Sorin D: Get the texture coordinate UV0.
		Var *inUV = getInTexCoord( "texCoord", "float2", true, componentList );
 
		//Sorin D: Calculate and set the UV's offset.
		meta->addStatement(new GenOp( " @ = float2( @.x, @.y + @ );\r\n", inUV, inUV, inUV, uvOffset));
 
		output = meta;
	}
Link to comment
Share on other sites

Very cool!


Also, as a heads up, you can format a block of code via the code2 tags, like:


Open the block with code2=cpp, for cpp-style syntax highlighting(with it in brackets, of course) and then close with /code2(also in brackets)


Went ahead and did that for your post :)

Link to comment
Share on other sites

No. I'm on the road right now, always on the move. :) I will double check if everything gets initialized properly. I basically looked at how WindDeform and Visibility do it. Is there a way to Debug the Shaders through Torque?

 

Through torque itself no, though (except for Macs. Because Apple.) If there's an error, you should get console spew if for whatever reason a generated shader is non-compiling. If it's tacking the code-inserts on right, I'd look to the variable pass-along chain.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...