Skip to content

Commit

Permalink
texture animations loaded from toml
Browse files Browse the repository at this point in the history
  • Loading branch information
rainlizard committed Jan 27, 2025
1 parent 9019051 commit e09b0ff
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Autoload/Version.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extends Node

var major_minor = "0.50"
var major_minor = "0.51"
var patch = "0000"
var full = ""
var unearth_map_format_version:float = 1.06
Expand Down
5 changes: 4 additions & 1 deletion Scenes/CurrentTextures.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ onready var oGame3D = Nodelist.list["oGame3D"]
onready var oCustomSlabVoxelView = Nodelist.list["oCustomSlabVoxelView"]
onready var oColumnEditorVoxelView = Nodelist.list["oColumnEditorVoxelView"]
onready var oMapProperties = Nodelist.list["oMapProperties"]
onready var oTextureAnimation = Nodelist.list["oTextureAnimation"]


const IMAGE_FORMAT = Image.FORMAT_RGB8

Expand Down Expand Up @@ -95,7 +97,8 @@ func start():
# This is important to do here if updating textures while a map is already open
if oDataSlab.get_cell(0,0) != -1:
set_current_texture_pack()


oTextureAnimation.generate_animation_database()

func scan_dk_data_directory():
var path = oGame.DK_DATA_DIRECTORY
Expand Down
3 changes: 2 additions & 1 deletion Scenes/CustomTooltip.gd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extends Control
onready var oTextureAnimation = Nodelist.list["oTextureAnimation"]

var offset = Vector2(0,-35)

Expand Down Expand Up @@ -48,5 +49,5 @@ func set_floortexture(floorTextureValue):
#oTooltipPic.material.set_shader_param("slabIdData", preload("res://Shaders/Bedrock3x3.png"))
oTooltipPic.material.set_shader_param("slabIdData", preload("res://Shaders/Black3x3.png"))
oTooltipPic.material.set_shader_param("fieldSizeInSubtiles", Vector2(1, 1))
oTooltipPic.material.set_shader_param("animationDatabase", preload("res://Shaders/textureanimationdatabase.png"))
oTooltipPic.material.set_shader_param("animationDatabase", oTextureAnimation.animation_database_texture)
oTooltipPic.material.set_shader_param("viewTextures", dataTexture)
2 changes: 1 addition & 1 deletion Scenes/GenerateTerrainOLD.gd
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func create_surface_materials():
mat.set_shader_param("dkTextureMap_Split_A2", oTextureCache.cachedTextures[map][1])
mat.set_shader_param("dkTextureMap_Split_B1", oTextureCache.cachedTextures[map][2])
mat.set_shader_param("dkTextureMap_Split_B2", oTextureCache.cachedTextures[map][3])
mat.set_shader_param("animationDatabase", preload("res://Shaders/textureanimationdatabase.png"))
mat.set_shader_param("animationDatabase", oTextureAnimation.animation_database_texture)

materialArray.append(mat)

Expand Down
6 changes: 5 additions & 1 deletion Scenes/Main.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=190 format=2]
[gd_scene load_steps=191 format=2]

[ext_resource path="res://Scenes/SlabStyle.gd" type="Script" id=1]
[ext_resource path="res://Scenes/SlabPlacement.gd" type="Script" id=2]
Expand Down Expand Up @@ -91,6 +91,7 @@
[ext_resource path="res://Scenes/LineEditFloat.gd" type="Script" id=89]
[ext_resource path="res://Scenes/SlabNameDisplay.gd" type="Script" id=90]
[ext_resource path="res://Scenes/SlabsetPathsLabel.gd" type="Script" id=91]
[ext_resource path="res://Scenes/TextureAnimation.gd" type="Script" id=92]
[ext_resource path="res://Scenes/SlabOwnerCheckBox.gd" type="Script" id=93]
[ext_resource path="res://Scenes/Inspector.gd" type="Script" id=94]
[ext_resource path="res://Shaders/cursorMat.tres" type="Material" id=95]
Expand Down Expand Up @@ -342,6 +343,9 @@ script = ExtResource( 46 )
[node name="ReadPalette" type="Node" parent="TextureCache"]
script = ExtResource( 75 )
[node name="TextureAnimation" type="Node" parent="TextureCache"]
script = ExtResource( 92 )
[node name="SlabPlacement" type="Node" parent="."]
script = ExtResource( 2 )
Expand Down
3 changes: 2 additions & 1 deletion Scenes/OverheadGraphics.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ onready var oDataLevelStyle = Nodelist.list["oDataLevelStyle"]
onready var oUndoStates = Nodelist.list["oUndoStates"]
onready var oQuickMapPreviewDisplay = Nodelist.list["oQuickMapPreviewDisplay"]
onready var oMessage = Nodelist.list["oMessage"]
onready var oTextureAnimation = Nodelist.list["oTextureAnimation"]

signal column_graphics_completed

Expand Down Expand Up @@ -110,7 +111,7 @@ func createDisplayField(setMap, showStyle):

mat.set_shader_param("showOnlySpecificStyle", showStyle)
mat.set_shader_param("fieldSizeInSubtiles", Vector2((M.xSize*3), (M.ySize*3)))
mat.set_shader_param("animationDatabase", preload("res://Shaders/textureanimationdatabase.png"))
mat.set_shader_param("animationDatabase", oTextureAnimation.animation_database_texture)
mat.set_shader_param("viewTextures", overheadTexData)
mat.set_shader_param("slxData", oDataSlx.slxTexData)
mat.set_shader_param("slabIdData", oDataSlab.idTexData)
Expand Down
4 changes: 3 additions & 1 deletion Scenes/SlabDisplay.gd
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ func set_visual(columnArray):

dataTexture.set_data(dataImage)

