Skip to content

Commit

Permalink
Merge branch 'addtmapb' into add-players
Browse files Browse the repository at this point in the history
  • Loading branch information
PieterVdc committed Apr 28, 2024
2 parents d4718d9 + 79fbe86 commit bb1ed2a
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ custom_slabs.cfg
custom_objects.cfg

unearthdata/tmapa0*
unearthdata/tmapb0*
textures

### Godot ###
Expand Down
81 changes: 54 additions & 27 deletions Scenes/CurrentTextures.gd
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func scan_dk_data_directory():
var fileName = dir.get_next()
while fileName != "":
if dir.current_is_dir() == false:
if fileName.to_upper().begins_with("TMAPA") == true: # Get file regardless of case (case insensitive)
if fileName.to_upper().begins_with("TMAP") == true: # Get file regardless of case (case insensitive)
if fileName.to_upper().get_extension() == "DAT":
if fileName.to_upper().begins_with("TMAPANIM") == false:
var getModifiedTime = File.new().get_modified_time(path.plus_file(fileName))
Expand Down Expand Up @@ -152,52 +152,69 @@ func save_image_as_png(img, inputPath):

func load_cache_filename(path):
var fileName = path.get_file().get_basename().to_lower()
var cachePath = Settings.unearthdata.plus_file(fileName + ".png")

if(fileName.to_lower().find("tmapa") == -1):
return

var cachePathtmapa = Settings.unearthdata.plus_file(fileName + ".png")
var cachePathtmapb = Settings.unearthdata.plus_file(fileName.replace("tmapa","tmapb") + ".png")
var tmapaNumber = int(fileName.to_lower().trim_prefix("tmapa")) # Get the specific position to create within the array
if File.new().file_exists(cachePath) == true:

if File.new().file_exists(cachePathtmapa) == true:
# Need to call load() on an Image class if I want the save and load to work correctly (otherwise it saves too fast and doesn't load or something)
var img = Image.new()
img.load(cachePath)
load_image_into_cache(img, tmapaNumber)
var imgA = Image.new()
imgA.load(cachePathtmapa)
var imgB = Image.new()
imgB.load(cachePathtmapb)
load_image_into_cache(imgA,imgB, tmapaNumber)
#print('Loaded cache file: ' + cachePath)
return OK
else:
print('Cache file not found: ' + cachePath)
print('Cache file not found: ' + cachePathtmapa)
cachedTextures.clear()
return FAILED

func load_image_into_cache(img, tmapaNumber):
func load_image_into_cache(imgA, imgB,tmapaNumber):
tmapaNumber = int(tmapaNumber)
while cachedTextures.size() <= tmapaNumber: # Fill all array positions, in case a tmapa00#.dat file inbetween is deleted
cachedTextures.append([null, null])
cachedTextures[tmapaNumber] = convert_img_to_two_texture_arrays(img)
cachedTextures[tmapaNumber] = convert_img_to_two_texture_arrays(imgA,imgB)

# SLICE COUNT being too high is the reason TextureArray doesn't work on old PC. (NOT IMAGE SIZE, NOT MIPMAPS EITHER)
# RES files might actually take longer to generate a TextureArray from than PNG, not sure.
func convert_img_to_two_texture_arrays(img):
if img.get_format() != IMAGE_FORMAT:
img.convert(IMAGE_FORMAT)

func convert_img_to_two_texture_arrays(imgA,imgB):
if imgA.get_format() != IMAGE_FORMAT:
imgA.convert(IMAGE_FORMAT)
if imgB.get_format() != IMAGE_FORMAT:
imgB.convert(IMAGE_FORMAT)

var twoTextureArrays = [
TextureArray.new(),
TextureArray.new(),
TextureArray.new(),
TextureArray.new(),
]
var xSlices = 8
var ySlices = 34
var sliceWidth = 32 #img.get_width() / xSlices;
var sliceHeight = 32 #img.get_height() / ySlices;
twoTextureArrays[0].create(sliceWidth, sliceHeight, xSlices*ySlices, IMAGE_FORMAT, TextureLayered.FLAG_MIPMAPS+TextureLayered.FLAG_ANISOTROPIC_FILTER)
twoTextureArrays[1].create(sliceWidth, sliceHeight, xSlices*ySlices, IMAGE_FORMAT, TextureLayered.FLAG_MIPMAPS+TextureLayered.FLAG_ANISOTROPIC_FILTER)
twoTextureArrays[2].create(sliceWidth, sliceHeight, xSlices*ySlices, IMAGE_FORMAT, TextureLayered.FLAG_MIPMAPS+TextureLayered.FLAG_ANISOTROPIC_FILTER)
twoTextureArrays[3].create(sliceWidth, sliceHeight, xSlices*ySlices, IMAGE_FORMAT, TextureLayered.FLAG_MIPMAPS+TextureLayered.FLAG_ANISOTROPIC_FILTER)

