x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Change mesh vertex at runtime (Apply custom Transformation on mesh)

Hello everyone, I'm working on a game system that allow the player to transform certain object . Translataion, Rotation and Scale are pretty straighforward, but the tricky part is the player must be able to Shear and Reflect the object.

Beleive me, if I could avoid these transformations I would, but it's a boss's request :( .

I know how to access the vertices with that

         FPositionVertexBuffer* vBuffer = &_mesh->StaticMesh->RenderData->LODResources[0].PositionVertexBuffer;

I do my calculus stuffs without problem, but how could I force the mesh reconstruction ???? (the problem)

I know several persons says "it's impossible to change vertices at runtime", but there's certainly a solution.

I know even if it works, it will be slow, but I don't care at this point because it won't happen often.

I digged the core mesh/render code for several days without success.

Any suggestions/solutions or anything else will be appreciate.

If you need more details let me know.

gamer08

Product Version: UE 4.8
Tags:
more ▼

asked Sep 17 '15 at 01:01 PM in C++ Programming

avatar image

gamer08
261 17 17 35

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

Hey again Gamer08, moving over here since it will easier for other people to find relevant information. I'm currently at work and I'm not tasked with vertices manipulation so I'll have to refresh my memory once I get home but have you checked out the FDynamicRHI::RHICreateVertexBuffer function?

If you have the vertex buffer you want then this should apply it to the mesh. According to the API you can even call it at runtime. Here's an example (written by Epic in the 4.7 engine) of how to use it to create a unit cube:

 class FUnitCubeVertexBuffer : public FVertexBuffer
 {
 public:
     /**
     * Initialize the RHI for this rendering resource
     */
     void InitRHI() override
     {
         const int32 NumVerts = 8;
         TResourceArray<FVector4, VERTEXBUFFER_ALIGNMENT> Verts;
         Verts.SetNumUninitialized(NumVerts);
 
         for (uint32 Z = 0; Z < 2; Z++)
         {
             for (uint32 Y = 0; Y < 2; Y++)
             {
                 for (uint32 X = 0; X < 2; X++)
                 {
                     const FVector4 Vertex = FVector4(
                         (X ? -1 : 1),
                         (Y ? -1 : 1),
                         (Z ? -1 : 1),
                         1.0f
                         );
 
                     Verts[GetCubeVertexIndex(X, Y, Z)] = Vertex;
                 }
             }
         }
 
         uint32 Size = Verts.GetResourceDataSize();
 
         // Create vertex buffer. Fill buffer with initial data upon creation
         FRHIResourceCreateInfo CreateInfo(&Verts);
         VertexBufferRHI = RHICreateVertexBuffer(Size, BUF_Static, CreateInfo);
     }
 };

Hope it helps!

more ▼

answered Sep 21 '15 at 08:13 PM

avatar image

PhilipRamirez
96 2 10 17

avatar image gamer08 Sep 22 '15 at 10:49 AM

Hey Philip thanks again,

I'll try when I get home.

I let you know.

gamer08

avatar image gamer08 Sep 24 '15 at 11:50 AM

Hey philip, I'm really busy right now, but i did not forget you.

I'll test ans let you know.

gamer08

avatar image gamer08 Sep 27 '15 at 03:59 PM

Hello Philip,

After few days of trying couple of things, your idea thanks anyway.

I'll post my code and the "result on screen" for future starting point if someone wanna try.

First the code,

 TArray<FVector> vec;
     FMatrix shearMatrix = FMatrix(FVector(1, 0, 0), 
                                    FVector(0, 1, 0.1), 
                                    FVector(0, 0, 1), 
                                    FVector(0, 0, 0));
 
 if(!IsValidLowLevel()) return;
 if(!_mesh) return;
 if(!_mesh->StaticMesh) return;
 if(!_mesh->StaticMesh->RenderData) return;
 
     if (_mesh->StaticMesh->RenderData->LODResources.Num() > 0)
     {
         FPositionVertexBuffer* vBuffer = &_mesh->StaticMesh->RenderData->LODResources[0].PositionVertexBuffer;
 
         if (vBuffer)
         {
             const int32 nbVertices = vBuffer->GetNumVertices();
             for (int32 index = 0; index < nbVertices; index++)
             {
                 const FVector position = GetActorLocation() + GetTransform().TransformVector(vBuffer->VertexPosition(index));
                 vec.Add(position);
             }
         }
 
         for (FVector& v : vec)
             v = MatrixMultiplication(shearMatrix, v);
         
         _mesh->StaticMesh->RenderData->LODResources[0].PositionVertexBuffer.Init(vec);
         _mesh->StaticMesh->RenderData->LODResources[0].PositionVertexBuffer.InitRHI();
 
         _mesh->MarkRenderStateDirty();
         _mesh->MarkRenderDynamicDataDirty();
         _mesh->MarkRenderTransformDirty();
     }

Before function call :

alt text

After function call :

alt text

Thanks again and if someones have an idea how to achieve mesh reconstruction at runtime, I'm open to everything.

In the mean time, I did a "pseudo shear and reflection" with the tranform even if not right.

gamer08

before.png (677.2 kB)
after.png (669.4 kB)
avatar image tianqi Apr 25 '16 at 11:01 AM

Thx for sharing the code. However, there is a problem which you can see from your screenshot: all the actor using the same static mesh is changed. Is there a way to only modify one actor's mesh without change others?

avatar image gamer08 Apr 25 '16 at 05:39 PM

Hi tianqi,

To be honest I don't know and as I said my stuff did not work at first.

If someone can give an answer it could be cool even if my topic is old.

gamer08

avatar image 476934847b Oct 19 '18 at 10:29 AM

Hello Philip,

Your answer is excellent! But I want to ask a question. If I change the TResourceArray, would the VertexBufferRHI change accordingly? And one more question, can it be done if I use the C float array not the TResourceArray to create the VertexBufferRHI?

(comments are locked)
10|2000 characters needed characters left

The solution would be to generate a new static mesh from the old one, modify the vertex data and then finally replace the selected actor's mesh.

more ▼

answered Oct 21 '16 at 10:15 AM

avatar image

gallant_pigeon
1

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question