Improved Papervision3D Hello World
I thought I’d delve a little deeper into the various materials which Papervision3D provides and have updated my hello world to use a Phong shader to improve the way that the Earth looks. Using the Phong shader, I’ve put a bump map on which makes the mountains etc pop out a little and it also means that there is light and shadow.
If you’re interested in the code, I’ve posted it below. I built it using Flash Builder, if you’re using Flash CS, it might not work (I haven’t tested it).
The bitmap files I used are these:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
package { import flash.display.BitmapData; import flash.display.Sprite; import flash.display.Stage; import flash.events.Event; import org.papervision3d.cameras.Camera3D; import org.papervision3d.lights.PointLight3D; import org.papervision3d.materials.BitmapMaterial; import org.papervision3d.materials.shaders.PhongShader; import org.papervision3d.materials.shaders.ShadedMaterial; import org.papervision3d.materials.shaders.Shader; import org.papervision3d.materials.special.ParticleMaterial; import org.papervision3d.objects.DisplayObject3D; import org.papervision3d.objects.primitives.Sphere; import org.papervision3d.objects.special.ParticleField; import org.papervision3d.render.BasicRenderEngine; import org.papervision3d.scenes.Scene3D; import org.papervision3d.view.Viewport3D; [SWF (backgroundColor="0x000000", frameRate="30")] public class Main extends Sprite { // PV3D core bits private var scene : Scene3D; private var camera : Camera3D; private var renderer : BasicRenderEngine; private var viewport : Viewport3D; // bits for drawing/shading the Earth private var earth : Sphere; private var earthShader : Shader; private var sunLight : PointLight3D; // bitmap materials for the Earth [Embed(source="assets/earth_map.jpg")] private var EarthMap : Class; [Embed(source="assets/earthbump.jpg")] private var EarthBump : Class; public function Main() { init(); } private function init () : void { // setup PV3D bits camera = new Camera3D(); scene = new Scene3D(); renderer = new BasicRenderEngine(); viewport = new Viewport3D(0,0,true,false); addChild (viewport); // build the solar system createObjects (); // animation loop addEventListener (Event.ENTER_FRAME, loop); } private function createObjects () : void { // light from the sun. Will be used by the Phong Shader to shade the Earth sunLight = new PointLight3D(); sunLight.z = -1000; sunLight.x = -300; // Map of the world var earthmap : BitmapData = new EarthMap().bitmapData; var earthmapmat : BitmapMaterial = new BitmapMaterial (earthmap); // bump map (to add texture) var earthbump : BitmapData = new EarthBump().bitmapData; // Phong shader uses the sunLight and bump map earthShader = new PhongShader (sunLight, 0xFFFFFF, 0x333333, 0, earthbump); // Apply the Phong material to the Earth var earthmat : ShadedMaterial = new ShadedMaterial (earthmapmat, earthShader); earth = new Sphere (earthmat, 300, 16, 10); // tilt the axis earth.rotationZ = -12; // add the Earth to the scene scene.addChild(earth); // build a star field. var particlemat : ParticleMaterial = new ParticleMaterial (0xFFFFFF, 1, ParticleMaterial.SHAPE_CIRCLE); var stars : DisplayObject3D = new ParticleField (particlemat, 1000, 4, 2000, 2000, 1000); // move the star field to behind the Earth, then add it to the scene stars.z = 500; scene.addChild (stars); } private function loop (e : Event) : void { // rotate the Earth by 2 degrees earth.yaw (-2); // render the scene renderer.renderScene (scene, camera, viewport); } } } |