for i in 2:
for i in 4:
var yOffset = 0
if i == 1:
if i == 1 or i == 3:
yOffset = 34

for y in ySlices:
for x in xSlices:
var slice = img.get_rect(Rect2(x*sliceWidth, (y+yOffset)*sliceHeight, sliceWidth, sliceHeight))
var slice
if i < 2:
slice = imgA.get_rect(Rect2(x*sliceWidth, (y+yOffset)*sliceHeight, sliceWidth, sliceHeight))
else:
slice = imgB.get_rect(Rect2(x*sliceWidth, (y+yOffset)*sliceHeight, sliceWidth, sliceHeight))
slice.generate_mipmaps() #Important otherwise it's black when zoomed out
twoTextureArrays[i].set_layer_data(slice, (y*xSlices)+x)

Expand All @@ -215,29 +232,39 @@ func set_current_texture_pack():

# 2D
if oOverheadGraphics.arrayOfColorRects.size() > 0:
oOverheadGraphics.arrayOfColorRects[0].get_material().set_shader_param("dkTextureMap_Split_A", cachedTextures[value][0])
oOverheadGraphics.arrayOfColorRects[0].get_material().set_shader_param("dkTextureMap_Split_B", cachedTextures[value][1])
oOverheadGraphics.arrayOfColorRects[0].get_material().set_shader_param("dkTextureMap_Split_A1", cachedTextures[value][0])
oOverheadGraphics.arrayOfColorRects[0].get_material().set_shader_param("dkTextureMap_Split_A2", cachedTextures[value][1])
oOverheadGraphics.arrayOfColorRects[0].get_material().set_shader_param("dkTextureMap_Split_B1", cachedTextures[value][2])
oOverheadGraphics.arrayOfColorRects[0].get_material().set_shader_param("dkTextureMap_Split_B2", cachedTextures[value][3])

# 3D
if oGame3D.materialArray.size() > 0:
oGame3D.materialArray[0].set_shader_param("dkTextureMap_Split_A", cachedTextures[value][0])
oGame3D.materialArray[0].set_shader_param("dkTextureMap_Split_B", cachedTextures[value][1])
oGame3D.materialArray[0].set_shader_param("dkTextureMap_Split_A1", cachedTextures[value][0])
oGame3D.materialArray[0].set_shader_param("dkTextureMap_Split_A2", cachedTextures[value][1])
oGame3D.materialArray[0].set_shader_param("dkTextureMap_Split_B1", cachedTextures[value][2])
oGame3D.materialArray[0].set_shader_param("dkTextureMap_Split_B2", cachedTextures[value][3])

for nodeID in get_tree().get_nodes_in_group("VoxelViewer"):
if nodeID.oAllVoxelObjects.mesh != null:
nodeID.oAllVoxelObjects.mesh.surface_get_material(0).set_shader_param("dkTextureMap_Split_A", cachedTextures[value][0])
nodeID.oAllVoxelObjects.mesh.surface_get_material(0).set_shader_param("dkTextureMap_Split_B", cachedTextures[value][1])
nodeID.oAllVoxelObjects.mesh.surface_get_material(0).set_shader_param("dkTextureMap_Split_A1", cachedTextures[value][0])
nodeID.oAllVoxelObjects.mesh.surface_get_material(0).set_shader_param("dkTextureMap_Split_A2", cachedTextures[value][1])
nodeID.oAllVoxelObjects.mesh.surface_get_material(0).set_shader_param("dkTextureMap_Split_B1", cachedTextures[value][2])
nodeID.oAllVoxelObjects.mesh.surface_get_material(0).set_shader_param("dkTextureMap_Split_B2", cachedTextures[value][3])
if nodeID.oSelectedVoxelObject.mesh != null:
nodeID.oSelectedVoxelObject.mesh.surface_get_material(0).set_shader_param("dkTextureMap_Split_A", cachedTextures[value][0])
nodeID.oSelectedVoxelObject.mesh.surface_get_material(0).set_shader_param("dkTextureMap_Split_B", cachedTextures[value][1])
nodeID.oSelectedVoxelObject.mesh.surface_get_material(0).set_shader_param("dkTextureMap_Split_A1", cachedTextures[value][0])
nodeID.oSelectedVoxelObject.mesh.surface_get_material(0).set_shader_param("dkTextureMap_Split_A2", cachedTextures[value][1])
nodeID.oSelectedVoxelObject.mesh.surface_get_material(0).set_shader_param("dkTextureMap_Split_B1", cachedTextures[value][2])
nodeID.oSelectedVoxelObject.mesh.surface_get_material(0).set_shader_param("dkTextureMap_Split_B2", cachedTextures[value][3])

