Sliced Cube

Thursday, April 9th, 2009 | examples

This is kind of combination of my last two examples which turned out pretty nicely.

I’m sorry the code is a big pile of crap. I’m too lazy right now to clean it up :)


source

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
113
package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
 
	import gs.TweenMax;
	import gs.easing.Quad;
 
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="900", height="480", backgroundColor="0xffffff", frameRate="31")]
	public class SlicedCube extends BasicView
	{
		[Embed(source="/assets/car.jpg")]
		private var carAsset:Class;
 
		[Embed(source="/assets/back.jpg")]
		private var backAsset:Class;
 
		[Embed(source="/assets/brabus.jpg")]
		private var brabusAsset:Class;
 
		[Embed(source="/assets/ferrari.jpg")]
		private var ferrariAsset:Class;
 
		private var mouseDownX:Number = 0;
		private var targetRotation:Number = 0;
 
		private var NUM_SLICES:int = 5;
 
		private var cubes:Array = [];
 
		public function SlicedCube()
		{
			super(900, 480);
 
			opaqueBackground = 0x000000;
			camera.focus = 100;
			camera.zoom = 10;
			camera.ortho = false;
 
			for(var i:int = 0; i < NUM_SLICES; i++)
			{
				var materialsList:MaterialsList = new MaterialsList();
				var sliceWidth:Number = 900 / NUM_SLICES;
				var sliceX:Number = sliceWidth * i;
 
 
				materialsList.addMaterial(createSlicedBitmapMaterialFromAsset(carAsset, sliceX, sliceWidth, true), "front");
				materialsList.addMaterial(createSlicedBitmapMaterialFromAsset(backAsset, sliceX, sliceWidth), "back");
				materialsList.addMaterial(createSlicedBitmapMaterialFromAsset(brabusAsset, sliceX, sliceWidth), "top");
				materialsList.addMaterial(createSlicedBitmapMaterialFromAsset(ferrariAsset, sliceX, sliceWidth, true), "bottom");
				materialsList.addMaterial(new ColorMaterial(0xffffff), "right");
				materialsList.addMaterial(new ColorMaterial(0xffffff), "left");
 
				var cube:Cube = new Cube(materialsList, sliceWidth, 480, 480);
				cube.x = sliceX - 450 + sliceWidth / 2;
				cube.z = 240;
 
				scene.addChild(cube);
 
 
				cubes.push(cube);
			}
 
			startRendering();
 
			addEventListener(MouseEvent.CLICK, mouseDownHandler);
		}
 
		private function createSlicedBitmapMaterialFromAsset(asset:Class, sliceX:Number, sliceWidth:Number, isFlipped:Boolean = false):BitmapMaterial
		{
			var bitmap:Bitmap = new asset() as Bitmap;
			var bitmapData:BitmapData = new BitmapData(sliceWidth, 480, false, 0xcc0000);
			if(isFlipped)
			{
				var sliceMatrix:Matrix = new Matrix();
				sliceMatrix.translate(-180, -240);
				sliceMatrix.rotate(Math.PI);
				sliceMatrix.translate(sliceX, 240);
				bitmapData.draw(bitmap, sliceMatrix);
			}
			else
			{
				bitmapData.copyPixels(bitmap.bitmapData, new Rectangle(sliceX, 0, sliceWidth, 480), new Point());
			}
			var material:BitmapMaterial = new BitmapMaterial(bitmapData, true);
 
			return material;
		}
 
		private function mouseDownHandler(event:MouseEvent):void
		{
			targetRotation += 90 % 360;
			cubes.forEach(cubes_forEachCallback);
		}
 
		private function cubes_forEachCallback(cube:Cube, index:int, array:Array):void
		{
			index++;
			var time:Number = .3 * index;
			TweenMax.to(cube, time, {rotationX:targetRotation, ease:Quad.easeInOut});
		}
	}
}

