• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By davemacdo
      I'm a formally trained composer (doctorate from Michigan State) who writes what most people would call avant-garde concert music. I love weird abstract projects, and I would like to work with somebody making a weird, abstract, artsy game. 
      You can find more about me and my music on my site. I have worked with acoustic and electronic sounds, including some algorithmic and interactive computer music. 
      In particular, I would like to work on a project that lets me use Fmod to prepare an adaptive score for a game built on Unity or Unreal. I've been a music professor and would like to get experience working in this medium so that I can be a better mentor for my students. Send me a DM or email <davidjohnmacdonald@gmail.com> if you would like to discuss working on a project together. 
    • By Tuner_z

      Name: One Level: Stickman Jailbreak
      Price: Free
      Developer: RTU Studio
      Platform: Android
      Language: C# (Unity3D)
      Google Play: https://play.google.com/store/apps/details?id=com.RTU.OneLevel
       
      Hello!
      I want to show you my game! "One level: Stickman Jailbreak" is a puzzle game with unusual gameplay where you must help the character to escape from prison. You just need to take the key and get out alive. The game has only one level, and there are many ways to complete it. Not everything is as simple as it might seem at first glance, so there are clues in the game.
       
      Short description:
      Nobody escapes from here!
       
      Description:
      Tommy got into trouble again! Our hero is behind bars. But he's not going to stay in jail for a long time and he decides to escape. Tommy steals a key and gets out of the jail cell. But our friend doesn't go free: Tommy suddenly finds himself in the same room from which he just escaped! The conditions for escaping change every time. In order to go free Tommy will have to solve logical puzzles and you can help him in this!
      At first it will be easy, but the tension will increase, and the tasks will become more complicated with each level. You should use your brain for all 100%, but if your skill is not enough, you can use a hint or ask for help from friends!
      You can solve the puzzles alone or with your friends and spend time well!
       
      Features: 
      Features:
      - 48 unique levels;
      - the game is translated into 10 languages: English, French, German, Spanish, Italian, Portuguese, Russian, Japanese, Chinese, Korean;
      - the function of "help from friend";
      - hints;
      - instructions.
       
      Trailer:
       
      Screenshots:





       
    • By Brandon Marx
      Hello forum,
      I have some decent amount of experience in Unity making games for Software Engineering projects in college, these were very specific projects however and I still am fairly new to building games. I wanted to make a game that uses the shadows of objects for collision detecting (i.e. shooting a gun at a characters shadow causes that character damage. What is the best engine to do this in (game will be 3D), and does anyone have any advice on how to approach this concept? I consider myself fairly experienced in programming, but game dev is just an entirely different beast.
    • By juicyz
      Hey all,
      I've been slowly working on my game called AotW for a while now.  I have come to the conclusions that it would be nice to cooperate with 1 or 2 others to help finish it.  Ive been trying to keep my GDD up to date with my ideas and development so that would give a better overview of the game when the time comes.  Currently I have a basic skeleton of the RPG elements needed but everything can still be discussed and talked about and we can transform my idea to something the group likes.
      The premise of the game is a Diablo-like procedurally generated map with RPG elements that include sockets, inventory, classes, abilities, crafting, loot, items, sockets, and enchanting.  This will be done in a 2D iso view as I can't do 3D art and I enjoy 2D games a lot.
       
      I don't plan on releasing this as this is more of a hobby project for me and I have a full-time job.  Though I'd like to start putting more hours into development and having others definitely will be motivation.  I also want to be able to say I have finally "finished" a game idea to some degree.  If the time comes and we want to release it, then we can go ahead and do so but that's not my purpose or plan. 
       
      Discord:
      Juicyz#3683
       
       
      Thanks,
      Juicyz
    • By Arnold // Golden Donkey Productions
      Hi, I've been working on this issue for a while and haven't yet found an answer.
      Does anyone know the best way to convert unity's LAT & LONG into a vector 3 position that I could use in a virtual world (if it's even possible). 
      Thankyou in advance
  • Advertisement
  • Advertisement
Sign in to follow this  

Unity Optimizing voxel mesh data

This topic is 705 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Currently, I handle the mesh data in my game with the following code:

 

Block class:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


[System.Serializable()]

public class Block
{
    public BlockType type;

