forked from: 3dtext.in API Text Generator Demo
3D日本語文字ジェネレーター
3DText.in APIを開発中
デモアプリを書いてみる。
// forked from TX_298's 3dtext.in API Text Generator Demo
package {
/**
* 3D日本語文字ジェネレーター
* 3DText.in APIを開発中
* デモアプリを書いてみる。
*/
import flash.net.*;
import flash.text.*;
import flash.system.*;
import flash.display.*;
import flash.geom.*;
import flash.events.*;
import org.papervision3d.view.*;
import org.papervision3d.objects.*;
import org.papervision3d.materials.*;
import org.papervision3d.events.FileLoadEvent;
import caurina.transitions.Tweener;
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="30")]
public class Main extends BasicView
{
private var mouseDown: Boolean = false;
private var pt:Point=new Point(0,0);
private var rootNode : DisplayObject3D;
private var text:_3DText;
private var pos:int=0;
private var bitmat:BitmapFileMaterial;
private var in_arr:Array=new Array();
private var sel_arr:Array=new Array();
public function Main()
{
super(465, 465, true,false, "Free");
Security.loadPolicyFile("");
rootNode = scene.addChild( new DisplayObject3D( "rootNode" ) );
scene.addChild(rootNode); camera.target = rootNode;
text = new _3DText(new WireframeMaterial(0x00ff00), "3D文字", 1.5, 0, "ipagp");
text.addEventListener(FileLoadEvent.LOAD_COMPLETE, handleFileLoaded);
rootNode.addChild(text);
}
private function handleFileLoaded(e:FileLoadEvent):void
{
create_ctl();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
viewport.stage.addEventListener(MouseEvent.MOUSE_DOWN, function():void{mouseDown=true;pt.x=mouseX;pt.y=mouseY;});
viewport.stage.addEventListener(MouseEvent.MOUSE_UP, function():void{mouseDown=false;});
startRendering();
bitmat = new BitmapFileMaterial("http://www.3dtext.in/image/0.jpg");
bitmat.addEventListener(FileLoadEvent.LOAD_COMPLETE, handleBitmapFileLoaded);
}
private function handleBitmapFileLoaded(e:FileLoadEvent):void {text.set_letters_material(bitmat);}
private function onEnterFrame(event:Event):void
{
if(mouseDown){
rootNode.rotationY=-(viewport.stage.mouseX-pt.x)/4;
rootNode.rotationX=(viewport.stage.mouseY-pt.y)/4;
}else{
rootNode.rotationY*=0.7;rootNode.rotationX*=0.7;
}
camera.x = -viewport.stage.mouseX + viewport.width/2;
camera.y = -viewport.stage.mouseY + viewport.height / 2;
if(bitmat.loaded)scrollBitmap(bitmat.bitmap, -1, -1);
}
private function create_ctl():void {
var carr:Array = [ { text:"Text", val:"3D文字", width:90,max:7,rest:null }, { text:"Depth", val:2, width:35,max:3,rest:"0-9." }, { text:"Bevel", val:0, width:35,max:3,rest:"0-9." } ];
var barr:Array = [{text:"Generate",val:Btn_Press,width:70},{text:" + ",val:Zoom_in,width:17},{text:" - ",val:Zoom_out,width:17}];
var shp:Shape = new Shape();
shp.graphics.beginFill(0xeeeeee);
shp.graphics.drawRoundRect(0,0,stage.stageWidth,30,9);
shp.graphics.endFill();
addChild(shp);
var left:int = 0;
for (var k:int = 0; k < carr.length; k++ ) {
var label:TextField = new TextField();
label.y = 5; label.x = left + 5;
label.selectable = false;
label.autoSize = TextFieldAutoSize.LEFT;
label.htmlText = "<b>"+carr[k].text+"</b>";
addChild(label);
var inp:TextField= new TextField();
inp.y = 5;
inp.x = label.textWidth+label.x+8;
inp.width =carr[k].width;
inp.text = carr[k].val;
inp.border = true;
inp.height = 20;
left = inp.x + carr[k].width;
inp.type = TextFieldType.INPUT;
inp.selectable = true;
inp.maxChars = carr[k].max;
inp.restrict = carr[k].rest;
addChild(inp);
in_arr.push(inp);
}
left += 15;
for (var l:int = 0; l < barr.length; l++ ) {
var btn:Sprite = new Sprite();
btn.buttonMode = true;
btn.y = 5;btn.x = left;
btn.graphics.beginFill(0xcccccc);
btn.graphics.lineStyle(1, 0x888888);
btn.graphics.drawRect(0,0,barr[l].width,20);
btn.graphics.endFill();
btn.mouseChildren = false;
var label2:TextField = new TextField();
label2.width = barr[l].width;
label2.htmlText = "<p align='center'>"+barr[l].text+"</p>";
btn.addChild(label2);
left = btn.x + barr[l].width+7;left+=(l==0)?10:0
addChild(btn);
btn.addEventListener(MouseEvent.CLICK,barr[l].val);
}
var pl:Sprite = new Sprite();pl.x = 5;pl.y = stage.stageHeight - 27;
pl.buttonMode = true;
addChild(pl);
var ld:Loader = new Loader();
ld.load(new URLRequest("http://www.3dtext.in/image/tool.png"));
pl.addChild(ld);
sel_arr.push(new Sprite());sel_arr.push(new Sprite());
pl.addChild(sel_arr[0]); pl.addChild(sel_arr[1]);
pl.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
pl.addEventListener(MouseEvent.ROLL_OUT, function():void{sel_arr[0].graphics.clear();});
pl.addEventListener(MouseEvent.CLICK, mouseClickHandler);
select_image(sel_arr[1],pos,true);
}
private function mouseClickHandler(e:MouseEvent):void {
pos = e.localX / 24;
select_image(sel_arr[1], pos, true);
bitmat = new BitmapFileMaterial("http://www.3dtext.in/image/"+pos+".jpg");
bitmat.addEventListener(FileLoadEvent.LOAD_COMPLETE, handleBitmapFileLoaded);
}
private function select_image(s:Sprite,a:int,b:Boolean):void {
s.graphics.clear();
if(b){s.graphics.lineStyle(1,0xff0000);}else{s.graphics.beginFill(0xcccccc,0.5);}
s.graphics.drawRect(a * 24-1, 0, 24, 24);
s.graphics.endFill();
}
private function mouseMoveHandler(e:MouseEvent):void {
var x:int = e.localX / 24;
if(x!=pos)select_image(sel_arr[0],x,false);
}
private function Btn_Press(e:MouseEvent):void {
var d:Number = Number(in_arr[1].text); if (d > 5) { d = 5; in_arr[1].text = d;}
var b:Number = Number(in_arr[2].text); if (b > 5){b = 5; in_arr[2].text = b;}
text.removeLetters();
text = new _3DText(bitmat, in_arr[0].text,d,b, "ipagp");
rootNode.addChild(text);
}
private function Zoom_in(e:MouseEvent):void {
Tweener.addTween(rootNode, { scale:rootNode.scale * 1.1 ,time:.2,transition:"easeOutQuint"} );
}
private function Zoom_out(e:MouseEvent):void {
Tweener.addTween(rootNode, { scale:rootNode.scale * 0.9 ,time:.2,transition:"easeOutQuint"} );
}
private function scrollBitmap(bm:BitmapData,sx:int,sy:int):void{
var divx:int = Math.abs(sx), divy:int = Math.abs(sy);
var rect:Array = [new Rectangle(bm.width - divx, 0, divx, bm.height), new Rectangle(0, 0, divx, bm.height),
new Rectangle(0,0,bm.width,divy),new Rectangle(0,bm.height-divy,bm.width,divy)];
var p:Array=[new Point(0,0),new Point(bm.width-divx,0),new Point(0,0),new Point(0,bm.height-divy)];
var tmp:BitmapData = new BitmapData(bm.width,bm.height,bm.transparent,0x000000);
(sx>0)?tmp.copyPixels(bm,rect[0],p[0]) : tmp.copyPixels(bm,rect[1], p[1]);
bm.scroll(sx,0);
(sx>0)?bm.copyPixels(tmp,rect[1],p[0]) : bm.copyPixels(tmp,rect[0], p[1]);
(sy>0)?tmp.copyPixels(bm,rect[3],p[2]) : tmp.copyPixels(bm,rect[2], p[3]);
bm.scroll(0,sy);
(sy>0)?bm.copyPixels(tmp,rect[2],p[2]) : bm.copyPixels(tmp,rect[3], p[3]);
tmp.dispose();
}
}
}
import org.papervision3d.core.geom.*
import org.papervision3d.core.geom.renderables.*;
import org.papervision3d.core.math.*;
import org.papervision3d.core.proto.MaterialObject3D
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.materials.*;
import org.papervision3d.events.FileLoadEvent;
import flash.events.*;
import flash.net.*;
import flash.utils.*;
class _3DText extends TriangleMesh3D {
public var loaded :Boolean;
public var letters:Array;
private var spacer:int = 30;
private var _Mesh:DisplayObject3D;
private var mat:MaterialObject3D;
private var _filename:String;
public function _3DText(material:MaterialObject3D,word:String,depth:Number,bevel:Number,font:String=null)
{
super( material, new Array(), new Array(), null );
_Mesh = this; mat = material; letters = new Array();
var val:URLVariables = new URLVariables();
val.text=word; //作成する立体文字
val.depth=depth; //文字の厚さ
val.bevel=bevel; //文字べベル
val.font = font; //フォント名
var req:URLRequest = new URLRequest("http://www.3dtext.in/api/for_wonderfl_demo_key");
req.method = URLRequestMethod.POST;req.data=val;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.load(req);
}
public function set_letters_material(m:MaterialObject3D):void {
for(var j:int=0;j<letters.length;j++)letters[j].material=m;
}
public function removeLetters():void {
for each( var l:DisplayObject3D in letters){
removeChild(l);
}
letters.length = 0;
parent.removeChild( this );
}
private function completeHandler(event:Event):void
{
var data:ByteArray = event.target.data;
data.endian = Endian.LITTLE_ENDIAN
var q:int;var sum:int=0
try {
while (q = data.readUnsignedShort()) {
var t:TriangleMesh3D = new TriangleMesh3D(mat, new Array(), new Array());
var arr: Array = new Array();
if(q>1)for (var a:int = 0; a < q; a++) {
var v1:Vertex3D = new Vertex3D(data.readFloat(), data.readFloat(),data.readFloat())
var v2:Vertex3D = new Vertex3D(data.readFloat(), data.readFloat(),data.readFloat())
var v3:Vertex3D = new Vertex3D(data.readFloat(), data.readFloat(),data.readFloat())
t.geometry.vertices.push(v1);t.geometry.vertices.push(v2);t.geometry.vertices.push(v3);
t.geometry.faces.push(new Triangle3D(t, [v1,v2,v3],mat, [new NumberUV(data.readFloat(),data.readFloat()),new NumberUV(data.readFloat(),data.readFloat()),new NumberUV(data.readFloat(),data.readFloat())]));
}
for (var i:int = 0; i < 9; i++) {arr.push(data.readFloat());}
t.extra = { stat:arr }; sum += arr[0];
letters.push(t);
}
} catch (err:Error) {
}finally {
if (letters.length > 1) { sum +=(letters.length - 1)* spacer; }
letters[0].x = -sum / 2+letters[0].extra.stat[0]/2;
for (var i:int = 0; i < letters.length; i++ ) {
_Mesh.addChild(letters[i]);
if (i > 0) { letters[i].x=letters[i-1].x+spacer+(letters[i-1].extra.stat[0]/2)+(letters[i].extra.stat[0]/2); }
}
this.geometry.ready = true;
var fileEvent:FileLoadEvent = new FileLoadEvent( FileLoadEvent.LOAD_COMPLETE, _filename );
this.dispatchEvent( fileEvent );
this.loaded = true;
}
}
}