3D World – SimpleDemo

Posted on January 13, 2009
Filed Under experiments, Opensource | 4 Comments

experiment_se

Simple demo showing the world in 3D, using a bitmap as texture combined with a phongshader and a shadedmaterial which combines the bitmap and the phongshader. For sure its not new, but its still fun.

Here’s the code:

  1. package
  2. {
  3.  // tweenmax
  4. import gs.easing.Back;
  5.  import gs.TweenMax;
  6.  
  7.  // papervision
  8.  import org.papervision3d.materials.shaders.ShadedMaterial;
  9.  import org.papervision3d.materials.shaders.PhongShader;
  10.  import org.papervision3d.core.effects.view.ReflectionView;
  11.  import org.papervision3d.lights.PointLight3D;
  12.  import org.papervision3d.materials.BitmapMaterial;
  13.  import org.papervision3d.objects.primitives.Sphere;
  14.  
  15.  // flash
  16.  import flash.filters.BlurFilter;
  17.  import flash.display.BitmapData;
  18.  import flash.events.Event;
  19.  
  20.  public class BitmapEarth extends ReflectionView
  21.  {
  22.   private var _light:PointLight3D;
  23.   private var _sphere:Sphere;
  24.  
  25.   public function BitmapEarth()
  26.   {
  27.    initialize();
  28.   }
  29.  
  30.   private function initialize():void
  31.   {
  32.    // setup reflection
  33.    viewportReflection.filters = [new BlurFilter(6,6,1)];
  34.    setReflectionColor(.5, .5, .5);
  35.    surfaceHeight = -200;
  36.  
  37.    // camera
  38.    camera.z = -300;
  39.  
  40.    // create light
  41.    _light = new PointLight3D();
  42.  
  43.    // create bitmap material from library asset
  44.    var bmClass:Class = getDefinitionByName("EarthTexture") as Class;
  45.    var bmd:BitmapData = new bmClass(0,0);
  46.    var bMaterial:BitmapMaterial = new BitmapMaterial(bmd, true);
  47.  
  48.    // create shader
  49.    var pShader:PhongShader = new PhongShader(_light, 0xFFFFFF, 0×666666, 20);
  50.  
  51.    //create shaded material from my bitmap from library and the phongshader
  52.    var shadedMaterial:ShadedMaterial = new ShadedMaterial(bMaterial, pShader);
  53.  
  54.    // create sphere and apply shaded material
  55.    _sphere = new Sphere(shadedMaterial, 100, 24, 18);
  56.    _sphere.x = 700;
  57.    _sphere.z = -1200;
  58.    scene.addChild(_sphere);
  59.  
  60.    // render
  61.    addEventListener(Event.ENTER_FRAME, update);
  62.  
  63.    // move sphere to center
  64.    TweenMax.to(_sphere, 5, {x: 0, y: 0, z: 0, ease: Back.easeOut});
  65.   }
  66.  
  67.   private function update(event:Event):void
  68.   {
  69.    camera.lookAt(_sphere);
  70.  
  71.    _sphere.yaw(1.5); // y-axis
  72. //   _sphere.pitch(1.25); // x-axis
  73. //   _sphere.roll(1);// z-axis
  74.  
  75.    singleRender();
  76.   }
  77.  }
  78. }

The only thing you have to do now is add it to your stage:

  1. var earth:BitmapEarth = new BitmapEarth();
  2. addChild(earth);

Comments

4 Responses to “3D World – SimpleDemo”

  1. WTF on February 16th, 2009 6:33 pm

    Fuck man!
    Do you care to explain those things!?
    We do not care what you can do with papervision, but HOW do you do it!

  2. Jan Willem on February 16th, 2009 8:24 pm

    @WTF: You are far to kind, offcourse I should have added the code. But there are many ways to ask for it!

  3. john on March 17th, 2009 12:05 pm

    dude what version of papervision are you using?

  4. Jan Willem on March 17th, 2009 11:38 pm

    @John: I use the latest revision from the repository which is at this point r894. and Papervision version 2.0.0

-->