Skip to content

Commit

Permalink
add texture atlas
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed Apr 9, 2020
1 parent 156c063 commit 857f857
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 197 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.mono
.import
.idea
testserver
testserver
packages
5 changes: 3 additions & 2 deletions Godotcraft.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@
<Compile Include="scripts\world\ChunkData.cs" />
<Compile Include="scripts\world\ChunkSection.cs" />
<Compile Include="scripts\world\WorldGen.cs" />
<Compile Include="test\MeshInstance.cs" />
<Compile Include="test\MeshInstanceV2.cs" />
<Compile Include="test\ChunkRenderer.cs" />
<Compile Include="test\TextureAtlas.cs" />
<Compile Include="test\UVMap.cs" />
</ItemGroup>
<ItemGroup>
<Content Include=".gitignore" />
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

Minecraft, but using the godot engine

# Assets

If you want to run it yourself, you need to download the minecraft assets and put them into the mcassets folder into the user folder.
Its located at `C:\Users\Martin\AppData\Roaming\Godot\app_userdata\Godotcraft` for me.
InventiveTalent has a repo with all the stuff: https://api.github.com/repos/InventivetalentDev/minecraft-assets/zipball/1.15.2

# Status

08.04.2002: Random experiments with voxel rendering, including trying to do culling
09.04.2020: More rendering, better culling, texture atlas!
08.04.2020: Random experiments with voxel rendering, including trying to do culling
04.04.2020: first pass on adding a console/chat view, had to rewrite stuff to c#...
03.04.2020: reworked the server list, its actually working now
02.04.2020: worked on a better protocol impl the last few days, got joining working, chat working, no real ui yet
Expand Down
6 changes: 3 additions & 3 deletions test/Camera_Pivot.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extends Spatial
# The percentage of the screen that will move the camera when the mouse is over it.
const SCREEN_AMOUNT = 0.1;
# The speed the camera will rotate at.
const ROTATION_SPEED = 2;
const ROTATION_SPEED = 1.7;

func _ready():
pass
Expand All @@ -20,10 +20,10 @@ func _process(delta):
# the percentage of the screen that will move the camera on the left side.
if (mouse_position.x < amount_to_look_for):
# Rotate the camera.
rotate_y(-ROTATION_SPEED * delta);
rotate_y(ROTATION_SPEED * delta);

# If the mouse is more than screen_width minus amount_to_look_for, then the mouse
# most be over the percentage of the screen that will move the camera on the right side.
elif (mouse_position.x > screen_width - amount_to_look_for):
# Rotate the camera.
rotate_y(ROTATION_SPEED * delta);
rotate_y(-ROTATION_SPEED * delta);
4 changes: 3 additions & 1 deletion test/MeshInstanceV2.cs → test/ChunkRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
using Godotcraft.scripts.world;

namespace Godotcraft.test {
public class MeshInstanceV2 : Godot.MeshInstance {
public class ChunkRenderer : Godot.MeshInstance {
const float CUBE_SIZE = 0.5f;

private readonly SurfaceTool tool = new SurfaceTool();

public override void _Ready() {
VisualServer.SetDebugGenerateWireframes(true);
ChunkData chunkData = new ChunkData();
TextureAtlas.instance.atlas.ToString();
// MaterialOverride = new SpatialMaterial {AlbedoTexture = TextureAtlas.instance.atlas};
Mesh = createMesh(chunkData.getSection(0));
}

Expand Down
188 changes: 0 additions & 188 deletions test/MeshInstance.cs

This file was deleted.

2 changes: 1 addition & 1 deletion test/Testing_Scene.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[gd_scene load_steps=7 format=2]

[ext_resource path="res://test/Camera_Pivot.gd" type="Script" id=1]
[ext_resource path="res://test/MeshInstanceV2.cs" type="Script" id=3]
[ext_resource path="res://test/ChunkRenderer.cs" type="Script" id=3]

[sub_resource type="SpatialMaterial" id=1]
vertex_color_use_as_albedo = true
Expand Down
104 changes: 104 additions & 0 deletions test/TextureAtlas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using Godot;
using File = System.IO.File;

namespace Godotcraft.test {
public class TextureAtlas {
public static readonly TextureAtlas instance = new TextureAtlas();

public AtlasTexture atlas { get; }

public TextureAtlas() {
atlas = createAtlas();
}

private AtlasTexture createAtlas() {
string texturePath = "user://mcassets/assets/minecraft/textures/block/";
Directory textureDir = new Directory();
textureDir.Open(texturePath);

List<string> images = getImages(textureDir);
GD.Print($"found {images.Count} images");

int pixelWidth = 16;
int pixelHeight = 16;
int atlasWidth = Mathf.CeilToInt((Mathf.Sqrt(images.Count) + 1) * pixelWidth);
int atlasHeight = Mathf.CeilToInt((Mathf.Sqrt(images.Count) + 1) * pixelHeight);

Image texture = new Image();
texture.Create(atlasWidth, atlasHeight, false, Image.Format.Rgba8);
int count = 0;

for (int x = 0; x < atlasWidth / pixelHeight; x++) {
for (int y = 0; y < atlasHeight / pixelHeight; y++) {
if (count >= images.Count) {
goto end;
}

Image img = new Image();
img.Load(texturePath + images[count]);

for (var i = 0; i < pixelWidth; i++) {
for (var j = 0; j < pixelHeight; j++) {
texture.Lock();
img.Lock();
texture.SetPixel(x * pixelWidth + i, y * pixelHeight + j, img.GetPixel(i, j));
}
}

float startX = x * pixelWidth;
float startY = y * pixelHeight;
float perPixelRatioX = 1f / texture.GetWidth();
float perPixelRatioY = 1f / texture.GetHeight();
startX *= perPixelRatioX;
startY *= perPixelRatioY;
// float endX = startX + (perPixelRatioX * pixelWidth);
// float endY = startY + (perPixelRatioY * pixelHeight);
float endX = startX + pixelWidth;
float endY = startY + pixelHeight;

// new UVMap(images[count].Replace(".png", ""), new[] {
// new Vector2(startX, startY),
// new Vector2(startX, endY),
// new Vector2(endX, startY),
// new Vector2(endX, endY),
// }).register();
new UVMap(images[count].Replace(".png", ""), new[] {
new Vector2(perPixelRatioX, perPixelRatioY),
new Vector2(endX, endY)
}).register();

count++;
}
}

end:

ImageTexture imageTexture = new ImageTexture();
imageTexture.CreateFromImage(texture);

texture.SavePng("user://test.png");

return new AtlasTexture {Atlas = imageTexture, Region = new Rect2(0,0,atlasWidth, atlasHeight)};
}

private List<string> getImages(Directory textureDir) {
List<string> images = new List<string>();
textureDir.ListDirBegin(true, true);
while (true) {
string file = textureDir.GetNext();
if (file.Equals("")) {
break;
}
else if (file.EndsWith(".png")) {
images.Add(file);
}
}

textureDir.ListDirEnd();

return images;
}
}
}
Loading

0 comments on commit 857f857

Please sign in to comment.