    public Block(BlockType type1)
    {
        type = type1;
    }

    public enum Direction { north, east, south, west, up, down };

    [System.SerializableAttribute()]
    public struct Tile { public int x; public int y;}

    const float tileSize = 0.0625f;


    public virtual MeshData Blockdata
     (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)
    {

        meshData.useRenderDataForCol = true;

        if (type != BlockTypes.typeEmpty)
        {

            if (!planetchunk.GetBlock(x, y + 1, z).IsSolid)
            {
                meshData = FaceDataUp(planetchunk, x, y, z, meshData);
            }

            if (!planetchunk.GetBlock(x, y - 1, z).IsSolid)
            {
                meshData = FaceDataDown(planetchunk, x, y, z, meshData);
            }

            if (!planetchunk.GetBlock(x, y, z + 1).IsSolid)
            {
                meshData = FaceDataNorth(planetchunk, x, y, z, meshData);
            }

            if (!planetchunk.GetBlock(x, y, z - 1).IsSolid)
            {
                meshData = FaceDataSouth(planetchunk, x, y, z, meshData);
            }

            if (!planetchunk.GetBlock(x + 1, y, z).IsSolid)
            {
                meshData = FaceDataEast(planetchunk, x, y, z, meshData);
            }

            if (!planetchunk.GetBlock(x - 1, y, z).IsSolid)
            {
                meshData = FaceDataWest(planetchunk, x, y, z, meshData);
            }



            if (type.light == true)
            {

                if (planetchunk.GetBlock(x - 1, y, z).IsSolid == false || planetchunk.GetBlock(x + 1, y, z).IsSolid == false || planetchunk.GetBlock(x, y, z - 1).IsSolid == false ||
                    planetchunk.GetBlock(x, y, z + 1).IsSolid == false || planetchunk.GetBlock(x, y - 1, z).IsSolid == false || planetchunk.GetBlock(x, y + 1, z).IsSolid == false)
                {
                    meshData.AddLight(x, y, z, type.LR, type.LG, type.LB, type.LA, type.LightRange, type.LightIntensity);
                }

            }

            return meshData;

        }

        return meshData;
    }

    protected virtual MeshData FaceDataUp
        (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)
    {
        meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f));
        meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f));
        meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f));
        meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f));

        meshData.AddQuadTriangles();
        meshData.uv.AddRange(FaceUVs(Direction.up));
        return meshData;
    }

    protected virtual MeshData FaceDataDown
        (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)
    {
        meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z - 0.5f));
        meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z - 0.5f));
        meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z + 0.5f));
        meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z + 0.5f));

        meshData.AddQuadTriangles();
        meshData.uv.AddRange(FaceUVs(Direction.down));
        return meshData;
    }

    protected virtual MeshData FaceDataNorth
        (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)
    {
        meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z + 0.5f));
        meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f));
        meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f));
        meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z + 0.5f));

        meshData.AddQuadTriangles();
        meshData.uv.AddRange(FaceUVs(Direction.north));
        return meshData;
    }

    protected virtual MeshData FaceDataEast
        (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)
    {
        meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z - 0.5f));
        meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f));
        meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f));
        meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z + 0.5f));

        meshData.AddQuadTriangles();
        meshData.uv.AddRange(FaceUVs(Direction.east));
        return meshData;
    }

    protected virtual MeshData FaceDataSouth
        (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)
    {
        meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z - 0.5f));
        meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f));
        meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f));
        meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z - 0.5f));

        meshData.AddQuadTriangles();
        meshData.uv.AddRange(FaceUVs(Direction.south));
        return meshData;
    }

    protected virtual MeshData FaceDataWest
        (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)
    {
        meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z + 0.5f));
        meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f));
        meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f));
        meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z - 0.5f));
            

        meshData.AddQuadTriangles();
        meshData.uv.AddRange(FaceUVs(Direction.west));
        return meshData;
    }


    public virtual Vector2[] FaceUVs(Direction direction)
    {
        Vector2[] UVs = new Vector2[4];
        Tile tilePos = type.tile;

        UVs[0] = new Vector2(tileSize * tilePos.x + tileSize,
            tileSize * tilePos.y);
        UVs[1] = new Vector2(tileSize * tilePos.x + tileSize,
            tileSize * tilePos.y + tileSize);
        UVs[2] = new Vector2(tileSize * tilePos.x,
            tileSize * tilePos.y + tileSize);
        UVs[3] = new Vector2(tileSize * tilePos.x,
            tileSize * tilePos.y);

        return UVs;
    }

