Papervision Custom Primitive Source code for use with cool particle field effect.

Papervision Particle field

Carlos Lunetta was kind enough to send me his custom primitive and vertex particle classes for this blog entry. These are similar to the ones that he used for the Terra TV website.  In this example that concept is rather simple. You create a primitive object (Plane, Cube, Sphere) and run a for loop on the verticles.

As shown here:

Actionscript:
  1. for (var i:int = 0; i <disc.geometry.vertices.length; i++)
  2. {
  3. var discVert:Vertex3D = disc.geometry.vertices[i];
  4. }

Link to my example

Source Code

Now that you are looping through the verticles of this primitive you are now privy to information such as the x y and z locations of each point. So what can we do with that.....

One thing we can do is attach particles to it.. such as

Actionscript:
  1. for (var i:int = 0; i <disc.geometry.vertices.length; i++)
  2. {
  3. var discVert:Vertex3D = disc.geometry.vertices[i];
  4.  
  5. var vertexParticle:VertexParticle = new VertexParticle(particalMaterial, 5, discVert);
  6.  
  7. vertexParticle.x = discVert.x;
  8. vertexParticle.y = discVert.y;
  9. vertexParticle.z = discVert.z;
  10.  
  11. myParticles.addParticle(vertexParticle);
  12. }

Doing this lends itself to some neat particle field effects such as the one shown above. Of course you can mess around with the settings to optimize it for your needs.

Here is how you put it all together:

Actionscript:
  1. package com.cs54.papervision
  2. {
  3.  
  4. import flash.events.Event;
  5.  
  6. import org.papervision3d.core.geom.Particles;
  7. import org.papervision3d.core.geom.renderables.Particle;
  8. import org.papervision3d.core.geom.renderables.Vertex3D;
  9. import org.papervision3d.core.geom.Vertices3D;
  10. import org.papervision3d.materials.ColorMaterial;
  11. import org.papervision3d.materials.special.MovieAssetParticleMaterial;
  12. import org.papervision3d.materials.special.ParticleMaterial;
  13. import org.papervision3d.materials.utils.MaterialsList;
  14. import org.papervision3d.materials.WireframeMaterial;
  15. import org.papervision3d.objects.DisplayObject3D;
  16. import org.papervision3d.objects.primitives.Cone;
  17. import org.papervision3d.objects.primitives.Plane;
  18.  
  19. import src.primitives.Dome;
  20. import src.primitives.Disc;
  21. import src.primitives.VertexParticle;
  22.  
  23. public class PV3DScene extends PaperBase
  24. {
  25.  
  26. protected var displayContainer    :DisplayObject3D;
  27. protected var particle            :Particle
  28. protected var myParticles        :Particles;
  29. protected var domeParticles        :Particles;
  30. protected var partMat            :ParticleMaterial;
  31. protected var colorMaterial        :ColorMaterial;
  32. protected var disc                :Disc;
  33. protected var particalMaterial    :ParticleMaterial;
  34. protected var particalMaterial2    :ParticleMaterial;
  35. protected var container            :DisplayObject3D;
  36. protected var domeContainer        :DisplayObject3D;
  37. protected var dome                :Dome;
  38.  
  39. override public function init(vpWidth:Number = 800, vpHeight:Number = 600):void
  40. {
  41. super.init(vpWidth, vpHeight);
  42. default_camera.y = 800;
  43. default_camera.rotationX = 20;
  44. }
  45.  
  46. override protected function createChildren3D():void
  47. {
  48. super.createChildren3D();
  49.  
  50. container = new DisplayObject3D();
  51. domeContainer = new DisplayObject3D();
  52.  
  53. default_scene.addChild(container);
  54. default_scene.addChild(domeContainer);
  55.  
  56. default_camera.zoom                 = 50;
  57. default_camera.z                     = -3000
  58. colorMaterial                         = new ColorMaterial(0xFF0000, 1, true);
  59. colorMaterial.doubleSided             = true;
  60. particalMaterial                     = new ParticleMaterial(0xFF00CC, 100, ParticleMaterial.SHAPE_CIRCLE);
  61. particalMaterial2                     = new ParticleMaterial(0x000000, 100, ParticleMaterial.SHAPE_CIRCLE);
  62. myParticles                         = new Particles();
  63. domeParticles                        = new Particles();
  64. disc                                 = new Disc(colorMaterial, 500,15,2,3,this);
  65. dome                                = new Dome(colorMaterial, 500, 100, 10, 5, 2);
  66.  
  67. for (var i:int = 0; i <disc.geometry.vertices.length; i++)
  68. {
  69. var discVert:Vertex3D = disc.geometry.vertices[i];
  70.  
  71. var vertexParticle:VertexParticle = new VertexParticle(particalMaterial2, 5, discVert);
  72.  
  73. vertexParticle.x = discVert.x;
  74. vertexParticle.y = discVert.y;
  75. vertexParticle.z = discVert.z;
  76.  
  77. myParticles.addParticle(vertexParticle);
  78. }
  79.  
  80. for (var j:int = 0; j <dome.geometry.vertices.length; j++)
  81. {
  82. var domeVert:Vertex3D = dome.geometry.vertices[j];
  83.  
  84. var domeVertexParticle:VertexParticle = new VertexParticle(particalMaterial, 5, domeVert);
  85.  
  86. domeVertexParticle.x = domeVert.x;
  87. domeVertexParticle.y = domeVert.y;
  88. domeVertexParticle.z = domeVert.z;
  89.  
  90. domeParticles.addParticle(domeVertexParticle);
  91. }
  92.  
  93. container.addChild(myParticles)
  94. domeContainer.addChild(domeParticles)
  95.  
  96. domeContainer.y = -10
  97. default_scene.addChild(container)
  98. default_scene.addChild(domeContainer);
  99. }
  100.  
  101. override protected function renderEnterFrame(ThisEvent:Event):void
  102. {
  103. super.renderEnterFrame(ThisEvent);
  104.  
  105. if (default_camera.z <0)
  106. {
  107. default_camera.z += (0 - default_camera.z)*.01
  108. }
  109.  
  110. default_camera.y += (50 - default_camera.y)*.01;
  111.  
  112. container.yaw(1);
  113. domeContainer.yaw(1);
  114. }
  115.  
  116. }
  117.  
  118. }

