PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Onyx-VJ 4.6.0/Onyx-VJ-Patches/src/Octophobia.as

http://onyx-vj.googlecode.com/
ActionScript | 445 lines | 381 code | 59 blank | 5 comment | 45 complexity | b42d2d03398edc9c48a5b7b64dc1acb1 MD5 | raw file
  1. /**
  2. * Copyright flashmafia ( http://wonderfl.net/user/flashmafia )
  3. * MIT License ( http://www.opensource.org/licenses/mit-license.php )
  4. * Downloaded from: http://wonderfl.net/c/xrgQ
  5. */
  6. package {
  7. import flash.display.Sprite;
  8. import onyx.core.*;
  9. import onyx.display.*;
  10. import onyx.plugin.*;
  11. public class Octophobia extends Patch {
  12. public static var vpl : VolumetricPointLight;
  13. private var sprite:Sprite;
  14. public function Octophobia() {
  15. sprite = new Sprite();
  16. sprite.addChild(vpl = new VolumetricPointLight(DISPLAY_WIDTH, DISPLAY_HEIGHT, new OctopusGenerator(465, 465), [0x687072, 0x121108, 0], [0.88, 0.22, 0.77], [8, 16, 244]));
  17. vpl.startRendering();
  18. vpl.scale = 4;
  19. vpl.opaqueBackground = 0x000000;
  20. }
  21. override public function render(info:RenderInfo):void
  22. {
  23. info.render(sprite);
  24. }
  25. }
  26. }
  27. import flash.display.Bitmap;
  28. import flash.display.BitmapData;
  29. import flash.display.BlendMode;
  30. import flash.display.DisplayObject;
  31. import flash.display.GradientType;
  32. import flash.display.LineScaleMode;
  33. import flash.display.PixelSnapping;
  34. import flash.display.Shape;
  35. import flash.display.Sprite;
  36. import flash.events.Event;
  37. import flash.filters.BlurFilter;
  38. import flash.geom.ColorTransform;
  39. import flash.geom.Matrix;
  40. import flash.geom.Point;
  41. import flash.geom.Rectangle;
  42. import flash.geom.Vector3D;
  43. class OctopusGenerator extends Bitmap {
  44. private const OFFSET : Number = 96;
  45. private const CURVE_QUANT : uint = 128;
  46. private const CURVE_LENGTH : uint = 12;
  47. private const N_AMP : Number = 8;
  48. private const N_AMP2 : Number = N_AMP * 24;
  49. private const TANGENT_OFFSET : Number = 1.5;
  50. private const TIME_SCALE : Number = 128;
  51. private var _t : Number = 0;
  52. private var _controlPoints : Vector.<Vector.<Vector3D>> = new Vector.<Vector.<Vector3D>>(CURVE_QUANT, true);
  53. private var _startPoints : Vector.<Vector3D> = new Vector.<Vector3D>(CURVE_QUANT, true);
  54. private var _endPoint : Vector3D;
  55. private var _fadeCT : ColorTransform = new ColorTransform(1, 1, 1, 0.72);
  56. private var _container : Sprite = new Sprite();
  57. private var _bezierMath : BezierMath = new BezierMath();
  58. public function OctopusGenerator(w : uint, h : uint) : void {
  59. super(new BitmapData(w, h, true, 0), PixelSnapping.AUTO, false);
  60. var n : uint = 0;
  61. while (n < CURVE_QUANT) {
  62. _controlPoints[n] = new Vector.<Vector3D>(0, false);
  63. ++n;
  64. }
  65. n = 1;
  66. while (n < CURVE_LENGTH) {
  67. _controlPoints[0].push(new Vector3D(465 * Math.random(), 465 * Math.random(), 0, 1));
  68. ++n;
  69. }
  70. var nn : uint = 0;
  71. while (nn < CURVE_QUANT) {
  72. _controlPoints[nn].push(_controlPoints[0][0]);
  73. n = 1;
  74. while (n < CURVE_LENGTH) {
  75. var o : Vector3D = _controlPoints[0][n];
  76. _controlPoints[nn].push(new Vector3D(o.x - N_AMP + N_AMP2 * Math.random(), o.y - N_AMP + N_AMP2 * Math.random(), o.z - N_AMP + N_AMP2 * Math.random(), o.w));
  77. ++n;
  78. }
  79. ++nn;
  80. }
  81. n = CURVE_QUANT;
  82. while (--n != 0) {
  83. _startPoints[n] = _controlPoints[n][0];
  84. }
  85. addEventListener(Event.ENTER_FRAME, render, false, 0, true);
  86. }
  87. private function render(e : Event) : void {
  88. var xs : Number = 0;
  89. var ys : Number = 0;
  90. var zs : Number = 0;
  91. _container.graphics.clear();
  92. var q : uint = CURVE_QUANT;
  93. var n : uint = q;
  94. while (--n) {
  95. _endPoint = _bezierMath.bezier(_t / TIME_SCALE, _controlPoints[n]);
  96. var startVec : Vector3D = _startPoints[n];
  97. var gurf : Number = 1 + (zs / q) / 25;
  98. _container.graphics.lineStyle(gurf, 0x000000, 1.0, false, LineScaleMode.NONE);
  99. _container.graphics.moveTo(startVec.x, startVec.y);
  100. _container.graphics.lineTo(_endPoint.x, _endPoint.y);
  101. xs += _endPoint.x;
  102. ys += _endPoint.y;
  103. zs += _endPoint.z;
  104. _startPoints[n] = _endPoint;
  105. }
  106. Octophobia.vpl.srcX = xs / q;
  107. Octophobia.vpl.srcY = ys / q;
  108. Octophobia.vpl.intensity = 1.5 + Math.pow(2, (zs / q) / 150);
  109. bitmapData.lock();
  110. bitmapData.colorTransform(bitmapData.rect, _fadeCT);
  111. bitmapData.draw(_container, null, null, null, null, false);
  112. bitmapData.unlock();
  113. _t++;
  114. if (_t == TIME_SCALE) {
  115. _t = n = 0;
  116. var vec1 : Vector3D;
  117. var vec2 : Vector3D;
  118. var vec3 : Vector3D;
  119. while (n < CURVE_QUANT) {
  120. vec1 = _controlPoints[n][CURVE_LENGTH - 2];
  121. (vec3 = (vec2 = _controlPoints[n][(CURVE_LENGTH - 1)]).subtract(vec1)).scaleBy(TANGENT_OFFSET);
  122. (vec3 = vec3.add(vec2)).w = 1;
  123. _controlPoints[n][0] = vec2;
  124. _controlPoints[n][1] = vec3;
  125. var nn : uint;
  126. if (n != 0) {
  127. nn = 2;
  128. while (nn < CURVE_LENGTH) {
  129. var o : Vector3D = _controlPoints[0][nn];
  130. _controlPoints[n][nn] = new Vector3D(o.x - N_AMP + N_AMP2 * Math.random(), o.y - N_AMP + N_AMP2 * Math.random(), o.z - N_AMP + N_AMP2 * Math.random(), o.w);
  131. ++nn;
  132. }
  133. } else {
  134. nn = 2;
  135. while (nn < CURVE_LENGTH) {
  136. _controlPoints[n][nn] = new Vector3D(OFFSET + Math.random() * (width - 2 * OFFSET), OFFSET + Math.random() * (height - 2 * OFFSET), OFFSET + Math.random() * (height - 2 * OFFSET), 1);
  137. ++nn;
  138. }
  139. }
  140. ++n;
  141. }
  142. }
  143. }
  144. }
  145. class BezierMath {
  146. private const FACTORIAL_MAXEXACT : Number = 20;
  147. private var combi : Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>(13, true);
  148. private var fcache : Vector.<Number>;
  149. public function BezierMath() {
  150. combi[0] = Vector.<Number>([0]);
  151. combi[1] = Vector.<Number>([1]);
  152. combi[2] = Vector.<Number>([1, 1]);
  153. combi[3] = Vector.<Number>([1, 2, 1]);
  154. combi[4] = Vector.<Number>([1, 3, 3, 1]);
  155. combi[5] = Vector.<Number>([1, 4, 6, 4, 1]);
  156. combi[6] = Vector.<Number>([1, 5, 10, 10, 5, 1]);
  157. combi[7] = Vector.<Number>([1, 6, 15, 20, 15, 6, 1]);
  158. combi[8] = Vector.<Number>([1, 7, 21, 35, 35, 21, 7, 1]);
  159. combi[9] = Vector.<Number>([1, 8, 28, 56, 70, 56, 28, 8, 1]);
  160. combi[10] = Vector.<Number>([1, 9, 36, 84, 126, 126, 84, 36, 9, 1]);
  161. combi[11] = Vector.<Number>([1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1]);
  162. combi[12] = Vector.<Number>([1, 11, 56, 165, 330, 462, 462, 330, 165, 56, 11, 1]);
  163. fcache = new Vector.<Number>(FACTORIAL_MAXEXACT, true);
  164. var fact : Number = 1;
  165. fcache[0] = 1;
  166. for (var tmp : int = 1; tmp < FACTORIAL_MAXEXACT; tmp++) {
  167. fact *= tmp;
  168. fcache[tmp] = fact;
  169. }
  170. }
  171. public function bezier(t : Number, controlPoints : Vector.<Vector3D>) : Vector3D {
  172. var out : Vector3D = new Vector3D();
  173. var coefficients : Vector.<Number> = new Vector.<Number>(0, false);
  174. var len : Number = controlPoints.length;
  175. if (combi[len] == null) {
  176. combi[len] = new Vector.<Number>(0, false);
  177. var n : uint = len;
  178. while (n-- != 0) {
  179. combi[len].push(fcache[len - 1] / (fcache[n] * fcache[len - 1 - n]));
  180. }
  181. }
  182. n = 0;
  183. while (n <= (len - 1)) {
  184. coefficients[n] = combi[len][n] * Math.pow(t, n) * Math.pow((1 - t), (len - 1) - n);
  185. ++n;
  186. }
  187. var delta : Number = 0;
  188. n = len;
  189. while (n-- != 0) {
  190. out.x += coefficients[n] * controlPoints[n].x * controlPoints[n].w;
  191. out.y += coefficients[n] * controlPoints[n].y * controlPoints[n].w;
  192. out.z += coefficients[n] * controlPoints[n].z * controlPoints[n].w;
  193. delta += coefficients[n] * controlPoints[n].w;
  194. }
  195. out.x = out.x / delta;
  196. out.y = out.y / delta;
  197. out.z = out.z / delta;
  198. return out;
  199. }
  200. }
  201. class EffectContainer extends Sprite {
  202. public var blur : Boolean = false;
  203. public var colorIntegrity : Boolean = false;
  204. public var intensity : Number = 4;
  205. public var passes : uint = 6;
  206. public var rasterQuality : String = null;
  207. public var scale : Number = 2;
  208. public var smoothing : Boolean = true;
  209. public var srcX : Number;
  210. public var srcY : Number;
  211. protected var _blurFilter : BlurFilter = new BlurFilter(2, 2);
  212. protected var _emission : DisplayObject;
  213. protected var _occlusion : DisplayObject;
  214. protected var _ct : ColorTransform = new ColorTransform;
  215. protected var _halve : ColorTransform = new ColorTransform(0.5, 0.5, 0.5);
  216. protected var _occlusionLoResBmd : BitmapData;
  217. protected var _occlusionLoResBmp : Bitmap;
  218. protected var _baseBmd : BitmapData;
  219. protected var _bufferBmd : BitmapData;
  220. protected var _lightBmp : Bitmap = new Bitmap;
  221. protected var _bufferSize : uint = 0x8000;
  222. protected var _bufferRect : Rectangle = new Rectangle;
  223. protected var _viewportWidth : uint;
  224. protected var _viewportHeight : uint;
  225. protected var _mtx : Matrix = new Matrix;
  226. protected var _zero : Point = new Point;
  227. public function EffectContainer(width : uint, height : uint, emission : DisplayObject, occlusion : DisplayObject = null) {
  228. if (!emission) throw(new Error("emission DisplayObject must not be null."));
  229. addChild(_emission = emission);
  230. if (occlusion) addChild(_occlusion = occlusion);
  231. setViewportSize(width, height);
  232. _lightBmp.blendMode = BlendMode.ADD;
  233. addChild(_lightBmp);
  234. srcX = width / 2;
  235. srcY = height / 2;
  236. }
  237. public function setViewportSize(width : uint, height : uint) : void {
  238. _viewportWidth = width;
  239. _viewportHeight = height;
  240. scrollRect = new Rectangle(0, 0, width, height);
  241. _updateBuffers();
  242. }
  243. public function setBufferSize(size : uint) : void {
  244. _bufferSize = size;
  245. _updateBuffers();
  246. }
  247. protected function _updateBuffers() : void {
  248. var aspect : Number = _viewportWidth / _viewportHeight;
  249. _bufferRect.height = int(Math.max(1, Math.sqrt(_bufferSize / aspect)));
  250. _bufferRect.width = int(Math.max(1, _bufferRect.height * aspect));
  251. dispose();
  252. _baseBmd = new BitmapData(_bufferRect.width, _bufferRect.height, false, 0);
  253. _bufferBmd = new BitmapData(_bufferRect.width, _bufferRect.height, false, 0);
  254. _occlusionLoResBmd = new BitmapData(_bufferRect.width, _bufferRect.height, true, 0);
  255. _occlusionLoResBmp = new Bitmap(_occlusionLoResBmd);
  256. }
  257. public function render(e : Event = null) : void {
  258. if (!(_lightBmp.visible = intensity > 0)) return;
  259. var mul : Number = colorIntegrity ? intensity : intensity / (1 << passes);
  260. _ct.redMultiplier = _ct.greenMultiplier = _ct.blueMultiplier = mul;
  261. _drawLoResEmission();
  262. if (_occlusion) _eraseLoResOcclusion();
  263. var s : Number = 1 + (scale - 1) / (1 << passes);
  264. var tx : Number = srcX / _viewportWidth * _bufferRect.width;
  265. var ty : Number = srcY / _viewportHeight * _bufferRect.height;
  266. _mtx.identity();
  267. _mtx.translate(-tx, -ty);
  268. _mtx.scale(s, s);
  269. _mtx.translate(tx, ty);
  270. _applyEffect(_baseBmd, _bufferRect, _bufferBmd, _mtx, passes);
  271. _lightBmp.bitmapData = _baseBmd;
  272. _lightBmp.width = _viewportWidth;
  273. _lightBmp.height = _viewportHeight;
  274. _lightBmp.smoothing = smoothing;
  275. }
  276. protected function _drawLoResEmission() : void {
  277. _copyMatrix(_emission.transform.matrix, _mtx);
  278. _mtx.scale(_bufferRect.width / _viewportWidth, _bufferRect.height / _viewportHeight);
  279. _baseBmd.fillRect(_bufferRect, 0);
  280. _baseBmd.draw(_emission, _mtx, colorIntegrity ? null : _ct);
  281. }
  282. protected function _eraseLoResOcclusion() : void {
  283. _occlusionLoResBmd.fillRect(_bufferRect, 0);
  284. _copyMatrix(_occlusion.transform.matrix, _mtx);
  285. _mtx.scale(_bufferRect.width / _viewportWidth, _bufferRect.height / _viewportHeight);
  286. _occlusionLoResBmd.draw(_occlusion, _mtx);
  287. _baseBmd.draw(_occlusionLoResBmp, null, null, BlendMode.ERASE);
  288. }
  289. public function startRendering() : void {
  290. addEventListener(Event.ENTER_FRAME, render);
  291. }
  292. public function stopRendering() : void {
  293. removeEventListener(Event.ENTER_FRAME, render);
  294. }
  295. protected function _applyEffect(bmd : BitmapData, rect : Rectangle, buffer : BitmapData, mtx : Matrix, passes : uint) : void {
  296. while (passes--) {
  297. if (colorIntegrity) bmd.colorTransform(rect, _halve);
  298. buffer.copyPixels(bmd, rect, _zero);
  299. bmd.draw(buffer, mtx, null, BlendMode.ADD, null, true);
  300. mtx.concat(mtx);
  301. }
  302. if (colorIntegrity) bmd.colorTransform(rect, _ct);
  303. if (blur) bmd.applyFilter(bmd, rect, _zero, _blurFilter);
  304. }
  305. public function dispose() : void {
  306. if (_baseBmd) _baseBmd.dispose();
  307. if (_occlusionLoResBmd) _occlusionLoResBmd.dispose();
  308. if (_bufferBmd) _bufferBmd.dispose();
  309. _baseBmd = _occlusionLoResBmd = _bufferBmd = _lightBmp.bitmapData = null;
  310. }
  311. protected function _copyMatrix(src : Matrix, dst : Matrix) : void {
  312. dst.a = src.a;
  313. dst.b = src.b;
  314. dst.c = src.c;
  315. dst.d = src.d;
  316. dst.tx = src.tx;
  317. dst.ty = src.ty;
  318. }
  319. }
  320. class VolumetricPointLight extends EffectContainer {
  321. protected var _colors : Array;
  322. protected var _alphas : Array;
  323. protected var _ratios : Array;
  324. protected var _gradient : Shape = new Shape;
  325. protected var _gradientMtx : Matrix = new Matrix;
  326. protected var _gradientBmp : Bitmap = new Bitmap;
  327. protected var _lastSrcX : Number;
  328. protected var _lastSrcY : Number;
  329. protected var _lastIntensity : Number;
  330. protected var _lastColorIntegrity : Boolean = false;
  331. protected var _gradientLoResBmd : BitmapData;
  332. protected var _gradientLoResDirty : Boolean = true;
  333. public function VolumetricPointLight(width : uint, height : uint, occlusion : DisplayObject, colors : Array, alphas : Array, ratios : Array) {
  334. _colors = colors;
  335. _ratios = ratios;
  336. _alphas = alphas;
  337. super(width, height, _gradientBmp, occlusion);
  338. }
  339. protected function _drawGradient() : void {
  340. var size : Number = 2 * Math.sqrt(_viewportWidth * _viewportWidth + _viewportHeight * _viewportHeight);
  341. _gradientMtx.createGradientBox(size, size, 0, -size / 2 + srcX, -size / 2 + srcY);
  342. _gradient.graphics.clear();
  343. _gradient.graphics.beginGradientFill(GradientType.RADIAL, _colors, _alphas, _ratios, _gradientMtx);
  344. _gradient.graphics.drawRect(0, 0, _viewportWidth, _viewportHeight);
  345. _gradient.graphics.endFill();
  346. if (_gradientBmp.bitmapData) _gradientBmp.bitmapData.dispose();
  347. _gradientBmp.bitmapData = new BitmapData(_viewportWidth, _viewportHeight, true, 0);
  348. _gradientBmp.bitmapData.draw(_gradient);
  349. }
  350. override protected function _drawLoResEmission() : void {
  351. if (_gradientLoResDirty) {
  352. super._drawLoResEmission();
  353. _gradientLoResBmd.copyPixels(_baseBmd, _bufferRect, _zero);
  354. _gradientLoResDirty = false;
  355. } else {
  356. _baseBmd.copyPixels(_gradientLoResBmd, _bufferRect, _zero);
  357. }
  358. }
  359. override protected function _updateBuffers() : void {
  360. super._updateBuffers();
  361. _gradientLoResBmd = new BitmapData(_bufferRect.width, _bufferRect.height, false, 0);
  362. _gradientLoResDirty = true;
  363. }
  364. override public function setViewportSize(width : uint, height : uint) : void {
  365. super.setViewportSize(width, height);
  366. _drawGradient();
  367. _gradientLoResDirty = true;
  368. }
  369. override public function render(e : Event = null) : void {
  370. var srcChanged : Boolean = _lastSrcX != srcX || _lastSrcY != srcY;
  371. if (srcChanged) _drawGradient();
  372. _gradientLoResDirty ||= srcChanged;
  373. _gradientLoResDirty ||= (!colorIntegrity && (_lastIntensity != intensity));
  374. _gradientLoResDirty ||= (_lastColorIntegrity != colorIntegrity);
  375. _lastSrcX = srcX;
  376. _lastSrcY = srcY;
  377. _lastIntensity = intensity;
  378. _lastColorIntegrity = colorIntegrity;
  379. super.render(e);
  380. }
  381. override public function dispose() : void {
  382. super.dispose();
  383. if (_gradientLoResBmd) _gradientLoResBmd.dispose();
  384. _gradientLoResBmd = null;
  385. }
  386. }