PV3dでFlickrの検索結果を表示してみる
/**
* Copyright termat ( http://wonderfl.net/user/termat )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/5clF
*/
package {
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.Security;
import org.papervision3d.cameras.SpringCamera3D;
import org.papervision3d.core.math.Number3D;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
[SWF(width="480", height="480", backgroundColor="0x000000", frameRate="60")];
public class Flickr3 extends Sprite{
public var camera:SpringCamera3D;
public var rootNode:DisplayObject3D;
private var scene:Scene3D;
private var viewport:Viewport3D;
private var render:BasicRenderEngine;
private var cursor:Plane;
private var ang:Number = 0;
private const p:Number = 10.0;
private const r:Number = 28.0;
private const b:Number = 8.0/3.0;
public function Flickr3() {
Security.loadPolicyFile("http://farm1.static.flickr.com/crossdomain.xml");
Security.loadPolicyFile("http://farm2.static.flickr.com/crossdomain.xml");
Security.loadPolicyFile("http://farm3.static.flickr.com/crossdomain.xml");
Security.loadPolicyFile("http://farm4.static.flickr.com/crossdomain.xml");
this.graphics.beginFill(0x000000);
this.graphics.drawRect(0, 0, 5000, 5000);
this.graphics.endFill();
scene = new Scene3D();
viewport = new Viewport3D(0, 0, true, true);
addChild(viewport);
rootNode = new DisplayObject3D();
scene.addChild(rootNode);
camera = new SpringCamera3D();
camera.zoom = 300;
camera.mass = 20, camera.damping = 30, camera.stiffness = 1;
camera.lookOffset = new Number3D(0, 0, 0);
camera.positionOffset = new Number3D(0, 0, -50);
render = new BasicRenderEngine();
addEventListener(Event.ENTER_FRAME, update);
var wm:WireframeMaterial = new WireframeMaterial(0x000000, 0.1);
cursor = new Plane(wm, 1, 1);
cursor.x = 0, cursor.y = 0, cursor.z = 0;
rootNode.addChild(cursor);
SearchFlickr.obj = rootNode;
SearchFlickr.cam = camera;
SearchFlickr.base = cursor;
camera.target = cursor;
var ss:SearchFlickr = new SearchFlickr();
ss.setQuery("sun");
}
private function update(e:Event):void {
render.renderScene(scene, camera, viewport);
var p:DisplayObject3D = camera.target;
ang += (p.rotationZ - ang) * 0.1;
camera.roll(ang);
}
}
}
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.utils.escapeMultiByte;
import org.papervision3d.cameras.SpringCamera3D;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.events.InteractiveScene3DEvent;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;
class SearchFlickr {
private var media:Namespace = new Namespace("http://search.yahoo.com/mrss/");
private var xml:XML;
public static var numOfImage:int = 120;
public static var cam:SpringCamera3D;
public static var obj:DisplayObject3D;
public static var base:DisplayObject3D;
private const p:Number = 10.0;
private const r:Number = 28.0;
private const b:Number = 8.0/3.0;
private function getURL(query:String):String {
var url:String = "http://pipes.yahooapis.com/pipes/pipe.run?_id=1FZD9tbQ3RGdxrAHbbsjiw&_render=rss&n=" + String(numOfImage) + "&q=" + escapeMultiByte(query);
return url;
}
public function setQuery(query:String):void {
var url:String = getURL(query);
getImageFromFlickr(url);
}
private function getImageFromFlickr(url:String):void {
var req:URLRequest = new URLRequest(url);
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
req.method = "GET";
loader.addEventListener(Event.COMPLETE, loadCompleted);
try {
loader.load(req);
} catch (error:Error) {}
}
private function loadCompleted(e:Event):void {
var loader:URLLoader = URLLoader(e.target);
xml = new XML(loader.data);
default xml namespace = media;
for each(var element:Object in xml.channel.item){
var imgURL:String = element.media::group.media::content.@url;
var ll:Loader = new Loader();
ll.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadPhotoCompleted);
ll.load(new URLRequest(imgURL), new LoaderContext(true));
}
}
private function imageLoadPhotoCompleted(e:Event):void {
var loader:Loader = e.target.loader;
var bmp:BitmapData = new BitmapData(loader.width, loader.height);
bmp.draw(loader);
var bm:BitmapMaterial = new BitmapMaterial(bmp);
bm.oneSide = false;
bm.interactive = true;
var p:Plane = new Plane(bm, 8, 4, 2, 2);
p.x = Math.random() * 200-100;
p.y = Math.random() * 200-100;
p.z = Math.random() * 1000;
p.rotationZ = Math.random() * 360;
p.name = obj.numChildren.toString();
obj.addChild(p);
p.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS,onPress);
}
private function onPress(e:InteractiveScene3DEvent):void {
if(cam.target == e.displayObject3D){
cam.target = base;
}else {
cam.target = e.displayObject3D;
}
}
}