Dynamic Mesh Spawning in UE4

In the eternal struggle to accumulate reputation on StackOverflow, I noticed a question that seemed easily solvable (by the standards of SO at least).

The link to the question:

http://stackoverflow.com/questions/29628032/how-do-you-dynamically-render-quads-in-ue4

The question asker was requesting for a concrete example of how to create a static mesh/ quad (as he puts it) at runtime.
It is also required that this quad be rendered with a texture on it.

Textures in Unreal 4 are implemented through the use of Materials (which allow for the composition of multiple textures into a single texture when applied to an object, and are a really handy tool)

When I first saw this question, it seemed quite cut and dry. Unreal Blueprints make it simple to achieve a lot of tasks without resorting to actual coding, and I made a suggestion towards the same.

However the asker said that he had yet to see a single actual example in code that allowed one to do this either in the documentation or on the Unreal 4 AnswerHub and after some Googling I found that this was indeed true.

This pretty much hooked me right in. I had to figure it out, and I felt bad for giving the asker the same vague high-level answer that he had found everywhere else. I resolved to have something in the way of an actual code example to help him on his way.

2 hours later, this was the final result:

A quad created an runtime with a texture on it.

M5VGt

I set it up so that an instance of my class ‘DynamicMeshSpawner’ would be spawned everytime I hit ‘P’ on the keyboard. When the instance of this class is created, it calls the constructor, which spawns the cube with the material applied. I did the class instance spawning stuff in Blueprints using the SpawnActor node.

Capture

ADynamicMeshSpawner::ADynamicMeshSpawner()
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;

    // Using a SphereComponent is not particularly necessary or relevant, but the cube refused to spawn without a root component to attach to, or so I surmise. Yay Unreal. =/
    USphereComponent* CubeComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
    RootComponent = CubeComponent;
    CubeComponent->InitSphereRadius(40.0f);
    CubeComponent->SetCollisionProfileName(TEXT("Pawn"));

    // Create and position a mesh component so we can see where our cube is
    UStaticMeshComponent* CubeVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
    CubeVisual->AttachTo(RootComponent);
    static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube'"));
    if (SphereVisualAsset.Succeeded())
    {
        CubeVisual->SetStaticMesh(SphereVisualAsset.Object);
        CubeVisual->SetRelativeLocation(FVector(-200.0f, 0.0f, 100.0f));
        CubeVisual->SetWorldScale3D(FVector(2.0f));
    }
    // Create a material to be applied on the StaticMeshComponent
    static ConstructorHelpers::FObjectFinder<UMaterial> Material(TEXT("Material'/Game/StarterContent/Materials/M_Tech_Hex_Tile_Pulse.M_Tech_Hex_Tile_Pulse'"));

    if (Material.Object != NULL)
    {
        TheMaterial = (UMaterial*)Material.Object;
    }

    CubeVisual->SetMaterial(0, TheMaterial);
}

This was the headerfile:

UCLASS()
class MYPROJECT_API ADynamicMeshSpawner : public AActor
{
    GENERATED_BODY()

public: 
    // Sets default values for this actor's properties
    ADynamicMeshSpawner();

    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    // Called every frame
    virtual void Tick( float DeltaSeconds ) override;
    // Pointer to the material that needs to be used
    UMaterial* TheMaterial;
};

Thats pretty much it!

 

Advertisements

Converting from Decimal To Binary

I was on StackOverflow the other day and saw a question posed about how one might convert from Decimal to Binary, when the initial information is stored in a string. It seemed like a fun little program to take a whack at, so I did. I’ve posted my answer as well as the code solution below:

Image obtained here:

http://pictures.4ever.eu/cartoons/binary-code-161219

The original question and my answer can be found here:

http://stackoverflow.com/questions/34381002/is-there-a-way-to-convert-a-number-represented-as-a-string-to-its-binary-equiv/34381419#34381419

————————————————————-

