Example #1
0
    void MakeTemplateChildOfPallette(VoxelPallette targetPallette, VoxelTemplate voxelTemplate)
    {
        AssetDatabase.AddObjectToAsset(voxelTemplate, targetPallette);
        voxelTemplate.hideFlags = HideFlags.HideInHierarchy;

        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(voxelTemplate));
        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(targetPallette));
    }
Example #2
0
    VoxelTemplate CopyVoxelTemplate(VoxelPallette voxelPallette, VoxelTemplate original)
    {
        VoxelTemplate newVoxelTemplate = ScriptableObject.CreateInstance <VoxelTemplate>();

        newVoxelTemplate.shouldDraw = original.shouldDraw;
        newVoxelTemplate.name       = original.name;

        newVoxelTemplate.atlasScale    = original.atlasScale;
        newVoxelTemplate.voxelPallette = voxelPallette;


        newVoxelTemplate.drawFacesInCenter = original.drawFacesInCenter;
        newVoxelTemplate.color             = original.color;

        newVoxelTemplate.UVOffsetFront  = original.UVOffsetFront;
        newVoxelTemplate.UVOffsetBack   = original.UVOffsetBack;
        newVoxelTemplate.UVOffsetTop    = original.UVOffsetTop;
        newVoxelTemplate.UVOffsetBottom = original.UVOffsetBottom;
        newVoxelTemplate.UVOffsetLeft   = original.UVOffsetLeft;
        newVoxelTemplate.UVOffsetRight  = original.UVOffsetRight;


        newVoxelTemplate.SpriteFront  = original.SpriteFront;
        newVoxelTemplate.SpriteBack   = original.SpriteBack;
        newVoxelTemplate.SpriteTop    = original.SpriteTop;
        newVoxelTemplate.SpriteBottom = original.SpriteBottom;
        newVoxelTemplate.SpriteLeft   = original.SpriteLeft;
        newVoxelTemplate.SpriteRight  = original.SpriteRight;


        newVoxelTemplate.drawFront  = original.drawFront;
        newVoxelTemplate.drawBack   = original.drawBack;
        newVoxelTemplate.drawLeft   = original.drawLeft;
        newVoxelTemplate.drawRight  = original.drawRight;
        newVoxelTemplate.drawTop    = original.drawTop;
        newVoxelTemplate.drawBottom = original.drawBottom;



        MakeTemplateChildOfPallette(voxelPallette, newVoxelTemplate);
        newVoxelTemplate.GenerateAssetPreview();


        EditorUtility.SetDirty(newVoxelTemplate);

        return(newVoxelTemplate);
    }
Example #3
0
    void AddNewVoxelTemplate(VoxelPallette voxelPallette, Sprite sprite)
    {
        VoxelTemplate newVoxelTemplate = ScriptableObject.CreateInstance <VoxelTemplate>();

        newVoxelTemplate.shouldDraw = true;
        newVoxelTemplate.name       = sprite.name;

        newVoxelTemplate.atlasScale    = new Vector2(sprite.rect.width / voxelPallette.AtlasMaterial.mainTexture.width, sprite.rect.height / voxelPallette.AtlasMaterial.mainTexture.height);
        newVoxelTemplate.voxelPallette = voxelPallette;



        newVoxelTemplate.SpriteFront  = sprite;
        newVoxelTemplate.SpriteBack   = sprite;
        newVoxelTemplate.SpriteLeft   = sprite;
        newVoxelTemplate.SpriteRight  = sprite;
        newVoxelTemplate.SpriteTop    = sprite;
        newVoxelTemplate.SpriteBottom = sprite;

        var offset = new Vector2(sprite.rect.x / voxelPallette.AtlasMaterial.mainTexture.width, sprite.rect.y / voxelPallette.AtlasMaterial.mainTexture.height);

        newVoxelTemplate.UVOffsetFront  = offset;
        newVoxelTemplate.UVOffsetBack   = offset;
        newVoxelTemplate.UVOffsetTop    = offset;
        newVoxelTemplate.UVOffsetBottom = offset;
        newVoxelTemplate.UVOffsetLeft   = offset;
        newVoxelTemplate.UVOffsetRight  = offset;


        AssetDatabase.AddObjectToAsset(newVoxelTemplate, voxelPallette);
        voxelPallette.voxelTemplateList.Add(newVoxelTemplate);

        newVoxelTemplate.hideFlags = HideFlags.HideInHierarchy;


        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newVoxelTemplate));
        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(voxelPallette));

        newVoxelTemplate.GenerateAssetPreview();
        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newVoxelTemplate));
        EditorUtility.SetDirty(newVoxelTemplate);
    }
