Muse Get mp3 Ozzy Osbourne Large MP store Artist list Alexandra Burke Dave Matthews Band Nightwish Eva Cassidy Amy Winehouse Jefferson Airplane Coil Mylene Farmer Jason Williams MP3 list

Monthly Archive for January, 2008Page 2 of 2

5. Basic Texturing

**UPDATE**
This tutorial has been updated:
view-update

Before starting this tutorial, please note that it follows on from Tutorial 4 - Basic Template Usage. If you haven't followed that tutorial, I suggest that you do so.

Be modifying a few lines, we're going to give our spinning cone a texture. The result will be something like this:

Firstly, open up the project that you made in tutorial 4. I'm going to show you how to use the BitmapFileMaterial, so before we start, you need to import org.papervision3d.materials.BitmapFileMaterial. To do this, find where you have two lines starting with import and just below them, add the line:

import org.papervision3d.materials.BitmapFileMaterial;

That line will import the BitmapFileMaterial package into your project, so that you can use it.

Now, we need to add a BitmapFileMaterial to our cone. We can do this when we create it, but first I'll explain BitmapFileMaterial.

BitmapFileMaterial is a papervision 3d material which takes a url of an image file, and creates a texture from it. This is very useful when loading collada models, and it opens possibilities of having things like user-uploaded textures.

When you create a new BitmapFileMaterial object, you pass it a URL from which the texture will be fetched.

To add the material to our cone, we're going to pass a BitmapFileMaterial object to the cone when we create it. This is really simple to do, simply change the line:

public var cone:Cone = new Cone();

to:

public var cone:Cone = new Cone(
    new BitmapFileMaterial(
        "http://papervision2.com/wp-content/downloads/ourtex.jpg"
    )
);

This will now get the file from my server and use it as the texture on your cone.

Now, when you run your code you'll see the red and white texture on your cone. Simple!

My final code looks like this:

Actionscript:
  1. package  {
  2.    
  3.     import PaperBase;
  4.     import org.papervision3d.objects.primitives.Cone;
  5.     import org.papervision3d.materials.BitmapFileMaterial;
  6.    
  7.     public class Main extends PaperBase {
  8.        
  9.         public var cone:Cone = new Cone(new BitmapFileMaterial("http://papervision2.com/wp-content/downloads/ourtex.jpg"), 20, 200);
  10.         // I've added a width and height to change the shape of my cone.
  11.        
  12.         public function Main() {
  13.             init();
  14.         }
  15.        
  16.         override protected function init3d():void {
  17.             cone.scale = 3;
  18.             cone.pitch( -30);
  19.             default_scene.addChild(cone);
  20.         }
  21.        
  22.         override protected function processFrame():void {
  23.             cone.yaw(20);
  24.             // Here, I've made my cone spin faster by increasing the amount sent to yaw();
  25.         }
  26.        
  27.     }
  28.    
  29. }

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

4. Basic Template Usage

**UPDATE**
Creating and using this custom template is no longer needed. Papervision now includes a BasicView.as see updated tutorial:
Getting started using BasicView.as
view-update

Now that you've created you're base template, you'll probably be eager to try it out.
Let's start by simply creating a rotating cone, much like this one:

By using the base class that you created, you can really quickly build this simple example.

First, create a new project in FlashDevelop.

Call it "ConeExample" or something similar.

Go to "Project > Properties..." and type "cone.swf" into the Output File field.

Under the Classpaths tab, you need to add two classpaths this time.Firstly in the classpaths tab, click Add Classpath...

Navigate to your GreatWhite source code directory and click OK

Now we need to add our template to the project. Click on "Add Classpath..." again, and this time go into the directory where you saved your base template. This will probably be "My Documents/PaperBase", if you called the project the same name as mine.

Find the folder that contains PaperBase.as and click OK.

We now need to create the code.

Right click on "ConeExample" under the project tree, go to Add and choose New Class.

Name the class Main.as.

Right click on Main.as, under ConeExample in the project tree, and click on Always Compile.

You're now ready to start coding.

Firstly, add the following lines to Main.as, under the line "package {"

Actionscript:
  1. import PaperBase;
  2. import org.papervision3d.objects.primitives.Cone;