?}

MeshData:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MeshData
{
    public List<Vector3> vertices = new List<Vector3>();
    public List<int> triangles = new List<int>();
    public List<Vector2> uv = new List<Vector2>();
    public List<Vector3> colVertices = new List<Vector3>();
    public List<int> colTriangles = new List<int>();
    public Color32[,,] lightcolors = new Color32[localVars.chunklength, localVars.chunklength, localVars.chunklength];
    public float[,,] lightintensity = new float[localVars.chunklength, localVars.chunklength, localVars.chunklength];
    public float[,,] lightrange = new float[localVars.chunklength, localVars.chunklength, localVars.chunklength];
    public List<Vector3> lightpositions = new List<Vector3>(localVars.chunklength * localVars.chunklength * localVars.chunklength);

    public bool useRenderDataForCol;

    public void AddQuadTriangles()
    {
        triangles.Add(vertices.Count - 4);
        triangles.Add(vertices.Count - 3);
        triangles.Add(vertices.Count - 2);

        triangles.Add(vertices.Count - 4);
        triangles.Add(vertices.Count - 2);
        triangles.Add(vertices.Count - 1);

        if (useRenderDataForCol)
        {
            colTriangles.Add(colVertices.Count - 4);
            colTriangles.Add(colVertices.Count - 3);
            colTriangles.Add(colVertices.Count - 2);
            colTriangles.Add(colVertices.Count - 4);
            colTriangles.Add(colVertices.Count - 2);
            colTriangles.Add(colVertices.Count - 1);
        }
    }

    public void AddVertex(Vector3 vertex)
    {
        vertices.Add(vertex);

        if (useRenderDataForCol)
        {
            colVertices.Add(vertex);
        }

    }

    public void AddTriangle(int tri)
    {
        triangles.Add(tri);

        if (useRenderDataForCol)
        {
            colTriangles.Add(tri - (vertices.Count - colVertices.Count));
        }
    }

    public void AddLight (int x, int y, int z, byte R, byte G, byte B, byte A, float range, float intensity)
    {
        lightcolors[x, y, z] = new Color32(R, G, B, A);
        lightintensity[x, y, z] = intensity;
        lightrange[x, y, z] = range;
        lightpositions.Add(new Vector3(x, y, z));
    }

    public void Clear()
    {
        vertices.Clear();
        triangles.Clear();
        uv.Clear();
        colVertices.Clear();
        colTriangles.Clear();
        lightcolors = new Color32[16, 16, 16];
        lightintensity = new float[16, 16, 16];
        lightrange = new float[16, 16, 16];
        lightpositions.Clear();
    }
}

And Rendering:

    // Sends the calculated mesh information
    // to the mesh and collision components
    void RenderMesh()
    {

        filter.mesh.Clear();
        filter.mesh.vertices = meshData.vertices.ToArray();
        filter.mesh.triangles = meshData.triangles.ToArray();
        filter.mesh.uv = meshData.uv.ToArray();

        filter.mesh.RecalculateNormals();

        meshnew.Clear();

        

        meshnew.vertices = meshData.colVertices.ToArray();
        meshnew.triangles = meshData.colTriangles.ToArray();
        
        foreach (Vector3 position in meshData.lightpositions)
        {

            int x = (int)position.x;
            int y = (int)position.y;
            int z = (int)position.z;

            if (lights[x, y, z] == null)
            {
                

                Color32 color = meshData.lightcolors[x, y, z]; //Optimize lights later
                float intensity = meshData.lightintensity[x, y, z];
                float range = meshData.lightrange[x, y, z];

                GameObject light = new GameObject("Light " + x + " " + y + " " + z);
                Light lightComp = light.AddComponent<Light>();
                lightComp.color = color;
                lightComp.intensity = intensity;
                lightComp.range = range;
                light.transform.parent = this.transform;
                light.transform.localPosition = new Vector3(x, y, z);

                lights[x, y, z] = light;

            }

        }

        meshnew.RecalculateNormals();
        coll.sharedMesh = meshnew;

    }

