My Playground
Ha you have reached my playground, for all those moments at work when you think this would be cool but then I need more time. Or the client simply just does not like it for some reason. And ofcourse to show prototypes that did got used in projects, hopefully in different versions to show the work in progress flow, which in my opinion is always nice to see.
Some of my experiments use classes from wismanas3lib repository, so please get a copy from the repository so you can retrieve possible updates aswell. Or download them here (!! the classes will get updated so it’s better to use the googlecode repository !!).
Cheers JW
FLARToolkit Single & MultipleMarker Detection
Last modified on 2009-03-23 06:51:55 GMT. 11 comments. Top.
What you will need:
1. Photoshop (any will do)
2. Papervision3D rev. 817 or later
3. FlarToolkit
4. ArToolkit Marker Generator (flash player 10)
5. webcam
6. Fash CS3 or Higher
There are allready some great sites experimenting and writing tutorials about Augmented reality in flash. This is nothing new, just my experience in writing the code to create a simple version so I can learn from the others who started it. So before I start credits go out to squidder.com for some amazing tutorials and examples and saqoosha.net for starting the FlarToolkit.
For creating the best markers go and see the tutorial at squidder.com
For the starters kit you can get flartoolkit via libspark repository or download the starterskit at saqoosha.net.
Example of a Single Marker Detection:
Code Single Marker detection:
-
package nl.wisman.codexperiments.augmented
-
{
-
// papervision
-
import org.papervision3d.materials.utils.MaterialsList;
-
import org.papervision3d.objects.primitives.Cube;
-
import org.papervision3d.lights.PointLight3D;
-
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
-
import org.papervision3d.render.LazyRenderEngine;
-
import org.papervision3d.scenes.Scene3D;
-
import org.papervision3d.view.Viewport3D;
-
-
// flash
-
import flash.display.PixelSnapping;
-
import flash.display.Bitmap;
-
import flash.net.URLRequest;
-
import flash.events.Event;
-
import flash.net.URLLoaderDataFormat;
-
import flash.net.URLLoader;
-
import flash.display.BitmapData;
-
import flash.media.Video;
-
import flash.media.Camera;
-
import flash.display.Sprite;
-
import org.papervision3d.objects.DisplayObject3D;
-
-
// libspark
-
import org.libspark.flartoolkit.core.FLARCode;
-
import org.libspark.flartoolkit.core.param.FLARParam;
-
import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
-
import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
-
import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
-
import org.libspark.flartoolkit.pv3d.FLARBaseNode;
-
import org.libspark.flartoolkit.pv3d.FLARCamera3D;
-
-
public class FlarSingleMarkerDemo extends Sprite
-
{
-
// flar properties
-
private var _camParameters:FLARParam;
-
private var _marker4Pattern:FLARCode;
-
private var _raster:FLARRgbRaster_BitmapData;
-
private var _detector:FLARSingleMarkerDetector;
-
private var _flarCam3D:FLARCamera3D;
-
private var _resultMat:FLARTransMatResult;
-
private var _baseNode:FLARBaseNode;
-
-
// flash properties
-
private var _cam:Camera;
-
private var _vid:Video;
-
private var _capture:Bitmap;
-
private var _loader:URLLoader;
-
-
// papervision3d
-
private var _vp:Viewport3D;
-
private var _scene:Scene3D;
-
private var _renderer:LazyRenderEngine;
-
private var _light:PointLight3D;
-
private var _cube:Cube;
-
-
public function FlarSingleMarkerDemo()
-
{
-
loadCamera();
-
}
-
-
private function loadCamera():void
-
{
-
_loader = new URLLoader();
-
_loader.dataFormat = URLLoaderDataFormat.BINARY;
-
_loader.addEventListener(Event.COMPLETE, onLoadCamParam);
-
_loader.load(new URLRequest("camera_para.dat"));
-
}
-
-
// camera parameters are loaded, load the marker you want to use
-
private function onLoadCamParam(event:Event):void
-
{
-
_loader.removeEventListener(Event.COMPLETE, onLoadCamParam);
-
_camParameters = new FLARParam();
-
_camParameters.loadARParam(_loader.data);
-
_camParameters.changeScreenSize(500, 500);
-
-
_loader.dataFormat = URLLoaderDataFormat.TEXT;
-
_loader.addEventListener(Event.COMPLETE, onLoadCode);
-
_loader.load(new URLRequest("yourmarker.pat"));
-
}
-
-
// marker now also loaded, so remove _loader and create a FlarCode
-
private function onLoadCode(event:Event):void
-
{
-
initWebcam();
-
-
_marker4Pattern = new FLARCode(4, 4, 65, 65);
-
_marker4Pattern.loadARPatt(_loader.data);
-
-
_loader.removeEventListener(Event.COMPLETE, onLoadCode);
-
_loader = null;
-
-
// create bitmap and bitmapdata where we can draw the webcam into
-
var bmd:BitmapData = new BitmapData(500, 500, false, 0);
-
_capture = new Bitmap(bmd, PixelSnapping.AUTO,false);
-
_capture.width = 500;
-
_capture.height = 500;
-
addChild(_capture);
-
-
_raster = new FLARRgbRaster_BitmapData(_capture.bitmapData);
-
_detector = new FLARSingleMarkerDetector(_camParameters, _marker4Pattern, 65);
-
-
initFlar();
-
initPV3D();
-
initListeners();
-
}
-
-
private function initWebcam():void
-
{
-
_cam = Camera.getCamera();
-
_cam.setMode(500, 500, 60);
-
_vid = new Video(500, 500);
-
_vid.attachCamera(_cam);
-
}
-
-
private function initFlar():void
-
{
-
_flarCam3D = new FLARCamera3D(_camParameters);
-
_resultMat = new FLARTransMatResult();
-
_baseNode = new FLARBaseNode();
-
}
-
-
private function initPV3D():void
-
{
-
_vp = new Viewport3D(500, 500);
-
addChild(_vp);
-
-
_scene = new Scene3D();
-
_scene.addChild(_baseNode);
-
-
_light = new PointLight3D();
-
-
// add a cube or whatever we want to show when pattern is recognised
-
// in the basenode (FLARBaseNode, which extends from a DisplayObject3D)
-
var fmat:FlatShadeMaterial = new FlatShadeMaterial(_light, 0×7DC202, 0xCCCCCC);
-
_cube = new Cube(new MaterialsList({all: fmat}), 40, 40, 80);
-
_cube.z = 20;
-
_cube.name = "cube";
-
_baseNode.addChild(_cube);
-
-
_renderer = new LazyRenderEngine(_scene, _flarCam3D, _vp);
-
}
-
-
private function initListeners():void
-
{
-
addEventListener(Event.ENTER_FRAME, render);
-
}
-
-
private function render(event:Event):void
-
{
-
_capture.bitmapData.draw(_vid);
-
-
if (_detector.detectMarkerLite(_raster, 65) && _detector.getConfidence() > 0.5)
-
{
-
// when deteactmarker finds the marker you created show the baseNode
-
_detector.getTransformMatrix(_resultMat);
-
_baseNode.setTransformMatrix(_resultMat);
-
_baseNode.visible = true;
-
}
-
else
-
{
-
_baseNode.visible = false;
-
}
-
-
if(_baseNode.visible)
-
{
-
Cube(_baseNode.getChildByName("cube")).roll(1);
-
}
-
-
_renderer.render();
-
}
-
}
-
}
Example of a Multiple Marker Detection: (its still a work in progress, still shifts cube from one pattern to another but he…)
Code Multiple Marker detection:
-
package
-
{
-
// bulkloader
-
import br.com.stimuli.loading.BulkLoader;
-
-
// papervision
-
import org.papervision3d.materials.utils.MaterialsList;
-
import org.papervision3d.objects.primitives.Cube;
-
import org.papervision3d.lights.PointLight3D;
-
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
-
import org.papervision3d.render.LazyRenderEngine;
-
import org.papervision3d.scenes.Scene3D;
-
import org.papervision3d.view.Viewport3D;
-
-
// flash
-
import flash.display.PixelSnapping;
-
import flash.display.Bitmap;
-
import flash.net.URLRequest;
-
import flash.events.Event;
-
import flash.net.URLLoaderDataFormat;
-
import flash.net.URLLoader;
-
import flash.display.BitmapData;
-
import flash.media.Video;
-
import flash.media.Camera;
-
import flash.display.Sprite;
-
import org.papervision3d.objects.DisplayObject3D;
-
-
// libspark
-
import org.libspark.flartoolkit.core.FLARCode;
-
import org.libspark.flartoolkit.core.param.FLARParam;
-
import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
-
import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
-
import org.libspark.flartoolkit.detector.FLARMultiMarkerDetector;
-
import org.libspark.flartoolkit.pv3d.FLARBaseNode;
-
import org.libspark.flartoolkit.pv3d.FLARCamera3D;
-
-
public class FlarMultiMarkerDemo extends Sprite
-
{
-
// flar properties
-
private var _camParameters:FLARParam;
-
private var _raster:FLARRgbRaster_BitmapData;
-
private var _detector:FLARMultiMarkerDetector;
-
private var _flarCam3D:FLARCamera3D;
-
private var _resultMat:FLARTransMatResult;
-
-
// flash properties
-
private var _cam:Camera;
-
private var _vid:Video;
-
private var _capture:Bitmap;
-
private var _loader:URLLoader;
-
private var _allMarkers:Array;
-
-
// papervision3d
-
private var _vp:Viewport3D;
-
private var _scene:Scene3D;
-
private var _renderer:LazyRenderEngine;
-
private var _light:PointLight3D;
-
private var _bLoader:BulkLoader;
-
-
public function FlarMultiMarkerDemo()
-
{
-
_allMarkers = [
-
{markerSource: "path/your_first_marker.pat", segments: 4, size: 65},
-
{markerSource: "path/your_second_marker.pat", segments: 4, size: 65},
-
{markerSource: "path/your_third_marker.pat", segments: 4, size: 65},
-
];
-
-
loadCamera();
-
}
-
-
private function loadCamera():void
-
{
-
_loader = new URLLoader();
-
_loader.dataFormat = URLLoaderDataFormat.BINARY;
-
_loader.addEventListener(Event.COMPLETE, onLoadCamParam);
-
_loader.load(new URLRequest("camera_para.dat"));
-
}
-
-
// camera parameters are loaded, load the marker you want to use
-
// camera parameters are loaded, load the marker you want to use
-
private function onLoadCamParam(event:Event):void
-
{
-
_loader.removeEventListener(Event.COMPLETE, onLoadCamParam);
-
-
// setup camera parameters
-
_camParameters = new FLARParam();
-
_camParameters.loadARParam(_loader.data);
-
_camParameters.changeScreenSize(500, 500);
-
-
// reset loader
-
_loader = null;
-
-
_bLoader = new BulkLoader("markerLoader");
-
_bLoader.addEventListener(Event.COMPLETE, markersLoadedHandler);
-
// now load multiple markers (for now its 3)
-
for (var i:int = 0; i < _allMarkers.length; i++)
-
{
-
_bLoader.add(_allMarkers[i].markerSource, {id: "marker_" + i.toString() });
-
}
-
_bLoader.start();
-
}
-
-
private function markersLoadedHandler(event:Event):void
-
{
-
var codes:Array = new Array();
-
var sizes:Array = new Array();
-
for (var i:int = 0; i < _allMarkers.length; i++)
-
{
-
var code:FLARCode = new FLARCode(_allMarkers[i].segments, _allMarkers[i].segments, _allMarkers[i].size, _allMarkers[i].size);
-
code.loadARPatt(_bLoader.getContent( "marker_" + i.toString()));
-
codes.push(code);
-
sizes.push(_allMarkers[i].size);
-
}
-
-
initWebcam();
-
-
// create bitmap and bitmapdata where we can draw the webcam into
-
var bmd:BitmapData = new BitmapData(500, 500, false, 0);
-
_capture = new Bitmap(bmd, PixelSnapping.AUTO,false);
-
_capture.width = 500;
-
_capture.height = 500;
-
addChild(_capture);
-
-
_raster = new FLARRgbRaster_BitmapData(_capture.bitmapData);
-
_detector = new FLARMultiMarkerDetector(_camParameters, codes, sizes, codes.length);
-
-
initFlar();
-
initPV3D();
-
initListeners();
-
}
-
-
private function initWebcam():void
-
{
-
_cam = Camera.getCamera();
-
_cam.setMode(500, 500, 60);
-
_vid = new Video(500, 500);
-
_vid.attachCamera(_cam);
-
}
-
-
private function initFlar():void
-
{
-
_flarCam3D = new FLARCamera3D(_camParameters);
-
_resultMat = new FLARTransMatResult();
-
}
-
-
private function initPV3D():void
-
{
-
_vp = new Viewport3D(500, 500);
-
addChild(_vp);
-
-
_scene = new Scene3D();
-
-
_light = new PointLight3D();
-
-
// add a cube or whatever we want to show when pattern is recognised
-
// in the basenode (FLARBaseNode, which extends from a DisplayObject3D)
-
for (var i:int = 0; i < _allMarkers.length; i++)
-
{
-
var fmat:FlatShadeMaterial = new FlatShadeMaterial(_light, 0×7DC202 * Math.random(), 0xCCCCCC);
-
var cube:Cube = new Cube(new MaterialsList({all: fmat}), 40, 40, 80);
-
cube.z = 20;
-
cube.name = "cube_" + i.toString();
-
var baseNode:FLARBaseNode = new FLARBaseNode();
-
baseNode.name = "baseNode_" + i;
-
baseNode.addChild(cube);
-
_scene.addChild(baseNode);
-
}
-
-
_renderer = new LazyRenderEngine(_scene, _flarCam3D, _vp);
-
}
-
-
private function initListeners():void
-
{
-
addEventListener(Event.ENTER_FRAME, render);
-
}
-
-
private function render(event:Event):void
-
{
-
_capture.bitmapData.draw(_vid);
-
-
for (var i:int = 0; i< _allMarkers.length; i++)
-
{
-
if(_detector.detectMarkerLite(_raster, 65) && _detector.getConfidence(i) > .5)
-
{
-
_detector.getTransmationMatrix(i, _resultMat);
-
FLARBaseNode(_scene.getChildByName("baseNode_" + i)).setTransformMatrix(_resultMat);
-
_scene.getChildByName("baseNode_" + i).visible = true;
-
}
-
else
-
{
-
_scene.getChildByName("baseNode_" + i).visible = false;
-
}
-
}
-
-
_renderer.render();
-
}
-
}
-
}
And last but not least a flash version to play with:
Try it yourself, but don’t forget to download the patterns first.
Brownian Motion + CollisionGrid
Last modified on 2009-03-01 21:43:27 GMT. 3 comments. Top.
Started reading Advanced Actionscript 3 Animation written by Keith Peters & Seb Lee-DeLisle (technical reviewer) so here’s a little experiment with grid collision using the FP10 Vector class. Let it run for a while and it creates a nice weird image.
Brownian CollisionDemo:
-
package
-
{
-
import flash.display.BlendMode;
-
import flash.filters.BlurFilter;
-
import flash.display.Bitmap;
-
import flash.display.BitmapData;
-
import flash.events.Event;
-
import flash.display.StageScaleMode;
-
import flash.display.StageAlign;
-
import flash.display.DisplayObject;
-
import flash.display.Sprite;
-
-
public class CollisionDemo extends Sprite
-
{
-
private const GRID_SIZE:Number = 30;
-
private const RADIUS:Number = 15;
-
-
private var _screen:BitmapData = new BitmapData(800, 600, false, 0);
-
private var _balls:Vector.<DisplayObject>;
-
private var _grid:CollisionGrid;
-
private var _numBalls:int = 100;
-
private var _ballContainer:Sprite = new Sprite();
-
-
public function CollisionDemo()
-
{
-
stage.align = StageAlign.TOP_LEFT;
-
stage.scaleMode = StageScaleMode.NO_SCALE;
-
-
addChild(new Bitmap(_screen));
-
addChild(_ballContainer);
-
-
_grid = new CollisionGrid(stage.stageWidth, stage.stageHeight, GRID_SIZE);
-
_grid.drawGrid(graphics, 0, .1);
-
-
makeBalls();
-
-
addEventListener(Event.ENTER_FRAME, onEnterFrame);
-
}
-
-
function onEnterFrame(event:Event):void
-
{
-
updateBalls();
-
_grid.check(_balls);
-
-
var numChecks:int = _grid.checks.length;
-
for(var j:int = 0; j < numChecks; j += 2)
-
{
-
checkCollision(_grid.checks[j] as Circle, _grid.checks[j + 1] as Circle);
-
}
-
-
_screen.draw(_ballContainer);
-
}
-
-
private function makeBalls():void
-
{
-
_balls = new Vector.<DisplayObject>(_numBalls);
-
for(var i:int = 0; i < _numBalls; i++)
-
{
-
var circle:Circle = new Circle(RADIUS);
-
circle.x = Math.random() * stage.stageWidth;
-
circle.y = Math.random() * stage.stageHeight;
-
circle.vx = Math.random() * 10 - 5;
-
circle.vy = Math.random() * 10 - 5;
-
_ballContainer.addChild(circle);
-
_balls[i] = circle;
-
}
-
-
_ballContainer.visible = false;
-
}
-
-
private function updateBalls():void
-
{
-
for(var i:int = 0; i < _numBalls; i++)
-
{
-
var circle:Circle = _balls[i] as Circle;
-
circle.update();
-
-
circle.vx = Math.random() * 10 - 5;
-
circle.vy = Math.random() * 10 - 5;
-
-
if(circle.x < RADIUS)
-
{
-
circle.x = RADIUS;
-
circle.vx *= -1;
-
}
-
else if(circle.x > stage.stageWidth - RADIUS)
-
{
-
circle.x = stage.stageWidth - RADIUS;
-
circle.vx *= -1;
-
}
-
if(circle.y < RADIUS)
-
{
-
circle.y = RADIUS;
-
circle.vy *= -1;
-
}
-
else if(circle.y > stage.stageHeight - RADIUS)
-
{
-
circle.y = stage.stageHeight - RADIUS;
-
circle.vy *= -1;
-
}
-
circle.color = 0xffffff;
-
}
-
}
-
-
private function checkCollision(circleA:Circle, circleB:Circle):void
-
{
-
var dx:Number = circleB.x - circleA.x;
-
var dy:Number = circleB.y - circleA.y;
-
var dist:Number = Math.sqrt(dx * dx + dy * dy);
-
if(dist < circleA.radius + circleB.radius)
-
{
-
circleA.color = 0×7DC202;
-
circleB.color = 0×7DC202;
-
}
-
}
-
}
-
}
CollisionGrid Class:
-
package
-
{
-
import flash.display.DisplayObject;
-
import flash.display.Graphics;
-
import flash.events.EventDispatcher;
-
-
public class CollisionGrid extends EventDispatcher
-
{
-
private var _checks:Vector.<DisplayObject>;
-
private var _grid:Vector.<Vector.<DisplayObject>>;
-
private var _gridSize:Number;
-
private var _height:Number;
-
private var _width:Number;
-
private var _numCells:int;
-
private var _numCols:int;
-
private var _numRows:int;
-
-
public function CollisionGrid(width:Number, height:Number, gridSize:Number)
-
{
-
_width = width;
-
_height = height;
-
_gridSize = gridSize;
-
_numCols = Math.ceil(_width / _gridSize);
-
_numRows = Math.ceil(_height / _gridSize);
-
_numCells = _numCols * _numRows;
-
}
-
-
public function drawGrid(graphics:Graphics, color:int = 0×333333, alpha:Number = 1):void
-
{
-
graphics.lineStyle(0, color, alpha);
-
for (var i:int = 0; i < _width; i += _gridSize)
-
{
-
graphics.moveTo(i, 0);
-
graphics.lineTo(i, _height);
-
}
-
-
for (var j:int = 0; j < _height; j += _gridSize)
-
{
-
graphics.moveTo(0, j);
-
graphics.lineTo(_width, j);
-
}
-
}
-
-
public function check(objects:Vector.<DisplayObject>):void
-
{
-
var numObjects:int = objects.length;
-
_grid = new Vector.<Vector.<DisplayObject>>(_numCells);
-
_checks = new Vector.<DisplayObject>();
-
for(var i:int = 0; i < numObjects; i++)
-
{
-
var obj:DisplayObject = objects[i];
-
var index:int = Math.floor(obj.y / _gridSize) * _numCols + Math.floor(obj.x / _gridSize);
-
if(_grid[index] == null) _grid[index] = new Vector.<DisplayObject>;
-
_grid[index].push(obj);
-
}
-
-
checkGrid();
-
}
-
-
private function checkGrid():void
-
{
-
for (var i:int = 0; i <_numCols; i++)
-
{
-
for (var j:int = 0; j < _numRows; j++)
-
{
-
checkOneCell(i,j);
-
checkTwoCells(i, j, i + 1, j); // right
-
checkTwoCells(i, j, i - 1, j + 1); // lower left
-
checkTwoCells(i, j, i , j + 1); // lower
-
checkTwoCells(i, j, i + 1, j + 1); // lower right
-
}
-
}
-
}
-
-
private function checkOneCell(x:int, y:int):void
-
{
-
var cell:Vector.<DisplayObject> = _grid[y * _numCols + x];
-
if (cell == null) return;
-
-
var cellLength:int = cell.length;
-
for (var i:int = 0; i < cellLength - 1; i++)
-
{
-
var objA:DisplayObject = cell[i];
-
-
for (var j:int = i + 1; j < cellLength; j++)
-
{
-
var objB:DisplayObject = cell[j];
-
_checks.push(objA, objB);
-
}
-
}
-
}
-
-
private function checkTwoCells(x1:int, y1:int, x2:int, y2:int):void
-
{
-
// make sure cell exists
-
if (x2 >= _numCols || x2 < 0 || y2 >= _numRows) return;
-
-
var cellA:Vector.<DisplayObject> = _grid[y1 * _numCols + x1];
-
var cellB:Vector.<DisplayObject> = _grid[y2 * _numCols + x2];
-
if (cellA == null || cellB == null) return;
-
-
var cellALength:int = cellA.length;
-
var cellBLength:int = cellB.length;
-
-
for (var i:int = 0; i < cellALength; i++)
-
{
-
var objA:DisplayObject = cellA[i];
-
-
for(var j:int = 0; j < cellBLength; j++)
-
{
-
var objB:DisplayObject = cellB[j];
-
_checks.push(objA, objB);
-
}
-
}
-
}
-
-
public function get checks():Vector.<DisplayObject>
-
{
-
return _checks;
-
}
-
}
-
}
PV3D - Playing with light and cubes
Last modified on 2009-02-08 11:21:50 GMT. 8 comments. Top.
Little thingy in between, playing with light in the most simplest way there is. It has no purpose…, but thought he why not. Click and drag the cube. On click the light will tween out of the center outside the cubes, onrelease move back to the center. At this point i’m still looking around to play with multiple lights so if anyone has suggestions of ideas…leave a message
And here is the code:
-
package
-
{
-
// tweener
-
import gs.easing.Back;
-
import caurina.transitions.Tweener;
-
-
// flash
-
import flash.events.MouseEvent;
-
import flash.filters.BlurFilter;
-
import flash.events.Event;
-
import flash.display.Sprite;
-
-
// papervision
-
import org.papervision3d.view.BasicView;
-
import org.papervision3d.objects.DisplayObject3D;
-
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
-
import org.papervision3d.objects.primitives.Cube;
-
import org.papervision3d.lights.PointLight3D;
-
import org.papervision3d.materials.utils.MaterialsList;
-
import org.papervision3d.view.AbstractView;
-
import org.papervision3d.core.proto.CameraObject3D;
-
import org.papervision3d.core.proto.MaterialObject3D;
-
-
[SWF(width="500", height="500", backgroundColor="#000000", frameRate="60")]
-
public class LightDemo3D extends Sprite
-
{
-
//
-
// private constants
-
//
-
private static const GRID_SPACE:int = 50;
-
private static const CUBE_SIZE:int = 150;
-
-
//
-
// private properties
-
//
-
private var _viewBasic:BasicView;
-
private var _mList:MaterialsList;
-
private var _pl:PointLight3D;
-
private var _flatMat:FlatShadeMaterial;
-
private var _mainCube:DisplayObject3D;
-
private var _isDown:Boolean;
-
private var _prevMouseX:Number;
-
private var _prevMouseY:Number;
-
private var _cameraPitch:Number = 90;
-
private var _cameraYaw:Number = 270;
-
private var _cameraTarget:DisplayObject3D;
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Constructor
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
public function LightDemo3D()
-
{
-
addEventListener(Event.ADDED_TO_STAGE, initialize);
-
}
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Public methods
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
// none
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Private methods
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
private function initialize(event:Event):void
-
{
-
removeEventListener(Event.ADDED_TO_STAGE, initialize);
-
-
// setup view
-
initView();
-
-
// materials
-
initMaterials();
-
-
// objects
-
initObjects();
-
-
// listeners
-
addEventListener(Event.ENTER_FRAME, render);
-
-
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
-
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
-
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
-
stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler);
-
}
-
-
private function initView():void
-
{
-
_viewBasic = new BasicView(800, 600, true, true);
-
addChild(_viewBasic);
-
-
_viewBasic.viewport.filters = [new BlurFilter(2,2,1)];
-
-
// Camera
-
_cameraTarget = _viewBasic.scene.addChild(new DisplayObject3D());
-
_viewBasic.camera.lookAt(_cameraTarget);
-
_viewBasic.camera.z = -1200;
-
-
// PointLight
-
_pl = new PointLight3D(true);
-
-
}
-
-
private function initMaterials():void
-
{
-
_flatMat = new FlatShadeMaterial(_pl, 0×7DC202, 0×000000);
-
_flatMat.doubleSided = false;
-
_flatMat.fillAlpha = .85;
-
-
_mList = new MaterialsList();
-
_mList.addMaterial(_flatMat, "all");
-
}
-
-
private function initObjects():void
-
{
-
/*
-
* create grid of 4×4x4 cubes with a
-
* space of 10px between all objects
-
*/
-
var rows:int = 0;
-
var cols:int = 0;
-
var depth:int = 0;
-
-
_mainCube = new DisplayObject3D();
-
_viewBasic.scene.addChild(_mainCube);
-
-
for (var i:int = 0; i < 64; i++)
-
{
-
var cube:Cube = new Cube(_mList, CUBE_SIZE, CUBE_SIZE, CUBE_SIZE, 2, 2, 2);
-
cube.name = "cube_" + i;
-
cube.x = -320 + rows * (CUBE_SIZE + GRID_SPACE);
-
cube.y = -320 + cols * (CUBE_SIZE + GRID_SPACE);
-
cube.z = -320 + depth * (CUBE_SIZE + GRID_SPACE);
-
-
if (++rows > 3)
-
{
-
rows = 0;
-
if (++depth > 3)
-
{
-
depth = 0;
-
cols++;
-
}
-
}
-
-
_mainCube.addChild(cube);
-
}
-
-
// place light in the center
-
_pl.x = _mainCube.x;
-
_pl.y = _mainCube.y;
-
_pl.z = _mainCube.z;
-
}
-
-
private function mouseDownHandler(event:MouseEvent):void
-
{
-
_isDown = true;
-
_prevMouseX = event.stageX;
-
_prevMouseY = event.stageY;
-
-
Tweener.addTween(_pl, {time: 2, z: 1500, transition: Back.easeInOut});
-
}
-
-
private function mouseUpHandler(event:MouseEvent = null):void
-
{
-
_isDown = false;
-
-
Tweener.addTween(_pl, {time: 2, z: -320, transition: Back.easeInOut});
-
}
-
-
private function mouseMoveHandler(event:MouseEvent):void
-
{
-
var differenceX:Number = event.stageX - _prevMouseX;
-
var differenceY:Number = event.stageY - _prevMouseY;
-
-
if(_isDown)
-
{
-
// with many thanx to Lee Brimelow
-
// blog post: http://pv3d.org/tag/orbit/
-
_cameraPitch += differenceY;
-
_cameraYaw += differenceX;
-
-
_cameraPitch %= 360;
-
_cameraYaw %= 360;
-
-
_cameraPitch = _cameraPitch > 0 ? _cameraPitch : 0.0001;
-
_cameraPitch = _cameraPitch < 90 ? _cameraPitch : 89.9999;
-
-
_prevMouseX = event.stageX;
-
_prevMouseY = event.stageY;
-
-
_viewBasic.camera.orbit(_cameraPitch, _cameraYaw, true, _mainCube);
-
}
-
-
}
-
-
private function mouseLeaveHandler(event:Event):void
-
{
-
mouseUpHandler();
-
}
-
-
private function render(event:Event):void
-
{
-
_viewBasic.singleRender();
-
}
-
}
-
}
PV3D - Changing Cube Materials at runtime
Last modified on 2009-02-05 09:01:43 GMT. 3 comments. Top.
A little demo illustrating how to change cube materials at runtime. In this demo you can click to go to the next side of the cube. Or just use your left and right arrow keys,
-
_sidesLeftToRight = ["back", "right", "front", "left"];
-
_currpageID = 0;
-
-
private function updateSkin():void
-
{
-
var faceName:String = _sidesLeftToRight[(_currpageID % _sidesLeftToRight.length)];
-
var material:MovieAssetMaterial = new MovieAssetMaterial(_list[_currpageID], true, true, true, true);
-
material.animated = true;
-
material.doubleSided = true;
-
_cube.replaceMaterialByName(material, faceName);
-
}
Where _list is an array with all the linkId’s to library assets. And _currPageID is updated by using the arrow keys (left & right) or mouse.
Will commit it to codexperience at googlecode soon
Just fun with dragging 3D Object
Last modified on 2009-02-16 18:55:30 GMT. 5 comments. Top.
With many thanx to Andy Zupko. Next step adding more objects to it and apply some physics (JigLibFlash) to it.
[UPDATE] Had to remove ground texture, file became way to big.
-
/**
-
* with many thanx to: Andy Zupko
-
*/
-
package
-
{
-
// FLASH
-
import flash.filters.BlurFilter;
-
import flash.display.Sprite;
-
import flash.ui.Mouse;
-
import flash.display.Bitmap;
-
import flash.events.Event;
-
-
// PAPERVISION
-
import org.papervision3d.materials.MovieMaterial;
-
import org.papervision3d.core.proto.LightObject3D;
-
import org.papervision3d.materials.BitmapMaterial;
-
import org.papervision3d.materials.special.CompositeMaterial;
-
import org.papervision3d.view.layer.util.ViewportLayerSortMode;
-
import org.papervision3d.lights.PointLight3D;
-
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
-
import org.papervision3d.core.math.Quaternion;
-
import org.papervision3d.objects.primitives.Sphere;
-
import org.papervision3d.core.math.Plane3D;
-
import org.papervision3d.core.math.Number3D;
-
import org.papervision3d.objects.DisplayObject3D;
-
import org.papervision3d.materials.ColorMaterial;
-
import org.papervision3d.objects.primitives.Plane;
-
import org.papervision3d.view.BasicView;
-
import org.papervision3d.view.layer.ViewportLayer;
-
import org.papervision3d.core.proto.MaterialObject3D;
-
-
[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
-
public class MouseDepth extends BasicView
-
{
-
private var _plane:Plane;
-
private var _lookAt:DisplayObject3D;
-
private var _vz:Number = 0;
-
private var _vy:Number = 0;
-
private var _vx:Number = 0;
-
private var _plane3d:Plane3D;
-
private var _sphere:Sphere;
-
private var _radius:Number = 50;
-
private var _light:PointLight3D;
-
private var _ground:Plane;
-
private var _globalLight:LightObject3D;
-
private var _shadowPlane:Plane;
-
-
[Embed(source="tennisball.jpg")]
-
public var ball:Class;
-
-
public function MouseDepth()
-
{
-
initPapervision();
-
}
-
-
private function initPapervision():void
-
{
-
Mouse.hide();
-
-
// set sortmode
-
viewport.containerSprite.sortMode = ViewportLayerSortMode.INDEX_SORT;
-
-
// create light for ball
-
_light = new PointLight3D(false);
-
_light.y = 300;
-
-
// create general light
-
_globalLight = new PointLight3D(true);
-
_globalLight.y = 400;
-
_globalLight.x = 300;
-
-
// create plane3d
-
_plane3d = new Plane3D();
-
_plane3d.setNormalAndPoint(new Number3D(0, 1, 0), new Number3D(0, 0, 0));
-
-
// ground
-
// add some effect to it
-
var gravelFM:FlatShadeMaterial = new FlatShadeMaterial(_globalLight, 0xFFFFFF, 0×333333);
-
gravelFM.fillAlpha = .2;
-
_ground = new Plane(gravelFM, 2000,2000);
-
_ground.rotationX = 90;
-
scene.addChild(_ground);
-
viewport.getChildLayer(_ground).layerIndex = 0;
-
-
// create mouse object
-
var cm:ColorMaterial = new ColorMaterial(0×333333);
-
cm.fillAlpha = .5;
-
-
_plane = new Plane(cm, 50, 50, 2, 2);
-
_plane.rotationX = 90;
-
-
// -radius, to place the sphere right on top of our plane
-
_plane.y = -_radius;
-
scene.addChild(_plane);
-
viewport.getChildLayer(_plane).layerIndex = 1;
-
-
// create materials for sphere
-
var bmat:BitmapMaterial = new BitmapMaterial(Bitmap(new ball()).bitmapData, true);
-
bmat.tiled = true;
-
bmat.smooth = true;
-
-
var fm:FlatShadeMaterial = new FlatShadeMaterial(_light, 0xFFFFFF, 0×333333);
-
fm.fillAlpha = .5;
-
-
var sm:CompositeMaterial = new CompositeMaterial();
-
sm.addMaterial(bmat);
-
sm.addMaterial(fm);
-
-
// create sphere
-
_sphere = new Sphere(sm, _radius, 20, 18);
-
scene.addChild(_sphere);
-
viewport.getChildLayer(_sphere).layerIndex = 3;
-
-
// object to look at
-
_lookAt = new DisplayObject3D();
-
camera.focus = 100;
-
camera.zoom = 11;
-
camera.lookAt(_lookAt);
-
-
// place camera higher up
-
camera.y = 800;
-
-
createShadow();
-
-
// render
-
startRendering();
-
}
-
-
public function createShadow():void
-
{
-
var floorSprite:Sprite = new Sprite();
-
floorSprite.graphics.beginFill(0xdddddd, 0);
-
floorSprite.graphics.drawRect(0, 0, 400, 400);
-
floorSprite.graphics.endFill();
-
-
var shadow:Sprite = new Sprite();
-
shadow.graphics.beginFill(0×101010, 1);
-
shadow.graphics.drawCircle(0, 0, 100);
-
shadow.graphics.endFill();
-
floorSprite.addChild(shadow);
-
-
shadow.filters = [new BlurFilter(30, 30, 8)];
-
shadow.x = 200;
-
shadow.y = 200;
-
-
_shadowPlane = new Plane(new MovieMaterial(floorSprite, true, false, true), 200, 200, 1, 1);
-
_shadowPlane.pitch(90);
-
scene.addChild(_shadowPlane);
-
_shadowPlane.y = -_radius;
-
viewport.getChildLayer(_shadowPlane).layerIndex = 2;
-
}
-
-
override protected function onRenderTick(event:Event = null):void
-
{
-
// move plane
-
var cameraPosition:Number3D = new Number3D(camera.x, camera.y, camera.z);
-
-
//get the direction vector of the mouse position
-
var ray:Number3D = camera.unproject(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY);
-
-
//convert ray to a 3d point in the ray direction from the camera
-
ray = Number3D.add(ray, cameraPosition);
-
-
//find the intersection of the line defined by the camera and the ray position with the plane3D
-
var intersect:Number3D = _plane3d.getIntersectionLineNumbers(cameraPosition, ray);
-
-
//find distance to object and add to velocity
-
_vx += (_sphere.x-intersect.x)/1200;
-
_vy += (_sphere.y-intersect.y)/100;
-
_vz += (_sphere.z-intersect.z)/1200;
-
-
// update vel
-
_vx *= .98;
-
_vy *= .94;
-
_vz *= .98;
-
-
// move out plane to mouse coordinates
-
_plane.x -= (_plane.x - intersect.x)/4;
-
_plane.z -= (_plane.z - intersect.z)/4;
-
-
// apply vel
-
_sphere.x -= _vx;
-
_sphere.y -= _vy;
-
_sphere.z -= _vz;
-
-
// move shadow allong with sphere
-
_shadowPlane.x = _sphere.x;
-
_shadowPlane.z = _sphere.z;
-
-
// move light with ball
-
_light.x = _sphere.x;
-
_light.z = _sphere.z;
-
-
// the function written by Andy Zupko (blog.zupko.info), so usefull!
-
rollMe(_vx, _vy, _vz);
-
-
super.onRenderTick(event);
-
}
-
-
private function rollMe(vx:Number, vy:Number, vz:Number):void
-
{
-
// var pos:Number3D = new Number3D(_sphere.x, _sphere.y, _sphere.z);
-
var dif:Number3D = new Number3D(-vx, -vy, -vz);
-
var dist:Number = Math.sqrt(dif.x*dif.x+dif.z*dif.z);
-
-
//find the cross of the up axis with the direction vector
-
var rotAxis:Number3D = Number3D.cross(dif, new Number3D(0, 1, 0));
-
rotAxis.normalize();
-
-
//rotate around that axis by how long the direction vector is relative to the radius
-
var rotation:Quaternion = Quaternion.createFromAxisAngle(rotAxis.x, rotAxis.y, rotAxis.z, dist/_radius);
-
rotation.normalize();
-
-
_sphere.transform.calculateMultiply3×3(rotation.matrix, _sphere.transform);
-
}
-
}
-
}
JigLib Flash + Papervision3D Demo
Last modified on 2009-03-18 20:55:52 GMT. 2 comments. Top.
I had to play immediately with the JigLibFlash library. Like always there is a learning curve, but the syntax is at least better written/understandable than Box2DFlash, which doesn’t mean that I don’t like Box2DFlash, cause I do!
In this example you use the mouse to move the magenta ball, when you keep your mouse at the center of the stage you don’t apply any speed to the magenta ball. All the balls can drop of the floor on the front side. Have FUN!
-
/**
-
* @author janwillemwisman
-
*
-
* positioning 3d papervision objects, only via the jBox.moveTo,
-
* otherwise object will be centered when added to the PhysicsSystem.
-
*
-
* when you dont use physicsStep, jBoxes will not be place correctly. nothing will happen basically
-
*/
-
package
-
{
-
// flash
-
import flash.utils.getTimer;
-
import flash.events.Event;
-
import flash.display.Sprite;
-
-
// wismanas3lib
-
import nl.wisman.utils.MathExt;
-
-
// papervision
-
import org.papervision3d.cameras.Camera3D;
-
import org.papervision3d.lights.PointLight3D;
-
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
-
import org.papervision3d.materials.utils.MaterialsList;
-
import org.papervision3d.objects.DisplayObject3D;
-
import org.papervision3d.objects.primitives.Cube;
-
import org.papervision3d.objects.primitives.Sphere;
-
import org.papervision3d.render.BasicRenderEngine;
-
import org.papervision3d.scenes.Scene3D;
-
import org.papervision3d.view.Viewport3D;
-
import org.papervision3d.view.layer.ViewportLayer;
-
import org.papervision3d.view.layer.util.ViewportLayerSortMode;
-
-
// jiglibflash
-
import jiglib.physics.PhysicsSystem;
-
import jiglib.math.JMatrix3D;
-
import jiglib.math.JNumber3D;
-
import jiglib.geometry.JSphere;
-
import jiglib.geometry.JBox;
-
-
public class JigLibDemo extends Sprite
-
{
-
//
-
// private properties
-
//
-
private var _scene:Scene3D;
-
private var _viewport:Viewport3D;
-
private var _renderer:BasicRenderEngine;
-
private var _sceneLight:PointLight3D;
-
private var _camera:Camera3D;
-
private var _cameraTarget:DisplayObject3D;
-
private var _newTime:int;
-
private var _deltaTime:Number;
-
private var _currentTime:Number;
-
private var _physicsSpeed:Number = 10;
-
private var _speedBall:int = 10;
-
private var _maxBall:Number = 10;
-
private var _jBalls:Array;
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Constructor
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
public function JigLibDemo()
-
{
-
addEventListener(Event.ADDED_TO_STAGE, initialize);
-
}
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Public methods
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
// none
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Private methods
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
private function initialize(event:Event):void
-
{
-
removeEventListener(Event.ADDED_TO_STAGE, initialize);
-
-
// setup
-
_currentTime = getTimer();
-
-
initPapervision3D();
-
initArea();
-
init3DObjects();
-
setupEventListeners();
-
}
-
-
// ———————————————————————————–
-
private function initPapervision3D():void
-
{
-
_scene = new Scene3D();
-
_viewport = new Viewport3D(0,0,true);
-
addChild(_viewport);
-
-
// set _viewport sortmode to index
-
_viewport.containerSprite.sortMode = ViewportLayerSortMode.INDEX_SORT;
-
-
_renderer = new BasicRenderEngine();
-
-
// light settings
-
_sceneLight = new PointLight3D(true, true);
-
_sceneLight.x = 0;
-
_sceneLight.y = 400;
-
_sceneLight.z = -300;
-
-
// _camera settings
-
_camera = new Camera3D();
-
_camera.x = 0;
-
_camera.y = 450;
-
_camera.z = -1000;
-
_camera.zoom = 5;
-
_camera.focus = 100;
-
-
// _camera target, helper object to make the _camera a target type
-
_cameraTarget=new DisplayObject3D();
-
_cameraTarget.x=0;
-
_cameraTarget.y=250;
-
_cameraTarget.z=0;
-
// set the _camera target
-
_camera.target = _cameraTarget;
-
}
-
-
// ———————————————————————————–
-
private function initArea():void
-
{
-
// general material
-
var materialGround:MaterialsList = new MaterialsList();
-
materialGround.addMaterial(new FlatShadeMaterial(_sceneLight, 0×999999), "all");
-
-
// create back wall
-
var backWall3D:Cube = new Cube(materialGround, 1000, 50, 1000, 2, 2, 2);
-
_scene.addChild(backWall3D);
-
-
var vplBackWall3D:ViewportLayer = _viewport.getChildLayer(backWall3D);
-
vplBackWall3D.layerIndex = 1;
-
-
var jBackWall:JBox = new JBox(backWall3D, false, 1000, 50, 1000);
-
jBackWall.MoveTo( new JNumber3D(0, 0, 500), JMatrix3D.IDENTITY);
-
PhysicsSystem.getInstance().AddBody(jBackWall);
-
-
// create left wall
-
var leftWall3D:Cube = new Cube(materialGround, 50, 1000, 1000, 2, 2, 2);
-
_scene.addChild(leftWall3D);
-
-
var vplLeftWall3D:ViewportLayer = _viewport.getChildLayer(leftWall3D);
-
vplLeftWall3D.layerIndex = 2;
-
-
var jLeftWall:JBox = new JBox(leftWall3D, false, 50, 1000, 1000);
-
jLeftWall.MoveTo( new JNumber3D(500, 0, 0), JMatrix3D.IDENTITY);
-
PhysicsSystem.getInstance().AddBody(jLeftWall);
-
-
// create right wall
-
var rightWall3D:Cube = new Cube(materialGround, 50, 1000, 1000, 2, 2, 2);
-
_scene.addChild(rightWall3D);
-
-
var vplRightWall3D:ViewportLayer = _viewport.getChildLayer(rightWall3D);
-
vplRightWall3D.layerIndex = 3;
-
-
var jRightWall:JBox = new JBox(rightWall3D, false, 50, 1000, 1000);
-
jRightWall.MoveTo( new JNumber3D(-500, 0, 0), JMatrix3D.IDENTITY);
-
PhysicsSystem.getInstance().AddBody(jRightWall);
-
-
// Ground pv3d
-
var pv3dGround:Cube = new Cube(materialGround,1000,1000,50,2,2,2);
-
_scene.addChild(pv3dGround);
-
-
// Viewport layer ground
-
var vplGround:ViewportLayer = _viewport.getChildLayer(pv3dGround);
-
vplGround.layerIndex = 4;
-
-
// Ground physics
-
var jGround:JBox = new JBox(pv3dGround,false,1000,1000,50);
-
PhysicsSystem.getInstance().AddBody(jGround);
-
}
-
-
// ———————————————————————————–
-
private function init3DObjects():void
-
{
-
// VIEWPORT LAYER OBJECTS
-
// balls boxes chain sphere will be placed on it
-
var vplObjects:ViewportLayer = new ViewportLayer(_viewport,null);
-
vplObjects.layerIndex = 4;
-
vplObjects.sortMode = ViewportLayerSortMode.Z_SORT; // sort mode for this viewportlayer based on z
-
_viewport.containerSprite.addLayer(vplObjects);
-
-
_jBalls = new Array();
-
for (var i:int = 0; i < _maxBall; i++)
-
{
-
// create sphere
-
var colorSphere:uint = (i == 0)?0xe20074:0xeeee00;
-
var pv3dBall:Sphere = new Sphere( new FlatShadeMaterial(_sceneLight, colorSphere) ,50,20,16);
-
-
// add sphere to viewportlayer for objects
-
vplObjects.addDisplayObject3D(pv3dBall);
-
-
// add to _scene
-
_scene.addChild(pv3dBall);
-
-
// add papervision sphere to jSphere
-
_jBalls[i] = new JSphere(pv3dBall,true,50);
-
_jBalls[i].MoveTo(new JNumber3D(MathExt.random(-100, 250), MathExt.random(400, 650), MathExt.random(100, 350)), JMatrix3D.IDENTITY); // start position x,y,z
-
-
// add to physicsSystem
-
PhysicsSystem.getInstance().AddBody(_jBalls[i]);
-
}
-
}
-
-
// ———————————————————————————–
-
private function setupEventListeners():void
-
{
-
addEventListener(Event.ENTER_FRAME, update);
-
}
-
-
// ———————————————————————————–
-
private function update(event:Event):void
-
{
-
moveBall();
-
physicsStep();
-
checkFallenObjects();
-
_renderer.renderScene( _scene, _camera, _viewport );
-
}
-
-
// Calculate the deltaTime
-
// ————————————————————————
-
private function physicsStep():void
-
{
-
_newTime = getTimer();
-
_deltaTime = ((_newTime - _currentTime) / 1000) * _physicsSpeed;
-
_currentTime = _newTime;
-
PhysicsSystem.getInstance().Integrate(_deltaTime); // phyisc step 5 is the speed
-
}
-
-
// ———————————————————————————–
-
private function checkFallenObjects():void
-
{
-
for (var i:String in _jBalls)
-
{
-
if (_jBalls[i].CurrentState.Position.y < -50)
-
{
-
_jBalls[i].MoveTo(new JNumber3D(MathExt.random(-100, 250), MathExt.random(400, 650), MathExt.random(100, 350)), JMatrix3D.IDENTITY); // reposition balls
-
}
-
}
-
}
-
-
// ———————————————————————————–
-
private function moveBall():void
-
{
-
// area left
-
if (mouseX < 350)
-
{
-
_jBalls[0].AddWorldForce(new JNumber3D(-_speedBall,0,0), _jBalls[0].CurrentState.Position);
-
}
-
// area right
-
if (mouseX > 450)
-
{
-
_jBalls[0].AddWorldForce(new JNumber3D(_speedBall,0,0), _jBalls[0].CurrentState.Position);
-
}
-
// top area
-
if (mouseY < 250)
-
{
-
_jBalls[0].AddWorldForce(new JNumber3D(0,0,_speedBall), _jBalls[0].CurrentState.Position);
-
}
-
// bottom area
-
if (mouseY > 350)
-
{
-
_jBalls[0].AddWorldForce(new JNumber3D(0,0,-_speedBall), _jBalls[0].CurrentState.Position);
-
}
-
}
-
}
-
}
Papervision3D + Collada
Last modified on 2009-02-16 19:00:09 GMT. 0 comments. Top.
Playing around with collada (dae) files which use textures. You can get lots of models at google’s 3D warehouse, check it and start playing around with it. Its really easy!
-
package
-
{
-
// import flash
-
import flash.filters.BlurFilter;
-
import flash.display.StageScaleMode;
-
import flash.display.StageAlign;
-
import flash.display.Sprite;
-
import flash.events.Event;
-
-
//import main papervision assets
-
import org.papervision3d.cameras.Camera3D;
-
import org.papervision3d.lights.PointLight3D;
-
import org.papervision3d.render.QuadrantRenderEngine;
-
import org.papervision3d.scenes.Scene3D;
-
import org.papervision3d.view.Viewport3D;
-
-
//import objects
-
import org.papervision3d.objects.DisplayObject3D;
-
import org.papervision3d.objects.parsers.DAE;
-
-
//import event listener
-
import org.papervision3d.events.FileLoadEvent;
-
-
public class EifelTowerCollada extends Sprite
-
{
-
//papervision main assets
-
private var scene:Scene3D;
-
private var viewport:Viewport3D;
-
private var camera:Camera3D;
-
private var light:PointLight3D;
-
private var quadRenderer:QuadrantRenderEngine;
-
-
//other things needed for dae
-
public var universe:DisplayObject3D;
-
private var daeFile:DAE;
-
private var bl:Number;
-
private var bt:Number;
-
private var per:Number;
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Constructor
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
public function EifelTowerCollada()
-
{
-
addEventListener(Event.ADDED_TO_STAGE, initialize);
-
}
-
-
// ———————————————————————————–
-
// constructor listener
-
private function initialize(event:Event):void
-
{
-
removeEventListener(Event.ADDED_TO_STAGE, initialize);
-
-
// blur for more realness
-
filters = [new BlurFilter(1.5,1.5,3)];
-
-
//Setup stage.
-
stage.align = StageAlign.TOP_LEFT;
-
stage.scaleMode = StageScaleMode.NO_SCALE;
-
stage.quality = "BEST";
-
-
//Setup viewport, add to stage
-
viewport = new Viewport3D(0,0,true);
-
addChild(viewport);
-
-
//Setup renderer
-
quadRenderer = new QuadrantRenderEngine(1);
-
-
//Setup camera
-
camera = new Camera3D();
-
camera.y = 100;
-
camera.z = -200;
-
-
//Setup light
-
light = new PointLight3D(true);
-
light.x = 0;
-
light.y = 500;//camera.y;
-
light.z = -200;//camera.z;
-
-
//Setup scene
-
scene = new Scene3D();
-
-
//add dae object
-
daeFile = new DAE();
-
daeFile.load("daefiles/eifelTower.dae");
-
-
// scale
-
daeFile.y = -100;
-
-
//add loading listeners to your dae
-
daeFile.addEventListener(FileLoadEvent.LOAD_COMPLETE, handleLoadComplete);
-
daeFile.addEventListener(FileLoadEvent.LOAD_ERROR, handleLoadError);
-
daeFile.addEventListener(FileLoadEvent.LOAD_PROGRESS, handleProgress);
-
daeFile.addEventListener(FileLoadEvent.SECURITY_LOAD_ERROR, handleSecurity);
-
daeFile.addEventListener(FileLoadEvent.COLLADA_MATERIALS_DONE, handleMaterialsDone);
-
-
//Setup container, add dae to container, add container to scene.
-
universe = new DisplayObject3D();
-
universe.addChild(daeFile);
-
scene.addChild(universe);
-
-
//Listen to enter frame
-
addEventListener(Event.ENTER_FRAME, handleRender);
-
}
-
-
private function handleLoadComplete(e:Event):void
-
{
-
trace("IN EVENT LISTENER, LOAD COMPLETE");
-
};
-
-
private function handleLoadError(e:Event):void
-
{
-
trace("THERE HAS BEEN A LOADING ERROR");
-
};
-
-
private function handleProgress(e:Event):void
-
{
-
bl = e.target.bytesLoaded;
-
bt = e.target.bytesTotal;
-
per = Math.round(bl/bt*100);
-
trace("COLLADA "+per+"% LOADED, PLEASE WAIT");
-
};
-
-
private function handleSecurity(e:Event):void
-
{
-
trace("THERE HAS BEEN A SECURITY ERROR");
-
};
-
-
private function handleMaterialsDone(e:Event):void
-
{
-
trace("COLLADA MATERIALS LOAD COMPLETE");
-
};
-
-
// _______________________________________________________________________
-
// render on enterframe
-
-
private function handleRender(e:Event):void
-
{
-
camera.lookAt(universe);
-
quadRenderer.renderScene(scene, camera, viewport);
-
}
-
}
-
}
Now add the object to your stage
-
_eifelTower = new EifelTowerCollada();
-
addChild(_eifelTower);
Fun with Ribbons3D
Last modified on 2009-02-16 19:05:50 GMT. 0 comments. Top.
Old experiment I found with the ribbon3D class thought might as well post it. With many thanx to Soulwire for writing this class. Based on where your mouse positions it tweens to that location via bezier points.
-
package
-
{
-
// papervision
-
import org.papervision3d.core.effects.view.ReflectionView;
-
import org.papervision3d.lights.PointLight3D;
-
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
-
import org.papervision3d.objects.DisplayObject3D;
-
-
// tweenmax
-
import gs.TweenMax;
-
import gs.easing.Linear;
-
-
// flash
-
import flash.display.DisplayObjectContainer;
-
import flash.events.Event;
-
import flash.filters.BlurFilter;
-
-
-
public class CirclingRibbon extends ReflectionView
-
{
-
private var _light:PointLight3D;
-
private var _ribbon:Ribbon3D;
-
private var _universe:DisplayObject3D;
-
private var _centerX:Number = 0;
-
private var _centerY:Number = 0;
-
private static const UNIVERSE:Number = 500;
-
-
public function CirclingRibbon()
-
{
-
initialize();
-
}
-
-
private function initialize():void
-
{
-
// setup reflection
-
viewportReflection.filters = [new BlurFilter(6,6,1)];
-
setReflectionColor(.25, .25, .25);
-
surfaceHeight = -400;
-
-
camera.z = -1500;
-
-
// light
-
_light = new PointLight3D();
-
_light.x = camera.x;
-
_light.y = camera.y - 500;
-
_light.z = -camera.z;
-
-
viewport.filters = [new BlurFilter(2,2,1)];
-
-
// universe
-
_universe = new DisplayObject3D();
-
scene.addChild(_universe);
-
-
// setup line material
-
var fMat:FlatShadeMaterial = new FlatShadeMaterial(_light, 0XE20074, 0xF7F7F7);
-
fMat.doubleSided = true;
-
-
// setup line
-
_ribbon = new Ribbon3D(fMat, 40, 40);
-
_universe.addChild(_ribbon);
-
-
addEventListener(Event.ENTER_FRAME, update);
-
-
tween(_ribbon);
-
}
-
-
public function setStage(inClip:DisplayObjectContainer)
-
{
-
_centerX = inClip.stage.stageWidth * .5;
-
_centerY = inClip.stage.stageHeight * .5;
-
}
-
-
private function tween(object:Ribbon3D):void
-
{
-
if(object is Ribbon3D)
-
{
-
var b:Array = new Array();
-
var d:int = random( 3, 6 );
-
for (var i:int = 0; i < d * .6 << 0; i++)
-
{
-
b.push( {
-
x:random( -UNIVERSE, UNIVERSE ),
-
y:400, // we don't want this to be lower than the reflectionViews surface
-
z:random( -UNIVERSE, UNIVERSE )
-
} );
-
}
-
-
var posX:Number = -_centerX + mouseX;
-
var posY:Number = _centerY + -mouseY;
-
var posZ:Number = (_ribbon.x - _ribbon.y) * 2;
-
TweenMax.to(_ribbon, 1, {x:posX, y:posY, z:posZ, ease:Linear.easeNone, bezierThrough:b, onComplete: tween, onCompleteParams: [object] });
-
}
-
}
-
-
private function update(event:Event):void
-
{
-
_ribbon.draw();
-
_universe.yaw(1);
-
camera.lookAt(_universe);
-
singleRender();
-
}
-
-
// ———————————————————————————–
-
public static function random( min:Number, max:Number = NaN ):Number
-
{
-
if (isNaN(max)) { max = min; min = 0; }
-
return Math.random() * (max - min) + min;
-
}
-
}
-
}
Now add it to your stage and set the stage:
-
var cRibbon:CirclingRibbon = new CirclingRibbon();
-
cRibbon.setStage(this);
-
addChild(cRibbon);
3D World - SimpleDemo
Last modified on 2009-02-16 18:22:25 GMT. 4 comments. Top.
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:
-
package
-
{
-
// tweenmax
-
import gs.easing.Back;
-
import gs.TweenMax;
-
-
// papervision
-
import org.papervision3d.materials.shaders.ShadedMaterial;
-
import org.papervision3d.materials.shaders.PhongShader;
-
import org.papervision3d.core.effects.view.ReflectionView;
-
import org.papervision3d.lights.PointLight3D;
-
import org.papervision3d.materials.BitmapMaterial;
-
import org.papervision3d.objects.primitives.Sphere;
-
-
// flash
-
import flash.filters.BlurFilter;
-
import flash.display.BitmapData;
-
import flash.events.Event;
-
-
public class BitmapEarth extends ReflectionView
-
{
-
private var _light:PointLight3D;
-
private var _sphere:Sphere;
-
-
public function BitmapEarth()
-
{
-
initialize();
-
}
-
-
private function initialize():void
-
{
-
// setup reflection
-
viewportReflection.filters = [new BlurFilter(6,6,1)];
-
setReflectionColor(.5, .5, .5);
-
surfaceHeight = -200;
-
-
// camera
-
camera.z = -300;
-
-
// create light
-
_light = new PointLight3D();
-
-
// create bitmap material from library asset
-
var bmClass:Class = getDefinitionByName("EarthTexture") as Class;
-
var bmd:BitmapData = new bmClass(0,0);
-
var bMaterial:BitmapMaterial = new BitmapMaterial(bmd, true);
-
-
// create shader
-
var pShader:PhongShader = new PhongShader(_light, 0xFFFFFF, 0×666666, 20);
-
-
//create shaded material from my bitmap from library and the phongshader
-
var shadedMaterial:ShadedMaterial = new ShadedMaterial(bMaterial, pShader);
-
-
// create sphere and apply shaded material
-
_sphere = new Sphere(shadedMaterial, 100, 24, 18);
-
_sphere.x = 700;
-
_sphere.z = -1200;
-
scene.addChild(_sphere);
-
-
// render
-
addEventListener(Event.ENTER_FRAME, update);
-
-
// move sphere to center
-
TweenMax.to(_sphere, 5, {x: 0, y: 0, z: 0, ease: Back.easeOut});
-
}
-
-
private function update(event:Event):void
-
{
-
camera.lookAt(_sphere);
-
-
_sphere.yaw(1.5); // y-axis
-
// _sphere.pitch(1.25); // x-axis
-
// _sphere.roll(1);// z-axis
-
-
singleRender();
-
}
-
}
-
}
The only thing you have to do now is add it to your stage:
-
var earth:BitmapEarth = new BitmapEarth();
-
addChild(earth);
GoogleMaps API - SimpleDemo
Last modified on 2009-02-16 19:09:47 GMT. 0 comments. Top.
Simple demo showing google maps in flash, add a marker as an overlay with a listener to open its information window. (Up at codexperiments.googlecode.com).
-
package
-
{
-
// flash
-
import flash.geom.Point;
-
import flash.events.Event;
-
import flash.display.MovieClip;
-
-
// google maps
-
import com.google.maps.Map;
-
import com.google.maps.MapEvent;
-
import com.google.maps.MapType;
-
import com.google.maps.LatLng;
-
import com.google.maps.InfoWindowOptions;
-
import com.google.maps.MapOptions;
-
import com.google.maps.overlays.Marker;
-
import com.google.maps.MapMouseEvent;
-
import com.google.maps.controls.NavigationControl;
-
import com.google.maps.controls.MapTypeControl;
-
-
public class ShowMapsDemo extends MovieClip
-
{
-
private var _map:Map;
-
private var _marker:Marker;
-
-
public function ShowMapsDemo()
-
{
-
addEventListener(Event.ADDED_TO_STAGE, initialize);
-
}
-
-
private function initialize(event:Event):void
-
{
-
removeEventListener(Event.ADDED_TO_STAGE, initialize);
-
-
// setup google map
-
_map = new Map();
-
-
// place your own googlemaps api key at:http://code.google.com/apis/maps/signup.html
-
_map.key = "your googlemaps api key goes here";
-
_map.setSize(new Point(550, 400));
-
_map.addEventListener(MapEvent.MAP_PREINITIALIZE, mapPreInitializeHandler);
-
_map.addEventListener(MapEvent.MAP_READY, mapReadyHandler);
-
_map.addControl(new NavigationControl());
-
_map.addControl(new MapTypeControl());
-
-
// add to stage
-
addChild(_map);
-
}
-
-
private function mapReadyHandler(event:MapEvent):void
-
{
-
_marker = new Marker(new LatLng(52.365983,4.80757));
-
_marker.addEventListener(MapMouseEvent.CLICK, onMarkerClicked);
-
_map.addOverlay(_marker);
-
-
onMarkerClicked();
-
}
-
-
private function mapPreInitializeHandler(event:MapEvent):void
-
{
-
_map.removeEventListener(MapEvent.MAP_PREINITIALIZE, mapPreInitializeHandler);
-
-
var mapOptions:MapOptions = new MapOptions();
-
mapOptions.center = new LatLng(52.365983,4.80757);
-
mapOptions.zoom = 17;
-
mapOptions.mapType = MapType.SATELLITE_MAP_TYPE;
-
_map.setInitOptions(mapOptions);
-
}
-
-
private function onMarkerClicked(event:MapMouseEvent = null):void
-
{
-
_marker.openInfoWindow(new InfoWindowOptions({title: "Home sweet home", content: "Ooh no, my house isn't available!!"}));
-
}
-
}
-
}
Papervision3D Multiple Objects hitTest
Last modified on 2009-02-16 20:27:46 GMT. 1 comment. Top.
A little research on hitTest with multiple objects. You could imagine what you could do with it, for instance create a game like Guitar Hero (with a setup like this that is).
Code is now committed at codexperiments.googlecode.com, hope you enjoy it
If you want to add more hitArea’s (planes) on the stage to do hitTests with use for instance the following line of code:_world.addHitArea(0x0000FF, 500, -25, 600);.

1. Click here to see it in action with multiple hitarea’s

2. And add some nice reflections, and it will look like this
Code of the reflection version:
Main Class
-
/**
-
* @author janwillemwisman
-
* We have 3 lanes, in this case: -500, 0 and 500
-
*/
-
package
-
{
-
// wismanas3lib
-
import nl.wisman.utils.MathExt;
-
import nl.wisman.components.Slider;
-
-
// flash
-
import flash.events.TimerEvent;
-
import flash.utils.Timer;
-
import flash.events.Event;
-
import flash.display.MovieClip;
-
-
public class DemoHitTestReflection extends MovieClip
-
{
-
//
-
// on stage
-
//
-
public var thumb:MovieClip;
-
public var track:MovieClip;
-
-
//
-
// private properties
-
//
-
private var _world:GuitarHeroSetupReflection;
-
private var _leftLaneTimer:Timer;
-
private var _centerLaneTimer:Timer;
-
private var _rightLaneTimer:Timer;
-
-
// wismanas3lib
-
private var _slider:Slider;
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Constructor
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
public function DemoHitTestReflection()
-
{
-
addEventListener(Event.ADDED_TO_STAGE, initialize);
-
}
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Public methods
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
// none
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Private methods
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
private function initialize(event:Event):void
-
{
-
removeEventListener(Event.ADDED_TO_STAGE, initialize);
-
-
// create world
-
_world = new GuitarHeroSetupReflection();
-
-
// set start and end points
-
_world.startPointZ = 2000;
-
_world.endPointZ = -1000;
-
-
// set hitarea positions
-
// (back) 1000 -> 0 -> -1000 (front) on z-axis
-
// left
-
_world.addHitArea(0xFF0000, -500, -25, 1200);
-
_world.addHitArea(0xFF0000, -500, -25, 0);
-
// center
-
_world.addHitArea(0xFFFFFF, 0, -25, 900);
-
_world.addHitArea(0xFFFFFF, 0, -25, 0);
-
// right
-
_world.addHitArea(0×0000FF, 500, -25, 1600);
-
_world.addHitArea(0×0000FF, 500, -25, 600);
-
_world.addHitArea(0×0000FF, 500, -25, 0);
-
-
// initialize timers
-
_leftLaneTimer = new Timer(1000);
-
_leftLaneTimer.addEventListener(TimerEvent.TIMER, leftLaneTimerHandler);
-
_leftLaneTimer.start();
-
-
_centerLaneTimer = new Timer(2000);
-
_centerLaneTimer.addEventListener(TimerEvent.TIMER, centerLaneTimerHandler);
-
_centerLaneTimer.start();
-
-
_rightLaneTimer = new Timer(3000);
-
_rightLaneTimer.addEventListener(TimerEvent.TIMER, rightLaneTimerHandler);
-
_rightLaneTimer.start();
-
-
// add to stage
-
addChild(_world);
-
-
// Slider
-
_slider = new Slider(stage, thumb, track);
-
_slider.restricted = true;
-
_slider.friction = .90;
-
_slider.addEventListener(Event.CHANGE, sliderUpdate);
-
}
-
-
// ———————————————————————————–
-
private function sliderUpdate(event:Event):void
-
{
-
var rot:Number = _slider.percentage;
-
-
// only from front to back (1.8 instead of 3.6)
-
_world.universe.rotationY = rot * 1.8;
-
}
-
-
// ———————————————————————————–
-
private function leftLaneTimerHandler(event:TimerEvent):void
-
{
-
_world.addNote(0xFF0000, -500, 0, MathExt.random(1, 4));
-
}
-
-
// ———————————————————————————–
-
private function centerLaneTimerHandler(event:TimerEvent):void
-
{
-
_world.addNote(0xFFFFFF, 0, 0, MathExt.random(2, 5));
-
}
-
-
// ———————————————————————————–
-
private function rightLaneTimerHandler(event:TimerEvent):void
-
{
-
_world.addNote(0×0000FF, 500, 0, MathExt.random(3, 6));
-
}
-
}
-
}
3D World/setup Class
-
package
-
{
-
// flash
-
import flash.filters.BlurFilter;
-
import flash.events.Event;
-
-
// tweenmax
-
import gs.TweenMax;
-
-
// papervision3d
-
import org.papervision3d.objects.primitives.Plane;
-
import org.papervision3d.objects.DisplayObject3D;
-
import org.papervision3d.core.proto.MaterialObject3D;
-
import org.papervision3d.lights.PointLight3D;
-
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
-
import org.papervision3d.materials.utils.MaterialsList;
-
import org.papervision3d.objects.primitives.Cube;
-
import org.papervision3d.core.effects.view.ReflectionView;
-
import org.papervision3d.materials.WireframeMaterial;
-
-
-
public class GuitarHeroSetupReflection extends ReflectionView
-
{
-
//
-
// public properties
-
//
-
// the higher it is, the further away
-
public var startPointZ:Number = 500;
-
// the lower the closer it will come
-
public var endPointZ:Number = -500;
-
-
//
-
// private properties
-
//
-
private var _light:PointLight3D;
-
private var _matList:MaterialsList = new MaterialsList();
-
private var _hitAreaList:Array = new Array();
-
private var _noteList:Array = new Array();
-
private var _universe:DisplayObject3D;
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Constructor
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
public function GuitarHeroSetupReflection()
-
{
-
// use reflection
-
viewportReflection.filters = [new BlurFilter(6,6,1)];
-
setReflectionColor(.5, .5, .5);
-
surfaceHeight = -100;
-
-
_universe = new DisplayObject3D();
-
scene.addChild(_universe);
-
-
initialize();
-
}
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Public methods
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
public function addHitArea(hitColor:Number = 0×000000, xPos:Number = 0, yPos:Number = 0, zPos:Number = 0):void
-
{
-
var material:MaterialObject3D = new WireframeMaterial(hitColor, .5, 1);
-
material.doubleSided = true;
-
var plane:Plane = new Plane(material, 200, 200, 2, 2);
-
plane.x = xPos;
-
plane.y = yPos;
-
plane.z = zPos;
-
plane.rotationX = 90;
-
universe.addChild(plane);
-
-
_hitAreaList.push(plane);
-
}
-
-
// ———————————————————————————–
-
public function addNote(noteColor:Number = 0xFF0000, xPosition:Number = 0, yPosition:Number = 0, speed:Number = 1):void
-
{
-
// create material
-
var material:MaterialObject3D = new FlatShadeMaterial(_light, noteColor ,0X333333);
-
_matList.addMaterial(material, "all");
-
-
// create box
-
var box:Cube = new Cube(_matList, 200, 200,50, 2, 2, 2);
-
box.x = xPosition;
-
box.y = yPosition;
-
box.z = startPointZ;
-
box.extra = {hitted: false};
-
-
// add box
-
universe.addChild(box);
-
-
// store all 3d objecs
-
_noteList.push(box);
-
-
// start tween
-
startTween(box, speed);
-
}
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Getters & Setters
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
public function get universe():DisplayObject3D
-
{
-
return _universe;
-
}
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Private methods
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
private function initialize():void
-
{
-
// place camera higher-up and look down
-
camera.y = 400;
-
-
// light
-
_light = new PointLight3D();
-
_light.x = camera.x + 500;
-
_light.y = camera.y;
-
-
var do3d:DisplayObject3D = new DisplayObject3D();
-
scene.addChild(do3d);
-
camera.lookAt(do3d);
-
_light.lookAt(do3d);
-
-
// render
-
addEventListener(Event.ENTER_FRAME, update);
-
}
-
-
// ———————————————————————————–
-
private function startTween(box:Cube, speed:Number):void
-
{
-
TweenMax.to(box, speed, {z: endPointZ});
-
}
-
-
// ———————————————————————————–
-
private function update(event:Event):void
-
{
-
for (var i:int = 0; i < _noteList.length; ++i)
-
{
-
for (var j:int = 0; j < _hitAreaList.length; ++j)
-
{
-
var note:Cube = _noteList[i] as Cube;
-
var hitArea:Plane = _hitAreaList[j] as Plane;
-
-
// reset when needed
-
if (!note.hitTestObject(hitArea) && note.extra.hitted)
-
{
-
note.y = 0;
-
note.extra.hitted = false;
-
}
-
-
// set note when hits a plane
-
if (note.hitTestObject(hitArea))
-
{
-
note.extra.hitted = true;
-
note.y = 100;
-
break;
-
}
-
}
-
-
// remove when needed
-
if (note.z <= endPointZ)
-
{
-
_universe.removeChild(note);
-
_noteList.slice(i,0);
-
}
-
}
-
-
// render reflection
-
singleRender();
-
}
-
}
-
}
Papervision3D 2.0 + Google Sketchup - v2
Last modified on 2009-01-07 19:24:23 GMT. 3 comments. Top.
After loading the collada file and apply a texture on the whole object (see previous post), we now give each seperate object from the collada file its own texture and make them interactive.
get main node:
var holder:DisplayObject3D = daeFile.getChildByName("COLLADA_Scene").getChildByName("Model");
Within the “Model” we find all our 6 mesh shapes.
loop through all meshes:
for (var i:int = 0; i < holder.numChildren; i++)
{
var object3D:DisplayObject3D = holder.getChildByName("mesh" + (i+1).toString());
object3D.extra = {x: object3D.x, y: object3D.y, z: object3D.z};
// do not use materialslist but set it directly, get the colors from a class variable called _colors (Array)
var mat:FlatShadeMaterial = new FlatShadeMaterial(light, _colors[i].diffuse, _colors[i].ambient);
mat.interactive = true;
object3D.material = mat;
object3D.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, object3dClick);
}
Papervision3D 2.0 and Google Sketchup
Last modified on 2009-01-07 13:14:45 GMT. 4 comments. Top.
Its a first tryout, so any feedback/help/examples it’s all welcome, just drop a line/comment. Thanx in advance!
UPDATE: instead of using the BasicRenderEngine I implemented the QuadrantRenderEngine with the type number set to 1, so now there are no face ‘issues’ anymore.
And here are the source files: download
Particles v3
Last modified on 2009-02-16 19:50:36 GMT. 0 comments. Top.
Particles with random velocity on x-axis aswell on the y-axis and gravitation.
Using the same Particle class as in Particles v1 (post), is this another demo:
-
package
-
{
-
// FLASH
-
import flash.filters.BlurFilter;
-
import flash.utils.getDefinitionByName;
-
import flash.events.Event;
-
import flash.display.MovieClip;
-
-
public class Particles003 extends MovieClip
-
{
-
//
-
// Private properties
-
//
-
private var _particles:Array;
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Constructor
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
public function Particles003()
-
{
-
_particles = new Array();
-
filters = [new BlurFilter(6,6,1)];
-
addEventListener(Event.ENTER_FRAME, enterframeHandler);
-
}
-
-
private function enterframeHandler(event : Event) : void
-
{
-
var particle:Particle;
-
-
// loop through particles
-
for (var i:int = 0; i < _particles.length; i++)
-
{
-
particle = Particle(_particles[i]);
-
particle.update();
-
}
-
-
// create particles
-
particle = new Particle(getDefinitionByName("Circle") as Class, this, mouseX, mouseY);
-
particle.xVel = random(-2, 2);
-
particle.yVel = random(-6, -10);
-
-
particle.drag = .99;
-
particle.shrink = 1.05;
-
particle.gravity = .3;
-
particle.fade = .02;
-
_particles.push(particle);
-
-
// add particles
-
while (_particles.length > 60)
-
{
-
// remove particle from array _particles
-
particle = _particles.shift();
-
-
// remove from displaylist
-
particle.destroy();
-
}
-
}
-
// ———————————————————————————–
-
public static function random( min:Number, max:Number = NaN ):Number
-
{
-
if (isNaN(max)) { max = min; min = 0; }
-
return Math.random() * (max - min) + min;
-
}
-
}
-
}
Particles v2
Last modified on 2009-02-16 19:51:41 GMT. 0 comments. Top.
Particles with no velocity on the axis’s, but instead we use negative gravitation.
Using the same Particle class as in Particles v1 (post), is this demo 2:
-
package
-
{
-
// FLASH
-
import flash.filters.BlurFilter;
-
import flash.utils.getDefinitionByName;
-
import flash.events.Event;
-
import flash.display.MovieClip;
-
-
public class Particles002 extends MovieClip
-
{
-
//
-
// Private properties
-
//
-
private var _particles:Array;
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Constructor
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
public function Particles002()
-
{
-
_particles = new Array();
-
filters = [new BlurFilter(6,6,1)];
-
-
addEventListener(Event.ENTER_FRAME, enterframeHandler);
-
}
-
-
private function enterframeHandler(event : Event) : void
-
{
-
var particle:Particle;
-
-
// loop through particles
-
for (var i:int = 0; i < _particles.length; i++)
-
{
-
particle = Particle(_particles[i]);
-
particle.update();
-
}
-
-
// create particles
-
particle = new Particle(getDefinitionByName("Circle") as Class, this, mouseX, mouseY);
-
-
particle.drag = .98;
-
particle.shrink = .95;
-
particle.gravity = -.3;
-
particle.fade = .02;
-
_particles.push(particle);
-
-
// add particles
-
while (_particles.length > 60)
-
{
-
// remove particle from array _particles
-
particle = _particles.shift();
-
-
// remove from displaylist
-
particle.destroy();
-
}
-
}
-
}
-
}
Particles v1
Last modified on 2009-02-16 19:45:55 GMT. 0 comments. Top.
Particles with random velocity on x-axis aswell on the y-axis.
The Particle Class
-
package
-
{
-
import flash.display.DisplayObjectContainer;
-
import flash.display.DisplayObject;
-
-
public class Particle
-
{
-
//
-
// public properties
-
//
-
public var clip:DisplayObject;
-
public var xVel:Number = 0;
-
public var yVel:Number = 0;
-
public var drag:Number = 1;
-
public var gravity:Number = 0;
-
public var shrink:Number = 1;
-
public var fade:Number = 0;
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Constructor
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
public function Particle(symbolClass:Class, target:DisplayObjectContainer, xpos:Number, ypos:Number)
-
{
-
// make particle clip
-
clip = new symbolClass();
-
-
// add particle to target
-
target.addChild(clip);
-
-
// set position
-
clip.x = xpos;
-
clip.y = ypos;
-
}
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Public methods
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
public function update():void
-
{
-
clip.x += xVel;
-
clip.y += yVel;
-
-
// apply drag
-
xVel *= drag;
-
yVel *= drag;
-
-
yVel += gravity;
-
-
clip.scaleX *= shrink;
-
clip.scaleY *= shrink;
-
-
clip.alpha -=fade;
-
}
-
-
// ———————————————————————————–
-
public function destroy():void
-
{
-
clip.parent.removeChild(clip);
-
}
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Private methods
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
// none
-
}
-
}
Class Demo 1:
-
package
-
{
-
// FLASH
-
import flash.filters.BlurFilter;
-
import flash.utils.getDefinitionByName;
-
import flash.events.Event;
-
import flash.display.MovieClip;
-
-
public class Particles001 extends MovieClip
-
{
-
//
-
// Private properties
-
//
-
private var _particles:Array;
-
-
//////////////////////////////////////////////////////////////////////////////////////
-
//
-
// Constructor
-
//
-
//////////////////////////////////////////////////////////////////////////////////////
-
-
// ———————————————————————————–
-
public function Particles001()
-
{
-
_particles = new Array();
-
filters = [new BlurFilter()];
-
addEventListener(Event.ENTER_FRAME, enterframeHandler);
-
}
-
-
private function enterframeHandler(event : Event) : void
-
{
-
var particle:Particle;
-
-
// loop through particles
-
for (var i:int = 0; i < _particles.length; i++)
-
{
-
particle = Particle(_particles[i]);
-
particle.update();
-
}
-
-
// create particles
-
particle = new Particle(getDefinitionByName("Circle") as Class, this, 250, 150);
-
particle.xVel = random(-5, 5);
-
particle.yVel = random(-5, 5);
-
particle.clip.alpha = random(.25, 1);
-
-
_particles.push(particle);
-
-
// add particles
-
while (_particles.length > 100)
-
{
-
// remove particle from array _particles
-
particle = _particles.shift();
-
-
// remove from displaylist
-
particle.destroy();
-
}
-
}
-
-
// ———————————————————————————–
-
public static function random( min:Number, max:Number = NaN ):Number
-
{
-
if (isNaN(max)) { max = min; min = 0; }
-
return Math.random() * (max - min) + min;
-
}
-
}
-
}
-
}
PerlinNoise
Last modified on 2008-12-29 10:27:14 GMT. 0 comments. Top.
Started (finally) experimenting with Perlin Noise….its a first but anyways…
Finally figured out that the number of Octaves and offset points are connected to eachother, which helps a lot!
Droppin’ Boxes
Last modified on 2008-12-29 00:10:14 GMT. 2 comments. Top.
A Little experiment with Box2DFlash and papervision3D
Christmas Ribbons
Last modified on 2008-12-29 00:10:55 GMT. 0 comments. Top.
Merry Christmas to you all!! (click on the image)
With many thanx to soulwire for the code!
View experiment. 