Example #4
0
    void AddNewVoxelTemplate(VoxelPallette voxelPallette)
    {
        VoxelTemplate newVoxelTemplate = ScriptableObject.CreateInstance <VoxelTemplate>();

        newVoxelTemplate.shouldDraw    = true;
        newVoxelTemplate.name          = "New Voxel Template";
        newVoxelTemplate.voxelPallette = voxelPallette;
        newVoxelTemplate.DisplayOrder  = voxelPallette.voxelTemplatesSorted.Count();


        AssetDatabase.AddObjectToAsset(newVoxelTemplate, voxelPallette);
        voxelPallette.voxelTemplateList.Add(newVoxelTemplate);

        newVoxelTemplate.hideFlags = HideFlags.HideInHierarchy;


        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newVoxelTemplate));
        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(voxelPallette));

        newVoxelTemplate.GenerateAssetPreview();
        EditorUtility.SetDirty(newVoxelTemplate);
    }
Example #5
0
    //The magic:
    //Loop through each voxel,
    //based on visibility/neighbors,
    //determine which of the faces should be created,
    //if they should, get the verts, tris, and uvs for the face and
    //add it to the mesh.
    public void Draw()
    {
        if(pallette==null){
            Debug.LogError("No Pallette Selected");

        }else{
            mf = GetComponent<MeshFilter>();
            mc = GetComponent<MeshCollider>();

            ClampDimensions();

            //Destroy all children
        //		int childs = transform.childCount;
        //        for (int i = childs - 1; i > 0; i--)
        //        {
        //            GameObject.DestroyImmediate(transform.GetChild(i).gameObject);
        //        }

            var currentStepCount = 1;
            var currentStepFour = currentStepCount * 4;
            var currentStepSix = currentStepCount * 6;

            if(verts==null || verts.Length!=Width*Height*Depth*24){
                verts  = new Vector3[Width*Height*Depth*24];
                tris   = new int    [Width*Height*Depth*36];
                uvs    = new Vector2[Width*Height*Depth*24];
                colors = new Color32[Width*Height*Depth*24];
                tans   = new Vector4[Width*Height*Depth*24];
            }else{
                Array.Clear(verts,0,verts.Length);
                Array.Clear(tris,0,tris.Length);
                Array.Clear(uvs,0,uvs.Length);
                Array.Clear(colors,0,colors.Length);
                Array.Clear(tans,0,tans.Length);
            }

            //The Main Drawing Loop. Lots of room for optimization,
            //but it seems to run quickly enough.
            for(int x = 0; x < Width; x++){
                for(int y = 0; y < Height; y++){
                    for(int z = 0; z < Depth; z++){

                        c = GetVoxel(x,y,z);
                        voxelType = GetVoxelType(c);

                        if(voxelType!= null && voxelType.shouldDraw){
                            voxelColor = voxelType.color;

                            if(!voxelType.drawFacesInCenter){

                                #region voxel sides
                                //Get all neighboring voxels to determine whether or not to draw the corresponding face
                                f = GetVoxel(x,y,z-1);
                                fType = GetVoxelType(f);
                                k = GetVoxel(x,y,z+1);
                                kType = GetVoxelType(k);
                                r = GetVoxel(x+1,y,z);
                                rType = GetVoxelType(r);
                                l = GetVoxel(x-1,y,z);
                                lType = GetVoxelType(l);
                                t = GetVoxel(x,y+1,z);
                                tType = GetVoxelType(t);
                                b = GetVoxel(x,y-1,z);
                                bType = GetVoxelType(b);

                                //front face
                                if(DrawFront && voxelType.drawFront && (fType == null || fType.drawFacesInCenter || !fType.shouldDraw )){

                                    verts[currentStepFour+0] = new Vector3(x,y,z);
                                    verts[currentStepFour+1] = new Vector3(x,y+1,z);
                                    verts[currentStepFour+2] = new Vector3(x+1,y+1,z);
                                    verts[currentStepFour+3] = new Vector3(x+1,y,z);

                                    tris[currentStepSix+0] = currentStepFour+0;
                                    tris[currentStepSix+1] = currentStepFour+1;
                                    tris[currentStepSix+2] = currentStepFour+2;

                                    tris[currentStepSix+3] = currentStepFour+2;
                                    tris[currentStepSix+4] = currentStepFour+3;
                                    tris[currentStepSix+5] = currentStepFour+0;

                                    var myuvs = GetVoxelUvs(voxelType, 'f');
                                    uvs[currentStepFour+0] = myuvs[0];
                                    uvs[currentStepFour+1] = myuvs[1];
                                    uvs[currentStepFour+2] = myuvs[2];
                                    uvs[currentStepFour+3] = myuvs[3];

                                    colors[currentStepFour+0] = voxelColor;
                                    colors[currentStepFour+1] = voxelColor;
                                    colors[currentStepFour+2] = voxelColor;
                                    colors[currentStepFour+3] = voxelColor;

                                    tans[currentStepFour+0] = Vector4.zero;
                                    tans[currentStepFour+1] = Vector4.zero;
                                    tans[currentStepFour+2] = Vector4.zero;
                                    tans[currentStepFour+3] = Vector4.zero;
                                    currentStepCount ++;
                                    currentStepFour = 4 * currentStepCount;
                                    currentStepSix = 6 * currentStepCount;
                                }
                                //back face
                                if(DrawBack && voxelType.drawBack && (kType==null || kType.drawFacesInCenter || !kType.shouldDraw)){
                                    verts[currentStepFour+0] = new Vector3(x+1,y,z+1);
                                    verts[currentStepFour+1] = new Vector3(x+1,y+1,z+1);
                                    verts[currentStepFour+2] = new Vector3(x,y+1,z+1);
                                    verts[currentStepFour+3] = new Vector3(x,y,z+1);

                                    tris[currentStepSix+0] = currentStepFour+0;
                                    tris[currentStepSix+1] = currentStepFour+1;
                                    tris[currentStepSix+2] = currentStepFour+2;

                                    tris[currentStepSix+3] = currentStepFour+2;
                                    tris[currentStepSix+4] = currentStepFour+3;
                                    tris[currentStepSix+5] = currentStepFour+0;

                                    var myuvs = GetVoxelUvs(voxelType, 'k');
                                    uvs[currentStepFour+0] = myuvs[0];
                                    uvs[currentStepFour+1] = myuvs[1];
                                    uvs[currentStepFour+2] = myuvs[2];
                                    uvs[currentStepFour+3] = myuvs[3];

                                    colors[currentStepFour+0] = voxelColor;
                                    colors[currentStepFour+1] = voxelColor;
                                    colors[currentStepFour+2] = voxelColor;
                                    colors[currentStepFour+3] = voxelColor;

                                    tans[currentStepFour+0] = Vector4.zero;
                                    tans[currentStepFour+1] = Vector4.zero;
                                    tans[currentStepFour+2] = Vector4.zero;
                                    tans[currentStepFour+3] = Vector4.zero;

                                    currentStepCount ++;
                                    currentStepFour = 4 * currentStepCount;
                                    currentStepSix = 6 * currentStepCount;
                                }
                                //top
                                if(DrawTop && voxelType.drawTop && (tType==null || tType.drawFacesInCenter || !tType.shouldDraw)){

                                    verts[currentStepFour+0] = new Vector3(x,y+1,z);
                                    verts[currentStepFour+1] = new Vector3(x,y+1,z+1);
                                    verts[currentStepFour+2] = new Vector3(x+1,y+1,z+1);
                                    verts[currentStepFour+3] = new Vector3(x+1,y+1,z);

                                    tris[currentStepSix+0] = currentStepFour+0;
                                    tris[currentStepSix+1] = currentStepFour+1;
                                    tris[currentStepSix+2] = currentStepFour+2;

                                    tris[currentStepSix+3] = currentStepFour+2;
                                    tris[currentStepSix+4] = currentStepFour+3;
                                    tris[currentStepSix+5] = currentStepFour+0;

                                    var myuvs = GetVoxelUvs(voxelType, 't');
                                    uvs[currentStepFour+0] = myuvs[0];
                                    uvs[currentStepFour+1] = myuvs[1];
                                    uvs[currentStepFour+2] = myuvs[2];
                                    uvs[currentStepFour+3] = myuvs[3];

                                    colors[currentStepFour+0] = voxelColor;
                                    colors[currentStepFour+1] = voxelColor;
                                    colors[currentStepFour+2] = voxelColor;
                                    colors[currentStepFour+3] = voxelColor;

                                    tans[currentStepFour+0] = Vector4.zero;
                                    tans[currentStepFour+1] = Vector4.zero;
                                    tans[currentStepFour+2] = Vector4.zero;
                                    tans[currentStepFour+3] = Vector4.zero;

                                    currentStepCount ++;
                                    currentStepFour = 4 * currentStepCount;
                                    currentStepSix = 6 * currentStepCount;
                                }
                                //bottom
                                if(DrawBottom && voxelType.drawBottom && (bType==null || bType.drawFacesInCenter  || !bType.shouldDraw)){

                                    verts[currentStepFour+3] = new Vector3(x,y,z);
                                    verts[currentStepFour+2] = new Vector3(x,y,z+1);
                                    verts[currentStepFour+1] = new Vector3(x+1,y,z+1);
                                    verts[currentStepFour+0] = new Vector3(x+1,y,z);

                                    tris[currentStepSix+0] = currentStepFour+0;
                                    tris[currentStepSix+1] = currentStepFour+1;
                                    tris[currentStepSix+2] = currentStepFour+2;

                                    tris[currentStepSix+3] = currentStepFour+2;
                                    tris[currentStepSix+4] = currentStepFour+3;
                                    tris[currentStepSix+5] = currentStepFour+0;

                                    var myuvs = GetVoxelUvs(voxelType, 'b');
                                    uvs[currentStepFour+0] = myuvs[0];
                                    uvs[currentStepFour+1] = myuvs[1];
                                    uvs[currentStepFour+2] = myuvs[2];
                                    uvs[currentStepFour+3] = myuvs[3];

                                    colors[currentStepFour+0] = voxelColor;
                                    colors[currentStepFour+1] = voxelColor;
                                    colors[currentStepFour+2] = voxelColor;
                                    colors[currentStepFour+3] = voxelColor;

                                    tans[currentStepFour+0] = Vector4.zero;
                                    tans[currentStepFour+1] = Vector4.zero;
                                    tans[currentStepFour+2] = Vector4.zero;
                                    tans[currentStepFour+3] = Vector4.zero;

                                    currentStepCount ++;
                                    currentStepFour = 4 * currentStepCount;
                                    currentStepSix = 6 * currentStepCount;
                                }
                                //right
                                if(DrawRight && voxelType.drawRight && (rType==null || rType.drawFacesInCenter || !rType.shouldDraw)){

                                    verts[currentStepFour+0] = new Vector3(x+1,y,z);
                                    verts[currentStepFour+1] = new Vector3(x+1,y+1,z);
                                    verts[currentStepFour+2] = new Vector3(x+1,y+1,z+1);
                                    verts[currentStepFour+3] = new Vector3(x+1,y,z+1);

                                    tris[currentStepSix+0] = currentStepFour+0;
                                    tris[currentStepSix+1] = currentStepFour+1;
                                    tris[currentStepSix+2] = currentStepFour+2;

                                    tris[currentStepSix+3] = currentStepFour+2;
                                    tris[currentStepSix+4] = currentStepFour+3;
                                    tris[currentStepSix+5] = currentStepFour+0;

                                    var myuvs = GetVoxelUvs(voxelType, 'r');
                                    uvs[currentStepFour+0] = myuvs[0];
                                    uvs[currentStepFour+1] = myuvs[1];
                                    uvs[currentStepFour+2] = myuvs[2];
                                    uvs[currentStepFour+3] = myuvs[3];

                                    colors[currentStepFour+0] = voxelColor;
                                    colors[currentStepFour+1] = voxelColor;
                                    colors[currentStepFour+2] = voxelColor;
                                    colors[currentStepFour+3] = voxelColor;

                                    tans[currentStepFour+0] = Vector4.zero;
                                    tans[currentStepFour+1] = Vector4.zero;
                                    tans[currentStepFour+2] = Vector4.zero;
                                    tans[currentStepFour+3] = Vector4.zero;

                                    currentStepCount ++;
                                    currentStepFour = 4 * currentStepCount;
                                    currentStepSix = 6 * currentStepCount;
                                }
                                //left
                                if(DrawLeft && voxelType.drawLeft && (lType==null || lType.drawFacesInCenter  || !lType.shouldDraw)){

                                    verts[currentStepFour+3] = new Vector3(x,y,z);
                                    verts[currentStepFour+2] = new Vector3(x,y+1,z);
                                    verts[currentStepFour+1] = new Vector3(x,y+1,z+1);
                                    verts[currentStepFour+0] = new Vector3(x,y,z+1);

                                    tris[currentStepSix+0] = currentStepFour+0;
                                    tris[currentStepSix+1] = currentStepFour+1;
                                    tris[currentStepSix+2] = currentStepFour+2;

                                    tris[currentStepSix+3] = currentStepFour+2;
                                    tris[currentStepSix+4] = currentStepFour+3;
                                    tris[currentStepSix+5] = currentStepFour+0;

                                    var myuvs = GetVoxelUvs(voxelType, 'l');
                                    uvs[currentStepFour+0] = myuvs[0];
                                    uvs[currentStepFour+1] = myuvs[1];
                                    uvs[currentStepFour+2] = myuvs[2];
                                    uvs[currentStepFour+3] = myuvs[3];

                                    colors[currentStepFour+0] = voxelColor;
                                    colors[currentStepFour+1] = voxelColor;
                                    colors[currentStepFour+2] = voxelColor;
                                    colors[currentStepFour+3] = voxelColor;

                                    tans[currentStepFour+0] = Vector4.zero;
                                    tans[currentStepFour+1] = Vector4.zero;
                                    tans[currentStepFour+2] = Vector4.zero;
                                    tans[currentStepFour+3] = Vector4.zero;

                                    currentStepCount ++;
                                    currentStepFour = 4 * currentStepCount;
                                    currentStepSix = 6 * currentStepCount;
                                }
                                #endregion

                            }

                            //for voxels marked as 'drawFacesInCenter'
                            else{
                                if(DrawFront && voxelType.drawFront){
                                    verts[currentStepFour+0] = new Vector3(x,y,z+.5f);
                                    verts[currentStepFour+1] = new Vector3(x,y+1,z+.5f);
                                    verts[currentStepFour+2] = new Vector3(x+1,y+1,z+.5f);
                                    verts[currentStepFour+3] = new Vector3(x+1,y,z+.5f);

                                    tris[currentStepSix+0] = currentStepFour+0;
                                    tris[currentStepSix+1] = currentStepFour+1;
                                    tris[currentStepSix+2] = currentStepFour+2;

                                    tris[currentStepSix+3] = currentStepFour+2;
                                    tris[currentStepSix+4] = currentStepFour+3;
                                    tris[currentStepSix+5] = currentStepFour+0;

                                    var myuvs = GetVoxelUvs(voxelType, 'f');
                                    uvs[currentStepFour+0] = myuvs[0];
                                    uvs[currentStepFour+1] = myuvs[1];
                                    uvs[currentStepFour+2] = myuvs[2];
                                    uvs[currentStepFour+3] = myuvs[3];

                                    colors[currentStepFour+0] = voxelColor;
                                    colors[currentStepFour+1] = voxelColor;
                                    colors[currentStepFour+2] = voxelColor;
                                    colors[currentStepFour+3] = voxelColor;

                                    tans[currentStepFour+0] = Vector4.zero;
                                    tans[currentStepFour+1] = Vector4.zero;
                                    tans[currentStepFour+2] = Vector4.zero;
                                    tans[currentStepFour+3] = Vector4.zero;
                                    currentStepCount ++;
                                    currentStepFour = 4 * currentStepCount;
                                    currentStepSix = 6 * currentStepCount;
                                }
                                //back face
                                if(DrawBack && voxelType.drawBack){
                                    verts[currentStepFour+3] = new Vector3(x,y,z+.5f);
                                    verts[currentStepFour+2] = new Vector3(x,y+1,z+.5f);
                                    verts[currentStepFour+1] = new Vector3(x+1,y+1,z+.5f);
                                    verts[currentStepFour+0] = new Vector3(x+1,y,z+.5f);

                                    tris[currentStepSix+0] = currentStepFour+0;
                                    tris[currentStepSix+1] = currentStepFour+1;
                                    tris[currentStepSix+2] = currentStepFour+2;

                                    tris[currentStepSix+3] = currentStepFour+2;
                                    tris[currentStepSix+4] = currentStepFour+3;
                                    tris[currentStepSix+5] = currentStepFour+0;

                                    var myuvs = GetVoxelUvs(voxelType, 'k');
                                    uvs[currentStepFour+0] = myuvs[0];
                                    uvs[currentStepFour+1] = myuvs[1];
                                    uvs[currentStepFour+2] = myuvs[2];
                                    uvs[currentStepFour+3] = myuvs[3];

                                    colors[currentStepFour+0] = voxelColor;
                                    colors[currentStepFour+1] = voxelColor;
                                    colors[currentStepFour+2] = voxelColor;
                                    colors[currentStepFour+3] = voxelColor;

                                    tans[currentStepFour+0] = Vector4.zero;
                                    tans[currentStepFour+1] = Vector4.zero;
                                    tans[currentStepFour+2] = Vector4.zero;
                                    tans[currentStepFour+3] = Vector4.zero;
                                    currentStepCount ++;
                                    currentStepFour = 4 * currentStepCount;
                                    currentStepSix = 6 * currentStepCount;
                                }
                                //top
                                if(DrawTop && voxelType.drawTop){

                                    verts[currentStepFour+0] = new Vector3(x,y+.5f,z);
                                    verts[currentStepFour+1] = new Vector3(x,y+.5f,z+1);
                                    verts[currentStepFour+2] = new Vector3(x+1,y+.5f,z+1);
                                    verts[currentStepFour+3] = new Vector3(x+1,y+.5f,z);

                                    tris[currentStepSix+0] = currentStepFour+0;
                                    tris[currentStepSix+1] = currentStepFour+1;
                                    tris[currentStepSix+2] = currentStepFour+2;

                                    tris[currentStepSix+3] = currentStepFour+2;
                                    tris[currentStepSix+4] = currentStepFour+3;
                                    tris[currentStepSix+5] = currentStepFour+0;

                                    var myuvs = GetVoxelUvs(voxelType, 't');
                                    uvs[currentStepFour+0] = myuvs[0];
                                    uvs[currentStepFour+1] = myuvs[1];
                                    uvs[currentStepFour+2] = myuvs[2];
                                    uvs[currentStepFour+3] = myuvs[3];

                                    colors[currentStepFour+0] = voxelColor;
                                    colors[currentStepFour+1] = voxelColor;
                                    colors[currentStepFour+2] = voxelColor;
                                    colors[currentStepFour+3] = voxelColor;

                                    tans[currentStepFour+0] = Vector4.zero;
                                    tans[currentStepFour+1] = Vector4.zero;
                                    tans[currentStepFour+2] = Vector4.zero;
                                    tans[currentStepFour+3] = Vector4.zero;

                                    currentStepCount ++;
                                    currentStepFour = 4 * currentStepCount;
                                    currentStepSix = 6 * currentStepCount;
                                }
                                //bottom
                                if(DrawBottom && voxelType.drawBottom){

                                    verts[currentStepFour+3] = new Vector3(x,y+.5f,z);
                                    verts[currentStepFour+2] = new Vector3(x,y+.5f,z+1);
                                    verts[currentStepFour+1] = new Vector3(x+1,y+.5f,z+1);
                                    verts[currentStepFour+0] = new Vector3(x+1,y+.5f,z);

                                    tris[currentStepSix+0] = currentStepFour+0;
                                    tris[currentStepSix+1] = currentStepFour+1;
                                    tris[currentStepSix+2] = currentStepFour+2;

                                    tris[currentStepSix+3] = currentStepFour+2;
                                    tris[currentStepSix+4] = currentStepFour+3;
                                    tris[currentStepSix+5] = currentStepFour+0;

                                    var myuvs = GetVoxelUvs(voxelType, 'b');
                                    uvs[currentStepFour+0] = myuvs[0];
                                    uvs[currentStepFour+1] = myuvs[1];
                                    uvs[currentStepFour+2] = myuvs[2];
                                    uvs[currentStepFour+3] = myuvs[3];

                                    colors[currentStepFour+0] = voxelColor;
                                    colors[currentStepFour+1] = voxelColor;
                                    colors[currentStepFour+2] = voxelColor;
                                    colors[currentStepFour+3] = voxelColor;

                                    tans[currentStepFour+0] = Vector4.zero;
                                    tans[currentStepFour+1] = Vector4.zero;
                                    tans[currentStepFour+2] = Vector4.zero;
                                    tans[currentStepFour+3] = Vector4.zero;

                                    currentStepCount ++;
                                    currentStepFour = 4 * currentStepCount;
                                    currentStepSix = 6 * currentStepCount;
                                }
                                //right
                                if(DrawRight && voxelType.drawRight){

                                    verts[currentStepFour+0] = new Vector3(x+.5f,y,z);
                                    verts[currentStepFour+1] = new Vector3(x+.5f,y+1,z);
                                    verts[currentStepFour+2] = new Vector3(x+.5f,y+1,z+1);
                                    verts[currentStepFour+3] = new Vector3(x+.5f,y,z+1);

                                    tris[currentStepSix+0] = currentStepFour+0;
                                    tris[currentStepSix+1] = currentStepFour+1;
                                    tris[currentStepSix+2] = currentStepFour+2;

                                    tris[currentStepSix+3] = currentStepFour+2;
                                    tris[currentStepSix+4] = currentStepFour+3;
                                    tris[currentStepSix+5] = currentStepFour+0;

                                    var myuvs = GetVoxelUvs(voxelType, 'r');
                                    uvs[currentStepFour+0] = myuvs[0];
                                    uvs[currentStepFour+1] = myuvs[1];
                                    uvs[currentStepFour+2] = myuvs[2];
                                    uvs[currentStepFour+3] = myuvs[3];

                                    colors[currentStepFour+0] = voxelColor;
                                    colors[currentStepFour+1] = voxelColor;
                                    colors[currentStepFour+2] = voxelColor;
                                    colors[currentStepFour+3] = voxelColor;

                                    tans[currentStepFour+0] = Vector4.zero;
                                    tans[currentStepFour+1] = Vector4.zero;
                                    tans[currentStepFour+2] = Vector4.zero;
                                    tans[currentStepFour+3] = Vector4.zero;

                                    currentStepCount ++;
                                    currentStepFour = 4 * currentStepCount;
                                    currentStepSix = 6 * currentStepCount;
                                }
                                //left
                                if(DrawLeft && voxelType.drawLeft){

                                    verts[currentStepFour+3] = new Vector3(x+.5f,y,z);
                                    verts[currentStepFour+2] = new Vector3(x+.5f,y+1,z);
                                    verts[currentStepFour+1] = new Vector3(x+.5f,y+1,z+1);
                                    verts[currentStepFour+0] = new Vector3(x+.5f,y,z+1);

                                    tris[currentStepSix+0] = currentStepFour+0;
                                    tris[currentStepSix+1] = currentStepFour+1;
                                    tris[currentStepSix+2] = currentStepFour+2;

                                    tris[currentStepSix+3] = currentStepFour+2;
                                    tris[currentStepSix+4] = currentStepFour+3;
                                    tris[currentStepSix+5] = currentStepFour+0;

                                    var myuvs = GetVoxelUvs(voxelType, 'l');
                                    uvs[currentStepFour+0] = myuvs[0];
                                    uvs[currentStepFour+1] = myuvs[1];
                                    uvs[currentStepFour+2] = myuvs[2];
                                    uvs[currentStepFour+3] = myuvs[3];

                                    colors[currentStepFour+0] = voxelColor;
                                    colors[currentStepFour+1] = voxelColor;
                                    colors[currentStepFour+2] = voxelColor;
                                    colors[currentStepFour+3] = voxelColor;

                                    tans[currentStepFour+0] = Vector4.zero;
                                    tans[currentStepFour+1] = Vector4.zero;
                                    tans[currentStepFour+2] = Vector4.zero;
                                    tans[currentStepFour+3] = Vector4.zero;

                                    currentStepCount ++;
                                    currentStepFour = 4 * currentStepCount;
                                    currentStepSix = 6 * currentStepCount;
                                }

                            }
                        }

                    }
                }
            }

            //trim the arrays
            var subverts = verts.Take(currentStepCount*4).ToArray();

            if(subverts.Length<64000){

                if(UsePalletteTexture){
                    if(pallette.AtlasMaterial!=null)
                        GetComponent<Renderer>().material = pallette.AtlasMaterial;
                    else{
                        Debug.LogWarning("There is no atlas material set on the Voxel Pallette. Will use current Renderer Material instead.");
                    }
                }

                var subtris = tris.Take(currentStepCount*6).ToArray();
                var subuvs = uvs.Take(currentStepCount*4).ToArray();
                var subcolors = colors.Take(currentStepCount*4).ToArray();
                var subtans = tans.Take(currentStepCount*4).ToArray();

                var someMesh = mf.sharedMesh;
                if(someMesh!=null){
                    someMesh.Clear();
                }else{
                    someMesh = new Mesh();
                }
                someMesh.vertices = subverts;
                someMesh.triangles = subtris;
                someMesh.uv = subuvs;
                someMesh.colors32 = subcolors;
                someMesh.tangents = subtans;

                someMesh.RecalculateBounds();
                someMesh.RecalculateNormals();

                mf.sharedMesh = someMesh;

                #if UNITY_EDITOR
                if(GenerateSecondaryUvSet && subverts.Length>4){
                    Unwrapping.GenerateSecondaryUVSet(mf.sharedMesh);
                }
                #endif

                mc.sharedMesh = null;
                mc.sharedMesh = someMesh;

            }
            else{
                Debug.LogError("You are attempting to create a structure that would require more than 64k vertices, which is the upper limit in Unity. Please adjust the size of your structure. In the future, this will no longer happen. I'm sorry!");
            }

        }
    }