Problem: MeshData uses lists, and when the mesh is updated... yep, so are the lists. Clearing the lists create quite a bit of garbage. I wanted to know if there's a way I should be doing this that would be less garbage-prone. Thanks.

Share this post


Link to post
Share on other sites
Advertisement

For list see if you can set the Capacity property of the list when you clear. Not sure how it works if a list clears down to a small size or keeps the last capacity.

Another option should be to change to arrays and track the index yourself when you add things. Clearing it will then create no garbage as you only set indexes to zero. Then you might be able to use the array directly and put it into the unity mesh.

A link that with some information on unity's garbage handling. http://www.gamasutra.com/blogs/RobertZubek/20150504/242572/C_memory_and_performance_tips_for_Unity.php

Edited by Spinningcubes

Share this post


Link to post
Share on other sites

For list see if you can set the Capacity property of the list when you clear. Not sure how it works if a list clears down to a small size or keeps the last capacity.

Another option should be to change to arrays and track the index yourself when you add things. Clearing it will then create no garbage as you only set indexes to zero. Then you might be able to use the array directly and put it into the unity mesh.

A link that with some information on unity's garbage handling. http://www.gamasutra.com/blogs/RobertZubek/20150504/242572/C_memory_and_performance_tips_for_Unity.php

I set up the capacities, but I'm having trouble with arrays since I have no way to know how big  the arrays will be since the game is procedural.

Share this post


Link to post
Share on other sites

Here's my profiler if you're wondering where the GC is called:

image.png

 

The GC is called under MeshData..ctor, except my MeshData class has no contructor

Share this post


Link to post
Share on other sites

Hi SpikeViper!

Some facts and hints about your situation:
First and foremost garbage is generated when you are "letting go" references to objects when you are not using them anymore.
Every class will have an auto-generated default constructor even if you don't write one, which does absolutely nothing except for setting the default values (or the values preset by you) for the fields of the object.
GC.Collect will kick in from time to time no matter what you do, and it may start to run "anywhere", but of course this depends on the garbage collector implementation. That is it's "nature" :), it has to check the execution stack and the objects on the heap from time to time whether there is garbage to be freed, but this can be significantly reduced (the number of times it kicks in and the time it takes to check) with careful program design.

Now on to your concrete problems:

The List<T> class does not "compact" it's inner array automatically when calling Clear/Remove/RemoveAt/RemoveAll etc...
There is a method called TrimExecess to do that explicitly, more on this here:
https://msdn.microsoft.com/en-us/library/dwb5h52a%28v=vs.110%29.aspx
and here:
https://msdn.microsoft.com/en-us/library/ms132207%28v=vs.110%29.aspx#
So the capacity of a list only grows under default use cases, but indeed when you clear a List<T>, the held references are let go, so this can generate garbage if not designed carefully.

!!! Altough in your case it is important to note that the Unity Vector2 and Vector3 types are structs (value types), and they are allocated in place and not as a new object on the heap, so they DO NOT GENERATE GARBAGE when they are let go !!!
 

    public void Clear()
    {
        vertices.Clear();
        triangles.Clear();
        uv.Clear();
        colVertices.Clear();
        colTriangles.Clear();
        lightcolors = new Color32[16, 16, 16];
        lightintensity = new float[16, 16, 16];
        lightrange = new float[16, 16, 16];
        lightpositions.Clear();
    }

