Jump to content

Recommended Posts

Posted

This script will convert a 4x3 cube cross to a single DDS file, Just paste this in the editor console.

local file = RequestFile("Open File")
if file == "" then return end

local pixmap = LoadPixmap(file)
if pixmap == nil then return end

local w = pixmap.size.x / 4
if pixmap.size.y ~= w * 3 then
	Print("Error: Image must have a 4:3 aspect ratio")
	return
end

local mipmaps = {}

--[[ Right face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w * 2, w, w, w, face, 0, 0)
table.insert(mipmaps, face)

--[[ Left face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(0, w, w, w, face, 0, 0)
table.insert(mipmaps, face)

--[[ Top face ]]
local face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w, 0, w, w, face, 0, 0)
table.insert(mipmaps, face)

--[[ Bottom face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w, w * 2, w, w, face, 0, 0)
table.insert(mipmaps, face)

--[[ Front face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w, w, w, w, face, 0, 0)
table.insert(mipmaps, face)

--[[ Back face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w * 3, w, w, w, face, 0, 0)
table.insert(mipmaps, face)

SaveTexture("skybox.dds", TEXTURE_CUBE, mipmaps, 6)

 

  • Like 2

Let's build cool stuff and have fun. :)

Posted

Here is working tested code, with face rotation, for a vertical cube cross:

local file = RequestFile("Open File")
if file == "" then return end

local pixmap = LoadPixmap(file)
if pixmap == nil then return end

local w = pixmap.size.x / 3
if pixmap.size.y ~= w * 4 then
	Print("Error: Image must have a 3:4 aspect ratio")
	return
end

local mipmaps = {}

function RotationPixmap90(pixmap)
	local r = CreatePixmap(pixmap.size.x, pixmap.size.y, pixmap.format)
	local x, y, rgba
	for x = 0, pixmap.size.x - 1 do
		for y = 0, pixmap.size.y - 1 do
			rgba = pixmap:Sample(iVec2(x, y))
			r:WritePixel(y, pixmap.size.x - 1 - x, rgba)
		end
	end
	return r
end

--[[ Right face ]]
local face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w * 2, w, w, w, face, 0, 0)
face = RotationPixmap90(face)
face = RotationPixmap90(face)
face = RotationPixmap90(face)
table.insert(mipmaps, face)

--[[ Left face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(0, w, w, w, face, 0, 0)
face = RotationPixmap90(face)
table.insert(mipmaps, face)

--[[ Top face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w, w * 1, w, w, face, 0, 0)
table.insert(mipmaps, face)

--[[ Bottm face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w, w * 3, w, w, face, 0, 0)
table.insert(mipmaps, face)

--[[ Front face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w, w * 2, w, w, face, 0, 0)
table.insert(mipmaps, face)

--[[ Back face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w, 0, w, w, face, 0, 0)
face = RotationPixmap90(face)
face = RotationPixmap90(face)
table.insert(mipmaps, face)

SaveTexture("skybox.dds", TEXTURE_CUBE, mipmaps, 6)

 

  • Like 1

Let's build cool stuff and have fun. :)

  • Josh changed the title to Convert cube cross to cubemap
Posted

If you want to scale the brightness at the same time, this is the way:

local COLORSCALE = 1.5

local file = RequestFile("Open File")
if file == "" then return end

local pixmap = LoadPixmap(file)
if pixmap == nil then return end

local w = pixmap.size.x / 3
if pixmap.size.y ~= w * 4 then
	Print("Error: Image must have a 3:4 aspect ratio")
	return
end

local mipmaps = {}

function ScaleColor(pixmap, scale)
	if scale == 1.0 then return end
	local x, y, c
	for x = 0, pixmap.size.x - 1 do
		for y = 0, pixmap.size.y - 1 do
			c = pixmap:Sample(iVec2(x, y))
			pixmap:WritePixel(x, y, c * scale)
		end
	end	
end

function RotationPixmap90(pixmap)
	local r = CreatePixmap(pixmap.size.x, pixmap.size.y, pixmap.format)
	local x, y, rgba
	for x = 0, pixmap.size.x - 1 do
		for y = 0, pixmap.size.y - 1 do
			rgba = pixmap:Sample(iVec2(x, y))
			r:WritePixel(y, pixmap.size.x - 1 - x, rgba)
		end
	end
	return r
end

--[[ Right face ]]
local face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w * 2, w, w, w, face, 0, 0)
face = RotationPixmap90(face)
face = RotationPixmap90(face)
face = RotationPixmap90(face)
table.insert(mipmaps, face)

--[[ Left face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(0, w, w, w, face, 0, 0)
face = RotationPixmap90(face)
table.insert(mipmaps, face)

--[[ Top face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w, w * 1, w, w, face, 0, 0)
table.insert(mipmaps, face)

--[[ Bottm face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w, w * 3, w, w, face, 0, 0)
table.insert(mipmaps, face)

--[[ Front face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w, w * 2, w, w, face, 0, 0)
table.insert(mipmaps, face)

--[[ Back face ]]
face = CreatePixmap(w, w, pixmap.format)
pixmap:CopyRect(w, 0, w, w, face, 0, 0)
face = RotationPixmap90(face)
face = RotationPixmap90(face)
table.insert(mipmaps, face)

local n
for n = 1, #mipmaps do
	ScaleColor(mipmaps[n], COLORSCALE)
end

SaveTexture("skybox.dds", TEXTURE_CUBE, mipmaps, 6)

 

Let's build cool stuff and have fun. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...