Labs

It works in my machine!

Archive for the ‘papervision3d’ tag

[ papervision3d ] Texture switch at runtime

with 2 comments

Here we have a quick example on how to change the texture of a primitive object in papervision3d…

At the end this is what you get:

The Flash plugin is required to view this object.

This is the Document Class, its pretty simple and the methods are self descriptive so i don’t think you’ll have any trouble using it :

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
114
115
116
117
118
119
package  
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.SimpleButton;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.materials.BitmapAssetMaterial;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.render.BasicRenderEngine;
 
	/**
	* ...
	* @author Wolfito
	*/
	public class Main extends Sprite
	{
 
		private var scene:Scene3D;
		private var camera:Camera3D;
		private var viewport:Viewport3D;
		private var render_engine:BasicRenderEngine;
		private var my_plane:Plane;
		private var active_index:int = 0;
 
		public function Main() 
		{
			create3dEnviroment();
			buildPlane();
			setEvents();
			render3d();
		}
 
		private function create3dEnviroment():void
		{
			camera = new Camera3D();
			scene = new Scene3D();
			viewport = new Viewport3D(0, 0, true, true);
			addChild(viewport);
			render_engine = new BasicRenderEngine();
		}
 
		private function setEvents():void
		{
			//
			stage.addEventListener(MouseEvent.MOUSE_OVER, startRendering, false, 0, true);
			stage.addEventListener(Event.MOUSE_LEAVE, stopRendering, false, 0, true);
			//
                       // button placed on the stage:
			this.switchButton.addEventListener(MouseEvent.CLICK, switchTexture, false, 0, true);
		}
 
		private function startRendering(e:MouseEvent):void 
		{
			if (!this.hasEventListener(Event.ENTER_FRAME))
			{
				this.addEventListener(Event.ENTER_FRAME, render, false, 0, true);
			}
		}
 
		private function stopRendering(e:Event):void 
		{
			this.removeEventListener(Event.ENTER_FRAME, render);
		}
 
		private function buildPlane():void
		{
			var texture:BitmapAssetMaterial = new BitmapAssetMaterial("Pocoyo_0");
			texture.smooth = true;
			my_plane = new Plane(texture, 500, 500, 4, 4);
			scene.addChild(my_plane);
			camera.target = my_plane;
		}
 
		private function render(e:Event):void
		{
			render3d();
			moveCamera();
		}
 
		private function render3d():void
		{
			render_engine.renderScene(scene, camera, viewport);
		}
 
		private function moveCamera():void
		{
			camera.x = (this.stage.mouseX - (this.stage.stageWidth / 2)) * 2;
			camera.y = (this.stage.mouseY - (this.stage.stageHeight / 2)) * 2;
		}
 
		private function switchTexture(e:MouseEvent):void
		{
			var bitmapData:BitmapData;
			if (active_index == 0)
			{
                               //get bitmap linked from the library:
				bitmapData = new Pocoyo_1(283, 283);
				active_index = 1;
			}
			else
			{
                               //get bitmap linked from the library:
				bitmapData = new Pocoyo_0(283, 283);
				active_index = 0;
			}
			my_plane.material.bitmap.dispose();
			my_plane.material.bitmap = bitmapData;
			my_plane.material.updateBitmap();
		}
	}
 
}

you can download the source code too! =)

Written by wolf

November 20th, 2008 at 6:32 pm