Jump to content
  • Advertisement
Sign in to follow this  
sunsflower

Direct11 texture animation

This topic is 967 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I think a general method to achieve texture animation(I mean sprite animation...) is to change texture coordinate every frame. First I try to make some transform in vertex shader. But that cause a problem that you have to use the same relative postion of texture coordinate all the time(the shape of them). It is somewhat inconvenient though with default vertex buffer it is fast(is it?..). This is the shader code:
PS_Input VS_Main( VS_Input vertex ){    
    PS_Input vsOut = ( PS_Input )0;    
    vsOut.pos = mul( vertex.pos, mvp_ );    
    float2 coord = vertex.tex0 + trans_;    
    vsOut.tex0 = float2(coord.x * scale_.x, coord.y * scale_.y);    
    return vsOut;
}
Then I just use a default vertex buffer for position and a dynamic vertex buffer for texture coord which is updated every frame. With a small amount of objects it works well. But I often doubt that this is not a good solution...the code:
	for (list<DirectSprite*>::reverse_iterator it = spritequickaccess->rbegin(); it != spritequickaccess->rend(); it++)
	{
		XMMATRIX mvp = XMMatrixMultiply((*it)->getworldmatrix(), vpMatrix_);
		mvp = XMMatrixTranspose(mvp);

		int n = (*it)->getsetnum();
		int i = sets[n].indexcount;
		int s = sets[n].startindexlocation;
		int b = sets[n].basevertexlocation;

		{
			//update dynamic vertex buffer
			D3D11_MAPPED_SUBRESOURCE mappedData;
			d3dContext_->Map(vertexBuffer_[1], 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData);
			VertexT* VBuffer = reinterpret_cast<VertexT*>(mappedData.pData);

			vector<VertexT>* VT = (*it)->get_texco();
			for (int ti = 0; ti < VT->size(); ti++) {
				VBuffer[b + ti].tex0 = VT->operator[](ti).tex0;
			}

			//unmap vertex buffer
			d3dContext_->Unmap(vertexBuffer_[1], 0);

		}

		d3dContext_->UpdateSubresource(mvpCB_, 0, 0, &mvp, 0, 0);
		d3dContext_->VSSetConstantBuffers(0, 1, &mvpCB_);
		
		d3dContext_->DrawIndexed(i, s, b);
	}

er..so, am I using the proper method? or is there any other method that can be used?
thanks in advance

Share this post


Link to post
Share on other sites
Advertisement

i implement animated textures by using multiple textures (AKA a texture array in later versions of DX), and switching textures every few frames.

 

or are you simply trying to scroll the texture across the face of the mesh?

 

changing texture coords will only move the texture around on the mesh, not display multiple frames of animation, unless you put all the frames on a single texture like a texture atlas. that would work just fine. i did something similar when testing "double textures". IE put 2 textures on one big texture then set texture coords based on which one you want to use. works great.  didn't help what i was trying to optimize at the time, but it works just fine.

Share this post


Link to post
Share on other sites
oh yes, I put them in a big texture atlas(forget to say that..) so, maybe dynamic vertex buffer isn't quite a good idea..I'd better use texture array. thanks!

Share this post


Link to post
Share on other sites


oh yes, I put them in a big texture atlas(forget to say that..) so, maybe dynamic vertex buffer isn't quite a good idea..I'd better use texture array. thanks!

 

texture should have nothing to do with vb.

 

and dynamic vertex buffers are REALLY slow.  its often faster to create and cache static buffers on the fly as needed. only in very particular circumstances is dynamic the best solution.and odds are you're not in that situation, almost nobody ever is.

 

to animate a texture on a mesh, all you need is a static VB and IB, and a bunch of textures. they can be individual textures, or in a texture array, or in a texture atlas with changing texture coordinates. perhaps a couple other ways too.

 

when i'm trying to do something like that for the first time, i'll start by googling the web, and searching gamedev for answers. its expected before posting here. if i don't find the answer, i'll start a post such as "animated textures in dx11 - best methods". and ask how to do it. that way i avoid things like wasting time with texture atlases, texture coord code, and dynamic buffers when a texture array and a plain old static mesh are what i need. makes one a more efficient gamedev. step 1, research and learn the right way to do it. step 2: implement in code. really you shouldn't even turn on the pc until the code is already written in your head. but it takes knowledge and some practice to get there though.

Share this post


Link to post
Share on other sites

Couldn't you calculate the UV in the vertex shader dynamically based on shader constants, instead of supplying them with the VB? Then there's no need to update any VB :D

Share this post


Link to post
Share on other sites
Sorry for my clumsiness...I did search on bing(google is not available here) and got no useful result.. But when I add some object into the scene, I have to update VB anyway. Should I just use models and load them at initialzation time, so I can draw instances of them without having to change VB? Is this the only way?

Share this post


Link to post
Share on other sites

"But when I add some object into the scene, I have to update VB anyway" - no you don't, you just render the objects, the vertex buffers are already resident and defined.

"Should I just use models and load them at initialzation time, so I can draw instances of them without having to change VB?" - yes, but you don't need to instance them unless you're drawing 'A LOT', just draw them.

 

One way of animating the sprite is to simply switch the texture, it's actually that simple. 

Texture2D<float4> gTexture : register(t0); // this changes every few frames.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!