In your clear method though you are throwing away a couple of objects. "lightcolors", "lightintensity", "lightrange" are re-created on each call and since they seem to be fixed size (16*16*16, so there is no need for a new one or a modified one) this is extremely wasteful, especially if you call this clear method often. Instead set all the values on a clear call to their default values.
 

    void RenderMesh()
    {

        filter.mesh.Clear();
        filter.mesh.vertices = meshData.vertices.ToArray();
        filter.mesh.triangles = meshData.triangles.ToArray();
        filter.mesh.uv = meshData.uv.ToArray();

        filter.mesh.RecalculateNormals();

        meshnew.Clear();

        

        meshnew.vertices = meshData.colVertices.ToArray();
        meshnew.triangles = meshData.colTriangles.ToArray();

When you call ToArray on a List<T>, a new array is actually allocated and all the elements of the old list are copied to the new array. So in this case you are "re-creating" the array of mesh vertices and triangles etc. on every RenderMesh() call. I guess this is not really what you want if optimization and less garbage is the goal! To solve this problem, I think you should really consider using arrays, or look for a list implementation which exposes it's inner array. The list class is actually nothing more than an encapsulated array which "grows" when you add an element to it, so it is resized to be able to hold more elements. The capacity field in the list class is the actual length of the inner array. More on this, and how to do a resizable array:
https://msdn.microsoft.com/en-us/library/bb348051%28v=vs.110%29.aspx
http://www.dotnetperls.com/array-resize
http://stackoverflow.com/questions/4972951/listt-to-t-without-copying/4973190#4973190

Another topic, that may be useful is "object pooling", but this is not something which can be applied to your current problem with your mesh data, but it still good-to-know. There is a ridiculous number of articles written on garbage collection and object pooling on the net, some useful ones:
https://blogs.msdn.microsoft.com/shawnhar/2007/07/02/twin-paths-to-garbage-collector-nirvana/
http://swampthingtom.blogspot.hu/2007/06/generic-pool-collection-class.html
http://codecube.net/2010/01/xna-resource-pool/

Br. :wink:

Share this post


Link to post
Share on other sites

Thanks! I've (partially) fixed it. Here's the changes:

        meshData.vertices.TrimExcess();
        meshData.triangles.TrimExcess();
        meshData.uv.TrimExcess();

        verts = meshData.vertices._items;
        tris = meshData.triangles._items;

        filter.mesh.vertices = verts;
        filter.mesh.triangles = tris;
        filter.mesh.uv = meshData.uv._items;

        filter.mesh.RecalculateNormals();

        meshnew.vertices = verts;
        meshnew.triangles = tris;

I made my own "Resizeable Array" which has its array visible. I'm still getting a GC called on the meshData constructor, though. Any way to further reduce the trash?

 

SHOOT. Trimming them also copies the array.

Edited by SpikeViper

Share this post


Link to post
Share on other sites
I've hit a roadblock. Yes, I managed to switch to arrays from the start by doing "preprocessing" to find the size to make my arrays before actually adding to them. But, by not using lists, multiple vertices with the same value can be put into the mesh... resulting in (2x - 4x) the vertices needed. This is bad, because I can make sure that something hasn't been added already by adding more logic, but that throws off my triangle and uv counts. I think it's come to a time for me to re-think how I make the meshes for my chunks, and how I store that data in blocks. For those wondering, here's my current code. Any suggestions, ideas, or anything vaguely related would be very helpful. Thanks.

BLOCK:
using UnityEngine;using System.Collections;using System.Collections.Generic;[System.Serializable()]public class Block{    public BlockType type;    public Vector2[] UVs = new Vector2[4];    public Tile tilePos;    public bool blockupsolid;    public bool blockdownsolid;    public bool blockeastsolid;    public bool blockwestsolid;    public bool blocknorthsolid;    public bool blocksouthsolid;    public Block(BlockType type1)    {        type = type1;    }    public enum Direction { north, east, south, west, up, down };    [System.SerializableAttribute()]    public struct Tile { public int x; public int y;}    const float tileSize = 0.0625f;    public MeshData Blockdata     (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)    {        if (this.type != BlockTypes.typeEmpty)        {            meshData.useRenderDataForCol = true;            if (!blockupsolid)            {                meshData = FaceDataUp(planetchunk, x, y, z, meshData);            }            if (!blockdownsolid)            {                meshData = FaceDataDown(planetchunk, x, y, z, meshData);            }            if (!blocknorthsolid)            {                meshData = FaceDataNorth(planetchunk, x, y, z, meshData);            }            if (!blocksouthsolid)            {                meshData = FaceDataSouth(planetchunk, x, y, z, meshData);            }            if (!blockeastsolid)            {                meshData = FaceDataEast(planetchunk, x, y, z, meshData);            }            if (!blockwestsolid)            {                meshData = FaceDataWest(planetchunk, x, y, z, meshData);            }            if (type.light == true)            {                meshData.Light(type.light);                if (planetchunk.GetBlock(x - 1, y, z).IsSolid == false || planetchunk.GetBlock(x + 1, y, z).IsSolid == false || planetchunk.GetBlock(x, y, z - 1).IsSolid == false ||                    planetchunk.GetBlock(x, y, z + 1).IsSolid == false || planetchunk.GetBlock(x, y - 1, z).IsSolid == false || planetchunk.GetBlock(x, y + 1, z).IsSolid == false)                {                    meshData.AddLight(x, y, z, type.LR, type.LG, type.LB, type.LA, type.LightRange, type.LightIntensity);                }            }            return meshData;        }        return meshData;    }    protected MeshData FaceDataUp        (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)    {        meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f));        meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f));        meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f));        meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f));        meshData.AddQuadTriangles();        meshData.AddUVRange(FaceUVs(Direction.up));        return meshData;    }    protected MeshData FaceDataDown        (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)    {        meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z - 0.5f));        meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z - 0.5f));        meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z + 0.5f));        meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z + 0.5f));        meshData.AddQuadTriangles();        meshData.AddUVRange(FaceUVs(Direction.down));        return meshData;    }    protected MeshData FaceDataNorth        (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)    {        meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z + 0.5f));        meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f));        meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f));        meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z + 0.5f));        meshData.AddQuadTriangles();        meshData.AddUVRange(FaceUVs(Direction.north));        return meshData;    }    protected MeshData FaceDataEast        (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)    {        meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z - 0.5f));        meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f));        meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f));        meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z + 0.5f));        meshData.AddQuadTriangles();        meshData.AddUVRange(FaceUVs(Direction.east));        return meshData;    }    protected MeshData FaceDataSouth        (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)    {        meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z - 0.5f));        meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f));        meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f));        meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z - 0.5f));        meshData.AddQuadTriangles();        meshData.AddUVRange(FaceUVs(Direction.south));        return meshData;    }    protected MeshData FaceDataWest        (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)    {        meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z + 0.5f));        meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f));        meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f));        meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z - 0.5f));                    meshData.AddQuadTriangles();        meshData.AddUVRange(FaceUVs(Direction.west));        return meshData;    }    public Vector2[] FaceUVs(Direction direction)    {        tilePos = type.tile;        UVs[0] = new Vector2(tileSize * tilePos.x + tileSize,            tileSize * tilePos.y);        UVs[1] = new Vector2(tileSize * tilePos.x + tileSize,            tileSize * tilePos.y + tileSize);        UVs[2] = new Vector2(tileSize * tilePos.x,            tileSize * tilePos.y + tileSize);        UVs[3] = new Vector2(tileSize * tilePos.x,            tileSize * tilePos.y);        return UVs;    }    public void PreProcess(PlanetChunk planetchunk, int x, int y, int z, MeshData meshData)    {             blockupsolid = false;             blockdownsolid = false;              blocknorthsolid = false;             blocksouthsolid = false;             blockeastsolid = false;             blockwestsolid = false;        if (this.type != BlockTypes.typeEmpty)        {            if (planetchunk.GetBlock(x, y + 1, z).IsSolid)            {                blockupsolid = true;                meshData.AddFaceCount();            }            if (planetchunk.GetBlock(x, y - 1, z).IsSolid)            {                blockdownsolid = true;                meshData.AddFaceCount();            }            if (planetchunk.GetBlock(x, y, z + 1).IsSolid)            {                blocknorthsolid = true;                meshData.AddFaceCount();            }            if (planetchunk.GetBlock(x, y, z - 1).IsSolid)            {                blocksouthsolid = true;                meshData.AddFaceCount();            }            if (planetchunk.GetBlock(x + 1, y, z).IsSolid)            {                blockeastsolid = true;                meshData.AddFaceCount();            }            if (planetchunk.GetBlock(x - 1, y, z).IsSolid)            {                blockwestsolid = true;                meshData.AddFaceCount();            }        }    }}
MESHDATA:
using UnityEngine;using System.Collections;using System.Collections.Generic;public class MeshData{    public Vector3[] vertices;    public int[] triangles;    public Vector2[] uv;    public Color32[,,] lightcolors;    public float[,,] lightintensity;    public float[,,] lightrange;    public ResizeableArray<Vector3> lightpositions;    public int vertcount = 0;    public int tricount = 0;    public int uvcount = 0;    public int verts = 0;    public int tris = 0;    public int uvs = 0;    public bool light;    public bool useRenderDataForCol;        public void Light(bool val)    {        if (val == true)        {            lightpositions = new ResizeableArray<Vector3>(localVars.chunklength * localVars.chunklength * localVars.chunklength); //Fix this later            lightcolors = new Color32[localVars.chunklength, localVars.chunklength, localVars.chunklength];            lightintensity = new float[localVars.chunklength, localVars.chunklength, localVars.chunklength];            lightrange = new float[localVars.chunklength, localVars.chunklength, localVars.chunklength];            light = true;        }    }    public void AddFaceCount()    {        vertcount = vertcount + 4;        uvcount = uvcount + 4;        tricount = tricount + 6;    }    public void Rebuild()    {        vertices = new Vector3[vertcount];        triangles = new int[tricount];        uv = new Vector2[uvcount];    }    public void AddQuadTriangles()    {        triangles[tris++] = verts - 4;        triangles[tris++] = verts - 3;        triangles[tris++] = verts - 2;        triangles[tris++] = verts - 4;        triangles[tris++] = verts - 2;        triangles[tris++] = verts - 1;        //triangles.Add(vertices.Count - 4);        //triangles.Add(vertices.Count - 3);        //triangles.Add(vertices.Count - 2);        //triangles.Add(vertices.Count - 4);        //triangles.Add(vertices.Count - 2);        //triangles.Add(vertices.Count - 1);        //if (useRenderDataForCol)        //{        //    colTriangles.Add(colVertices.Count - 4);        //    colTriangles.Add(colVertices.Count - 3);        //    colTriangles.Add(colVertices.Count - 2);        //    colTriangles.Add(colVertices.Count - 4);        //    colTriangles.Add(colVertices.Count - 2);        //    colTriangles.Add(colVertices.Count - 1);        //}    }    public void AddVertex(Vector3 vertex)    {                vertices[verts++] = vertex;        //if (useRenderDataForCol)        //{        //    colVertices.Add(vertex);        //}    }    public void AddTriangle(int tri)    {        triangles[tris++] = tri;    }    public void AddLight (int x, int y, int z, byte R, byte G, byte B, byte A, float range, float intensity)    {        lightcolors[x, y, z] = new Color32(R, G, B, A);        lightintensity[x, y, z] = intensity;        lightrange[x, y, z] = range;        lightpositions.Add(new Vector3(x, y, z));    }    public void Clear()    {        if (light == true)        {            lightpositions.Clear();            light = false;        }    }    public void AddUVRange(Vector2[] vect2s)    {        uv[uvs++] = vect2s[0];        uv[uvs++] = vect2s[1];        uv[uvs++] = vect2s[2];        uv[uvs++] = vect2s[3];    }}
RENDERING:
// Sends the calculated mesh information    // to the mesh and collision components    void RenderMesh()    {        mesh.vertices = meshData.vertices;        mesh.triangles = meshData.triangles;        mesh.uv = meshData.uv;        colmesh.vertices = meshData.vertices;        colmesh.triangles = meshData.triangles;        colmesh.uv = meshData.uv;        if (meshData.light == true)        {            for (int i = 0; i < meshData.lightpositions.Count; i++)            {                Vector3 position = meshData.lightpositions[i];                int x = (int)position.x;                int y = (int)position.y;                int z = (int)position.z;                if (lights[x, y, z] == null)                {                    Color32 color = meshData.lightcolors[x, y, z];                    float intensity = meshData.lightintensity[x, y, z];                    float range = meshData.lightrange[x, y, z];                    GameObject light = new GameObject("Light " + x + " " + y + " " + z);                    Light lightComp = light.AddComponent<Light>();                    lightComp.color = color;                    lightComp.intensity = intensity;                    lightComp.range = range;                    light.transform.parent = this.transform;                    light.transform.localPosition = new Vector3(x, y, z);                    lights[x, y, z] = light;                }            }        }        filter.sharedMesh = mesh;        coll.sharedMesh = colmesh;        filter.sharedMesh.RecalculateNormals();        firstrender = false;    }

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement