Example #1
0
        public void Generate()
        {
            Debug.Log("Generating VoxelTilemap with [" + textures.Count + "] Voxels.");
            if (VoxelTilemap)
            {
#if UNITY_EDITOR
                // destroy
                UnityEditor.AssetDatabase.RemoveObjectFromAsset(VoxelTilemap);
                // destroy?
#endif
            }
            texturesInput = new List <Texture2D>();
            largestSize   = new float2();
            for (int i = 0; i < textures.Count; i++)
            {
                AddTexture(textures[i].texture as Texture2D);
            }
            for (int i = 0; i < texturesRaw.Count; i++)
            {
                AddTexture(texturesRaw[i]);
            }
            horizontalCount = textures.Count;   // get highest power of two (ceil)
            float textureWidth  = 1 / (float)(horizontalCount);
            float textureHeight = 1 / (float)(verticalCount);
            // for all textures set horizontalCount, verticalCount
            TilemapGenerator generator = new TilemapGenerator(horizontalCount, verticalCount, (int)largestSize.x, (int)largestSize.y);
            VoxelTilemap      = generator.CreateVoxelTilemap(texturesInput);
            VoxelTilemap.name = name + "_baked";
#if UNITY_EDITOR
            UnityEditor.AssetDatabase.AddObjectToAsset(VoxelTilemap, this);
            UnityEditor.AssetDatabase.SaveAssets();
#endif
            texturesInput.Clear();
        }
Example #2
0
        public void Generate()
        {
            Debug.Log("Generating VoxelTilemap with [" + voxels.Count + "] Voxels.");
            for (int i = 0; i < tilemaps.Count; i++)
            {
#if UNITY_EDITOR
                // destroy
                UnityEditor.AssetDatabase.RemoveObjectFromAsset(tilemaps[i]);
                // destroy?
#endif
            }
            tilemaps.Clear();
            textures         = new List <Texture2D>();
            largestSize      = new float2();
            texturePositions = new List <float2>();
            for (int i = 0; i < voxels.Count; i++)
            {
                //AddTexture(voxels[i].texture.texture as Texture2D);
                for (int j = 0; j < voxels[i].textures.Count; j++)
                {
                    AddTexture(voxels[i].textures[j].texture as Texture2D);
                }
            }
            horizontalCount = textures.Count;   // get highest power of two (ceil)
            float  textureWidth  = 1 / (float)(horizontalCount);
            float  textureHeight = 1 / (float)(verticalCount);
            float2 textureSize   = new float2(textureWidth, textureHeight);
            // for all textures, get size
            for (int i = 0; i < textures.Count; i++)
            {
                int    positionX = i % horizontalCount;
                int    positionY = i / horizontalCount;
                float2 position  = new float2(((float)positionX) * textureWidth, ((float)positionY) * textureHeight);
                texturePositions.Add(position);
            }
            //int sideCount = 0;
            for (int i = 0; i < voxels.Count; i++)
            {
                //  intiate new UV Map
                voxels[i].InitializeCubeMap();
                for (int j = 0; j < voxels[i].textures.Count; j++)
                {
                    int    textureIndex = textures.IndexOf((voxels[i].textures[j].texture as Texture2D));
                    float2 position     = texturePositions[textureIndex];
                    if (voxels[i].textureSides[j] == VoxelSide.Default)
                    {
                        for (int k = 1; k <= 6; k++)
                        {
                            voxels[i].uvMap.SetSide(textureSize, position, ((VoxelSide)k));
                        }
                    }
                    else
                    {
                        voxels[i].uvMap.SetSide(textureSize, position, voxels[i].textureSides[j]);
                    }
                }
#if UNITY_EDITOR
                UnityEditor.EditorUtility.SetDirty(voxels[i]);
#endif
            }
            // for all textures set horizontalCount, verticalCount
            TilemapGenerator generator = new TilemapGenerator(horizontalCount, verticalCount, (int)largestSize.x, (int)largestSize.y);
            // this should be done per material!
            tilemaps.Add(generator.CreateVoxelTilemap(textures));
            for (int i = 0; i < tilemaps.Count; i++)
            {
                tilemaps[i].name = name + "_baked";
                materials[i].SetTexture("_BaseMap", tilemaps[i]);
#if UNITY_EDITOR
                UnityEditor.AssetDatabase.AddObjectToAsset(tilemaps[i], this);
                UnityEditor.AssetDatabase.SaveAssets();
#endif
            }
            textures.Clear();
            texturePositions.Clear();
        }