completely overhaul this code by removing special block… · The Chat Archive
Explore / Programming completely overhaul this code by removing special block arrays, and dont store textures… Save Share Download ▾ Reportcompletely overhaul this code by removing special block arrays, and dont store textures locally. fetch textures and custom block models and other info from https://github.com/PrismarineJS/minecraft-assets/tree/master/data/1.20.2 . textures are stored in https://github.com/PrismarineJS/minecraft-assets/tree/master/data/1.20.2/blocks . block texture mappings are in https://github.com/PrismarineJS/minecraft-assets/blob/master/data/1.20.2/blocks_textures.json . Block models/directional texture mappings are in https://github.com/PrismarineJS/minecraft-assets/blob/master/data/1.20.2/blocks_models.json . give the full updated and compacted code
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json;
using System;
using PimDeWitte.UnityMainThreadDispatcher;
using System.Collections;
public class BlockFetcher : MonoBehaviour
{
private string serverUrl = "http://localhost:8000/blocks ";
public string texturePath = "Assets/Textures";
public Material blockMaterial;
public string defaultTextureName = "missing_texture";
private const int MAX_VERTICES_PER_CHUNK = 65000;
private List meshChunks = new();
public string[] woodTypes;
public Mesh slabMesh, fenceMesh, stairMesh;
private class MeshChunk
{
public List vertices = new(MAX_VERTICES_PER_CHUNK);
public List triangles = new();
public List uvs = new();
public bool isFull => vertices.Count + 4 >= MAX_VERTICES_PER_CHUNK;
}
[Serializable]
public class BlockTextureConfig
{
public string blockName, top, bottom, front, back, left, right, side;
public bool isBillboard;
public Mesh customMesh;
public void SetDefaults(string defaultName)
{
top ??= defaultName;
bottom ??= defaultName;
if (!string.IsNullOrEmpty(side))
front = back = left = right = side;
else
{
front ??= defaultName;
back ??= defaultName;
left ??= defaultName;
right ??= defaultName;
}
}
}
[Serializable]
public class BlockTintConfig
{
public string blockName;
public Color tintColor;
public string maskName;
public BlockTintConfig(string blockName, Color tintColor, string maskName = null)
{
this.blockName = blockName;
this.tintColor = tintColor;
this.maskName = maskName;
}
}
public BlockTextureConfig[] blockConfigs;
public BlockTintConfig[] blockTintConfigs;
Assistant2024-11-10 08:58 UTC Copy response
Copy first prompt
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date Nov 10, 2024
Added to archive Nov 16, 2026
ID 01m7h427a8fd8j1tnnfes6z4nzSource & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: c92708665cd86b0b10646e756c2000de
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
private Texture2D textureAtlas;
private Rect[] atlasRects;
private Dictionary<string, int> blockNameToTextureIndex = new(StringComparer.OrdinalIgnoreCase);
private Dictionary<string, BlockTextureConfig> blockConfigMap = new(StringComparer.OrdinalIgnoreCase);
private int defaultTextureIndex;
private Dictionary<int, Vector2[]> uvCache = new();
[Serializable]
public class BlockData
{
public string block_name;
public int x, y, z;
public string direction;
}
private static readonly Vector3[,] faceVertices = {
{ new(-0.5f, -0.5f, -0.5f), new(0.5f, -0.5f, -0.5f), new(0.5f, 0.5f, -0.5f), new(-0.5f, 0.5f, -0.5f) },
{ new(-0.5f, -0.5f, 0.5f), new(0.5f, -0.5f, 0.5f), new(0.5f, 0.5f, 0.5f), new(-0.5f, 0.5f, 0.5f) },
{ new(-0.5f, 0.5f, -0.5f), new(0.5f, 0.5f, -0.5f), new(0.5f, 0.5f, 0.5f), new(-0.5f, 0.5f, 0.5f) },
{ new(-0.5f, -0.5f, -0.5f), new(0.5f, -0.5f, -0.5f), new(0.5f, -0.5f, 0.5f), new(-0.5f, -0.5f, 0.5f) },
{ new(-0.5f, -0.5f, -0.5f), new(-0.5f, -0.5f, 0.5f), new(-0.5f, 0.5f, 0.5f), new(-0.5f, 0.5f, -0.5f) },
{ new(0.5f, -0.5f, -0.5f), new(0.5f, -0.5f, 0.5f), new(0.5f, 0.5f, 0.5f), new(0.5f, 0.5f, -0.5f) }
};
private static readonly Vector3[][] billboardVertices = {
new[] { new Vector3(-0.5f, -0.5f, -0.5f), new(0.5f, -0.5f, 0.5f), new(0.5f, 0.5f, 0.5f), new(-0.5f, 0.5f, -0.5f) },
new[] { new Vector3(-0.5f, -0.5f, 0.5f), new(0.5f, -0.5f, -0.5f), new(0.5f, 0.5f, -0.5f), new(-0.5f, 0.5f, 0.5f) }
};
void Start()
{
InitializeBlockConfigs();
BeginInitialization();
}
async void BeginInitialization()
{
await CreateTextureAtlasAsync();
await FetchAndProcessBlocksAsync();
}
private void InitializeBlockConfigs()
{
blockConfigMap.Clear();
foreach (var config in blockConfigs)
{
config.SetDefaults(defaultTextureName); // Use default texture name
blockConfigMap[config.blockName] = config;
}
}
private async Task CreateTextureAtlasAsync()
{
var maskTextures = LoadMaskTextures();
// Load all textures on the main thread to avoid Unity exceptions
var textureList = await LoadAllTexturesAsync(maskTextures);
textureAtlas = new Texture2D(2048, 2048, TextureFormat.RGBA32, false) { filterMode = FilterMode.Point };
atlasRects = textureAtlas.PackTextures(textureList.ToArray(), 0, 2048);
textureAtlas.Apply(false, true);
blockMaterial.mainTexture = textureAtlas;
}
private Dictionary<string, Texture2D> LoadMaskTextures()
{
var maskTextures = new Dictionary<string, Texture2D>(StringComparer.OrdinalIgnoreCase);
string maskPath = Path.Combine(texturePath, "Masks");
if (!Directory.Exists(maskPath)) return maskTextures;
foreach (string maskFile in Directory.GetFiles(maskPath, "*.png"))
{
string maskName = Path.GetFileNameWithoutExtension(maskFile);
Texture2D maskTexture = new Texture2D(2, 2, TextureFormat.RGBA32, false) { filterMode = FilterMode.Point };
maskTexture.LoadImage(File.ReadAllBytes(maskFile));
maskTextures[maskName] = maskTexture;
}
return maskTextures;
}
private async Task LoadAllTexturesAsync(Dictionary<string, Texture2D> maskTextures)
{
List texturesList = new();
string[] textureFiles = Directory.GetFiles(texturePath, "*.png");
// Load default texture
Texture2D defaultTexture = await LoadDefaultTextureAsync();
texturesList.Add(defaultTexture);
blockNameToTextureIndex[defaultTextureName] = 0;
defaultTextureIndex = 0;
int textureIndex = 1;
foreach (string textureFile in textureFiles)
{
string fileName = Path.GetFileNameWithoutExtension(textureFile);
if (fileName.Equals(defaultTextureName, StringComparison.OrdinalIgnoreCase)) continue;
Texture2D texture = await LoadAndProcessTextureAsync(textureFile, fileName, maskTextures);
texturesList.Add(texture);
blockNameToTextureIndex[fileName] = textureIndex++;
}
private async Task LoadDefaultTextureAsync()
{
string defaultTexturePath = Path.Combine(texturePath, defaultTextureName + ".png");
if (File.Exists(defaultTexturePath))
{
byte[] data = await ReadFileAsync(defaultTexturePath);
return await LoadTextureOnMainThreadAsync(data);
}
else
{
Debug.LogError($"Default texture {defaultTexturePath} not found! Creating fallback texture.");
return CreateFallbackTexture();
}
}
private async Task LoadAndProcessTextureAsync(string textureFile, string textureName, Dictionary<string, Texture2D> maskTextures)
{
byte[] fileData = await ReadFileAsync(textureFile);
// Load texture on main thread to avoid Unity exceptions
Texture2D texture = await LoadTextureOnMainThreadAsync(fileData);
var tintConfig = blockTintConfigs.FirstOrDefault(c => c.blockName.Equals(textureName, StringComparison.OrdinalIgnoreCase));
if (tintConfig != null)
{
ApplyTintToTexture(texture, tintConfig, maskTextures);
}
return texture;
}
private async Task LoadTextureOnMainThreadAsync(byte[] data)
{
TaskCompletionSource tcs = new();
UnityMainThreadDispatcher.Instance().Enqueue(() =>
{
Texture2D texture = new Texture2D(2, 2, TextureFormat.RGBA32, false) { filterMode = FilterMode.Point };
texture.LoadImage(data);
tcs.SetResult(texture);
});
return await tcs.Task;
}
private void ApplyTintToTexture(Texture2D texture, BlockTintConfig config, Dictionary<string, Texture2D> maskTextures)
{
Texture2D maskTexture = null;
if (!string.IsNullOrEmpty(config.maskName))
{
maskTextures.TryGetValue(config.maskName, out maskTexture);
}
Color[] pixels = texture.GetPixels();
Color[] maskPixels = maskTexture?.GetPixels();
for (int i = 0; i < pixels.Length; i++)
{
if (maskPixels != null)
{
float maskIntensity = maskPixels[i].grayscale;
pixels[i] = Color.Lerp(pixels[i], pixels[i] * config.tintColor, maskIntensity);
}
else
{
pixels[i] *= config.tintColor;
}
}
texture.SetPixels(pixels);
texture.Apply();
}
private Task<byte[]> ReadFileAsync(string filePath)
{
return Task.Run(() => File.ReadAllBytes(filePath));
}
private Texture2D CreateFallbackTexture()
{
Texture2D texture = new Texture2D(16, 16, TextureFormat.RGBA32, false) { filterMode = FilterMode.Point };
Color[] colors = Enumerable.Range(0, 256).Select(i => (i / 16 + i % 16) % 2 == 0 ? Color.magenta : Color.black).ToArray();
texture.SetPixels(colors);
texture.Apply();
return texture;
}
private async Task FetchAndProcessBlocksAsync()
{
string jsonResponse = await GetBlocksDataAsync();
var blockDataList = JsonConvert.DeserializeObject(jsonResponse);
// Process blocks on the main thread using a coroutine
UnityMainThreadDispatcher.Instance().Enqueue(() => StartCoroutine(ProcessBlocksCoroutine(blockDataList)));
}
private async Task GetBlocksDataAsync()
{
using UnityWebRequest webRequest = UnityWebRequest.Get(serverUrl);
var asyncOp = webRequest.SendWebRequest();
while (!asyncOp.isDone)
await Task.Yield();
if (webRequest.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Error fetching block data: " + webRequest.error);
return null;
}
else
{
return webRequest.downloadHandler.text;
}
}
private IEnumerator ProcessBlocksCoroutine(List blockDataList)
{
int batchSize = 1000; // Adjust as needed
int totalBlocks = blockDataList.Count;
int processedBlocks = 0;
while (processedBlocks < totalBlocks)
{
int blocksToProcess = Mathf.Min(batchSize, totalBlocks - processedBlocks);
for (int i = 0; i < blocksToProcess; i++)
{
AddBlockFaces(blockDataList[processedBlocks + i]);
}
processedBlocks += blocksToProcess;
// Optional: Update a progress indicator here
yield return null; // Wait for the next frame
private void AddBlockFaces(BlockData block)
{
Vector3 blockPosition = new(block.x, block.y, block.z);
if (!blockConfigMap.TryGetValue(block.block_name, out var config))
{
config = new BlockTextureConfig { blockName = block.block_name };
config.SetDefaults(block.block_name); // Use block's own name
}
var customMeshes = new Dictionary<string, Mesh>
{
{ "_slab", slabMesh },
{ "_fence", fenceMesh },
{ "_stairs", stairMesh }
};
Mesh selectedMesh = config.customMesh;
string replace = null, add = null;
if (selectedMesh == null)
{
foreach (var kvp in customMeshes)
{
if (config.blockName.Contains(kvp.Key))
{
selectedMesh = kvp.Value;
replace = kvp.Key;
if (woodTypes.Any(w => config.front.Contains(w)))
add = "_planks";
break;
}
}
}
if (selectedMesh != null)
AddCustomMeshToChunk(blockPosition, selectedMesh, config.front, replace, add);
else if (config.isBillboard)
AddBillboardFacesToChunk(blockPosition, config);
else
AddDefaultCubeFacesToChunk(blockPosition, config);
}
private MeshChunk GetCurrentChunk()
{
if (meshChunks.Count == 0 || meshChunks[^1].isFull)
meshChunks.Add(new MeshChunk());
return meshChunks[^1];
}
private void AddCustomMeshToChunk(Vector3 blockPosition, Mesh customMesh, string textureName, string replace, string add)
{
if (replace != null) textureName = textureName.Replace(replace, "") + add;
var chunk = GetCurrentChunk();
var textureUVs = GetUVsForTexture(textureName);
int vertCount = chunk.vertices.Count;
int uvIndex = 0;
foreach (var vertex in customMesh.vertices)
{
chunk.vertices.Add(vertex + blockPosition);
chunk.uvs.Add(textureUVs[uvIndex % 4]);
uvIndex++;
}
foreach (var triangle in customMesh.triangles)
chunk.triangles.Add(vertCount + triangle);
}
private void AddBillboardFacesToChunk(Vector3 blockPosition, BlockTextureConfig config)
{
var textureUVs = GetUVsForTexture(config.front);
foreach (var plane in billboardVertices)
{
var chunk = GetCurrentChunk();
int vertCount = chunk.vertices.Count;
for (int i = 0; i < 4; i++)
{
chunk.vertices.Add(plane[i] + blockPosition);
chunk.uvs.Add(textureUVs[i]);
}
chunk.triangles.AddRange(new[] { vertCount, vertCount + 2, vertCount + 1, vertCount, vertCount + 3, vertCount + 2 });
}
}
private void AddDefaultCubeFacesToChunk(Vector3 blockPosition, BlockTextureConfig config)
{
string[] faceTextures = { config.front, config.back, config.top, config.bottom, config.left, config.right };
for (int faceIndex = 0; faceIndex < 6; faceIndex++)
{
var chunk = GetCurrentChunk();
var textureUVs = GetUVsForTexture(faceTextures[faceIndex]);
int vertCount = chunk.vertices.Count;
for (int i = 0; i < 4; i++)
{
chunk.vertices.Add(faceVertices[faceIndex, i] + blockPosition);
chunk.uvs.Add(textureUVs[i]);
}
chunk.triangles.AddRange(new[] { vertCount, vertCount + 2, vertCount + 1, vertCount, vertCount + 3, vertCount + 2 });
}
}
private int GetTextureIndexForTextureName(string textureName)
{
if (string.IsNullOrEmpty(textureName))
return defaultTextureIndex;
return blockNameToTextureIndex.TryGetValue(textureName, out int index) ? index : defaultTextureIndex;
}
private Vector2[] GetUVsForTexture(string textureName)
{
int textureIndex = GetTextureIndexForTextureName(textureName);
if (textureIndex >= atlasRects.Length || textureIndex < 0)
{
Debug.LogWarning($"Invalid texture index: {textureIndex} for texture: {textureName}. Using default texture.");
textureIndex = defaultTextureIndex;
}
if (!uvCache.TryGetValue(textureIndex, out Vector2[] uvs))
{
Rect uvRect = atlasRects[textureIndex];
uvs = new[]
{
new Vector2(uvRect.xMin, uvRect.yMin),
new Vector2(uvRect.xMax, uvRect.yMin),
new Vector2(uvRect.xMax, uvRect.yMax),
new Vector2(uvRect.xMin, uvRect.yMax)
};
uvCache[textureIndex] = uvs;
}
return uvs;
}
private void CreateMesh()
{
GameObject chunksParent = new("ChunkedMeshes");
for (int i = 0; i < meshChunks.Count; i++)
{
var chunk = meshChunks[i];
GameObject chunkObject = new($"MeshChunk_{i}") { transform = { parent = chunksParent.transform } };
Mesh mesh = new() { indexFormat = UnityEngine.Rendering.IndexFormat.UInt32 };
mesh.SetVertices(chunk.vertices);
mesh.SetTriangles(chunk.triangles, 0);
mesh.SetUVs(0, chunk.uvs);
mesh.RecalculateNormals();
chunkObject.AddComponent().mesh = mesh;
chunkObject.AddComponent().material = blockMaterial;
chunkObject.AddComponent().sharedMesh = mesh;
}
}
}
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json;
using System.Collections;
using System.Linq;
using System;
public class BlockFetcher : MonoBehaviour
{
private string serverUrl = "http://localhost:8000/blocks";
public Material blockMaterial;
private const int MAX_VERTICES_PER_CHUNK = 65000;
private List<MeshChunk> meshChunks = new List<MeshChunk>();
private Texture2D textureAtlas;
private Rect[] atlasRects;
private Dictionary<string, int[]> blockNameToFaceTextureIndices;
private int defaultTextureIndex;
private Dictionary<int, Vector2[]> uvCache = new Dictionary<int, Vector2[]>();
[Serializable]
public class BlockData
{
public string block_name;
public int x, y, z;
public string direction;
}
private static readonly Vector3[,] faceVertices = {
{ new Vector3(-0.5f, -0.5f, -0.5f), new(0.5f, -0.5f, -0.5f), new(0.5f, 0.5f, -0.5f), new(-0.5f, 0.5f, -0.5f) }, // Front (-Z)
{ new Vector3(-0.5f, -0.5f, 0.5f), new(0.5f, -0.5f, 0.5f), new(0.5f, 0.5f, 0.5f), new(-0.5f, 0.5f, 0.5f) }, // Back (+Z)
{ new Vector3(-0.5f, 0.5f, -0.5f), new(0.5f, 0.5f, -0.5f), new(0.5f, 0.5f, 0.5f), new(-0.5f, 0.5f, 0.5f) }, // Top (+Y)
{ new Vector3(-0.5f, -0.5f, -0.5f), new(0.5f, -0.5f, -0.5f), new(0.5f, -0.5f, 0.5f), new(-0.5f, -0.5f, 0.5f) }, // Bottom (-Y)
{ new Vector3(-0.5f, -0.5f, -0.5f), new(-0.5f, -0.5f, 0.5f), new(-0.5f, 0.5f, 0.5f), new(-0.5f, 0.5f, -0.5f) }, // Left (-X)
{ new Vector3(0.5f, -0.5f, -0.5f), new(0.5f, -0.5f, 0.5f), new(0.5f, 0.5f, 0.5f), new(0.5f, 0.5f, -0.5f) } // Right (+X)
};
private Dictionary<string, BlockTextureData> blockTextures;
private Dictionary<string, BlockModelData> blockModels;
void Start()
{
BeginInitialization();
}
async void BeginInitialization()
{
await FetchAndParseAssetDataAsync();
await CreateTextureAtlasAsync();
await FetchAndProcessBlocksAsync();
}
async Task FetchAndParseAssetDataAsync()
{
string blocksTexturesJsonUrl = "https://raw.githubusercontent.com/PrismarineJS/minecraft-assets/master/data/1.20.2/blocks_textures.json";
string blocksModelsJsonUrl = "https://raw.githubusercontent.com/PrismarineJS/minecraft-assets/master/data/1.20.2/blocks_models.json";
string blocksTexturesJson = await FetchJsonFromUrl(blocksTexturesJsonUrl);
string blocksModelsJson = await FetchJsonFromUrl(blocksModelsJsonUrl);
blockTextures = JsonConvert.DeserializeObject<Dictionary<string, BlockTextureData>>(blocksTexturesJson);
blockModels = JsonConvert.DeserializeObject<Dictionary<string, BlockModelData>>(blocksModelsJson);
}
async Task<string> FetchJsonFromUrl(string url)
{
using UnityWebRequest webRequest = UnityWebRequest.Get(url);
var asyncOp = webRequest.SendWebRequest();
while (!asyncOp.isDone)
await Task.Yield();
if (webRequest.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Error fetching data: " + webRequest.error);
return null;
}
else
{
return webRequest.downloadHandler.text;
}
}
public class BlockTextureData
{
public Dictionary<string, string> textures;
}
public class BlockModelData
{
public string parent;
public Dictionary<string, string> textures;
// Additional fields can be added if necessary
}
private async Task CreateTextureAtlasAsync()
{
// Collect all texture paths
HashSet<string> texturePaths = new HashSet<string>();
foreach (var blockTextureEntry in blockTextures.Values)
{
if (blockTextureEntry.textures != null)
{
foreach (var texturePath in blockTextureEntry.textures.Values)
{
texturePaths.Add(texturePath);
}
}
}
// Map default texture
string defaultTexturePath = "blocks/missing_texture";
texturePaths.Add(defaultTexturePath);
// Create a list to hold the textures
List<Texture2D> textureList = new List<Texture2D>();
// Map texturePath to index
Dictionary<string, int> texturePathToIndex = new Dictionary<string, int>();
// Add default texture first
Texture2D defaultTexture = CreateFallbackTexture();
textureList.Add(defaultTexture);
texturePathToIndex[defaultTexturePath] = 0;
defaultTextureIndex = 0;
int textureIndex = 1;
foreach (var texturePath in texturePaths)
{
if (texturePath == defaultTexturePath)
continue;
Texture2D texture = await FetchTextureAsync(texturePath);
if (texture != null)
{
textureList.Add(texture);
texturePathToIndex[texturePath] = textureIndex++;
}
else
{
Debug.LogWarning($"Failed to load texture: {texturePath}. Using default texture.");
textureList.Add(defaultTexture);
texturePathToIndex[texturePath] = defaultTextureIndex;
}
}
// Create texture atlas
textureAtlas = new Texture2D(2048, 2048, TextureFormat.RGBA32, false) { filterMode = FilterMode.Point };
atlasRects = textureAtlas.PackTextures(textureList.ToArray(), 0, 2048, false);
textureAtlas.Apply(false, true);
blockMaterial.mainTexture = textureAtlas;
// Map block name to texture indices per face
blockNameToFaceTextureIndices = new Dictionary<string, int[]>();
string[] faceIndexToFaceName = new string[6] { "north", "south", "up", "down", "west", "east" };
foreach (var blockName in blockTextures.Keys)
{
var blockTextureData = blockTextures[blockName];
var textures = blockTextureData.textures;
int[] faceTextureIndices = new int[6];
for (int faceIndex = 0; faceIndex < 6; faceIndex++)
{
string faceName = faceIndexToFaceName[faceIndex];
string textureKey = null;
if (textures != null)
{
if (textures.ContainsKey(faceName))
textureKey = textures[faceName];
else if (textures.ContainsKey("side") && (faceName == "north" || faceName == "south" || faceName == "east" || faceName == "west"))
textureKey = textures["side"];
else if (textures.ContainsKey("all"))
textureKey = textures["all"];
else if (textures.ContainsKey("texture")) // For some models, texture may be specified as "texture"
textureKey = textures["texture"];
}
if (textureKey != null)
{
if (texturePathToIndex.TryGetValue(textureKey, out int texIndex))
{
faceTextureIndices[faceIndex] = texIndex;
}
else
{
faceTextureIndices[faceIndex] = defaultTextureIndex;
Debug.LogWarning($"Texture path not found in texturePathToIndex: {textureKey}");
}
}
else
{
faceTextureIndices[faceIndex] = defaultTextureIndex;
}
}
blockNameToFaceTextureIndices[blockName] = faceTextureIndices;
}
}
async Task<Texture2D> FetchTextureAsync(string texturePath)
{
string baseUrl = "https://raw.githubusercontent.com/PrismarineJS/minecraft-assets/master/data/1.20.2/";
string textureUrl = baseUrl + texturePath + ".png";
using UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(textureUrl);
var asyncOp = webRequest.SendWebRequest();
while (!asyncOp.isDone)
await Task.Yield();
if (webRequest.result != UnityWebRequest.Result.Success)
{
Debug.LogError($"Error fetching texture {texturePath}: {webRequest.error}");
return null;
}
else
{
Texture2D texture = DownloadHandlerTexture.GetContent(webRequest);
texture.filterMode = FilterMode.Point;
return texture;
}
}
private Texture2D CreateFallbackTexture()
{
Texture2D texture = new Texture2D(16, 16, TextureFormat.RGBA32, false) { filterMode = FilterMode.Point };
Color[] colors = Enumerable.Range(0, 256).Select(i => (i / 16 + i % 16) % 2 == 0 ? Color.magenta : Color.black).ToArray();
texture.SetPixels(colors);
texture.Apply();
return texture;
}
private async Task FetchAndProcessBlocksAsync()
{
string jsonResponse = await GetBlocksDataAsync();
var blockDataList = JsonConvert.DeserializeObject<List<BlockData>>(jsonResponse);
StartCoroutine(ProcessBlocksCoroutine(blockDataList));
}
private async Task<string> GetBlocksDataAsync()
{
using UnityWebRequest webRequest = UnityWebRequest.Get(serverUrl);
var asyncOp = webRequest.SendWebRequest();
while (!asyncOp.isDone)
await Task.Yield();
if (webRequest.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Error fetching block data: " + webRequest.error);
return null;
}
else
{
return webRequest.downloadHandler.text;
}
}
private IEnumerator ProcessBlocksCoroutine(List<BlockData> blockDataList)
{
int batchSize = 1000; // Adjust as needed
int totalBlocks = blockDataList.Count;
int processedBlocks = 0;
while (processedBlocks < totalBlocks)
{
int blocksToProcess = Mathf.Min(batchSize, totalBlocks - processedBlocks);
for (int i = 0; i < blocksToProcess; i++)
{
AddBlockFaces(blockDataList[processedBlocks + i]);
}
processedBlocks += blocksToProcess;
// Optional: Update a progress indicator here
yield return null; // Wait for the next frame
}
CreateMesh();
}
private void AddBlockFaces(BlockData block)
{
Vector3 blockPosition = new Vector3(block.x, block.y, block.z);
AddDefaultCubeFacesToChunk(blockPosition, block.block_name);
}
private void AddDefaultCubeFacesToChunk(Vector3 blockPosition, string blockName)
{
if (!blockNameToFaceTextureIndices.TryGetValue(blockName, out int[] faceTextureIndices))
{
faceTextureIndices = new int[6];
for (int i = 0; i < 6; i++) faceTextureIndices[i] = defaultTextureIndex;
}
for (int faceIndex = 0; faceIndex < 6; faceIndex++)
{
var chunk = GetCurrentChunk();
int textureIndex = faceTextureIndices[faceIndex];
var textureUVs = GetUVsForTextureIndex(textureIndex);
int vertCount = chunk.vertices.Count;
for (int i = 0; i < 4; i++)
{
chunk.vertices.Add(faceVertices[faceIndex, i] + blockPosition);
chunk.uvs.Add(textureUVs[i]);
}
chunk.triangles.AddRange(new[] { vertCount, vertCount + 2, vertCount + 1, vertCount, vertCount + 3, vertCount + 2 });
}
}
private Vector2[] GetUVsForTextureIndex(int textureIndex)
{
if (textureIndex >= atlasRects.Length || textureIndex < 0)
{
Debug.LogWarning($"Invalid texture index: {textureIndex}. Using default texture.");
textureIndex = defaultTextureIndex;
}
if (!uvCache.TryGetValue(textureIndex, out Vector2[] uvs))
{
Rect uvRect = atlasRects[textureIndex];
uvs = new[]
{
new Vector2(uvRect.xMin, uvRect.yMin),
new Vector2(uvRect.xMax, uvRect.yMin),
new Vector2(uvRect.xMax, uvRect.yMax),
new Vector2(uvRect.xMin, uvRect.yMax)
};
uvCache[textureIndex] = uvs;
}
return uvs;
}
private void CreateMesh()
{
GameObject chunksParent = new GameObject("ChunkedMeshes");
for (int i = 0; i < meshChunks.Count; i++)
{
var chunk = meshChunks[i];
GameObject chunkObject = new GameObject($"MeshChunk_{i}") { transform = { parent = chunksParent.transform } };
Mesh mesh = new Mesh() { indexFormat = UnityEngine.Rendering.IndexFormat.UInt32 };
mesh.SetVertices(chunk.vertices);
mesh.SetTriangles(chunk.triangles, 0);
mesh.SetUVs(0, chunk.uvs);
mesh.RecalculateNormals();
var meshFilter = chunkObject.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
var meshRenderer = chunkObject.AddComponent<MeshRenderer>();
meshRenderer.material = blockMaterial;
chunkObject.AddComponent<MeshCollider>().sharedMesh = mesh;
}
}
private class MeshChunk
{
public List<Vector3> vertices = new List<Vector3>(MAX_VERTICES_PER_CHUNK);
public List<int> triangles = new List<int>();
public List<Vector2> uvs = new List<Vector2>();
public bool isFull => vertices.Count + 4 >= MAX_VERTICES_PER_CHUNK;
}
private MeshChunk GetCurrentChunk()
{
if (meshChunks.Count == 0 || meshChunks[meshChunks.Count - 1].isFull)
meshChunks.Add(new MeshChunk());
return meshChunks[meshChunks.Count - 1];
}
}
Copy first response
Not yet included in a published release.