var oTextureAnimation = Nodelist.list["oTextureAnimation"]

material.set_shader_param("showOnlySpecificStyle", 0)
material.set_shader_param("slxData", preload("res://Shaders/Black3x3.png"))
material.set_shader_param("fieldSizeInSubtiles", Vector2(3, 3))
material.set_shader_param("animationDatabase", preload("res://Shaders/textureanimationdatabase.png"))
material.set_shader_param("animationDatabase", oTextureAnimation.animation_database_texture)
material.set_shader_param("viewTextures", dataTexture)
if slabID == 57:
material.set_shader_param("slabIdData", preload("res://Shaders/Bedrock3x3.png"))
Expand Down
73 changes: 73 additions & 0 deletions Scenes/TextureAnimation.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
extends Node
onready var oGame = Nodelist.list["oGame"]
onready var oMessage = Nodelist.list["oMessage"]


var animation_database_texture = preload("res://Shaders/textureanimationdatabase.png")

func generate_animation_database():
# Load TOML file
var file = File.new()
if file.open(oGame.DK_FXDATA_DIRECTORY.plus_file("textureanim.toml"), File.READ) != OK:
oMessage.big("error","couldn't find textureanim.toml")
return

# Create image for animation database
var img = Image.new()
# Width = 8 frames, Height = number of animations (456)
img.create(8, 456, false, Image.FORMAT_RGB8)
img.lock()

# Parse file line by line
var current_texture_index = -1

while !file.eof_reached():
var line = file.get_line().strip_edges()

# Skip empty lines and comments
if line.empty() or line.begins_with("#"):
continue

# Check for texture definition
if line.begins_with("[texture"):
var index_str = line.substr(8).trim_suffix("]")
current_texture_index = int(index_str)
continue

# Check for frames definition
if line.begins_with("frames = [") and current_texture_index >= 544:
# Extract numbers from frames array
var frames_str = line.substr(9).trim_suffix("]")
var frames = frames_str.split(",")

# Calculate animation index (0-based)
var anim_index = current_texture_index - 544

# Write each frame's data to the correct x,y position
for frame in range(8):
if frame < frames.size():
var texture_index = int(frames[frame])
# Convert texture_index to RGB components
var r = (texture_index >> 16) & 255
var g = (texture_index >> 8) & 255
var b = texture_index & 255
# Store the texture index across RGB channels
img.set_pixel(frame, anim_index, Color8(r, g, b))
else:
# For unused frames, set to black (0)
img.set_pixel(frame, anim_index, Color8(0, 0, 0))

img.unlock()
file.close()

# Create ImageTexture from the image
animation_database_texture = ImageTexture.new()
animation_database_texture.create_from_image(img, 0)

# Optionally save the image to disk
# var err = img.save_png("res://animationDatabase.png")
# if err != OK:
# printerr("Failed to save animation database: ", err)
# return

print("Successfully generated animation database")
3 changes: 2 additions & 1 deletion Scenes/View3D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extends Spatial
onready var oCamera3D = $'Player/Head/Camera3D'
onready var oTextureCache = Nodelist.list["oTextureCache"]
onready var oDataLevelStyle = Nodelist.list["oDataLevelStyle"]
onready var oTextureAnimation = Nodelist.list["oTextureAnimation"]

var materialArray = []

Expand All @@ -22,7 +23,7 @@ func create_material(map):
mat.set_shader_param("dkTextureMap_Split_A2", oTextureCache.cachedTextures[map][1])
mat.set_shader_param("dkTextureMap_Split_B1", oTextureCache.cachedTextures[map][2])
mat.set_shader_param("dkTextureMap_Split_B2", oTextureCache.cachedTextures[map][3])
mat.set_shader_param("animationDatabase", preload("res://Shaders/textureanimationdatabase.png"))
mat.set_shader_param("animationDatabase", oTextureAnimation.animation_database_texture)
return mat

func enable_or_disable_mipmaps_on_all_materials(enabled): # set to 0 or 1
Expand Down
8 changes: 3 additions & 5 deletions Shaders/display_texture_2d.shader
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ vec4 texelGet ( sampler2D tg_tex, ivec2 tg_coord, int tg_lod ) {
}

int getAnimationFrame(int frame, int index) {
// y coordinate = Animated Texture index
// x coordinate = frame number

// y coordinate = Animated Texture index
ivec2 coords = ivec2(frame, index);

vec3 value = texelGet(animationDatabase, coords, 0).rgb * vec3(255.0,255.0,255.0);
return int(value.r+value.g+value.b);
vec3 value = texelGet(animationDatabase, coords, 0).rgb;
return (int(value.r * 255.0) << 16) | (int(value.g * 255.0) << 8) | int(value.b * 255.0);
}

// Convert RGB values to one integer
Expand Down
8 changes: 4 additions & 4 deletions Shaders/display_texture_3d.shader
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ vec4 texelGet ( sampler2D tg_tex, ivec2 tg_coord, int tg_lod ) {
return texture(tg_tex, tg_getpos, float(tg_lod));
}

int getAnimationFrame(int frame, int index){
// y coordinate = Animated Texture index
int getAnimationFrame(int frame, int index) {
// x coordinate = frame number
// y coordinate = Animated Texture index
ivec2 coords = ivec2(frame, index);
vec3 value = texelGet(animationDatabase, coords, 0).rgb * vec3(255.0,255.0,255.0);
return int(value.r+value.g+value.b);
vec3 value = texelGet(animationDatabase, coords, 0).rgb;
return (int(value.r * 255.0) << 16) | (int(value.g * 255.0) << 8) | int(value.b * 255.0);
}

int getIndex(vec2 uv2) {
Expand Down

0 comments on commit e09b0ff

Please sign in to comment.