Stage3D and AGAL

3,857 views
3,693 views

Published on

Fundamentals of Stage3D and AGAL shaders.

Published in: Technology, Business
0 Comments
13 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,857
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
38
Comments
0
Likes
13
Embeds 0
No embeds

No notes for slide

Stage3D and AGAL

  1. 1. © Daniel Freeman 2013Stage3Dand AGALHFUG mix May 18Saturday, 18 May 2013
  2. 2. • What is Stage3D ?• Principles of 3D graphics• Introduction to AGAL• Shader Examples• Extrusion and LathesSaturday, 18 May 2013
  3. 3. What is Stage3D ?Saturday, 18 May 2013
  4. 4. “9 out of 10 top Facebook games are deliveredusing Flash Player.The biggest game networks in the world, likeDNA, GREE, and Tencent, deliver their gameswith Flash technology.The iOS App Store,Android Market, andAmazon Appstore feature best-selling, #1games built with Flash technologies.”http://gaming.adobe.com/whyflash/Saturday, 18 May 2013
  5. 5. Stage3DOpen GLMacStage3DDirectXWindowsStage3DOpen GL ESMobileStage3DSwiftShaderNo GPUUbiquitousSupport for older hardware (Intel GMA)Saturday, 18 May 2013
  6. 6. Low Levelmov ft3, v1sub ft0.xy, v0.xy, fc4.xxsub ft0.z, fc4.x, v2.xabs ft1.xy, ft0.xysub ft1.xy, ft1.xy, ft0.zzmul ft2.xy, ft1.xy, ft1.xyadd ft2.x, ft2.x, ft2.ysqt ft2.x, ft2.xadd ft2.z, fc4.w, v2.xsub ft2.z, ft2.z, ft2.xpow ft3.w, ft2.z, fc4.zsat ft3.w, ft3.wmov oc, ft3Saturday, 18 May 2013
  7. 7. Principles of 3DGraphicsSaturday, 18 May 2013
  8. 8. -x +x-y+y+z-z{x,y,z}originvertexVerticesSaturday, 18 May 2013
  9. 9. -x +x-y+y+z-zorigintriangleTrianglesSaturday, 18 May 2013
  10. 10. 3D MeshesSaturday, 18 May 2013
  11. 11. winding / backface cullingSaturday, 18 May 2013
  12. 12. depth bufferingSaturday, 18 May 2013
  13. 13. Saturday, 18 May 2013
  14. 14. Saturday, 18 May 2013
  15. 15. z = 0z = 1-1 +1-1+12d2d-1 +1-1+1WorldCoordinatesStageCoordinatesSaturday, 18 May 2013
  16. 16. Introduction to AGALSaturday, 18 May 2013
  17. 17. Vertex ShaderTriangleAssemblyViewportClippingtransformedverticestrianglestreamRasteriserFragmentShaderFrameBufferfragmentstreampixelstreamProgrammableindicesverticestexturesSaturday, 18 May 2013
  18. 18. VersatileVertexShader FragmentShaderv0-v7InterpolatedRegistersVertexshader runsonce pervertexFragmentshader runsonce perfragmentSaturday, 18 May 2013
  19. 19. Registersva0-va7 Attribute Registersvc0-vc127 Constant Registersfc0-fc127 Constant Registersfs0-fs7 Sampler Registersv0-v7 Varying Registersop Output Pointoc Output ColourSaturday, 18 May 2013
  20. 20. VertexShaderFragmentShaderopocv0-v7vc0-vc127fc0-fc127(Interpolated)va0-va7fs0-fs7vt0-vt7ft0-ft7RegistersSaturday, 18 May 2013
  21. 21. add v0, va0, va2sub v0, va0, va2mul v0, va0, va2div v0, va0, va2m44 v0, va0, vc0Saturday, 18 May 2013
  22. 22. pow ft2.x, ft0.y, fc0.wmul ft2.xyz, ft0.xxy, fc0.xxxx y z wSaturday, 18 May 2013
  23. 23. mul ft2.xyz, ft0.xxy, fc0.xxxft2.x = ft0.x * fc0.xft2.y = ft0.x * fc0.xft2.z = ft0.y * fc0.xx y z wSaturday, 18 May 2013
  24. 24. Saturday, 18 May 2013
  25. 25. Shader ExamplesSaturday, 18 May 2013
  26. 26. Example 1 : Solid ColourGreenSquare1Saturday, 18 May 2013
  27. 27. protected static const VERTICES:Vector.<Number> = Vector.<Number> ([ // X, Y, Z -0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.5, 0.5, 0.0, -0.5, 0.5, 0.0, ]); protected static const N:int = 3; protected static const INDICES:Vector.<uint> = Vector.<uint> ([ 0, 1, 2, 0, 2, 3 ]);3(-0.5, -0.5, 0)(-0.5, 0.5, 0)(0.5, -0.5, 0)(0.5, 0.5, 0)0 120 12023Saturday, 18 May 2013
  28. 28. vertex shadermov op, va0 output pointfragment shadermov oc, fc0 output colour_context3D.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, // fc0Vector.<Number>([ 1.0, 0.0, 0.0, 1.0]) // = Red);Saturday, 18 May 2013
  29. 29. Example 2 : Gradient FillGreenSquare3Saturday, 18 May 2013
  30. 30. protected static const VERTICES:Vector.<Number> = Vector.<Number> ([ // X, Y, Z, r, g, b -0.5, -0.5, 0.0, 0, 1, 0, 0.5, -0.5, 0.0, 1, 0, 0, 0.5, 0.5, 0.0, 1, 1, 1, -0.5, 0.5, 0.0, 0, 0, 1, ]); protected static const N:int = 6; protected static const INDICES:Vector.<uint> = Vector.<uint> ([ 0, 1, 2, 0, 2, 3 ]);3(-0.5, -0.5, 0)(-0.5, 0.5, 0)(0.5, -0.5, 0)(0.5, 0.5, 0)0 120 12023Saturday, 18 May 2013
  31. 31. vertex shadermov op, va0 output pointmov v0, va1 interpolate colourfragment shadermov oc, v0 output colourVertexShader FragmentShaderv0-v7InterpolatedRegistersSaturday, 18 May 2013
  32. 32. Example 3 : RotationGreenSquare4Saturday, 18 May 2013
  33. 33. vertex shaderm44 op, va0, vc0 transformfragment shadermov oc, fc0 output colour_transform.appendRotation( 2.0, Vector3D.Z_AXIS );_context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, //vc0_transform, true);Saturday, 18 May 2013
  34. 34. Example 4 : Gradient CubeFlash3DExperiment5Saturday, 18 May 2013
  35. 35. protected static const VERTICES:Vector.<Number> = Vector.<Number> ([ // X, Y, Z, r, g, b -0.5, -0.5, 0.5, 0, 1, 0, 0.5, -0.5, 0.5, 1, 0, 0, 0.5, 0.5, 0.5, 1, 1, 1, -0.5, 0.5, 0.5, 0, 0, 1, -0.5, -0.5, -0.5, 0, 1, 0, 0.5, -0.5, -0.5, 1, 0, 0, 0.5, 0.5, -0.5, 1, 1, 1, -0.5, 0.5, -0.5, 0, 0, 1, ]); protected static const N:int = 6; protected static const INDICES:Vector.<uint> = Vector.<uint> ([ 0, 1, 2, 0, 2, 3, 4, 6, 5, 4, 7, 6, 0, 4, 5, 0, 5, 1, 1, 5, 6, 1, 6, 2, 2, 6, 7, 2, 7, 3, 3, 7, 4, 3, 4, 0]);Saturday, 18 May 2013
  36. 36. vertex shaderm44 op, va0, vc0 transformmov v0, va1 interpolatefragment shadermov oc, v0 output colour_projectionMatrix.perspectiveFieldOfViewLH(45.0*Math.PI/180,stage.stageWidth/stage.stageHeight,0.1,1000.0);Saturday, 18 May 2013
  37. 37. Example 5 :Texture CubeFlash3DExperiment3Flash3DExperiment3aSaturday, 18 May 2013
  38. 38. protected static const VERTICES:Vector.<Number> = Vector.<Number> ([ // X, Y, Z, u, v, -1.0, -1.0, -1.0, 0, 0, 1.0, -1.0, -1.0, 0, 1, 1.0, 1.0, -1.0, 1, 1, -1.0, 1.0, -1.0, 1, 0, etc...etc..30 12uv0 11VertexShader FragmentShaderInterpolatedSaturday, 18 May 2013
  39. 39. vertex shaderm44 op, va0, vc0 transformmov v0, va1 interpolate uvfragment shadertex ft0, v0, fs0 <2d,linear,nomip>mov oc, ft0 output fragment_projectionMatrix.perspectiveFieldOfViewLH(45.0*Math.PI/180,stage.stageWidth/stage.stageHeight,0.1,1000.0);Saturday, 18 May 2013
  40. 40. Example 6 : Light SourceFlash3DExperiment7aSaturday, 18 May 2013
  41. 41. protected static const VERTICES:Vector.<Number> = Vector.<Number> ([ // X, Y, Z, nX, nY, nZ -1.0, -1.0, -1.0, 0, 0, -1.0, 1.0, -1.0, -1.0, 0, 0, -1.0, 1.0, 1.0, -1.0, 0, 0, -1.0, -1.0, 1.0, -1.0, 0, 0, -1.0, etc...etc...normalSaturday, 18 May 2013
  42. 42. vertex shaderm44 op, va0, vc0 transformm44 v0, va1, vc0 normalfragment shadernrm ft0.xzy, v0 normalisemov ft0.w, fc0.wdp3 ft0, ft0, fc1 dot productsat ft0, ft0 clampmul oc, ft0, fc0 multiply colour _context3D.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, Vector.<Number>([ 0.5, 0.5, 0.5, 1.0 ]) ); _context3D.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 1, Vector.<Number>([ X, Y, Z, 1.0 ]) );Saturday, 18 May 2013
  43. 43. Example 7 : Phong ShaderFlash3DExperimentASaturday, 18 May 2013
  44. 44. interpolatednormalsnormalSaturday, 18 May 2013
  45. 45. ABA • B = |A| |B| CosA is Half AngleB is Normal( A • B )nn = 1 n = 50n = 5= 0Saturday, 18 May 2013
  46. 46. vertex shaderm44 op, va0, vc0 transformm44 v0, va1, vc4 normalfragment shadernrm ft0.xyz, v0 normalise normalmov ft0.w, fc0.w set w to 1dp3 ft0.x, ft0, fc0 dot productsat ft0.x, ft0.x clamppow ft1.x, ft0.x, fc3.w dot product to powermul ft2.xyz, fc2.xyz, ft0.xxx diffuse colouradd ft2.xyz, fc1.xyz, ft2.xyz add ambient colourmul ft3.xyz, fc3.xyz, ft1.xxx specular colouradd oc, ft2.xyz, ft3.xyz combine and outputfc0 Half Angle Vector fc1 Ambient Colourfc2 Diffuse Colour fc3 Specular ColourSaturday, 18 May 2013
  47. 47. Example 8 :AGAL AnimationFlash3DExperimentESaturday, 18 May 2013
  48. 48. AGAL Animation• Advantageous to animate vertices in AGAL• AS3 is slow. AGAL is fast.• Manipulation of vertices in AS3 is slow• Computing a transformation matrix is slow• Better to interpolate vertices in AGALSaturday, 18 May 2013
  49. 49. AGAL AnimationManipulateVerticeswhere u = 1 and v = 1Saturday, 18 May 2013
  50. 50. vertex shadermov v1, va1 // interpolate uv m44 vt0, va0, vc0 // translate vertexm44 vt1, va0, vc4 // translate normalmov v0, vt1 // interpolate normalmul vt1.xyz, vc8.xxx, vt1.xyz // multiply t by normalmul vt2.x, va1.x, va1.y // multiply u by v// if ( u==1 && v==1)mul vt1.xyz, vt2.xxx, vt1.xyz // push vertex outadd op, vt0, vt1 // addva0 Vertices (Normals) va1 U-Vvc0 transform matrix vc1 rotation transformvc8.x interpolationSaturday, 18 May 2013
  51. 51. Extrude and LatheSaturday, 18 May 2013
  52. 52. LatheExtrudeSaturday, 18 May 2013
  53. 53. QuestionsSaturday, 18 May 2013

×