Paperbase.as

Actionscript:
  1. package  com.cs54.papervision
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import org.papervision3d.view.Viewport3D;
  6. import org.papervision3d.cameras.*;
  7. import org.papervision3d.scenes.Scene3D;
  8. import org.papervision3d.render.BasicRenderEngine;
  9.  
  10. public class PaperBase extends Sprite
  11. {
  12.  
  13. public var viewport:Viewport3D;
  14. public var renderer:BasicRenderEngine;
  15. public var default_scene:Scene3D;
  16. public var default_camera:Camera3D;
  17.  
  18. public function init(vpWidth:Number = 800, vpHeight:Number = 600):void
  19. {
  20. initPapervision(vpWidth, vpHeight);
  21. createChildren3D();
  22. createChildren2D();
  23. size();
  24. initEvents();
  25. }
  26. protected function initPapervision(vpWidth:Number, vpHeight:Number):void
  27. {
  28. viewport = new Viewport3D(vpWidth, vpHeight);
  29. addChild(viewport);
  30.  
  31. renderer = new BasicRenderEngine();
  32.  
  33. default_scene = new Scene3D();
  34.  
  35. default_camera = new Camera3D();
  36. }
  37. protected function createChildren3D():void
  38. {
  39. // This function should hold all of the stages needed
  40. // to initialise everything used for papervision.
  41. // Models, materials, cameras etc.
  42. }
  43. protected function createChildren2D():void
  44. {
  45. // This function should create all of the 2d items
  46. // that will be overlayed on your papervision project.
  47. // User interfaces, Heads up displays etc.
  48. }
  49. protected function size():void
  50. {
  51.  
  52. }
  53. protected function initEvents():void
  54. {
  55. addEventListener(Event.ENTER_FRAME, renderEnterFrame);
  56. }
  57. protected function processFrame():void
  58. {
  59. // Process any movement or animation here.
  60. }
  61. protected function renderEnterFrame( ThisEvent:Event ):void
  62. {
  63. //trace("RENDERING")
  64. //We need to render the scene and update anything here.
  65.  
  66. renderer.renderScene(default_scene, default_camera, viewport);
  67. processFrame();
  68. }
  69. }
  70. }

Here are Carlos Lunetta's classes for the Dome,Disc and VertexParticle

Dome.as

