Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "QuickStart.h"
- #include "TestGeomPawn.h"
- // Sets default values
- ATestGeomPawn::ATestGeomPawn() : OurMesh(NULL), bGrowing(false)
- {
- PrimaryActorTick.bCanEverTick = true;
- AutoPossessPlayer = EAutoReceiveInput::Player0;
- // root is a scene component so this is placeable in the editor
- RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
- UCameraComponent* Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
- Camera->SetupAttachment(RootComponent);
- Camera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
- Camera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
- //RegenGeometry();
- }
- // Called when the game starts or when spawned
- void ATestGeomPawn::BeginPlay()
- {
- Super::BeginPlay();
- //RegenGeometry();
- }
- // Called to bind functionality to input
- void ATestGeomPawn::SetupPlayerInputComponent(class UInputComponent* InComp)
- {
- Super::SetupPlayerInputComponent(InputComponent);
- InComp->BindAction("Regen", IE_Pressed, this, &ATestGeomPawn::RegenGeometry);
- InComp->BindAction("Grow", IE_Pressed, this, &ATestGeomPawn::StartGrowing);
- InComp->BindAction("Grow", IE_Released, this, &ATestGeomPawn::StopGrowing);
- }
- void ATestGeomPawn::RegenGeometry() {
- if (OurMesh) {
- OurMesh->DestroyComponent();
- OurMesh = NULL;
- }
- static const float rverts[8][3] = {
- { 1, 1, 1 },
- { 1, 1, -1 },
- { 1, -1, 1 },
- { 1, -1, -1 },
- { -1, 1, -1 },
- { -1, 1, 1 },
- { -1, -1, -1 },
- { -1, -1, 1 }
- };
- static const int rtris[12][3] = {
- { 1, 3, 4 },
- { 4, 2, 1 },
- { 5, 7, 8 },
- { 8, 6, 5 },
- { 5, 6, 1 },
- { 1, 2, 5 },
- { 8, 7, 4 },
- { 4, 3, 8 },
- { 6, 8, 3 },
- { 3, 1, 6 },
- { 2, 4, 7 },
- { 7, 5, 2 }
- };
- float scales[8];
- for (int n = 0; n < 8; ++n)
- scales[n] = FMath::RandRange(30.f, 60.f);
- TArray<FCustomMeshTriangle> tris;
- for (int n = 0; n < 12; ++n) {
- FCustomMeshTriangle t;
- int a = rtris[n][2] - 1;
- int b = rtris[n][1] - 1;
- int c = rtris[n][0] - 1;
- t.Vertex0 = FVector(rverts[a][0], rverts[a][1], rverts[a][2]) * scales[a];
- t.Vertex1 = FVector(rverts[b][0], rverts[b][1], rverts[b][2]) * scales[b];
- t.Vertex2 = FVector(rverts[c][0], rverts[c][1], rverts[c][2]) * scales[c];
- tris.Add(t);
- }
- // For generating different names:
- //static int counter = 1;
- //FName name = *(FString("RandomMesh") + FString::FormatAsNumber(counter));
- //++counter;
- //OurMesh = NewNamedObject<UCustomMeshComponent>(this, name);
- OurMesh = NewObject<UCustomMeshComponent>(this);
- OurMesh->SetCustomMeshTriangles(tris);
- OurMesh->SetupAttachment(RootComponent);
- }
- // Called every frame
- void ATestGeomPawn::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- // Handle growing and shrinking based on our "Grow" action
- if (OurMesh) {
- float CurrentScale = OurMesh->GetComponentScale().X;
- if (bGrowing)
- {
- // Grow to double size over the course of one second
- CurrentScale += DeltaTime;
- }
- else
- {
- // Shrink half as fast as we grow
- CurrentScale -= (DeltaTime * 0.5f);
- }
- // Make sure we never drop below our starting size, or increase past double size.
- CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
- OurMesh->SetWorldScale3D(FVector(CurrentScale));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement