-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDDSMipmaps.lua
More file actions
52 lines (43 loc) · 1.69 KB
/
DDSMipmaps.lua
File metadata and controls
52 lines (43 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- this file is beacuse mta's dxGetTexturePixels with DDS and mipmaps
-- is broken, so we have to do it manually
-- by default it returns one valid mipmap and the rest are just black
function getMipMapSize(width, height, d3dFormat)
if d3dFormat == EnumD3DFormat.DXT1 then
return math.max(1, math.floor((width+3)/4)) * math.max(1, math.floor((height+3)/4)) * 8
elseif d3dFormat == EnumD3DFormat.DXT3 or d3dFormat == EnumD3DFormat.DXT5 then
return math.max(1, math.floor((width+3)/4)) * math.max(1, math.floor((height+3)/4)) * 16
else
return width * height * 3
end
end
function getDdsPixels(texture, mipmapLevel)
local width, height = dxGetMaterialSize(texture)
local width = math.max(1, math.floor(width / (2^(mipmapLevel-1))))
local height = math.max(1, math.floor(height / (2^(mipmapLevel-1))))
local rt = dxCreateRenderTarget(width, height)
dxSetRenderTarget(rt)
dxDrawImage(0, 0, width, height, texture)
dxSetRenderTarget()
local pixels = dxGetTexturePixels(rt, 'dds', 'dxt1', true)
local readStream = ReadStream(pixels)
local size = getMipMapSize(width, height, EnumD3DFormat.DXT1)
readStream.readingPos = 129
local mipmap = DDSMipmap()
mipmap.size = size
mipmap:read(readStream)
return mipmap.data
end
function getDdsWithMipmapsManually(texture)
local width, height = dxGetMaterialSize(texture)
local writeStream = WriteStream()
local header = DDSHeader()
header.width = width
header.height = height
header.mipmapLevels = 9
header:write(writeStream)
for i = 1, 9 do
local data = getDdsPixels(texture, i)
writeStream:write(data, bytes, #data)
end
return writeStream:save()
end