Advertisement
Guest User

FullActor

a guest
Dec 5th, 2019
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.03 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "RuntimeChunkColumnActor.h"
  5. #include "Providers/RuntimeMeshProviderStatic.h"
  6. #include "Materials/Material.h"
  7.  
  8. // Sets default values
  9. ARuntimeChunkColumnActor::ARuntimeChunkColumnActor()
  10. {
  11.     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  12.     PrimaryActorTick.bCanEverTick = false;
  13.  
  14. }
  15.  
  16. // Called when the game starts or when spawned
  17. void ARuntimeChunkColumnActor::BeginPlay()
  18. {
  19.     Super::BeginPlay();
  20.  
  21. }
  22.  
  23. // Called every frame
  24. void ARuntimeChunkColumnActor::Tick(float DeltaTime)
  25. {
  26.     Super::Tick(DeltaTime);
  27.  
  28. }
  29.  
  30. void ARuntimeChunkColumnActor::RuntimeCreateMeshSection(UMaterialInterface* Material, int32 SectionIndex, const TArray<FVector>& Vertices, const TArray<int32>& Triangles, const TArray<FLinearColor>& VertexColors) {
  31.     URuntimeMeshProviderStatic* StaticProvider = NewObject<URuntimeMeshProviderStatic>(this);
  32.     GetRuntimeMeshComponent()->GetOrCreateRuntimeMesh()->Initialize(StaticProvider);
  33.  
  34.     StaticProvider->SetupMaterialSlot(SectionIndex, FName(*(FString("Material").Append(FString::FromInt(SectionIndex)))), Material);
  35.  
  36.     // Setup section
  37.     FRuntimeMeshSectionProperties Properties;
  38.     Properties.bCastsShadow = true;
  39.     Properties.bIsVisible = true;
  40.     Properties.MaterialSlot = SectionIndex;
  41.     StaticProvider->CreateSection(0, SectionIndex, Properties);
  42.  
  43.     FRuntimeMeshRenderableMeshData MeshData(false, false, 1, false);
  44.  
  45.     for (FVector f : Vertices) {
  46.         MeshData.Positions.Add(f);
  47.         MeshData.Tangents.Add(FVector(), FVector());
  48.     }
  49.  
  50.     for (FLinearColor f : VertexColors) {
  51.         MeshData.Colors.Add(f.ToFColor(false));
  52.     }
  53.    
  54.     for (int32 f : Triangles) {
  55.         MeshData.Triangles.Add(f);
  56.     }
  57.  
  58.     // Add the mesh
  59.     StaticProvider->UpdateSection(0, SectionIndex, MeshData);
  60. }
  61.  
  62. void ARuntimeChunkColumnActor::GenerateMeshes_Implementation() {}
  63. /*
  64. void ARuntimeChunkColumnActor::GenerateMeshes_Implementation()
  65. {
  66.     FVector BoxRadius(100, 100, 100);
  67.  
  68.  
  69.     URuntimeMeshProviderStatic* StaticProvider = NewObject<URuntimeMeshProviderStatic>(this);
  70.     GetRuntimeMeshComponent()->GetOrCreateRuntimeMesh()->Initialize(StaticProvider);
  71.  
  72.     StaticProvider->SetupMaterialSlot(0, FName("Cube Base"), UMaterial::GetDefaultMaterial(MD_Surface));
  73.  
  74.     // Setup section
  75.     FRuntimeMeshSectionProperties Properties;
  76.     Properties.bCastsShadow = true;
  77.     Properties.bIsVisible = true;
  78.     Properties.MaterialSlot = 0;
  79.     StaticProvider->CreateSection(0, 0, Properties);
  80.  
  81.  
  82.  
  83.     // Generate verts
  84.     FVector BoxVerts[8];
  85.     BoxVerts[0] = FVector(-BoxRadius.X, BoxRadius.Y, BoxRadius.Z);
  86.     BoxVerts[1] = FVector(BoxRadius.X, BoxRadius.Y, BoxRadius.Z);
  87.     BoxVerts[2] = FVector(BoxRadius.X, -BoxRadius.Y, BoxRadius.Z);
  88.     BoxVerts[3] = FVector(-BoxRadius.X, -BoxRadius.Y, BoxRadius.Z);
  89.  
  90.     BoxVerts[4] = FVector(-BoxRadius.X, BoxRadius.Y, -BoxRadius.Z);
  91.     BoxVerts[5] = FVector(BoxRadius.X, BoxRadius.Y, -BoxRadius.Z);
  92.     BoxVerts[6] = FVector(BoxRadius.X, -BoxRadius.Y, -BoxRadius.Z);
  93.     BoxVerts[7] = FVector(-BoxRadius.X, -BoxRadius.Y, -BoxRadius.Z);
  94.  
  95.     FVector TangentX, TangentY, TangentZ;
  96.  
  97.     // Setup the mesh data
  98.     FRuntimeMeshRenderableMeshData MeshData(false, false, 1, false);
  99.  
  100.     // helper function to add vertex to the mesh data
  101.     auto AddVertex = [&](const FVector& InPosition, const FVector& InTangentX, const FVector& InTangentZ, const FVector2D& InTexCoord)
  102.     {
  103.         MeshData.Positions.Add(InPosition);
  104.         MeshData.Tangents.Add(InTangentZ, InTangentX);
  105.         MeshData.Colors.Add(FColor::White);
  106.         MeshData.TexCoords.Add(InTexCoord);
  107.     };
  108.  
  109.     // Now add all the sides of the box
  110.  
  111.     // Pos Z
  112.     TangentZ = FVector(0.0f, 0.0f, 1.0f);
  113.     TangentX = FVector(0.0f, -1.0f, 0.0f);
  114.     AddVertex(BoxVerts[0], TangentX, TangentZ, FVector2D(0.0f, 0.0f));
  115.     AddVertex(BoxVerts[1], TangentX, TangentZ, FVector2D(0.0f, 1.0f));
  116.     AddVertex(BoxVerts[2], TangentX, TangentZ, FVector2D(1.0f, 1.0f));
  117.     AddVertex(BoxVerts[3], TangentX, TangentZ, FVector2D(1.0f, 0.0f));
  118.     MeshData.Triangles.AddTriangle(0, 1, 3);
  119.     MeshData.Triangles.AddTriangle(1, 2, 3);
  120.  
  121.     // Neg X
  122.     TangentZ = FVector(-1.0f, 0.0f, 0.0f);
  123.     TangentX = FVector(0.0f, -1.0f, 0.0f);
  124.     AddVertex(BoxVerts[4], TangentX, TangentZ, FVector2D(0.0f, 0.0f));
  125.     AddVertex(BoxVerts[0], TangentX, TangentZ, FVector2D(0.0f, 1.0f));
  126.     AddVertex(BoxVerts[3], TangentX, TangentZ, FVector2D(1.0f, 1.0f));
  127.     AddVertex(BoxVerts[7], TangentX, TangentZ, FVector2D(1.0f, 0.0f));
  128.     MeshData.Triangles.AddTriangle(4, 5, 7);
  129.     MeshData.Triangles.AddTriangle(5, 6, 7);
  130.  
  131.     // Pos Y
  132.     TangentZ = FVector(0.0f, 1.0f, 0.0f);
  133.     TangentX = FVector(-1.0f, 0.0f, 0.0f);
  134.     AddVertex(BoxVerts[5], TangentX, TangentZ, FVector2D(0.0f, 0.0f));
  135.     AddVertex(BoxVerts[1], TangentX, TangentZ, FVector2D(0.0f, 1.0f));
  136.     AddVertex(BoxVerts[0], TangentX, TangentZ, FVector2D(1.0f, 1.0f));
  137.     AddVertex(BoxVerts[4], TangentX, TangentZ, FVector2D(1.0f, 0.0f));
  138.     MeshData.Triangles.AddTriangle(8, 9, 11);
  139.     MeshData.Triangles.AddTriangle(9, 10, 11);
  140.  
  141.     // Pos X
  142.     TangentZ = FVector(1.0f, 0.0f, 0.0f);
  143.     TangentX = FVector(0.0f, 1.0f, 0.0f);
  144.     AddVertex(BoxVerts[6], TangentX, TangentZ, FVector2D(0.0f, 0.0f));
  145.     AddVertex(BoxVerts[2], TangentX, TangentZ, FVector2D(0.0f, 1.0f));
  146.     AddVertex(BoxVerts[1], TangentX, TangentZ, FVector2D(1.0f, 1.0f));
  147.     AddVertex(BoxVerts[5], TangentX, TangentZ, FVector2D(1.0f, 0.0f));
  148.     MeshData.Triangles.AddTriangle(12, 13, 15);
  149.     MeshData.Triangles.AddTriangle(13, 14, 15);
  150.  
  151.     // Neg Y
  152.     TangentZ = FVector(0.0f, -1.0f, 0.0f);
  153.     TangentX = FVector(1.0f, 0.0f, 0.0f);
  154.     AddVertex(BoxVerts[7], TangentX, TangentZ, FVector2D(0.0f, 0.0f));
  155.     AddVertex(BoxVerts[3], TangentX, TangentZ, FVector2D(0.0f, 1.0f));
  156.     AddVertex(BoxVerts[2], TangentX, TangentZ, FVector2D(1.0f, 1.0f));
  157.     AddVertex(BoxVerts[6], TangentX, TangentZ, FVector2D(1.0f, 0.0f));
  158.     MeshData.Triangles.AddTriangle(16, 17, 19);
  159.     MeshData.Triangles.AddTriangle(17, 18, 19);
  160.  
  161.     // Neg Z
  162.     TangentZ = FVector(0.0f, 0.0f, -1.0f);
  163.     TangentX = FVector(0.0f, 1.0f, 0.0f);
  164.     AddVertex(BoxVerts[7], TangentX, TangentZ, FVector2D(0.0f, 0.0f));
  165.     AddVertex(BoxVerts[6], TangentX, TangentZ, FVector2D(0.0f, 1.0f));
  166.     AddVertex(BoxVerts[5], TangentX, TangentZ, FVector2D(1.0f, 1.0f));
  167.     AddVertex(BoxVerts[4], TangentX, TangentZ, FVector2D(1.0f, 0.0f));
  168.     MeshData.Triangles.AddTriangle(20, 21, 23);
  169.     MeshData.Triangles.AddTriangle(21, 22, 23);
  170.  
  171.  
  172.  
  173.     // Add the mesh
  174.     StaticProvider->UpdateSection(0, 0, MeshData);
  175.  
  176.  
  177.  
  178.     // Now setup collision
  179.     FRuntimeMeshCollisionSettings Settings;
  180.     Settings.bUseAsyncCooking = true;
  181.     Settings.bUseComplexAsSimple = false;
  182.  
  183.     // Add a single box element for the simple collision
  184.     Settings.Boxes.Emplace(BoxRadius.X * 2, BoxRadius.Y * 2, BoxRadius.Z * 2);
  185.  
  186.  
  187.     // Setup Collision
  188.     FRuntimeMeshCollisionData CollisionData;
  189.     FRuntimeMeshCollisionVertexStream& CollisionVertices = CollisionData.Vertices;
  190.     FRuntimeMeshCollisionTriangleStream& CollisionTriangles = CollisionData.Triangles;
  191.  
  192.     // Generate verts
  193.     CollisionVertices.Add(FVector(-BoxRadius.X, BoxRadius.Y, BoxRadius.Z));
  194.     CollisionVertices.Add(FVector(BoxRadius.X, BoxRadius.Y, BoxRadius.Z));
  195.     CollisionVertices.Add(FVector(BoxRadius.X, -BoxRadius.Y, BoxRadius.Z));
  196.     CollisionVertices.Add(FVector(-BoxRadius.X, -BoxRadius.Y, BoxRadius.Z));
  197.  
  198.     CollisionVertices.Add(FVector(-BoxRadius.X, BoxRadius.Y, -BoxRadius.Z));
  199.     CollisionVertices.Add(FVector(BoxRadius.X, BoxRadius.Y, -BoxRadius.Z));
  200.     CollisionVertices.Add(FVector(BoxRadius.X, -BoxRadius.Y, -BoxRadius.Z));
  201.     CollisionVertices.Add(FVector(-BoxRadius.X, -BoxRadius.Y, -BoxRadius.Z));
  202.  
  203.     // Pos Z
  204.     CollisionTriangles.Add(0, 1, 3);
  205.     CollisionTriangles.Add(1, 2, 3);
  206.     // Neg X
  207.     CollisionTriangles.Add(4, 0, 7);
  208.     CollisionTriangles.Add(0, 3, 7);
  209.     // Pos Y
  210.     CollisionTriangles.Add(5, 1, 4);
  211.     CollisionTriangles.Add(1, 0, 4);
  212.     // Pos X
  213.     CollisionTriangles.Add(6, 2, 5);
  214.     CollisionTriangles.Add(2, 1, 5);
  215.     // Neg Y
  216.     CollisionTriangles.Add(7, 3, 6);
  217.     CollisionTriangles.Add(3, 2, 6);
  218.     // Neg Z
  219.     CollisionTriangles.Add(7, 6, 4);
  220.     CollisionTriangles.Add(6, 5, 4);
  221.  
  222.  
  223.     // add the collision mesh for complex collision
  224.     StaticProvider->SetCollisionMesh(CollisionData);
  225.  
  226. }
  227. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement