forked from: QuickBox2D and Papervision3D
/**
* Copyright Akiyah ( http://wonderfl.net/user/Akiyah )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/hgQt
*/
package{
import net.hires.debug.Stats;
import flash.display.MovieClip;
import flash.events.*;
import flash.geom.*;
import flash.filters.BlurFilter;
import flash.system.Security;
import org.papervision3d.core.geom.TriangleMesh3D;
import org.papervision3d.core.proto.MaterialObject3D;
import org.papervision3d.materials.*;
import org.papervision3d.materials.special.CompositeMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.*;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.view.BasicView;
import com.actionsnippet.qbox.*;
public class Sample extends MovieClip {
private const DEPTH:Number = 1;
private const camera_length:Number = 20;
private var sim:QuickBox2D;
private var view:BasicView;
private var mapping:Array = [];
private var balls:Array = [];
private var sx:Number;
private var sy:Number;
Security.loadPolicyFile("http://assets.wonderfl.net/crossdomain.xml");
private const path:String = "http://assets.wonderfl.net/images/related_images";
private const filename:String = path+"/3/35/3590/359061d357d721a573fae94cc4d8b2819a72122c";
private const filename1:String = path + "/e/ed/ed1c/ed1cadf0fdd830922653b0fa2a5667315bb85ef5";
private const filename2:String = path + "/2/29/2944/2944a982d2a8d7928f0e8add944f8d28158090e3";
//private const filename3:String = path + "/b/b0/b0bd/b0bd1ed4e975f909d61a84742f77faab50201f7b";
private const filename3:String = path + "/3/39/3933/393330f30c725032b7ddee52247d9d46c7041bfa"
private const sideMaterial:MaterialObject3D = new BitmapFileMaterial(filename3);
public function Sample() {
addChild(new Stats());
//new MovieClip();
sim = new QuickBox2D(this /*new MovieClip()*/);
sim.start();
view = new BasicView();
addChild(view);
sx = stage.width;
sy = stage.height;
var material:CompositeMaterial = new CompositeMaterial();
//material.addMaterial(new ColorMaterial(0x808080));
material.addMaterial(new BitmapFileMaterial(filename1));
material.addMaterial(new WireframeMaterial(0x000000));
var i:Number;
for (i = 0; i < 3; i++) {
balls.push(addPoly({x:0, y:-30, points:[0,0, 2,0, 2,1, 1,2, 0,2.5]}, material));
//balls.push(addPoly({x:0, y:-30, points:[0,0, 1,0, 2,0.5, 1,1, 0.5,1.5, 0,1]}, new ColorMaterial()));
//balls.push(addPoly({x:0, y:-30, points:[0,0, 1,0, 2,0.5, 1,1, 0.5,1.5, 0,1]}, new BitmapFileMaterial(filename1)));
//balls.push(addPoly({x:0, y:-30, points:[0,0, 1,0, 2,0.5, 1,1, 0,1]}, new BitmapFileMaterial(filename1)));
}
for (i = 0; i < 2; i++) {
addBox({x:-5, y:-10 + i*10, width:10, height:2, density:0, angle: 45/180*Math.PI}, new BitmapFileMaterial(filename1));
addBox({x: 5, y: -5 + i*10, width:10, height:2, density:0, angle:-45/180*Math.PI}, new BitmapFileMaterial(filename1));
}
addEventListener(Event.ENTER_FRAME, update);
}
public function update(e:Event):void{
for each(var ball:QuickObject in balls) {
if (ball.y > 15) {
ball.x = 0;
ball.y = -30;
}
}
for each(var v:Array in mapping) {
v[1].x = -v[0].x;
v[1].y = -v[0].y;
v[1].rotationZ = v[0].angle * 180 / Math.PI;
}
var c:Number = (mouseX / sx - 0.5) * (Math.PI);
var d:Number = (mouseY / sy) * (Math.PI /2);
view.camera.x = Math.sin(c) * Math.cos(d) * camera_length;
view.camera.z = Math.cos(c) * Math.cos(d) * camera_length;
view.camera.y = Math.sin(d) * camera_length;
view.singleRender();
}
private function create(obj:QuickObject, obj3d:TriangleMesh3D):QuickObject {
obj3d.rotationX = 90;
view.scene.addChild(obj3d);
mapping.push([obj, obj3d]);
return obj;
}
private function addBox(param:Object, material:MaterialObject3D):QuickObject {
var obj:QuickObject = sim.addBox(param);
var mlist:MaterialsList = new MaterialsList({all:sideMaterial, top:material, bottom:material});
var obj3d:TriangleMesh3D = new Cube(mlist, obj.params.width, obj.params.height, DEPTH);
return create(obj, obj3d);
}
private function addPoly(param:Object, material:MaterialObject3D):QuickObject {
var ps:Array = [];
for (var i:Number = 0; i < param.points.length; i += 2) {
ps.push(new Point(-param.points[i+1], param.points[i]));
}
var obj3d:TriangleMesh3D = new Polygon3D(ps, sideMaterial, DEPTH, material,-2,0,0,2);
param.points = param.points.concat([0,0]);
var obj:QuickObject = sim.addPoly(param);
//return obj;
return create(obj, obj3d);
}
}
}
import flash.geom.*;
import org.papervision3d.Papervision3D;
import org.papervision3d.core.geom.*;
import org.papervision3d.core.geom.renderables.*;
import org.papervision3d.core.math.*;
import org.papervision3d.core.proto.*;
class Polygon3D extends TriangleMesh3D
{
private var minx:Number;
private var miny:Number;
private var maxx:Number;
private var maxy:Number;
public function Polygon3D( points:Array, sideMaterial:MaterialObject3D, height:Number, surfaceMaterial:MaterialObject3D,
minx:Number, miny:Number, maxx:Number, maxy:Number)
{
super( sideMaterial, new Array(), new Array(), null );
this.minx = minx;
this.miny = miny;
this.maxx = maxx;
this.maxy = maxy;
var n:int = points.length;
var i:Number;
var vs0:Array = [];
var vs1:Array = [];
for each (var p:Point in points) {
var v:Vertex3D;
v = new Vertex3D(p.x, height * 0.5, p.y);
this.geometry.vertices.push(v);
vs0.push(v);
v = new Vertex3D(p.x, -height * 0.5, p.y);
this.geometry.vertices.push(v);
vs1.push(v);
}
var uv00:NumberUV = new NumberUV(0, 0);
var uv01:NumberUV = new NumberUV(0, 1);
var uv10:NumberUV = new NumberUV(1, 0);
var uv11:NumberUV = new NumberUV(1, 1);
for (i = 0; i < n; i++) {
var i2:Number = (i + 1) % n;
this.geometry.faces.push( new Triangle3D(this, [vs0[i], vs1[i2], vs0[i2]], sideMaterial, [uv00, uv11, uv10]) );
this.geometry.faces.push( new Triangle3D(this, [vs0[i], vs1[i], vs1[i2]], sideMaterial, [uv00, uv01, uv11]) );
}
var ps:Array;
for (i = 0; i < (n - 2)/2; i++) {
ps = [vs0[i], vs0[n-2-i], vs0[n-1-i]];
this.geometry.faces.push( new Triangle3D(this, ps, surfaceMaterial, to_uvs(ps)));
ps = [vs1[i], vs1[n-1-i], vs1[n-2-i]];
this.geometry.faces.push( new Triangle3D(this, ps, surfaceMaterial, rev(to_uvs(ps))));
}
for (i = 0; i < (n - 3)/2; i++) {
ps = [vs0[i], vs0[i+1], vs0[n-2-i]];
this.geometry.faces.push( new Triangle3D(this, ps, surfaceMaterial, to_uvs(ps)));
ps = [vs1[i], vs1[n-2-i], vs1[i+1]];
this.geometry.faces.push( new Triangle3D(this, ps, surfaceMaterial, rev(to_uvs(ps))));
}
this.geometry.ready = true;
if(Papervision3D.useRIGHTHANDED)
this.geometry.flipFaces();
}
private function to_uvs(ps:Array):Array
{
var uvs:Array = [];
for each (var p:Vertex3D in ps) {
uvs.push(new NumberUV((p.x - minx)/(maxx - minx), (p.z - miny)/(maxy - miny)));
}
return uvs;
}
private function rev(uvs:Array):Array
{
var ruvs:Array = [];
for each (var uv:NumberUV in uvs) {
ruvs.push(new NumberUV(uv.u, 1 - uv.v));
}
return ruvs;
}
}