Okay let’s break down the process you require here. (only one of an infinite number of ways to do this)

1) Conversion of a number represented as a string type into an integer type.

2) Conversion of the intermediary integer type into a binary number which is held in another string type. (judging by the return type of your function, which could just as easily return an integer by the way and save the headache of representing the binary equivalent as a string)

For step 1: Use the standard library function stoi. It does what you might imagine, extracts the numerical data from the string and stores it in an integer.

http://www.cplusplus.com/reference/string/stoi/

std::string numberstr = "123";
int numberint = std::stoi(numberstr);
std::cout << numberint << "\n";

Now you have the number as an integer.

For step 2:

1) This process involves the conversion of a number from base 10 (decimal) to base 2 (binary).

2) Divide the number by 2.

3) Store the remainder and the quotient of this division operation for further use.

4) The remainder becomes part of the binary representation, while the quotient is used as the next dividend.

5) This process repeats until the dividend becomes 1, at which point it too is included in the binary representation.

6) Reverse the string, and voila! You now have the binary representation of a number.

7) If you want to handle negative numbers (which I imagine you might), simply perform a check before the conversion to see if the converted integer is negative, and set a flag to true if it is.

8) Check this flag before reversing, and add a negative sign to end of the string before reversing.

The final function looks like this:

std::string str_to_bin(const std::string& str)
{
std::string binarystr = ""; // Output string

int remainder;
int numberint = std::stoi(str);
bool flagnegative = false;
// If negative number, beginning of binary equivalent is 1
if (numberint < 0)
{
    numberint = abs(numberint);
    flagnegative = true;
}
// If number is 0, don't perform conversion simply return 0
if (numberint == 0)
{
    binarystr = "0";
    return binarystr;
}
std::cout << numberint << "\n";

while (numberint != 1)
{
    remainder = numberint % 2;
    numberint /= 2;
    std::ostringstream convert; // stream used for the conversion
    convert << remainder;      // insert the textual representation of 'remainder' in the characters in the stream
    binarystr += convert.str();
}
std::ostringstream final;
final << numberint;         // To insert the last (or rather first once reversed) binary number
binarystr += final.str();
if (flagnegative == true)
    binarystr += "-";
std::reverse(binarystr.begin(), binarystr.end());
return binarystr;
}

OSVR HDK Unboxing

So I finally got my hands on the OSVR headset from Razer. I’m not going to lie, it took its sweet time getting here (about 2 months more than the initially projected date) but the sheer degree of awesome that is possessed by a VR headset helped negate all my irritation from the delay.

In other words, I was squealing like a child when I picked it up and began the unboxing. I hope my excitement translates to you, gentle reader!

IMG_20151015_143123436_HDRIMG_20151015_143134137IMG_20151015_143355573

About now is when the squealing reached supersonic levels.IMG_20151015_144431475IMG_20151015_144443954IMG_20151015_143516892

The HDK itself seems to be divided into:

  1. The headset
  2. HDMI cables for the headset
  3. Some sort of audio cables that I haven’t yet figured out how to use
  4. The power supply
  5. The positional tracking kit (i.e. the Camera, a tripod stand and relevant cabling)
  6. A cleaning brush
  7. A hub for all the cables to connect into

IMG_20151015_143835863IMG_20151015_144123339

And there you have it!
The OSVR HDK unboxed.IMG_20151015_144048049

The link below is the official GitHub repository of the OSVR project, and contains all the instructions I needed to get everything setup and test the headset out in an actual VR demo.

Suffice to say, I was blown away.

https://github.com/OSVR

AND

https://github.com/OSVR/OSVR-General/wiki/HDK-Unboxing-and-Getting-Started

I hope this was of help/interest to anyone looking for more info on the OSVR project.

Feel free to contact me at Nightmask3@gmail.com for any help or insight I can provide on being a part of this ambitious new venture!

Thanks!

Happy hacking.