assign_textures_to_slab_window(value)


func assign_textures_to_slab_window(value): # Called by SlabStyleWindow
for nodeID in get_tree().get_nodes_in_group("SlabDisplay"):
nodeID.get_material().set_shader_param("dkTextureMap_Split_A", cachedTextures[value][0])
nodeID.get_material().set_shader_param("dkTextureMap_Split_B", cachedTextures[value][1])
nodeID.get_material().set_shader_param("dkTextureMap_Split_A1", cachedTextures[value][0])
nodeID.get_material().set_shader_param("dkTextureMap_Split_A2", cachedTextures[value][1])
nodeID.get_material().set_shader_param("dkTextureMap_Split_B1", cachedTextures[value][2])
nodeID.get_material().set_shader_param("dkTextureMap_Split_B2", cachedTextures[value][3])



Expand Down
6 changes: 4 additions & 2 deletions Scenes/GenerateTerrainOLD.gd
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,10 @@ func create_surface_materials():

var mat = ShaderMaterial.new()
mat.shader = preload("res://Shaders/display_texture_3d.shader")
mat.set_shader_param("dkTextureMap_Split_A", oTextureCache.cachedTextures[map][0])
mat.set_shader_param("dkTextureMap_Split_B", oTextureCache.cachedTextures[map][1])
mat.set_shader_param("dkTextureMap_Split_A1", oTextureCache.cachedTextures[map][0])
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"))

materialArray.append(mat)
Expand Down
6 changes: 4 additions & 2 deletions Scenes/OverheadGraphics.gd
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ func createDisplayField(setMap, showStyle):
displayField.material = mat

if showStyle != 0: # Do not change the texturemap for default style
mat.set_shader_param("dkTextureMap_Split_A", oTextureCache.cachedTextures[setMap][0])
mat.set_shader_param("dkTextureMap_Split_B", oTextureCache.cachedTextures[setMap][1])
mat.set_shader_param("dkTextureMap_Split_A1", oTextureCache.cachedTextures[setMap][0])
mat.set_shader_param("dkTextureMap_Split_A2", oTextureCache.cachedTextures[setMap][1])
mat.set_shader_param("dkTextureMap_Split_B1", oTextureCache.cachedTextures[setMap][2])
mat.set_shader_param("dkTextureMap_Split_B2", oTextureCache.cachedTextures[setMap][3])

mat.set_shader_param("showOnlySpecificStyle", showStyle)
mat.set_shader_param("fieldSizeInSubtiles", Vector2((M.xSize*3), (M.ySize*3)))
Expand Down
6 changes: 4 additions & 2 deletions Scenes/View3D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ func create_material_array(numberOfSlabStyles):
func create_material(map):
var mat = ShaderMaterial.new()
mat.shader = preload("res://Shaders/display_texture_3d.shader")
mat.set_shader_param("dkTextureMap_Split_A", oTextureCache.cachedTextures[map][0])
mat.set_shader_param("dkTextureMap_Split_B", oTextureCache.cachedTextures[map][1])
mat.set_shader_param("dkTextureMap_Split_A1", oTextureCache.cachedTextures[map][0])
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"))
return mat

Expand Down
16 changes: 11 additions & 5 deletions Shaders/display_texture_2d.shader
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ const vec2 oneTileSize = vec2(32,32);
const float TEXTURE_ANIMATION_SPEED = 12.0;
uniform int showOnlySpecificStyle = 77777;
uniform sampler2D slxData;
uniform sampler2DArray dkTextureMap_Split_A;
uniform sampler2DArray dkTextureMap_Split_B;
uniform sampler2DArray dkTextureMap_Split_A1;
uniform sampler2DArray dkTextureMap_Split_A2;
uniform sampler2DArray dkTextureMap_Split_B1;
uniform sampler2DArray dkTextureMap_Split_B2;

uniform vec2 fieldSizeInSubtiles = vec2(0.0, 0.0);

Expand Down Expand Up @@ -61,7 +63,7 @@ void fragment() {
}

