|
|
Hi,
In this tutorial we're going to use Tweener to move a cube around. This will teach you basics of animation, and how to use Tweener (which is awesome).
The example that I've produced is this:
So, in this example our solid red cube is trying to stay inside our green wireframe cube. Every time the red cube gets itself positioned completley inside the green cube, the green cube moves.
Tweener is the package which is smoothly moving the red cube around. The Tweener package is also firing the event to move the green cube when the tween is complete.
1: To start off, you'll need Tweener. You can download it here if you don't already have it: http://code.google.com/p/tweener/
Once you've got tweener, either add the source directory containing the "caurina" folder to your project's classpaths, or add the caurina folder into your current project.
2: Import tweener - Add this line to your imports:
import caurina.transitions.Tweener;
3: Add a Tween. You can add tweens to any parameter of any object. The Tweener package will gradually change any parameter in your code to a new value over time, so only numeric values will work nicely. Anyway, you can read a load more about tweener at the google code page above.
To add a tween, we use the code
Tweener.addTween(object, {parameter1: value, parameter2: value2, time: X, transition: "transition", onComplete: functionname});
This may look a little bit confusing at first, but it's really simple. Let's show you a real example..
Tweener.addTween(cube, { x: 1000, z: 500, time: 2, onComplete: randomize } );
The line above will add a tween. This tween will change the values "x" and "y" on the object "cube" to 1000 and 500 respectively. It will smoothly change the values over a period of 2 seconds and once the tween is complete and the cube.x is 1000 and cube.y is 500, it will trigger the "randomize" function.
Experiment with more ways to tween your objects. You can apply tweens to anything, including cameras, lights, rotations, sizes and camera zooms. Any numerical value you can think of.
Here is my source code (Uses latest version of my base class):
Actionscript:
-
package {
-
import org.papervision3d.lights.PointLight3D;
-
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
-
import org.papervision3d.materials.utils.MaterialsList;
-
import org.papervision3d.materials.WireframeMaterial;
-
import org.papervision3d.objects.primitives.Cube;
-
import org.papervision3d.objects.primitives.Plane;
-
-
import caurina.transitions.Tweener;
-
-
public class Main extends PaperBase{
-
-
private var cube:Cube;
-
private var cube2:Cube;
-
private var mat:FlatShadeMaterial = new FlatShadeMaterial(new PointLight3D(), 0xFFFFFF, 0xFF0000);
-
private var mat2:WireframeMaterial = new WireframeMaterial(0x00FF00);
-
private var plane:Plane = new Plane(null, 2000, 2000, 10, 10);
-
-
public function Main() {
-
init(600, 300);
-
}
-
-
override protected function init3d():void{
-
cube = new Cube(new MaterialsList( { all: mat } ), 100, 100, 100);
-
cube.y = 0;
-
cube2 = new Cube(new MaterialsList( { all: mat2 } ), 100, 100, 100);
-
cube2.y = 0;
-
cube2.x = 1000;
-
cube2.z = 1000;
-
plane.material.lineColor = 0x777777;
-
plane.material.doubleSided = true;
-
plane.pitch(90);
-
plane.y = -50;
-
default_scene.addChild(plane);
-
default_scene.addChild(cube);
-
default_scene.addChild(cube2);
-
Tweener.addTween(cube, { x:1000, z:1000, time:2, onComplete: randomize } );
-
default_camera.x = 0;
-
default_camera.z = 1000;
-
default_camera.y = 1000;
-
}
-
-
public function randomize():void {
-
var xp:Number = (Math.random() * 2000) - 1000;
-
var yp:Number = (Math.random() * 2000) - 1000;
-
Tweener.addTween(cube, { x:xp, z:yp, time:2, onComplete: randomize } );
-
cube2.x = xp;
-
cube2.z = yp;
-
}
-
-
override protected function processFrame():void{
-
default_camera.lookAt(cube);
-
}
-
}
-
}
March 10th, 2008
Categories: Tutorials | Author: Luke | Comments: 22 Comments |
Hi,
I haven't written a post in a while so I felt it was time to share what I've been playing around with.
I've put up a new downloads section where you can download various pieces of code that I've been creating, including code for parsing Heightmaps (click here) and Sculpted Prims (click here).
I'll have a proper update soon, and another Tutorial, I promise
-Luke
March 6th, 2008
Categories: Tutorials | Author: Luke | Comments: No Comments |
*FIXED!!*
Here's a really simple example, all it does is texture a sphere and rotate it depending on what the camera is doing:
Click Here (1.3Mb download so I didn't embed this one)
Source Code (Uses the Base Class):
Actionscript:
-
/**
-
* ...
-
* @author Luke Mitchell
-
* @version 1
-
*/
-
-
package {
-
import org.papervision3d.materials.BitmapFileMaterial;
-
import flash.events.MouseEvent;
-
import org.papervision3d.materials.ColorMaterial;
-
import org.papervision3d.materials.utils.MaterialsList;
-
import org.papervision3d.objects.primitives.Cube;
-
-
public class Main extends PaperBase {
-
-
public var materials:MaterialsList = new MaterialsList(
-
{
-
front: new BitmapFileMaterial("http://papervision2.com/wp-content/pano/s1.gif"),
-
back: new BitmapFileMaterial("http://papervision2.com/wp-content/pano/s3.gif"),
-
left: new BitmapFileMaterial("http://papervision2.com/wp-content/pano/s4.gif"),
-
right: new BitmapFileMaterial("http://papervision2.com/wp-content/pano/s2.gif"),
-
top: new ColorMaterial(0x000000),
-
bottom: new ColorMaterial(0x000000)
-
});
-
public var cubemap:Cube = new Cube(materials, 2000, 2000, 900, 5, 5, 5,Cube.ALL);
-
-
public function Main() {
-
init(600,300);
-
cubemap.x = 0;
-
cubemap.y = 0;
-
cubemap.z = 0;
-
-
default_scene.addChild(cubemap);
-
default_camera.z = 0;
-
default_camera.x = 0;
-
default_camera.y = 0;
-
default_camera.lookAt(cubemap);
-
-
default_camera.zoom = 5;
-
stage.addEventListener(MouseEvent.MOUSE_WHEEL, mWheel);
-
}
-
-
public function mWheel(e:MouseEvent):void {
-
default_camera.zoom += e.delta / 5;
-
if (default_camera.zoom <5) {
-
default_camera.zoom = 5;
-
}else if (default_camera.zoom> 20) {
-
default_camera.zoom = 20;
-
}
-
}
-
-
override protected function processFrame():void {
-
cubemap.yaw( -((stage.mouseX - (stage.width / 2)) / stage.width)*4);
-
default_camera.rotationX = -((stage.mouseY - (stage.height / 2)) / stage.height) * (Math.sqrt(default_camera.zoom*6));
-
}
-
}
-
}
You can use this for pretty much any panoramic image, just change the image url and tweak it a bit
-Luke
February 27th, 2008
Categories: Examples | Author: Luke | Comments: 6 Comments |
This tutorial will show you how to handle full interactivity on an objects surface, just as if it's a normal movieclip.
For my example, I've made this. The red spot in the centre is a button. You can click it to toggle it's glow on and off. The other button will reveal how it works!
There are other ways to do this, but I think this way is the easiest and most hassle-free, and it's pretty fast.
I've super-commented the source code, so from reading the comments in the code you should be able to figure out exactly what's going on.
The basic idea is, you add the movie clip which is being used as the texture to the scene, but make it invisible. You then move it so that your mouse is over the correct part of the movieclip, when your mouse touches the texture on the 3d model. I think the "Show Movieclip" button on the cube above will explain better than I can
Actionscript:
-
package {
-
import flash.display.Bitmap;
-
import flash.display.MovieClip;
-
import flash.events.MouseEvent;
-
import flash.filters.GlowFilter;
-
import org.papervision3d.materials.MovieMaterial;
-
import org.papervision3d.materials.utils.MaterialsList;
-
import org.papervision3d.objects.primitives.Cube;
-
import org.papervision3d.events.InteractiveScene3DEvent;
-
-
public class Main extends PaperBase {
-
-
// This is the movieclip that we'll use as the texture.
-
private var movie:MovieClip = new MovieClip();
-
-
// This movieclip will be completley transparent and will hold the
-
// texture movieclip, then move it to the correct loaction under the mouse.
-
private var movieParent:MovieClip = new MovieClip();
-
-
// These are buttons that I'm going to add to the texture movieclip
-
private var button:MovieClip = new MovieClip();
-
private var showbutton:MovieClip = new MovieClip();
-
-
// A "MovieMaterial" will use a movieclip as a texture.
-
private var mat:MovieMaterial;
-
-
// The cube that we're going to texture..
-
private var cube:Cube;
-
-
// This will import the file "E:/Papervision/images/showtex.jpg" to the project, and
-
// store the data in "ButtonIm". (This is the "Show Movieclip" Button)..
-
[Embed(source = "E:/Papervision/images/showtex.jpg")] private var ButtonIm:Class;
-
-
public function Main() {
-
// Initiate, 400px wide, 400px tall..
-
init(400, 400);
-
}
-
-
override protected function init3d():void {
-
// - See the prepareMovieclip function. This just builds the movieclip called "movie".
-
// You can use ANY movie clip for this..
-
prepareMovieclip();
-
-
// Prepare out moviematerial. Make it animated and interactive.
-
mat = new MovieMaterial(movie, false, true);
-
mat.interactive = true;
-
-
// Zoom in a bit..
-
default_camera.zoom = 5;
-
-
// Prepare the cube.
-
cube = new Cube(new MaterialsList( { all: mat } ), 500, 500, 500, 4, 4, 4);
-
// Listen for when the mouse is moved over the cube.
-
// Trigger the mMove function when this happens.
-
cube.addEventListener( InteractiveScene3DEvent.OBJECT_MOVE, mMove);
-
-
// Add the cube to the scene.
-
default_scene.addChild(cube);
-
-
// The movieParent movieclip will hold the movieclip which is being used
-
// as the texture. It will then move depending on the mouse posirion.
-
movieParent.addChild(movie);
-
// Make it invisible.
-
movieParent.alpha = 0;
-
-
// Add the movieParent movieclip to the stage.
-
addChild(movieParent);
-
-
}
-
-
-
private function prepareMovieclip():void {
-
-
// - This code will set up our movieclip which is going to be used
-
// as the texture for the cube.
-
// Draw Outline:
-
movie.graphics.beginFill(0xFFFFFF);
-
movie.graphics.drawRect(0, 0, 500, 500);
-
movie.graphics.endFill();
-
movie.graphics.beginFill(0x000000);
-
movie.graphics.drawRect(0, 0, 10, 500);
-
movie.graphics.drawRect(490, 0, 10, 500);
-
movie.graphics.drawRect(0, 0, 500, 10);
-
movie.graphics.drawRect(0, 490, 500, 10);
-
movie.graphics.endFill();
-
-
// Draw Circular Button:
-
button.graphics.beginFill(0xBB0000);
-
button.graphics.drawCircle(0, 0, 50);
-
button.graphics.endFill();
-
button.x = 250;
-
button.y = 250;
-
button.buttonMode = true;
-
-
// Load bitmap button texture:
-
var Bim:Bitmap = new ButtonIm();
-
// Draw "Show Movieclip" Texture:
-
showbutton.graphics.beginBitmapFill(Bim.bitmapData);
-
showbutton.graphics.drawRect(0,0,100,30);
-
showbutton.graphics.endFill();
-
showbutton.buttonMode = true;
-
showbutton.x = 380;
-
showbutton.y = 450;
-
-
// Add the buttons to the movieclip
-
movie.addChild(showbutton);
-
movie.addChild(button);
-
-
// -- Listen for the buttons to be clicked --
-
button.addEventListener(MouseEvent.CLICK, mClickButton);
-
showbutton.addEventListener(MouseEvent.CLICK, showHide);
-
// --
-
}
-
-
private function showHide( e:MouseEvent ):void {
-
// The "Show Movieclip" button has been clicked
-
if (movieParent.alpha == 0) { // If it's invisible,
-
movieParent.alpha = 0.2; // Make it slightly visible.
-
}else {
-
movieParent.alpha = 0; // Or make it invisible again
-
}
-
}
-
-
private function mClickButton(e:MouseEvent):void {
-
// The cental round button has been clicked.
-
if(button.name == "on"){
-
button.filters = null;
-
button.name = "off"
-
}else {
-
button.filters = [new GlowFilter(0x000000, 1, 15, 15, 10, 1)];
-
button.name = "on";
-
}
-
}
-
-
private function mMove( e:InteractiveScene3DEvent ):void {
-
// This code is run when the mouse is moved on the cube.
-
// This will move the movieclip to the right place beneath
-
// the mouse.
-
movieParent.x = root.mouseX -e.x;
-
movieParent.y = root.mouseY -e.y;
-
}
-
-
override protected function processFrame():void {
-
// Spin our cube a bit.
-
cube.yaw(0.25);
-
cube.pitch(0.25);
-
}
-
-
-
}
-
}
The image that's embedded in the code can be found here: showtex.jpg
Have fun! More Examples coming soon.
February 21st, 2008
Categories: Tutorials | Author: Luke | Comments: 37 Comments |
We have forums!
I've set up a Papervision 3d forum at http://papervision2.com/forum/
Hopefully these forums will help everyone to solve common problems that they're having with Papervision. I'm going to fill them with some problems which we've resolved in the past so they're all in one place.
I've set up Help, General Chat, Suggestions and Examples forums. If you can think of anything else that the forum needs then make a thread in the suggestions forum and I'll get round to it.
Hope you like it!
-Luke
February 18th, 2008
Categories: News | Author: Luke | Comments: 5 Comments |
Next Page »
|