Showing posts with label blender. Show all posts
Showing posts with label blender. Show all posts

Saturday, 15 February 2014

Regarding Meshes in Unity

Maybe it's just my OpenGL (1.1) background but here's a few roadblocks I've hit while doing procedural Mesh generation in Unity, along with a few tips not many people seem to know of. Some of this knowledge has been useful even outside my procedural experiments.

Quick Overview (stuff you really should know)

  • You will read this until your eyes bleed
  • Mesh.vertices contains your vertices. Mesh.triangles contains indexes to the vertices array. 
  • Normals (and UVs) are per Vertice. E.g.: Mesh.normals[i] has the normal for Mesh.vertices[i] (Blender uses normals for each face, which sometimes confuses people)
  • Different materials means different submeshes (or a new Mesh if that's what you're into).

A few pitfalls and tips

  • Mesh.uv1 != Mesh.uv (WHY AREN'T MY TEXTURE COORDINATES DOING ANYTHING?)
  • If your mesh has more than  one material, you have to use submeshes to define which area has a certain material. When copying meshes, I've found that I have to manually SetTriangles() as no other way of copying submeshes seems to work.
  • The backface is determined by the order of the vertices in each triangle, NOT its normal. If your mesh looks like it's inside out this is probably why.
  • Some of Unity's built-in shaders (namely the bump mapped ones) do not calculate their own tangents. Each Mesh has their own tangent array for this specific purpose. Unfortunately they can't be auto-calculated.
    • Blender files do not have these by default but fortunately Unity can calculate them automatically. Sadly, we don't have access to their algorithm for this.
    • Fortunately there's some code floating around that works. 
  • Don't generate a new Mesh() every time. Use the same mesh instance and .Clear() it. New meshes will get fed to the GPU but won't replace the old data there, causing memory leaks.
  • 99% of the time you can just call Mesh.RecalculateNormals() at the end instead of reinventing the wheel and calculating your own normals. As far as I know this calculates the normals based on the order of the vertices. 
  • Don't forget to Mesh.RecalculateBounds() if having decent bounds is important to you.
  • When you add a collider Unity will automatically fit it to the current Meshfilter's bounds.

Saturday, 25 August 2012

Level workflow in Brian Storm

Here's how we're currently doing our Blender-Unity3D level design workflow for the Brian Storm project. This is a result of several experiments we've done and our experience with level design. We're open to suggestions and if you're trying it out yourself and have some doubts on how it's done, feel free to ask me.

This is meant to be short and isn't a Blender or Unity3D tutorial of any sort. Though I might go more in depth later if there's a demand for it.

Also, keep in mind none of these images contain final art and are mostly placeholders while we programmers are still figuring things out.


Here we go

First we make a level segment: some geometry for the player to see and some collision meshes.

The actual mesh we see and it's collision mesh, shown in wireframe

We make sure to add "_collision" to the collision meshs' name. I'll explain this in more detail later on.

_collision just means the mesh is just a collision and should not be rendered.


We then put both types of geometry into the same group. Usually we use the file's name for consistency's sake.


We then create a level.blend file which appends (links) the groups in the other files as building blocks to create the level.

Tip: if you hide a mesh in it's original file, it will stay hidden after you import it. Very useful if you don't want to see your collision meshes while previewing the level here.

Two LongCorridors making up a longer corridor

Now we just import level.blend directly on our Unity scenes and all the meshes linked in it are correctly presented in our level. However, we have no collision yet - adding it manually would be a lot of work for levels of even smaller sizes.

A custom script, extended from AssetPostprocessor checks if we're importing a "level" file. We then go through every GameObject that we imported and check if it's name contains "_collision" - in which case we remove it's renderer and create a mesh collider for it using it's own mesh.
Another option we have so far is "_canhide", which adds a box collider to the mesh and configures it in such a way that it can be hidden if it obstructs the player's view.

We then use the Unity3D editor to add waypoints (paths our characters follow during gameplay), along with other extras we didn't import from the blender file and we're set.

VoilĂ , a complete placeholder level


Issues

Here's some concerns we have with this method:
  • Waypoints have to be setup after importing the level. We should find a way to automate this as it get's very annoying to update huge clusters of waypoints after even slight changes to level layout.
    • The same is valid for enemies, player start position, checkpoints, lighting and other non-geometry objects
  • Having the level split up in "blocks", each with their own collision meshes might make the game suffer performance wise.

Note: We disabled importing of animations for the level file, as unity has some issues with Default takes on Blender.