hidden_doors_redo/snippet_create_texture.lua

94 lines
3.8 KiB
Lua

-- This file is intended to run as a snippet with https://content.luanti.org/packages/luk3yx/snippets/
-- it's used to test and debug the texture modifier for the door's texture, as the engine doesn't provide
-- sufficient support for a clean solution.
-- engine issue: https://github.com/luanti-org/luanti/issues/15054
-- base: transparent image with the size of the doors textures
-- (https://github.com/luanti-org/minetest_game/blob/master/mods/doors/textures/doors_door_wood.png)
-- texture: texture of the node the door shall imitate
-- both must be of the same scale (texture: 16x16px => base: 38x32 | texture: 32x32 => base: 76x64)
local base = "DOOR_MASK_BLANK"
local texture = "TEXTURE"
-- base = "doors_door_wood.png^[opacity:0"
-- texture = "default_stone_brick.png"
--[[
The final texture is intended to look like this (16px textures), otherwise upscaled
16px 16px 6px
------------ ------------ ------
1| | | |
6| | | |
p| 1 | 3 | 5 |
x| | | |
------------ ------------ ------
1| | | |
6| | | |
p| 2 | 4 | 6 |
x| | | |
------------ ------------ ------
Without informations about the texture size, we have only limited options:
- add a texture to the base at (0,0)
- flip base coordinates
- rotate 90° clockwise by combining the rotated texture onto the base texture again
- 1 -> 3
- 3 -> 4
- 4 -> 2
- 2 -> 1
- 5 -> NULL
- 6 -> NULL
Idea:
We generate each column of the final texture and overlay them onto each other in the final step
--]]
-- flip the texture around y, place it onto the base texture, flip the whole texture again
local t_2 = ("%s^[combine:0x0:0,0=(%s^[transform6)^[transform6"):format(base, texture)
-- t1 can be placed and is already in the correct position
local t_12 = ("%s^[combine:0x0:0,0=%s"):format(t_2, texture)
-- idea: flip t_12 around y and rotate 2x
-- first rotation brings the textures to places 1 & 3
local t_34_rot = ("%s^[combine:0x0:0,0=(%s^[transform6^[transform3)"):format(base, t_12)
local t_34 = ("%s^[combine:0x0:0,0=(%s^[transform3)"):format(base, t_34_rot)
-- 5 & 6 are only the sides of the door, it should be good enough to mirror t_12
local t_56 = ("%s^[transform4"):format(t_12)
-- t_34 and t_56 overlap, make sure t_34 is on top
local img = ("%s^[combine:0x0:0,0=(%s):0,0=(%s):0,0=(%s)"):format(base, t_56, t_34, t_12)
-- ^ and : need escaping if they are in parentheses...
local escape = function(s)
local p_c = 0 -- parentheses_count
local n_s = ""
for i = 0, #s do
local c = s:sub(i,i)
if c == "(" then
p_c = p_c + 1
elseif c == ")" then
p_c = p_c - 1
elseif c == "^" and p_c > 0 then
c = ("\\"):rep(math.pow(2, p_c-1)) .. c
elseif c == ":" and p_c > 0 then
c = ("\\"):rep(math.pow(2, p_c-1)) .. c
end
n_s = n_s .. c
end
return n_s
end
img = escape(img)
core.log(img)
-- core.show_formspec("singleplayer", "abc","formspec_version[6]size[38,32]image[0,0;38,32;"..img.."]")
-- -----------------------------------------------------------------------------------------------------------------
--[[output:
(DOOR_MASK_BLANK\^[combine\:0x0\:0,0=(TEXTURE\\^[transform6)\^[transform6\^[combine\:0x0\:0,0=TEXTURE\^[transform4)^
(DOOR_MASK_BLANK\^[combine\:0x0\:0,0=(DOOR_MASK_BLANK\\^[combine\\:0x0\\:0,0=(DOOR_MASK_BLANK\\\\^[combine\\\\:0x0\\
\\:0,0=(TEXTURE\\\\\\\\^[transform6)\\\\^[transform6\\\\^[combine\\\\:0x0\\\\:0,0=TEXTURE\\\\^[transform6\\\\^[trans
form3)\\^[transform3))^(DOOR_MASK_BLANK\^[combine\:0x0\:0,0=(TEXTURE\\^[transform6)\^[transform6\^[combine\:0x0\:0,0
=TEXTURE)
--]]