My favorites | Sign in
Logo
             
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package {
import alternativa.engine3d.materials.TextureMaterial;
import alternativa.engine3d.materials.TextureMaterialPrecision;
import alternativa.types.Texture;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;

/**
* Material that loads its texture from URL.
* @author makc
*/
public class TextureLoadMaterial extends TextureMaterial {

private var loader:Loader;
private var texturePlaceholder:Texture;

public function TextureLoadMaterial (texture:String,
// the rest are TextureMaterial arguments
alpha:Number = 1, repeat:Boolean = true, smooth:Boolean = false, blendMode:String = BlendMode.NORMAL, wireThickness:Number = -1, wireColor:uint = 0, precision:Number = TextureMaterialPrecision.MEDIUM) {

// 1st, create texture placeholder
texturePlaceholder = new Texture (new BitmapData (1, 1, false, 0));

// create material ready to be used
super (texturePlaceholder, alpha, repeat, smooth, blendMode, wireThickness, wireColor, precision);

// load texture
loader = new Loader;
addListeners (loader.contentLoaderInfo);
loader.load (new URLRequest (texture));
}

private function onLoadComplete (e:Event):void {
var inf:LoaderInfo = LoaderInfo (e.target);
removeListeners (inf);

var bmp:Bitmap = inf.content as Bitmap;
if (bmp != null) {
// replace the texture
texturePlaceholder.bitmapData.dispose ();
texturePlaceholder = new Texture (bmp.bitmapData);
texture = texturePlaceholder;
}
}

private function onLoadError (e:IOErrorEvent):void {
removeListeners (LoaderInfo (e.target));
}

private function addListeners (inf:LoaderInfo):void {
inf.addEventListener(Event.COMPLETE, onLoadComplete);
inf.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
inf.addEventListener(IOErrorEvent.NETWORK_ERROR, onLoadError);
inf.addEventListener(IOErrorEvent.VERIFY_ERROR, onLoadError);
}

private function removeListeners (inf:LoaderInfo):void {
inf.removeEventListener(Event.COMPLETE, onLoadComplete);
inf.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
inf.removeEventListener(IOErrorEvent.NETWORK_ERROR, onLoadError);
inf.removeEventListener(IOErrorEvent.VERIFY_ERROR, onLoadError);
}
}

}
Show details Hide details

Change log

r91 by makc.the.great on Aug 19, 2009   Diff
Sky box demo, Alternativa3D 5.5.0
Go to: 
Sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 2498 bytes, 71 lines
Hosted by Google Code