PageRenderTime 1079ms CodeModel.GetById 735ms app.highlight 8ms RepoModel.GetById 213ms app.codeStats 0ms

/src/away3d/core/managers/CubeTexture3DProxy.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 118 lines | 94 code | 21 blank | 3 comment | 18 complexity | e0b0db4ad4c428e12ea30e7e06f14907 MD5 | raw file
  1/**
  2 * Author: David Lenaerts
  3 */
  4package away3d.core.managers
  5{
  6	import away3d.arcane;
  7	import away3d.events.Stage3DEvent;
  8	import away3d.materials.utils.CubeMap;
  9
 10	import flash.display3D.Context3D;
 11	import flash.display3D.Context3DTextureFormat;
 12	import flash.display3D.textures.CubeTexture;
 13
 14	use namespace arcane;
 15
 16	public class CubeTexture3DProxy
 17	{
 18		private var _cubeMap : CubeMap;
 19		private var _textures : Vector.<CubeTexture>;
 20		private var _dirty : Vector.<Boolean>;
 21		private var _maxIndex : int = -1;
 22		private var _listensForDispose : Vector.<Boolean>;
 23
 24		public function CubeTexture3DProxy()
 25		{
 26			_textures = new Vector.<CubeTexture>(8);
 27			_dirty = new Vector.<Boolean>(8);
 28			_listensForDispose = new Vector.<Boolean>(8);
 29		}
 30
 31		public function get cubeMap() : CubeMap
 32		{
 33			return _cubeMap;
 34		}
 35
 36		public function set cubeMap(value : CubeMap) : void
 37		{
 38			if (value == _cubeMap) return;
 39
 40			if (_cubeMap) {
 41				if (value.size != _cubeMap.size)
 42					invalidateSize();
 43				else
 44					invalidateContent();
 45			}
 46
 47			_cubeMap = value;
 48		}
 49
 50		public function invalidateContent() : void
 51		{
 52			for (var i : int = 0; i <= _maxIndex; ++i) {
 53				_dirty[i] = true;
 54			}
 55		}
 56
 57		private function invalidateSize() : void
 58		{
 59			var tex : CubeTexture;
 60			for (var i : int = 0; i <= _maxIndex; ++i) {
 61				tex = _textures[i];
 62				if (tex) {
 63					tex.dispose();
 64					_textures[i] = null;
 65					_dirty[i] = false;
 66				}
 67			}
 68		}
 69
 70		public function dispose(deep : Boolean) : void
 71		{
 72			if (deep) {
 73				if (_cubeMap) _cubeMap.dispose();
 74				_cubeMap = null;
 75			}
 76
 77			for (var i : int = 0; i <= _maxIndex; ++i)
 78				if (_textures[i]) _textures[i].dispose();
 79		}
 80
 81		public function getTextureForContext(stage3DProxy : Stage3DProxy) : CubeTexture
 82		{
 83			var contextIndex : int = stage3DProxy._stage3DIndex;
 84			var context : Context3D = stage3DProxy._context3D;
 85
 86			if (contextIndex > _maxIndex) _maxIndex = contextIndex;
 87			var tex : CubeTexture = _textures[contextIndex];
 88
 89			if (!_listensForDispose[contextIndex]) {
 90				stage3DProxy.addEventListener(Stage3DEvent.CONTEXT3D_DISPOSED, onContext3DDisposed);
 91				_listensForDispose[contextIndex] = true;
 92			}
 93
 94			if (!tex || _dirty[contextIndex]) {
 95				if (!tex) _textures[contextIndex] = tex = context.createCubeTexture(_cubeMap.size, Context3DTextureFormat.BGRA, false);
 96				_cubeMap.upload(_textures[contextIndex]);
 97				_dirty[contextIndex] = false;
 98			}
 99
100			return tex;
101		}
102
103		private function onContext3DDisposed(event : Stage3DEvent) : void
104		{
105			var stage3DProxy : Stage3DProxy = Stage3DProxy(event.target);
106			var contextIndex : int = stage3DProxy._stage3DIndex;
107
108			if (_textures[contextIndex]) {
109				_textures[contextIndex].dispose();
110				_textures[contextIndex] = null;
111			}
112
113			_listensForDispose[contextIndex] = false;
114
115			stage3DProxy.removeEventListener(Stage3DEvent.CONTEXT3D_DISPOSED, onContext3DDisposed);
116		}
117	}
118}