Welcome,
Guest
. Please
login
or
register
.
February 15, 2025, 02:25:22 AM
Home
Help
Search
Login
Register
Tasharen Entertainment Forum
»
Support
»
NGUI 3 Support
»
Textures offset when copying individual sprites from UIAtlas to a new texture
« previous
next »
Print
Pages: [
1
]
Author
Topic: Textures offset when copying individual sprites from UIAtlas to a new texture (Read 2084 times)
Biggix
Newbie
Thank You
-Given: 2
-Receive: 0
Posts: 33
Textures offset when copying individual sprites from UIAtlas to a new texture
«
on:
September 21, 2016, 01:47:48 PM »
Hi,
I have a bunch of textures within a UIAtlas. I need to "extract" those textures from the atlas (in runtime) and have each one applied to a UITexture for a specific purpose. Textures are RGBA32.
However when I do the copying, the resulting textures are strangely offset by some non-constant Y value (around 512 in some cases). It's clear that something goes wrong around the GetPixels phase.
This is how I do it:
List
<
UISpriteData
>
spriteList
=
sourceAtlas
.
spriteList
;
spriteTextures
=
new
Texture2D
[
spriteList
.
Count
]
;
Texture2D src_tex2d
=
Instantiate
(
sourceTex
)
as
Texture2D
;
for
(
int
i
=
0
;
i
<
spriteList
.
Count
;
i
++
)
{
UISpriteData data
=
spriteList
[
i
]
;
Color
[
]
pix
=
src_tex2d
.
GetPixels
(
data
.
x
, data
.
y
, data
.
width
, data
.
height
)
;
spriteTextures
[
i
]
=
new
Texture2D
(
data
.
width
, data
.
height
)
;
spriteTextures
[
i
]
.
SetPixels
(
pix
)
;
spriteTextures
[
i
]
.
Apply
(
)
;
}
Please help!!
Logged
Biggix
Newbie
Thank You
-Given: 2
-Receive: 0
Posts: 33
Re: Textures offset when copying individual sprites from UIAtlas to a new texture
«
Reply #1 on:
September 21, 2016, 02:15:20 PM »
Update. I have tried with the following code. But the result is the same: the sprites are mysteriously shifted by different Y value! And I have checked the data.x and data.y properties using debug, they're just fine.
Texture2D src_tex2d
=
Instantiate
(
sourceTex
)
as
Texture2D
;
//(sourceAtlas.texture as Texture2D);
Color32
[
]
srcTexture
=
src_tex2d
.
GetPixels32
(
)
;
for
(
int
i
=
0
;
i
<
spriteList
.
Count
;
i
++
)
{
UISpriteData data
=
spriteList
[
i
]
;;
Color32
[
]
newPixels
=
new
Color32
[
data
.
width
*
data
.
height
]
;
for
(
int
y
=
0
;
y
<
data
.
height
;
++
y
)
{
for
(
int
x
=
0
;
x
<
data
.
width
;
++
x
)
{
int
newIndex
=
y
*
data
.
width
+
x
;
int
oldIndex
=
(
data
.
y
+
y
)
*
src_tex2d
.
width
+
(
data
.
x
+
x
)
;
newPixels
[
newIndex
]
=
srcTexture
[
oldIndex
]
;
}
}
spriteTextures
[
i
]
=
new
Texture2D
(
data
.
width
, data
.
height
)
;
spriteTextures
[
i
]
.
SetPixels32
(
newPixels
)
;
spriteTextures
[
i
]
.
Apply
(
)
;
}
Logged
Biggix
Newbie
Thank You
-Given: 2
-Receive: 0
Posts: 33
Re: Textures offset when copying individual sprites from UIAtlas to a new texture
«
Reply #2 on:
September 21, 2016, 02:33:29 PM »
Solved it! In case it would help anyone, this is the culprit line
Color
[
]
pix
=
src_tex2d
.
GetPixels
(
data
.
x
, sourceAtlas
.
texture
.
height
-
data
.
y
-
data
.
height
, data
.
width
, data
.
height
)
;
But - why!? ArenMook, any comments?
Logged
ArenMook
Administrator
Hero Member
Thank You
-Given: 337
-Receive: 1171
Posts: 22,128
Toronto, Canada
Re: Textures offset when copying individual sprites from UIAtlas to a new texture
«
Reply #3 on:
September 23, 2016, 03:48:06 PM »
IIRC, because texture data is specified from bottom-left, while UVs are from top-left.
Logged
Print
Pages: [
1
]
« previous
next »
Tasharen Entertainment Forum
»
Support
»
NGUI 3 Support
»
Textures offset when copying individual sprites from UIAtlas to a new texture