Textures
How to add textures to GameItems in the Kakara Engine.
Last updated
How to add textures to GameItems in the Kakara Engine.
Last updated
A gray cube is cool and all, but it would be nice to add our own texture to it. The texture format of a mesh depends on the texture coordinates (UVs) provided. For a primitive cube (via CubeData
) the format is:
We will be adding the above texture to our cube. You can pick your own texture to add if you want.
Before we can do anything with the code, we need to add the file to our game. We do this by adding it to the resources folder. We want to create the resources folder to put our resources inside of. If using Maven or Gradle there will already be a resources folder, you want to create another folder named resources
inside that folder.
The Kakara Engine provides an easy way to obtain resources within the engine. You can access it through the GameHandler
. (Add this code before setting the material of the cube mesh).
From there you can use the getResource()
method to get a resource.
Now that we have our ResourceManager we need to create a texture. We can do that by constructing a new texture and passing it our resource and the scene instance.
Textures are a Graphical process and can only be created on the primary game thread.
Now that we created our texture, we want to add it to the Material of our mesh. We can do that by using the setTexture()
method.
Now if you run the game you should see a fully texture cube:
If you get a GenericLoadException
that states Error: Cannot load specified image.
than that means the file cannot be found or the game engine does not have access to it. There should be a more specific message that following the above statement. Ensure that you typed the file path correctly and have the file in the right location.
You can also get an IOException
if the file is not found or if the engine does not have permission to read it. Once again ensure that you typed the file path correctly and the file is in the right location.