Python & Planets

     This week I enabled python in the project and I've begun the process of adding randomization to the planets.


Python

    I wanted to enable python scripts for the project because I knew that Unreal supports it, but I've never tried it. It's also been a while since I used Python and I wanted an excuse to use it again. The simplest use-case I could think so use it for was to generate the skybox texture. This would involve interfacing with the editor world, accessing actors from the world, and executing preexisting C++ code on said actor. All basic building blocks of things I may want to do in the future.
    I began by following the official Unreal docs, and I was able to get it up and running relatively quickly. I did hit a small snag in getting PyCharm to recognize the interpreter that comes with the engine. But of course I wasn't the first to have this issue and as always the forums were able to guide me through it. Once I had the interpreter and auto-complete working it was time to make that first script.
    Writing the script to generate the skybox was relatively simple. The UE Python API is quite straightforward and you can do things in much the same way as you would in C++. In fact, the unreal module exposes nearly everything that is exposed from C++ to blueprints. You're able to see everything in the gargantuan unreal.py file, which is hilarious to debug.
    So when I went to call MakeTexture() on the skybox actor it was already there waiting. This is the script that I came up with:


    It is very simple, but having this in my back pocket will allow me to use Python to script in the editor however I want in the future. Also, while I was doing this I realized how slow it is to generate the texture and it got me thinking that the work could be parallelized. It's not something I would be thrilled to go back to so soon, but I don't know, maybe I could put something quick together to make it a little smoother.

Randomization

    The main goal that I am now working towards is being able to generate an entire solar system fully procedurally. The player should be able to select a new game save and be able to pick between a preset system and a new random system. Maybe even with some parameters that can be tweaked for a custom starting system. This will be a multi-part undertaking as I need to implement ways to randomize multiple very different systems. 
    I am starting with the terrestrial planets as they are the most complex and will also have the most overlap with other systems. Finishing them first will make everything else much more simple. There are two steps to randomizing the planets: shape and color. The shape randomization was very simple. I was able to randomize the different float, integer, and vector parameters of the noise used for terrain generation within a range. I will need to revisit this later to tune the ranges and probably make some sort of algorithm to tie certain parameters together as right now it is capable of making some planet shapes that are totally incomprehensible. 
    Randomizing the color was a bit more involved. The color settings contains pointers to multiple UCurveLinearColor or color gradient assets. Of course I already had helpers to create the asset, but filling it with random colors would require a bit more thought. I knew I didn't want to just choose colors completely at random as that would probably result in some parts of the gradient going white as it transitioned between colors, and it would also be less cohesive.
    Looking at the way the engine picks a random color (FLinearColor::MakeRandomColor()) I noticed it was using a method that used a random hue. If I did the same thing and then clamped the delta between hues at sequential points then I could make a more visually pleasing gradient. So I came up with this:



    It uses this method to create colors for each biome that it makes as well as the "oceans". I'm quite happy with the results:


    This will certainly be an iterative process as I tune and refine the randomness to make a system that can consistently generate interesting planets. Next up I want to add similar randomization options to gas giants and stars.

Comments

Popular posts from this blog

Progress Update #19

Progress Update #9

Progress Update #17