|
nohappy, Newbie
Posted: 03 November 2011 05:47 PM Total Posts: 9
Hi,
I want to load an image(texture) into Plan.material.
However, I got exception from
var texture:BitmapFileMaterial = new BitmapFileMaterial(“image/a.png”);
It says “Invalid bitmapData! Must be power of 2 and not exceeding 2048”
Yes, I know that I could just resize my image, but it seems not very flexible. Also, 2048 is not too big.
I just want to display an image on Plan. Is there any solution or alternative way to do that?
Thanks!
|
Mr Margaret Scratcher, Sr. Member
Posted: 03 November 2011 06:56 PM Total Posts: 344
[ # 1 ]
Resize is the only way..
I rote a bit of code to resize on the fly, so no matter what size the image actually is, it will be stretched to fit the constraints..
|
Somokon, Member
Posted: 03 November 2011 07:00 PM Total Posts: 75
[ # 2 ]
No, you’ll have to resize your image. This is a hardware limitation that you must adhere to. Many graphics cards do not support non power of 2 dimensions (256, 512, 1024…) or textures bigger than 2048x2048.
|
nohappy, Newbie
Posted: 04 November 2011 01:23 AM Total Posts: 9
[ # 3 ]
That’s too bad. I used to use Papervision3d, and I think it does not have such limitation. How come?
|
proyb2, Newbie
Posted: 04 November 2011 07:57 AM Total Posts: 17
[ # 4 ]
PaperVision3D does not use Stage3D, not GPU accelerated. However, Power of 2 doesn’t mean a limitation as this has been specification implemented long ago which I think 3Dfx Voodoo/Banshee or OpenGL started it.
|
Fabrice Closier, Administrator
Posted: 04 November 2011 08:52 AM Total Posts: 1265
[ # 5 ]
Using power of 2 maps was already a good practice with Flash Player 10. If you were not using them with PV3D, you’ve probably missed a few free FPS here and there and in many cases, your project could have looked better.
Take a look at this 2007 post from Tinic.
http://www.kaourantin.net/2007/06/mip-map-what.html
|
nohappy, Newbie
Posted: 06 November 2011 12:21 PM Total Posts: 9
[ # 6 ]
Thank you Fabrice,
I’ve working for this several hours after your reply.
however I still can’t not re-size bitmapdata without distortion,
i wrote something like:
var resizedBmp:BitmapData = new BitmapData(1024,1024,true,0);
resizedBmp.draw(bmp);
var imageMaterial:BitmapMaterial = new BitmapMaterial(resizedBmp);
where bmp is original BitmapData which size is not power of 2.
However the texture I got is a little strange.
Is there any sample for dynamically load .jpg/.png file into texture?
(I’ve really working on this for a period of time, seems like most tutorial use embed, or the version of Away3d is too old)
I will be really appreciated if you can tell me how to do next…
|
Alex Bogartz, Sr. Member
Posted: 06 November 2011 05:26 PM Total Posts: 216
[ # 7 ]
nohappy - 06 November 2011 12:21 PM Thank you Fabrice,
I’ve working for this several hours after your reply.
however I still can’t not re-size bitmapdata without distortion,
i wrote something like:
var resizedBmp:BitmapData = new BitmapData(1024,1024,true,0);
resizedBmp.draw(bmp);
var imageMaterial:BitmapMaterial = new BitmapMaterial(resizedBmp);
where bmp is original BitmapData which size is not power of 2.
However the texture I got is a little strange.
Is there any sample for dynamically load .jpg/.png file into texture?
(I’ve really working on this for a period of time, seems like most tutorial use embed, or the version of Away3d is too old)
I will be really appreciated if you can tell me how to do next…
Why not just open the file up in Photoshop or Gimp and resize there? It’s not really necessary to resize in code if you don’t have to. Is it messing up your UVs?
|
John Brookes, Moderator
Posted: 06 November 2011 06:31 PM Total Posts: 732
[ # 8 ]
Something like
private function resizeBitmapData(bmData:BitmapData, bmdWidth:Number, bmdHeight:Number):BitmapData { var mat:Matrix = new Matrix(); mat.scale(bmdHeight/bmData.width, bmdHeight/bmData.height); var bmd:BitmapData = new BitmapData(bmdWidth, bmdHeight); bmd.draw(bmData, mat, null, null, null, true); return bmd; } private function initObjects():void { var bmd:BitmapData = new imgAsset().bitmapData//if its embed var bmMaterial:BitmapMaterial = new BitmapMaterial(resizeBitmapData(bmd,2048,2048)); p = new Plane(bmMaterial, bmd.width, bmd.height); scene.addChild(p);
|
Fabrice Closier, Administrator
Posted: 06 November 2011 09:30 PM Total Posts: 1265
[ # 9 ]
I would do as John suggest but photoshop, or any image editor would of course do just fine. The key is in the ratio.
Just wanted to add a little note “en passant”. In case your map holds text information and the receiver geometry doesn’t display the map in its original ratio: don’t forget to set mipmap to false. If you don’t, chances are that the text will become unreadable. No matter how detailed your maps would be.
|
nohappy, Newbie
Posted: 07 November 2011 05:55 AM Total Posts: 9
[ # 10 ]
Thank you guys very much!
Yes, my code was very similar to John’s, except that I want my bitmap to be transparent with .png file. So in John’s resizeBitmapData I change one line into var bmd:BitmapData = new BitmapData(bmdWidth, bmdHeight, true, 0);
It turn out have two weird things. First, it is not transparent but only with black background.(i.e. the same as BitmapData(bmdWidth, bmdHeight, false, 0);
Second, its anti-alias become really weak when I set the “Transparent” parameter to true. (as you can see in attachments image)
How should I do to make transparent happen with .png? And is it possible to solve the alias problem?
Again thank for your patience and profession!
|
Somokon, Member
Posted: 07 November 2011 07:17 PM Total Posts: 75
[ # 11 ]
nohappy - 07 November 2011 05:55 AM Thank you guys very much!
Yes, my code was very similar to John’s, except that I want my bitmap to be transparent with .png file. So in John’s resizeBitmapData I change one line into var bmd:BitmapData = new BitmapData(bmdWidth, bmdHeight, true, 0);
It turn out have two weird things. First, it is not transparent but only with black background.(i.e. the same as BitmapData(bmdWidth, bmdHeight, false, 0);
Second, its anti-alias become really weak when I set the “Transparent” parameter to true. (as you can see in attachments image)
How should I do to make transparent happen with .png? And is it possible to solve the alias problem?
Again thank for your patience and profession!
I think I had this problem before, and solved it by setting alpha = 0.999 (or anything strictly less than 1).
|
nohappy, Newbie
Posted: 08 November 2011 01:21 AM Total Posts: 9
[ # 12 ]
Thanks Somokon, I also found that in 4.0 it changed to alphaThreshold = 0.999. But isn’t it a really correct way to do that? since alphaThreshold will make binary threshold. (i.e. make alpha either 0 or 1)
Any better method?
|
nohappy, Newbie
Posted: 12 November 2011 03:47 PM Total Posts: 9
[ # 13 ]
Hi, I’m wondering that what if my original bitmapdata is like 90*80,
should I still need to resize it to 2048*2048 (square)?
|
John Brookes, Moderator
Posted: 12 November 2011 06:23 PM Total Posts: 732
[ # 14 ]
No, just choose the nearest size
2,4,8,16,32,64,128,256,512,1024,2048
Another version of the above code that does it for you
//import away3d.tools.utils.TextureUtils;
private function autoResizeBitmapData(bmData:BitmapData,smoothing:Boolean = true):BitmapData { if (TextureUtils.isBitmapDataValid(bmData)) return bmData; var max:Number = Math.max(bmData.width, bmData.height); max = TextureUtils.getBestPowerOf2(max); var mat:Matrix = new Matrix(); mat.scale(max/bmData.width, max/bmData.height); var bmd:BitmapData = new BitmapData(max, max); bmd.draw(bmData, mat, null, null, null, smoothing); return bmd; }
|
nohappy, Newbie
Posted: 12 November 2011 07:13 PM Total Posts: 9
[ # 15 ]
That’s perfect, thanks John!
|