Last active
October 10, 2019 07:13
-
-
Save unitycoder/e0bb32b9d6ece0b4bde7be270e2e2a6f to your computer and use it in GitHub Desktop.
Read Minecraft Worlds into Unity - Using Substrate.dll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Unity Substrate example | |
// https://unitycoder.com/blog/2019/10/05/reading-minecraft-world-into-unity-(using-substrate) | |
// reads chunks and spawns particles as blocks | |
using UnityEngine; | |
using Substrate; | |
using Vector3 = UnityEngine.Vector3; | |
public class MinecraftReader : MonoBehaviour | |
{ | |
// adjust this to read more chunks (but gets too slow eventually) | |
int maxChunksToRead = 3; | |
public ParticleSystem ps; | |
void Start() | |
{ | |
// enter your username, minecraft world name to this full path | |
NbtWorld world = NbtWorld.Open(@"C:\Users\YOURNAME\AppData\Roaming\.minecraft\saves\myworld\"); | |
//Debug.Log("world=" + world); | |
int chunkCount = 0; | |
int chunkWidth = 16; // used to calculate chunk world offsets | |
// iterate chunks | |
foreach (ChunkRef chunk in world.GetChunkManager()) | |
{ | |
chunkCount++; | |
// just to keep unity alive while we use particles.. | |
if (chunkCount > maxChunksToRead) break; | |
// sometimes null blocks? | |
if (chunk.Blocks == null) continue; | |
// get chunk size | |
int xDim = chunk.Blocks.XDim; | |
int yDim = chunk.Blocks.YDim; | |
int zDim = chunk.Blocks.ZDim; | |
// get world position for the chunk | |
var chunkRef = chunk.GetChunkRef(); | |
int cx = chunkRef.X * chunkWidth; | |
int cz = chunkRef.Z * chunkWidth; | |
// iterate blocks from this chunk | |
for (int y = 0; y < yDim; y++) | |
{ | |
for (int x = 0; x < xDim; x++) | |
{ | |
for (int z = 0; z < zDim; z++) | |
{ | |
// get single block | |
AlphaBlock block = chunk.Blocks.GetBlock(x, y, z); | |
// get color based on id | |
byte alpha = 255; | |
byte r = (byte)block.ID; | |
byte g = (byte)block.ID; | |
byte b = (byte)block.ID; | |
// TODO should make lookup table for colors matching the block.. | |
// air, skip | |
if (block.ID == 0) continue; | |
// water | |
if (block.ID == 9) { r = 0; g = 84; b = 255; alpha = 128; }; // NOTE alpha not supported in material | |
// cobblestone | |
if (block.ID == 4) { r = 88; g = 88; b = 88; }; | |
// stone | |
if (block.ID == 1) { r = 77; g = 77; b = 77; }; | |
// grass | |
if (block.ID == 2) { r = 96; g = 144; b = 34; }; | |
// log | |
if (block.ID == 17) { r = 90; g = 53; b = 11; }; | |
// dirt | |
if (block.ID == 3) { r = 106; g = 68; b = 18; }; | |
// leaves | |
if (block.ID == 18) { r = 8; g = 161; b = 0; }; | |
// create particle on that position | |
var pars = new ParticleSystem.EmitParams(); | |
pars.position = new Vector3(x + cx, y, z + cz); | |
pars.startColor = new Color32(r, g, b, alpha); | |
ps.Emit(pars, 1); | |
} | |
} | |
} | |
} | |
Debug.Log("Chunks read=" + chunkCount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment