Summer Work (Day 10)
In Editor Live Updating pt.2
As I stated in the last update, I am working on the planet generation settings. These are Shape Settings, which right now is just planet radius, and Color Settings, which is just a solid color. I am doing this with the use of Unreal's data assets. I did this because it is more or less the equivalent to Unity's scriptable objects, which is what's used in the tutorial.
At the time of writing, the system is capable of updating the color and radius of the planet when the data asset is changed. This is done with the help of the PostEditChangeProperty function I found last time, expanded for more property changes. However, right now the function only detects if the entire asset is changed, so in the future I would like for the data asset to be editable within the plant's blueprint and update based on that.
The first problem I ran into through this process was faulty engine generated files. Essentially, when I tried to create the ShapeSettings script, "intelli"-sense would red underline everything complaining that it was all wrong. I stumbled across a forum of someone with the same problem and someone suggested deleting the Intermediate folder and regenerate VS project files. I did exactly that and it compiled perfectly.
The next problem, and the one that took up the rest of my time, was trying to color the planet. After I finished the shape settings (radius) I went to sort out the color. I assumed that since the color parameter of the CreateMeshSection function was an array of colors, all I had to do was add the solid color to the array and pass that. In reality there were many more steps.
First thing I thought of was the UV's. I knew from past projects that "UVing" a model is important for texturing. In the procedural mesh though, I was passing it no UVs. This would mean that I would have to do quite a bit of math to make the UVs myself. I did some quick googling to make sure this was the reason the colors weren't working before starting on this.
While on this hunt for answers I found a blog that I had referenced when starting out with procedural meshes. In the blog, the author's mesh is colored, so I dug through their code to see what they did differently. The first thing I noticed was that they also had no UVs, so I threw that theory out. THe next thing I noticed was that their color array had more than one color in it. In fact, it had one color for each vertex on the mesh. This made much more sense when I saw the parameter of the CreateMeshSection function was called "VertexColors".
I also noticed that the blog had a special material that had Vertex Color as an input. They said that this was what would read the VertexColors array and actually color the mesh.
With this new information I went on to create a loop, adding the color to the array for every vertex. This still didn't work though. After some looking around for the issue I found that the output log was filled with errors. It was saying that it was trying to update the mesh with a different number of vertices. I thought this was very odd as I had pulled all the data about vertices and such directly from the TerrainFace script. After more fumbling around I decided it would just be so much easier if I did the updating on the TerrainFace as opposed to the Planet.
That one small refactor later and it worked! The color and radius would change in the editor based on the data asset that it was given. But I wasn't done yet. You see, to get the color change to happen on the TerrainFace I created an overload of the ConstructMesh function that took in color settings. That meant tt would do all the same stuff as the regular ConstructMesh function, including things like doing the math for all the vertices. This meant that it was essentially doing everything twice as I had it construct the mesh first, then later it was assigned a color and regenerated. I thought I was being all clever with the overloaded function, but I was unhappy with its inefficiency. What I ended up with was one function that would create the mesh section and another that would color it.
All of this functions as of commit: 303a70d
Comments
Post a Comment