Example #6
0
    //Determine what UVs to use for a given voxel face.
    //This may seem odd, but I'm doing this here rather than storing this data in the voxel class, since we want that class to be as small as possible.
    //This function only runs during mesh building, whereas storing all of this data would mean Unity uses a lot more memory... I think!
    //TODO: Abstract this to an 'atlas position' like 0,14 instead of 0f/16f, 14f/16f
    Vector2[] GetVoxelUvs(VoxelTemplate voxelType, char dir)
    {
        if(pallette){

            //var voxelType = GetVoxelType(voxelType);//pallette.voxelTypes[voxelType];
            if(voxelType!=null){
                if(dir=='f' || voxelType.useFrontUvsForAllFaces){
                    uvs[0].x=voxelType.UVOffsetFront.x;
                    uvs[0].y=voxelType.UVOffsetFront.y;

                    uvs[1].x=voxelType.UVOffsetFront.x;
                    uvs[1].y=voxelType.UVOffsetFront.y + voxelType.atlasScale.y;

                    uvs[2].x=voxelType.UVOffsetFront.x + voxelType.atlasScale.x;
                    uvs[2].y=voxelType.UVOffsetFront.y + voxelType.atlasScale.y;

                    uvs[3].x=voxelType.UVOffsetFront.x + voxelType.atlasScale.x;
                    uvs[3].y=voxelType.UVOffsetFront.y;
                }else if(dir=='k'){
                    uvs[0].x=voxelType.UVOffsetBack.x;
                    uvs[0].y=voxelType.UVOffsetBack.y;

                    uvs[1].x=voxelType.UVOffsetBack.x;
                    uvs[1].y=voxelType.UVOffsetBack.y + voxelType.atlasScale.y;

                    uvs[2].x=voxelType.UVOffsetBack.x + voxelType.atlasScale.x;
                    uvs[2].y=voxelType.UVOffsetBack.y + voxelType.atlasScale.y;

                    uvs[3].x=voxelType.UVOffsetBack.x + voxelType.atlasScale.x;
                    uvs[3].y=voxelType.UVOffsetBack.y;
                }else if(dir=='l'){
                    uvs[0].x=voxelType.UVOffsetLeft.x;
                    uvs[0].y=voxelType.UVOffsetLeft.y;

                    uvs[1].x=voxelType.UVOffsetLeft.x;
                    uvs[1].y=voxelType.UVOffsetLeft.y + voxelType.atlasScale.y;

                    uvs[2].x=voxelType.UVOffsetLeft.x + voxelType.atlasScale.x;
                    uvs[2].y=voxelType.UVOffsetLeft.y + voxelType.atlasScale.y;

                    uvs[3].x=voxelType.UVOffsetLeft.x + voxelType.atlasScale.x;
                    uvs[3].y=voxelType.UVOffsetLeft.y;
                }else if(dir=='r'){
                    uvs[0].x=voxelType.UVOffsetRight.x;
                    uvs[0].y=voxelType.UVOffsetRight.y;

                    uvs[1].x=voxelType.UVOffsetRight.x;
                    uvs[1].y=voxelType.UVOffsetRight.y + voxelType.atlasScale.y;

                    uvs[2].x=voxelType.UVOffsetRight.x + voxelType.atlasScale.x;
                    uvs[2].y=voxelType.UVOffsetRight.y + voxelType.atlasScale.y;

                    uvs[3].x=voxelType.UVOffsetRight.x + voxelType.atlasScale.x;
                    uvs[3].y=voxelType.UVOffsetRight.y;
                }else if(dir=='t'){
                    uvs[0].x=voxelType.UVOffsetTop.x;
                    uvs[0].y=voxelType.UVOffsetTop.y;

                    uvs[1].x=voxelType.UVOffsetTop.x;
                    uvs[1].y=voxelType.UVOffsetTop.y + voxelType.atlasScale.y;

                    uvs[2].x=voxelType.UVOffsetTop.x + voxelType.atlasScale.x;
                    uvs[2].y=voxelType.UVOffsetTop.y + voxelType.atlasScale.y;

                    uvs[3].x=voxelType.UVOffsetTop.x + voxelType.atlasScale.x;
                    uvs[3].y=voxelType.UVOffsetTop.y;
                }else if(dir=='b'){
                    uvs[0].x=voxelType.UVOffsetBottom.x;
                    uvs[0].y=voxelType.UVOffsetBottom.y;

                    uvs[1].x=voxelType.UVOffsetBottom.x;
                    uvs[1].y=voxelType.UVOffsetBottom.y + voxelType.atlasScale.y;

                    uvs[2].x=voxelType.UVOffsetBottom.x + voxelType.atlasScale.x;
                    uvs[2].y=voxelType.UVOffsetBottom.y + voxelType.atlasScale.y;

                    uvs[3].x=voxelType.UVOffsetBottom.x + voxelType.atlasScale.x;
                    uvs[3].y=voxelType.UVOffsetBottom.y;
                }
            }
        }

        //Uncomment this if you want to use the offset from above
        //bl
        uvs[0].x += ONE_PIXEL;
        uvs[0].y += ONE_PIXEL;

        //tl
        uvs[1].x += ONE_PIXEL;
        uvs[1].y -= ONE_PIXEL;

        //tr
        uvs[2].x -= ONE_PIXEL;
        uvs[2].y -= ONE_PIXEL;

        //br
        uvs[3].x -= ONE_PIXEL;
        uvs[3].y += ONE_PIXEL;

        return uvs;
    }