int index = getIndex(ivec2(subtileX,subtileY));
if (index >= 544) { // 544 is the index where the TexAnims start (544 - 585)
if (index >= 544 && index < 1000) { // 544 is the index where the TexAnims start (544 - 999)
int frame = int(mod(TIME * TEXTURE_ANIMATION_SPEED, 8));
index = getAnimationFrame(frame, index-544);
}
Expand All @@ -70,9 +72,13 @@ void fragment() {
float mipmapLevel = calc_mip_level(UV * resolutionOfField);

if (index < 272) { // Splitting the TextureArray into 2, so that it will work on older PCs.
COLOR = textureLod(dkTextureMap_Split_A, vec3((UV.x * fieldSizeInSubtiles.x)-float(subtileX), (UV.y * fieldSizeInSubtiles.y)-float(subtileY), float(index)), mipmapLevel);
COLOR = textureLod(dkTextureMap_Split_A1, vec3((UV.x * fieldSizeInSubtiles.x)-float(subtileX), (UV.y * fieldSizeInSubtiles.y)-float(subtileY), float(index)), mipmapLevel);
} else if (index < 544){
COLOR = textureLod(dkTextureMap_Split_A2, vec3((UV.x * fieldSizeInSubtiles.x)-float(subtileX), (UV.y * fieldSizeInSubtiles.y)-float(subtileY), float(index-272)), mipmapLevel);
} else if (index < 1272){
COLOR = textureLod(dkTextureMap_Split_B1, vec3((UV.x * fieldSizeInSubtiles.x)-float(subtileX), (UV.y * fieldSizeInSubtiles.y)-float(subtileY), float(index-1000)), mipmapLevel);
} else {
COLOR = textureLod(dkTextureMap_Split_B, vec3((UV.x * fieldSizeInSubtiles.x)-float(subtileX), (UV.y * fieldSizeInSubtiles.y)-float(subtileY), float(index-272)), mipmapLevel);
COLOR = textureLod(dkTextureMap_Split_B2, vec3((UV.x * fieldSizeInSubtiles.x)-float(subtileX), (UV.y * fieldSizeInSubtiles.y)-float(subtileY), float(index-1272)), mipmapLevel);
}
}

Expand Down
18 changes: 12 additions & 6 deletions Shaders/display_texture_3d.shader
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
shader_type spatial;
render_mode blend_mix, cull_back, depth_draw_opaque, skip_vertex_transform; //for the sake of performance avoid enabling transparency on all your terrain.
uniform sampler2DArray dkTextureMap_Split_A;
uniform sampler2DArray dkTextureMap_Split_B;
uniform sampler2DArray dkTextureMap_Split_A1;
uniform sampler2DArray dkTextureMap_Split_A2;
uniform sampler2DArray dkTextureMap_Split_B1;
uniform sampler2DArray dkTextureMap_Split_B2;

uniform int use_mipmaps = 1;

Expand Down Expand Up @@ -50,7 +52,7 @@ void fragment() {
// Adding 0.5 so the int() floor will be correct.
int index = getIndex(UV2);

if (index >= 544) { // 544 is the index where the TexAnims start (544 - 585)
if (index >= 544 && index < 1000) { // 544 is the index where the TexAnims start (544 - 999)
int frame = int(mod(TIME * TEXTURE_ANIMATION_SPEED, 8));
index = getAnimationFrame(frame, (index-544) );
}
Expand All @@ -59,9 +61,13 @@ void fragment() {
float mipmapLevel = calc_mip_level(UV * vec2(8.0,68.0)) * float(use_mipmaps);

if (index < 272) { // Splitting the TextureArray into 2, so that it will work on older PCs.
ALBEDO = textureLod(dkTextureMap_Split_A, vec3(UV.x, UV.y, float(index)), mipmapLevel).rgb;
} else {
ALBEDO = textureLod(dkTextureMap_Split_B, vec3(UV.x, UV.y, float(index-272)), mipmapLevel).rgb;
ALBEDO = textureLod(dkTextureMap_Split_A1, vec3(UV.x, UV.y, float(index)), mipmapLevel).rgb;
} else if (index < 544) {
ALBEDO = textureLod(dkTextureMap_Split_A2, vec3(UV.x, UV.y, float(index-272)), mipmapLevel).rgb;
} else if (index < 1272) { // Splitting the TextureArray into 2, so that it will work on older PCs.
ALBEDO = textureLod(dkTextureMap_Split_B1, vec3(UV.x, UV.y, float(index-1000)), mipmapLevel).rgb;
} else {
ALBEDO = textureLod(dkTextureMap_Split_B2, vec3(UV.x, UV.y, float(index-1272)), mipmapLevel).rgb;
}

// Forces the shader to convert albedo from sRGB space to linear space. A problem when using the same TextureArray while mixing 2D and 3D shaders and displaying both 2D and 3D at the same time.
Expand Down

0 comments on commit bb1ed2a

Please sign in to comment.