PageRenderTime 43ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/src/flashbackend/src/x3dom/Background.as

https://gitlab.com/oytunistrator/x3dom
ActionScript | 303 lines | 234 code | 53 blank | 16 comment | 33 complexity | 4850193693bc61dbd2c269115011e840 MD5 | raw file
  1. package x3dom
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.GradientType;
  6. import flash.display.Sprite;
  7. import flash.display3D.*;
  8. import flash.geom.Matrix;
  9. import x3dom.texturing.*;
  10. public class Background
  11. {
  12. private var _scene:X3DScene;
  13. private var _context3D:Context3D;
  14. private var _hasSkyTexture:Boolean = false;
  15. private var _hasCubeTexture:Boolean = false;
  16. private var _hasBackTexture:Boolean = false;
  17. private var _isSphereExists:Boolean = false;
  18. private var _texURLs:Array = new Array();
  19. private var _skyColor:Array = new Array(0, 0, 0);
  20. private var _skyAngle:Array = new Array();
  21. private var _groundColor:Array = new Array();
  22. private var _groundAngle:Array = new Array();
  23. private var _transparency:Number = 1.0;
  24. private var _sphere:Shape;
  25. private var _plane:Shape;
  26. public function Background()
  27. {
  28. this._context3D = FlashBackend.getContext();
  29. //Create background plane
  30. this.createPlane();
  31. //Create background sphere
  32. this.createSphere();
  33. }
  34. public function init() : void
  35. {
  36. //Check if there are more than one skyColor and skyAngels count is one less skyColors count
  37. if( (this._skyColor.length/3) > 1 && this._skyAngle.length == ((this._skyColor.length/3)-1) )
  38. {
  39. //Generate sky texture
  40. this.generateSkyTexture();
  41. this._hasSkyTexture = true;
  42. }
  43. else
  44. {
  45. this._hasSkyTexture = false;
  46. }
  47. //check if there are background textures
  48. if(this._texURLs[0] != "")
  49. {
  50. //Check if there are six textures for cubeTexture
  51. if( this._texURLs[0] != "" && this._texURLs[1] != "" && this._texURLs[2] != "" &&
  52. this._texURLs[3] != "" && this._texURLs[4] != "" && this._texURLs[5] != "" )
  53. {
  54. this._sphere.texture = new CubeMapTexture(this._texURLs[0], this._texURLs[1], this._texURLs[2],
  55. this._texURLs[3], this._texURLs[4], this._texURLs[5]);
  56. this._hasCubeTexture = true;
  57. this._hasBackTexture = false;
  58. this._hasSkyTexture = false;
  59. }
  60. else
  61. {
  62. this._plane.texture = new ImageTexture(this._texURLs[0]);
  63. this._hasBackTexture = true;
  64. this._hasCubeTexture = false;
  65. this._hasSkyTexture = false;
  66. }
  67. }
  68. else
  69. {
  70. this._hasBackTexture = false;
  71. this._hasCubeTexture = false;
  72. }
  73. }
  74. private function createPlane() :void
  75. {
  76. this._plane = new Shape();
  77. this._plane.setVertices( 0, Vector.<Number>( [-1,-1,0, 1,-1,0, 1,1,0, -1,1,0] ) );
  78. this._plane.setTexCoords( 0, Vector.<Number>( [0,1, 1,1, 1,0, 0,0] ) );
  79. this._plane.setIndices( 0, Vector.<uint>( [0,1,2, 2,3,0] ) );
  80. }
  81. private function createSphere() :void
  82. {
  83. var radius:Number = 10000;
  84. var latNumber:Number, longNumber:Number;
  85. var latitudeBands:Number = 24;
  86. var longitudeBands:Number = 24;
  87. var theta:Number, sinTheta:Number, cosTheta:Number;
  88. var phi:Number, sinPhi:Number, cosPhi:Number;
  89. var x:Number, y:Number, z:Number, u:Number, v:Number;
  90. var vertices:Vector.<Number> = new Vector.<Number>();
  91. var texCoords:Vector.<Number> = new Vector.<Number>();
  92. var indices:Vector.<uint> = new Vector.<uint>();
  93. for(latNumber = 0; latNumber<=latitudeBands ; latNumber++)
  94. {
  95. theta = (latNumber * Math.PI) / latitudeBands;
  96. sinTheta = Math.sin(theta);
  97. cosTheta = Math.cos(theta);
  98. for (longNumber = 0; longNumber <= longitudeBands; longNumber++)
  99. {
  100. phi = (longNumber * 2.0 * Math.PI) / longitudeBands;
  101. sinPhi = Math.sin(phi);
  102. cosPhi = Math.cos(phi);
  103. x = -cosPhi * sinTheta;
  104. y = -cosTheta;
  105. z = -sinPhi * sinTheta;
  106. u = 0.25 - ((1.0 * longNumber) / longitudeBands);
  107. v = latNumber / latitudeBands;
  108. vertices.push(radius * x);
  109. vertices.push(radius * y);
  110. vertices.push(radius * z);
  111. texCoords.push(u);
  112. texCoords.push(v);
  113. }
  114. }
  115. var first:Number, second:Number;
  116. for (latNumber = 0; latNumber < latitudeBands; latNumber++)
  117. {
  118. for (longNumber = 0; longNumber < longitudeBands; longNumber++)
  119. {
  120. first = (latNumber * (longitudeBands + 1)) + longNumber;
  121. second = first + longitudeBands + 1;
  122. indices.push(first);
  123. indices.push(second);
  124. indices.push(first + 1);
  125. indices.push(second);
  126. indices.push(second + 1);
  127. indices.push(first + 1);
  128. }
  129. }
  130. this._sphere = new Shape();
  131. this._sphere.setVertices(0, vertices);
  132. this._sphere.setTexCoords(0, texCoords);
  133. this._sphere.setIndices(0, indices);
  134. }
  135. private function generateSkyTexture() : void
  136. {
  137. //Create array for angles, colors and alphas
  138. var angles:Array = new Array();
  139. var colors:Array = new Array();
  140. var alphas:Array = new Array();
  141. //Fill color array with HEX-Colors
  142. for(var i:int=0; i<this._skyColor.length/3; i++) {
  143. colors[i] = x3dom.Utils.rgb2Hex( this._skyColor[i*3+0], this._skyColor[i*3+1], this._skyColor[i*3+2] );
  144. }
  145. //Fill angle array
  146. for(i=0; i<this._skyAngle.length; i++) {
  147. if(i == 0) angles[i] = 0;
  148. angles[i+1] = this._skyAngle[i];
  149. }
  150. if(this._groundAngle.length > 0)
  151. {
  152. if (angles[angles.length-1] < Math.PI / 2) {
  153. angles[angles.length] = Math.PI / 2 - 0.000001;
  154. colors[colors.length] = colors[colors.length - 1];
  155. }
  156. for (i=this._groundAngle.length-1; i>=0; i--) {
  157. if ( (i == this._groundAngle.length-1) && (Math.PI - this._groundAngle[i] <= Math.PI / 2) ) {
  158. angles[angles.length] = Math.PI / 2;
  159. colors[colors.length] = x3dom.Utils.rgb2Hex( this._groundColor[this._groundColor.length-3], this._groundColor[this._groundColor.length-2], this._groundColor[this._groundColor.length-1]);
  160. }
  161. angles[angles.length] = Math.PI - this._groundAngle[i];
  162. colors[colors.length] = x3dom.Utils.rgb2Hex(this._groundColor[i*3 + 0], this._groundColor[i*3 + 1], this._groundColor[i*3 + 2]);
  163. }
  164. angles[angles.length] = Math.PI;
  165. colors[colors.length] = x3dom.Utils.rgb2Hex(this._groundColor[0], this._groundColor[1], this._groundColor[2]);
  166. }
  167. else
  168. {
  169. if (angles[angles.length-1] < Math.PI)
  170. {
  171. angles[angles.length] = Math.PI;
  172. colors[colors.length] = colors[colors.length - 1];
  173. }
  174. }
  175. //Fill alpha array
  176. for (i=0; i<colors.length; i++) {
  177. alphas[i] = 1.0;
  178. }
  179. //Convert angles[0-PI] to ratios[0-255]
  180. for (i=0; i<angles.length; i++) {
  181. angles[i] = (angles[i]/Math.PI)*255.0;
  182. }
  183. //Create a Matrix instance and assign the Gradient Box
  184. var matrix:Matrix = new Matrix();
  185. matrix.createGradientBox( 512, 512, Math.PI/2, 0, 0 );
  186. //Create a sprite
  187. var sprite:Sprite = new Sprite();
  188. //Draw a gradient filled rect
  189. sprite.graphics.beginGradientFill( GradientType.LINEAR, colors, alphas, angles, matrix);
  190. sprite.graphics.drawRect( 0, 0, 512, 512 );
  191. //Create bitmapdata and draw the sprite into
  192. var bitmapData:BitmapData = new BitmapData(512, 512, false, 0x000000);
  193. bitmapData.draw( sprite );
  194. //FlashBackend.stage().addChild(new Bitmap(bitmapData));
  195. this._sphere.texture = new BitmapTexture( new Bitmap(bitmapData) );
  196. }
  197. public function set skyColor(skyColor:Array) : void
  198. {
  199. this._skyColor = skyColor;
  200. }
  201. public function get skyColor() : Array
  202. {
  203. return this._skyColor;
  204. }
  205. public function set skyAngle(skyAngle:Array) : void
  206. {
  207. this._skyAngle = skyAngle;
  208. }
  209. public function set groundColor(groundColor:Array) : void
  210. {
  211. this._groundColor = groundColor;
  212. }
  213. public function set groundAngle(groundAngle:Array) : void
  214. {
  215. this._groundAngle = groundAngle;
  216. }
  217. public function set transparency(transparency:Number) : void
  218. {
  219. this._transparency = transparency;
  220. }
  221. public function get transparency() : Number
  222. {
  223. return this._transparency;
  224. }
  225. public function set texURLs(texURLs:Array) : void
  226. {
  227. this._texURLs = texURLs;
  228. }
  229. public function hasBackTexture() : Boolean
  230. {
  231. return this._hasBackTexture;
  232. }
  233. public function hasSkyTexture() : Boolean
  234. {
  235. return this._hasSkyTexture;
  236. }
  237. public function hasCubeTexture() : Boolean
  238. {
  239. return this._hasCubeTexture;
  240. }
  241. public function get plane() : Shape
  242. {
  243. return this._plane;
  244. }
  245. public function get sphere() : Shape
  246. {
  247. return this._sphere;
  248. }
  249. }
  250. }