Those lines will import all of the packages we need.
Next, change the line:

Actionscript:
  1. public class Main {

To

Actionscript:
  1. public class Main extends PaperBase {

This will make our project import all of the functions and code that we wrote in PaperBase.as. We can now add just a few lines to make our papervision project work.
We'll need a cone object to add to the scene. Directly underneath the line

Actionscript:
  1. public class Main extends PaperBase {

You need to insert the line:

Actionscript:
  1. public var cone:Cone = new Cone();

This line will make a new cone object, and hold it in the variable called "cone"
Now, when the project is run, the code in "public function Main() {" is called. We want to initialise our papervision skeleton when our project runs. To do this, change the function so that it looks like this:

Actionscript:
  1. public function Main()
  2. {
  3.     init();
  4. }

This will run the PaperBase init function when your project runs.
You now need to override two functions from PaperBase. These are init3d and processFrame.
We'll need to add our cone to our scene in init3d, then rotate it a bit every time processFrame is called. To do this, add the following code directly below the Main() function.

Actionscript:
  1. override protected function init3d():void {
  2.     cone.scale = 3;
  3.     // Make the cone bigger    cone.pitch(-40);
  4.     // Tilt it towards the camera    default_scene.addChild(cone);
  5.     // Add it to the scene
  6. }
  7.  
  8. override protected function processFrame():void {
  9.     cone.yaw(5);
  10.     // Rotate it a bit
  11. }

These two "override" functions will be run instead of the ones in PaperBase. When you run your project you should see the cone rotating! Try playing with the values, adding and changing bits in the two functions above.

The final code should look like this:

Actionscript:
  1. package  {
  2.    
  3.     import PaperBase;
  4.     import org.papervision3d.objects.primitives.Cone;
  5.    
  6.     public class Main extends PaperBase {
  7.        
  8.         public var cone:Cone = new Cone();
  9.        
  10.         public function Main() {
  11.             init();
  12.         }
  13.        
  14.         override protected function init3d():void {
  15.             cone.scale = 3;
  16.             cone.pitch(-40)
  17.             default_scene.addChild(cone);
  18.         }
  19.        
  20.         override protected function processFrame():void {
  21.             cone.yaw(7);
  22.         }
  23.        
  24.     }
  25.    
  26. }

Flash IDE friendly version:

Actionscript:
  1. package  {
  2.     import PaperBase;
  3.     import org.papervision3d.objects.primitives.Cone;
  4.     public class Main extends PaperBase {
  5.         public var cone:Cone = new Cone();
  6.         protected var sceneWidth:Number;
  7.         protected var sceneHeight:Number;
  8.         public function Main() {
  9.             sceneWidth = stage.stageWidth
  10.             sceneHeight = stage.stageHeight;
  11.             init(sceneWidth,sceneHeight);
  12.         }
  13.         override protected function init3d():void {
  14.             cone.scale = 3;
  15.             cone.pitch(-10)
  16.             default_scene.addChild(cone);
  17.         }
  18.         override protected function processFrame():void {
  19.             cone.yaw(7);
  20.         }
  21.     }
  22. }

*EDIT*
Added Flash IDE friendly version.
Updated 20th August 2008 to accomodate GreatWhite codebase changes

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

3. Creating a Papervision Base Template

**UPDATE**
Creating and using this custom template is no longer needed. Papervision now includes a BasicView.as see updated tutorial:
Getting started using BasicView.as
view-update

The first thing that you should do when creating a Papervision 3D project is to create a "Base Class" which will serve as a template to speed up the start of each papervision project you attempt.

1) Create a new FlashDevelop Project

Open up FlashDevelop. On the start page, click on the New: Project... Under the Recent Projects Window.

New Project....

The New Project dialog box will appear. Under Actionscript 3, click Empty Project. Type a name into the Name textbox, and tick the box labelled "Create Directory for Project".

New Project Dialog
(Click To Enlarge)

Click OK to close the dialog box. You will now see your project under the Project window. We need to add a source file to the project.

The first thing you need to do to your project is to import the papervision source so that your program can use it. From the menu at the top, choose Project, then Settings.

The Project settings dialog box will appear. Click on the Classpaths tab and click Add Classpath.

You now need to browse to the folder in which you downloaded the GreatWhite source code in tutorial number 2. Select the greatwhite folder (It should contain directories called org, fl and com) and click OK.

You should now have an icon which looks like a Lego Brick containing the GreatWhite source code under your project.

Project Tree

You now need to make a script to be executed when your program runs. Right click on your project in the project tree and goto Add > New Class.

Add New Class

Name the new class PaperBase.as and click OK. You will now have the Main.as file open in your editor. The code will look like this:

Actionscript:
  1. /**
  2. * ...
  3. * @author Default
  4. * @version 0.1
  5. */
  6.  
  7. package {
  8.    
  9.     public class PaperBase {
  10.        
  11.         public function PaperBase() {
  12.            
  13.         }
  14.        
  15.     }
  16.    
  17. }

This is a blank class. Firstly, we need to make this class compile when we run the project. To do this, right click on Main.as under the project tree and select "Always Compile".

We also need to specify the output SWF location. To do this, from the Project menu, select Properties.

On the Output tab under General, you'll see an input box labeled "Output File". Click Browse and choose where to place the output file.

Now, change the source code so that it looks this (Comments in the code explain each step of the way):

Actionscript:
  1. package  {
  2.     // These lines make differant 'pieces' available in your code.
  3.     import flash.display.Sprite; // To extend this class
  4.     import flash.events.Event; // To work out when a frame is entered.
  5.    
  6.     import org.papervision3d.view.Viewport3D; // We need a viewport
  7.     import org.papervision3d.cameras.*; // Import all types of camera
  8.     import org.papervision3d.scenes.Scene3D; // We'll need at least one scene
  9.     import org.papervision3d.render.BasicRenderEngine; // And we need a renderer
  10.    
  11.     public class PaperBase extends Sprite { //Must be "extends Sprite"
  12.        
  13.         public var viewport:Viewport3D; // The Viewport
  14.         public var renderer:BasicRenderEngine; // Rendering engine
  15.         // -- Scenes -- //
  16.         public var default_scene:Scene3D; // A Scene
  17.         // -- Cameras --//
  18.         public var default_camera:Camera3D; // A Camera
  19.        
  20.         public function init(vpWidth:Number = 800, vpHeight:Number = 600):void {
  21.             initPapervision(vpWidth, vpHeight); // Initialise papervision
  22.             init3d(); // Initialise the 3d stuff..
  23.             init2d(); // Initialise the interface..
  24.             initEvents(); // Set up any event listeners..
  25.         }
  26.        
  27.         protected function initPapervision(vpWidth:Number, vpHeight:Number):void {
  28.             // Here is where we initialise everything we need to
  29.             // render a papervision scene.
  30.             viewport = new Viewport3D(vpWidth, vpHeight);
  31.             // The viewport is the object added to the flash scene.
  32.             // You 'look at' the papervision scene through the viewport
  33.             // window, which is placed on the flash stage.
  34.             addChild(viewport); // Add the viewport to the stage.
  35.             // Initialise the rendering engine.
  36.             renderer = new BasicRenderEngine();
  37.             // -- Initialise the Scenes -- //
  38.             default_scene = new Scene3D();
  39.             // -- Initialise the Cameras -- //
  40.             default_camera = new Camera3D(); // The argument passed to the camera
  41.             // is the object that it should look at. I've passed the scene object
  42.             // so that the camera is always pointing at the centre of the scene.
  43.         }
  44.        
  45.         protected function init3d():void {
  46.             // This function should hold all of the stages needed
  47.             // to initialise everything used for papervision.
  48.             // Models, materials, cameras etc.
  49.         }
  50.        
  51.         protected function init2d():void {
  52.             // This function should create all of the 2d items
  53.             // that will be overlayed on your papervision project.
  54.             // User interfaces, Heads up displays etc.
  55.         }
  56.        
  57.         protected function initEvents():void {
  58.             // This function makes the onFrame function get called for
  59.             // every frame.
  60.             addEventListener(Event.ENTER_FRAME, onEnterFrame);
  61.             // This line of code makes the onEnterFrame function get
  62.             // called when every frame is entered.
  63.         }
  64.        
  65.         protected function processFrame():void {
  66.             // Process any movement or animation here.
  67.         }
  68.        
  69.         protected function onEnterFrame( ThisEvent:Event ):void {
  70.             //We need to render the scene and update anything here.
  71.             processFrame();
  72.             renderer.renderScene(default_scene, default_camera, viewport);
  73.         }
  74.        
  75.     }
  76.    
  77. }

This is heavily commented, so it should explain everything that's happening.

When you run the project, you'll just see a blank screen, this is expected because you haven't added anything to the scene yet.

You can download the source file with the code comments or without using the links below:

Download Now

This file will now be used as the basis of any papervision projects that you do, and with very few lines added to that code, you can get really good results.

*EDIT*

Updated 20th August 2008, now works with the GreatWhite codebase changes

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

2. Getting the source

Welcome to tutorial number 2. This will walk you through the steps to get the Papervision 2.0a Source code.

** EDIT 10th September 2008 **

The SVN has changed recently, for now you can download the source by clicking here: http://papervision3d.googlecode.com/files/Papervision3D_2.0_beta_1_src.zip

Continue reading '2. Getting the source'

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

1. Preparing your tools

Welcome to Papervision2.com, This is the very first tutorial which will walk you through getting the tools required, and the source code for Papervision 2.0a.

Getting an IDE

There are various intergrated development enviroments available. The most widely used IDE for developing Papervision is Adobe Flex 2. You can get a 30-day free trial of Flex, or you can buy it. The downside of this is that it's expensive.

There is a free alternative if you are running windows. The kind people over at FlashDevelop have created a completley free IDE which coupled with the Flex 2 SDK, can be used as a completley free alternative to Adobe Flex. I'll walk through the steps to install FlashDevelop below.

Downloading FlashDevelop and the Flex SDK

The first thing you need to do is download the Flex SDK from the Adobe website.

You can download it using this direct link:
Flex 2 SDK (38.1 Mb)

You now need to download a build of FlashDevelop from their website.
I will be using version 3.0.0 Beta5 in my tutorials, but it shouldn't matter which version you choose.

You can download the version that I have using this direct link:
FlashDevelop 3.0.0 Beta5 (3.43Mb)

Installing FlashDevelop

Note: This installation requires the Java Runtime Environment version 1.6. If you don't have this, download it here:
http://filehippo.com/download_java_runtime/

Firstly, double click on the FlashDevelop installer. You should see the setup screen:
FlashDevelop Install Stage 1

Click Next, read and agree with the License agreement, then you will be presented with this page:
FlashDevelop Installer 2

I ticked the box to allow me to run multiple instances of the application, this is completley up to you.

Click Next, then Install.

FlashDevelop is now installed on your system. You still need to set it up with the Flex SDK to be able to use it to develop Papervision 3d.

Installing the Flex SDK

Firstly, you need to extract the files from the Flex SDK zip file that you downloaded.

I extracted all of the files to "C:\FlexSDK\" but you can extract them where you like.

Run FlashDevelop. From the menu at the very top of the application, click on Tools, then from the pull-down menu, Select "Program Settings". You will now have the FlashDevelop settings window on your screen.

In the left hand list, click on "AS3Context"

In the right hand pane, you will see a list of settings. Find the "Flex SDK Location" setting and click into the box to the right.
FlashDevelop Settings - Click to enlarge

A button will appear on the right with three dots in it. Click on the three dots and select the folder which you extracted the Flex SDK files into. Make sure that you select the folder which contains the subfolders "asdoc, bin, frameworks" etc.

Click Ok in the folder selection box, then Close in the Settings dialog.

FlashDevelop is now set up and ready to use.

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

Papervision 2.0

Welcome to Papervision2.com

Over the next few weeks, I will be posting tutorials for Papervision 2.0 Alpha (GreatWhite).

I will take a step-by-step approach covering each aspect of Papervision 3d as we go along.

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post


Follow WovenCharlie on Twitter

Flash and the City banner
2010 Flash And The City Speaker

RSS Feed