Actionscript:
  1. package src.primitives
  2. {
  3. import org.papervision3d.core.geom.*;
  4. import org.papervision3d.core.geom.renderables.Vertex3D;
  5. import org.papervision3d.core.proto.*;
  6. /**
  7. * ...
  8. * @author clunetta
  9. */
  10. public class Dome extends TriangleMesh3D
  11. {
  12. private var segments:uint;
  13. private var sides:uint;
  14. private var radius:Number
  15. private var height:Number;
  16. private var sideIncrement:Number;
  17. private var currentIncrement:Number = 0;
  18.  
  19. public function Dome (material:MaterialObject3D=null, radius:Number=100, height:Number = 100, segments:uint=0, sides:uint=3, sideIncrement:Number=0, initObject:Object=null )
  20. {
  21. super(material, new Array(), new Array());
  22.  
  23. this.segments         = 1 + segments;
  24. this.sides             = sides;
  25. this.radius         = radius;
  26. this.sideIncrement     = sideIncrement;
  27. this.height         = 2 * height;
  28.  
  29. buildDome();
  30. }
  31.  
  32. private function buildDome():void
  33. {
  34. var matInstance:MaterialObject3D = material;
  35.  
  36. var i:Number, j:Number, k:Number;
  37.  
  38. var aVertice:Array = this.geometry.vertices;
  39.  
  40. var oVtx:Vertex3D;
  41. oVtx = new Vertex3D(0,height,0);
  42. aVertice.push(oVtx);
  43.  
  44. for (j = 1; j <segments; j++) {
  45.  
  46. //var hRds:Number = (segments-j)* (height/segments)
  47. var fZ:Numberheight * Math.sin((segments-(j-1))(0.5*Math.PI) / segments);
  48.  
  49. //var fZ:Number = (height)*Math.sin((j-1)*(0.5*Math.PI/segments));
  50. currentIncrement += sideIncrement;
  51. var curSides:uint = uint(Math.floor(sides + currentIncrement));
  52.  
  53. for (i = 0; i <curSides; i++) {
  54. var fRds:Number = j * radius / segments;
  55. var fX:Number = fRds*Math.sin(i*(2*Math.PI/curSides));
  56. var fY:Number = fRds*Math.cos(i*(2*Math.PI/curSides));
  57. oVtx = new Vertex3D(fY,fZ,fX);
  58. aVertice.push(oVtx);
  59. }
  60. }
  61.  
  62. this.geometry.ready = true;
  63. }
  64.  
  65. }
  66.  
  67. }

Disc.as

Actionscript:
  1. package src.primitives
  2. {
  3. import org.papervision3d.core.geom.*;
  4. import org.papervision3d.core.geom.renderables.Vertex3D;
  5. import org.papervision3d.core.proto.*;
  6. /**
  7. * ...
  8. * @author clunetta
  9. */
  10. public class Disc extends TriangleMesh3D
  11. {
  12. private var segments :uint;
  13. private var sides :uint;
  14. private var radius :Number;
  15. private var sideIncrement:Number;
  16. private var currentIncrement:Number = 0;
  17.  
  18. public function Disc (material:MaterialObject3D=null, radius:Number=100, segments:uint=0, sides:uint=3, sideIncrement:Number=0, initObject:Object=null )
  19. {
  20. super(material, new Array(), new Array());
  21. this.segments = 1+ segments;
  22. this.sides = sides;
  23. this.radius = radius;
  24. this.sideIncrement = sideIncrement;
  25.  
  26. buildDisc();
  27. }
  28.  
  29. private function buildDisc():void
  30. {
  31. var matInstance:MaterialObject3D = material;
  32.  
  33. var i:Number, j:Number, k:Number;
  34.  
  35. var aVertice:Array = this.geometry.vertices;
  36.  
  37. var fZ:Number = 0;
  38. var oVtx:Vertex3D;
  39. oVtx = new Vertex3D(0,0,0);
  40. aVertice.push(oVtx);
  41. for (j = 0; j <segments; j++) {
  42. currentIncrement += sideIncrement;
  43. var curSides:uint = uint(Math.floor(sides + currentIncrement));
  44. for (i = 0; i <curSides; i++) {
  45. var fRds:Number = (j + 1) * radius / segments;
  46. var fX:Number = fRds*Math.sin(i*(2*Math.PI/curSides));
  47. var fY:Number = fRds*Math.cos(i*(2*Math.PI/curSides));
  48. oVtx = new Vertex3D(fY,fZ,fX);
  49. aVertice.push(oVtx);
  50. }
  51.  
  52. }
  53.  
  54. this.geometry.ready = true;
  55. }
  56.  
  57. }
  58.  
  59. }

VertexParticle.as

Actionscript:
  1. package src.primitives
  2. {
  3. import org.papervision3d.core.geom.renderables.Particle;
  4. import org.papervision3d.core.geom.renderables.Vertex3D;
  5. import org.papervision3d.materials.special.ParticleMaterial;
  6.  
  7. /**
  8. * ...
  9. * @author clunetta
  10. */
  11. public class VertexParticle extends Particle
  12. {
  13.  
  14. public function VertexParticle(material:ParticleMaterial, size:Number, vertex:Vertex3D)
  15. {
  16. super(material, size);
  17. super.vertex3D = vertex;
  18. }
  19.  
  20. }
  21.  
  22. }

Source Code

1 Responses to “Papervision Custom Primitive Source code for use with cool particle field effect.”


Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word


Follow papervision2 on Twitter

RSS Feed