Code Monkey home page Code Monkey logo

Comments (4)

WiggleWizard avatar WiggleWizard commented on August 16, 2024

Well if you built the mesh section manually you should definitely already know the tris, vertices, normals, UVs and vertex colors. If you created the section from one of the procedural methods of RMC then the outputs of the relevant parameters will tell you exactly what you want:

TArray<FVector>             Vertices;
TArray<int32>               Triangles;
TArray<FVector>             Normals;
TArray<FVector2D>           TextureCoords;
TArray<FRuntimeMeshTangent> Tangents;


URuntimeMeshLibrary::CreateBoxMesh(FVector(Size, Size, Size), Vertices, Triangles, Normals, TextureCoordinates, Tangents);

Where Vertices will be an array of vertex positions, Triangles will be an array of each vertex index in winding order, Normals will be an array of each triangle's normal in order and so on. With that you can determine that with this code:

int32 TriangleIndex;
TArray<FVector> TriangleVertices;

Triangle.Add(Vertices[Triangles[TriangleIndex * 3 + 0]]);
Triangle.Add(Vertices[Triangles[TriangleIndex * 3 + 1]]);
Triangle.Add(Vertices[Triangles[TriangleIndex * 3 + 2]]);

TriangleVertices will contain all 3 vertex positions of the triangle at TriangleIndex in winding order.

Also: knowing just the vertices and the indices you can get tris, normals, etc. Here's an accurate example of how to calculate the normals of a triangle index:

FVector CalculateTriangleNormal(int32 TriangleIndex)
{
	int32 VertOffset = TriangleIndex * 3;

	FVector Cross = FVector::CrossProduct(
		Vertices[Triangles[VertOffset + 2]] - Vertices[Triangles[VertOffset + 0]],  // Edge 1
		Vertices[Triangles[VertOffset + 1]] - Vertices[Triangles[VertOffset + 0]]); // Edge 2
	Cross.Normalize();

	return Cross;
}

I think you need to read this Wiki entry: Mesh Basics. I also recommend doing more research on 3D geometry to better understand how just knowing the vertex positions and windings can get you triangles, the normals of those triangles, tangents, etc.

PS: RMC is fucking awesome @Koderz, thanks for your hard work!

from realtimemeshcomponent.

mutp avatar mutp commented on August 16, 2024

Fair enough, thanks for the info @Zinglish

How would I get the Vertex Buffer info for a mesh? Although I created the mesh I dont want to hold all these values as I assume the RuntimeMeshComponent would. I just need a way to access the Vertex buffer whenever I need. Is it exposed?

from realtimemeshcomponent.

WiggleWizard avatar WiggleWizard commented on August 16, 2024

Again, you should do some more research and reading before opening an issue to talk about information that's readily available. There's examples on how to use RMC, in the Animated Terrain Demo you can see how to grab and use the vertex buffer. Regardless I will, for the sake of anyone who's stuck on this, provide a minimal approach below.

If you create a mesh doing something like

TArray<FRuntimeMeshVertexNoPosition> VertexData;

FBox BoundingBox = FBox(FVector(Size, Size, Size), FVector(-Size, -Size, -Size));

URuntimeMeshLibrary::CreateBoxMesh(FVector(Size, Size, Size), Vertices, Triangles, Normals, TextureCoordinates, Tangents);

for(int32 i = 0; i < Normals.Num(); i++)
		VertexData.Add(FRuntimeMeshVertexNoPosition(Normals[i], Tangents[i], TextureCoordinates[i]));

RMC->CreateMeshSectionDualBuffer(0, Vertices, VertexData, Triangles, BoundingBox, true, EUpdateFrequency::Frequent);

You can later get the vertex buffer with the following method

TArray<FVector>& Vertices = *RMC->BeginMeshSectionPositionUpdate(0);

You would then modify and vertices in the Vertices array then call RMC->EndMeshSectionPositionUpdate(0); which will force RMC to update the vertices in the UE4 renderer.

Do note that adding or removing any verts from the array returned from BeginMeshSectionPositionUpdate() will likely result in a crash or unexpected behaviour. If you wish to add or remove verts from your mesh, I believe you will need to rebuild it entirely, discarding all previous information about the mesh, I haven't experimented with this myself all that much tbh.

from realtimemeshcomponent.

mutp avatar mutp commented on August 16, 2024

I have no intention of updating an exisiting mesh so I really don't think using BeginMeshSectionPositionUpdate(0) is correct. It doesn't seem like it was meant to just get a reference.

Anyway, this worked for me -

const IRuntimeMeshVerticesBuilder* runtimeVertices;
const FRuntimeMeshIndicesBuilder* runtimeIndices;
Mesh->GetSectionMesh(0, runtimeVertices, runtimeIndices);

runtimeVertices now contains all vertex related data.

Thanks for your help @Zinglish.

from realtimemeshcomponent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.