Tags: , , ,

  • veer
    how to size the image
  • Myinnovativeidea
    I am using flash version cs3
  • Myinnovativeidea
    I am getting this error, anybody can help?

    TypeError: Error #1007: Instantiation attempted on a non-constructor.
    at SlicedCube/::createSlicedBitmapMaterialFromAsset()
    at SlicedCube$iinit()
  • Slowsay
    how use pv3d movieMaterial opposite property?
  • I guys,
    I forked it for Flash IDLE with external jpg...hope that somebody will like it ;)
    Ciao,
    Giulio [http://www.giuliograsso.nl]

    package
    {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    import gs.TweenMax;
    import gs.easing.Quad;

    import org.papervision3d.materials.BitmapMaterial;
    import org.papervision3d.materials.BitmapFileMaterial;
    import org.papervision3d.events.FileLoadEvent;
    import org.papervision3d.materials.ColorMaterial;
    import org.papervision3d.materials.utils.MaterialsList;
    import org.papervision3d.objects.primitives.Cube;
    import org.papervision3d.view.BasicView;


    //[SWF(width="900", height="480", backgroundColor="0xffffff", frameRate="31")]
    public class SlicedCube extends BasicView
    {

    private var carAsset:BitmapFileMaterial;
    private var backAsset:BitmapFileMaterial;
    private var brabusAsset:BitmapFileMaterial;
    private var ferrariAsset:BitmapFileMaterial;


    private var mouseDownX:Number = 0;
    private var targetRotation:Number = 0;

    private var NUM_SLICES:int = 5;

    private var cubes:Array = [];

    public function SlicedCube()
    {
    carAsset = new BitmapFileMaterial("assets/car.jpg");
    backAsset = new BitmapFileMaterial("assets/back.jpg");
    brabusAsset = new BitmapFileMaterial("assets/brabus.jpg");
    ferrariAsset = new BitmapFileMaterial("assets/ferrari.jpg");
    addEventListener(Event.ENTER_FRAME, _enterFrame);
    // I listen just for the last image to be loaded...but to make a good work, preload them all...I suggest with BulkLoader
    ferrariAsset.addEventListener(FileLoadEvent.LOAD_PROGRESS, picLoad);
    ferrariAsset.addEventListener(FileLoadEvent.LOAD_COMPLETE, picComplete);
    //super(900, 480);

    //opaqueBackground = 0x000000;
    camera.focus = 100;
    camera.zoom = 10;
    camera.ortho = false;


    }

    function picLoad(e:FileLoadEvent):void
    {
    //preload
    }

    function picComplete(e:FileLoadEvent):void
    {
    for(var i:int = 0; i < NUM_SLICES; i++)
    {
    var materialsList:MaterialsList = new MaterialsList();
    var sliceWidth:Number = 900 / NUM_SLICES;
    var sliceX:Number = sliceWidth * i;


    materialsList.addMaterial(createSlicedBitmapMaterialFromAsset(carAsset, sliceX, sliceWidth, true), "front");
    materialsList.addMaterial(createSlicedBitmapMaterialFromAsset(backAsset, sliceX, sliceWidth), "back");
    materialsList.addMaterial(createSlicedBitmapMaterialFromAsset(brabusAsset, sliceX, sliceWidth), "top");
    materialsList.addMaterial(createSlicedBitmapMaterialFromAsset(ferrariAsset, sliceX, sliceWidth, true), "bottom");
    materialsList.addMaterial(new ColorMaterial(0xffffff), "right");
    materialsList.addMaterial(new ColorMaterial(0xffffff), "left");

    var cube:Cube = new Cube(materialsList, sliceWidth, 480, 480);
    cube.x = sliceX - 450 + sliceWidth / 2;
    cube.z = 240;

    scene.addChild(cube);


    cubes.push(cube);
    }

    startRendering();

    addEventListener(MouseEvent.CLICK, mouseDownHandler);
    }


    private function createSlicedBitmapMaterialFromAsset(asset:BitmapFileMaterial, sliceX:Number, sliceWidth:Number, isFlipped:Boolean = false):BitmapMaterial
    {

    var bitmap:Bitmap = new Bitmap (asset.bitmap) ;

    var bitmapData:BitmapData = new BitmapData(sliceWidth, 480, false, 0x000000);
    if(isFlipped)
    {
    var sliceMatrix:Matrix = new Matrix();
    sliceMatrix.translate(-180, -240);
    sliceMatrix.rotate(Math.PI);
    sliceMatrix.translate(sliceX, 240);
    bitmapData.draw(bitmap, sliceMatrix);
    }
    else
    {
    bitmapData.copyPixels(bitmap.bitmapData, new Rectangle(sliceX, 0, sliceWidth, 480), new Point());
    }
    var material:BitmapMaterial = new BitmapMaterial(bitmapData, true);

    return material;
    }

    private function mouseDownHandler(event:MouseEvent):void
    {
    targetRotation += 90 % 360;
    cubes.forEach(cubes_forEachCallback);
    }

    private function cubes_forEachCallback(cube:Cube, index:int, array:Array):void
    {
    index++;
    var time:Number = .3 * index;
    TweenMax.to(cube, time, {rotationX:targetRotation, ease:Quad.easeInOut});
    }
    private function _enterFrame(event:Event = null):void
    {
    var rotY: Number = (mouseY-(stage.stageHeight/2))/(stage.height/3)*(400);
    var rotX: Number = (mouseX-(stage.stageWidth/2))/(stage.width/3)*(-400);
    camera.x= camera.x+(rotX-camera.x)/4;
    camera.y= camera.y+(rotY-camera.y)/4;
    renderer.renderScene(scene, camera, viewport);


    }
    }
    }
  • ram
    hi,

    all your projects are amazing but if they are with explanations it will be much helpful to learn

    thank you.
  • stunned
    Impressive! Just impressive!

    I imported the project in Flex, and it worked like a charm :)

    Well done, dude! Well done indeed!
  • bhoomit
    this is really helpful but i have one trouble i want it for images of different sizes together...

    i tried a lot but cant undestand where is the trouble can u comment out all ur code for better undestanding please...:)

    thanks
  • Basically the concept ripped straight from you example

    http://www.efflex.org/orgeffle...
  • Thanks for the awesome article! I put together a rough xml gallery based on it. Check it out here:

    http://blog.flexcommunity.net/...
  • Thank YOU for being online with this blog!
    ...I played around a bit with this source and managed to feed images via XML to the cubes - and now they are re-skinning where the top-side adds the next picture to it... (and so on)
    I might publish it on my page - if you think your code is crappy, then you should look at mine then :D

    Cheers!
  • Al
    Hi there. This effect is so cool.

    Any chance someone could give advice on how to make this owkr as a component in flex where you can stack images in a viewstack and then use a click event to fire the effect.

    Any guidance will be appreciated.

    Thanks for the tutorial, I am just back tracking to understand how to gt it to work similar to tink effects (efflex.org)

    Cheers
  • Amer Dababneh
    Very nice...
  • Knalle
    glad I could help :)
  • Benoit
    materialsList.addMaterial(createSlicedBitmapMaterialFromAsset(car1, sliceX, sliceWidth, true), "front");
    materialsList.addMaterial(createSlicedBitmapMaterialFromAsset(car4, sliceX, sliceWidth, true), "bottom");

    why this?
  • Hi Knalle,
    I am new to PV3D(AS3) here, wanna say thank you for the tips! I should've read through the thread before trying by myself. Your post totally helped!

    thanks man!!
  • the code is a bit difficult pasting here and not very friendly to look at :)

    anyway, don't forget to export the bitmaps from your library (linkage). And import flash.display.Bitmap in the top of your class.
  • Knalle
    This example was made for Flex.

    try something like:

    private var car1:BitmapData = new Pic1(0,0);
    private var car2:BitmapData = new Pic2(0,0);
    private var car3:BitmapData = new Pic3(0,0);
    private var car4:BitmapData = new Pic4(0,0);


    instead of:

    [Embed(source="/assets/car.jpg")]
    private var carAsset:Class;

    [Embed(source="/assets/back.jpg")]
    private var backAsset:Class;

    [Embed(source="/assets/brabus.jpg")]
    private var brabusAsset:Class;

    [Embed(source="/assets/ferrari.jpg")]
    private var ferrariAsset:Class;


    and


    instead of:

    private function createSlicedBitmapMaterialFromAsset(asset:Class,....)
    {
    var bitmap:Bitmap = new asset() as Bitmap;

    use:
    private function createSlicedBitmapMaterialFromAsset(asset:BitmapData,....)
    {
    var bitmap:Bitmap = new Bitmap(asset);
  • Great tutorial! Thanks!
  • Talal
    when i run slicedCube in Flash cs3 with as3 the result is..

    TypeError: Error #1007: Instantiation attempted on a non-constructor.
    at SlicedCube/::createSlicedBitmapMaterialFromAsset()
    at SlicedCube$iinit()

    Please help :)
  • Talal
    It is not working in flash CS3.
    could it possible to run this example in flash cs3..
  • Knalle
    cool :)
  • Too cool!
  • Nice!
    It's cool effect!
  • looks like the discovery channel. very cool.
  • Nice. FL2 site http://www.fl-2.com uses a similar effect
blog comments powered by Disqus

Search

Recommended Books

Speaking at FITC Toronto

 

April 2009
M T W T F S S
« Mar   May »
 12345
6789101112
13141516171819
20212223242526
27282930  

Preferred Video Tutorial Resolution

  • 1024x768 (53%, 85 Votes)
  • 1280x1024 (15%, 24 Votes)
  • 1920x1080 (15%, 24 Votes)
  • 800x600 (13%, 20 Votes)
  • 480x320 (4%, 6 Votes)
  • 640x480 (0%, 2 Votes)

Total Voters: 160

Loading ... Loading ...