开发者

AS3 fp10 drawtriangles and uvmapping, when no z-val

开发者 https://www.devze.com 2022-12-10 19:09 出处:网络
I\'m messing around a bit with the new fp10 drawing api and drawtriangles. i\'ve created a little something i think is kinda of cool, though i can\'t seem to get the uv-mapping working as i want. sinc

I'm messing around a bit with the new fp10 drawing api and drawtriangles. i've created a little something i think is kinda of cool, though i can't seem to get the uv-mapping working as i want. since i'm just moving points in z/y direction i don't really have and z-value to calculate the uv with. though the bitmap texture gets distorted. i've tried to calculate a "fake" z-value with x and y, but since my maths skills are terrible i can't really get it to work.

i've thrown together a little sample here below (code obviously needs cleaning up, but i'm gonna worry about that when i've got it working as i want to).

in this example z-val will always be 1 and no perspective fix开发者_Python百科ing will be done. so i really need your help to figure out how i can take my calculated x and y point and turn them into a "fake" z-point and use that to avoid distortion in the map/texture.

thanks!

  package {

  import flash.display.BitmapData;
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.geom.Point;

  public class Debug extends Sprite{

   private var bitmapData:BitmapData;

   private var plane:Plane;
   private var container:Sprite;

   public function Debug(){
    bitmapData = new slask(0, 0);

    container = new Sprite();
    container.x = 200;
    container.y = 200;
    this.addChild(container);

    plane = new Plane(bitmapData);
    container.addChild(plane);

    this.addEventListener(Event.ENTER_FRAME, render);
   }

   function render(e:Event):void{
    var mp:Point = new Point(container.mouseX, container.mouseY);
    plane.render(mp);
   }
  }

 }

 import flash.display.Sprite;
 import flash.geom.Point;
 import flash.geom.Vector3D;
 import flash.display.BitmapData;

 class Plane extends Sprite {

  private var indices:Vector.<int> = new Vector.<int>();  
  private var vertices:Vector.<Number>;
  private var uvtData:Vector.<Number>;

  private var focalLength:Number = 200;
  private var bitmapData:BitmapData;
  private var verts:Array;

  public function Plane(bmp:BitmapData){   
   bitmapData = bmp;

   verts = new Array();
   var sizes:Array = [
         {x: 0, y: 0, z:0},
         {x: 200, y: 0, z:0},
         {x: 0, y: 200, z:0},         
         {x: 200, y: 200, z:0}
        ];

   for(var i:int = 0; i < 4; i++){
    verts[i] = new Vert(sizes[i].x, sizes[i].y, sizes[i].z);
   }

   indices.push(0,1,2, 1,2,3);
  }

  public function render( mousePoint:Point ):void {
   for(var i:int = 0; i < 4; i++){
    verts[i].update(mousePoint);
   }
   drawMe();
  }

  private function drawMe():void {
   vertices = Vector.<Number>([]);
   uvtData = Vector.<Number>([]);

   var t1:Number = focalLength/(focalLength + verts[0].z);
   var t2:Number = focalLength/(focalLength + verts[1].z);
   var t3:Number = focalLength/(focalLength + verts[2].z);
   var t4:Number = focalLength/(focalLength + verts[3].z);

   vertices.push( verts[0].x*t1, verts[0].y*t1,
       verts[1].x*t2, verts[1].y*t2, 
       verts[2].x*t3, verts[2].y*t3, 
       verts[3].x*t4, verts[3].y*t4);

   uvtData.push(0,0,t1, 1,0,t2, 0,1,t3, 1,1,t4);

   this.graphics.clear();
   this.graphics.lineStyle(1, 0xFF0000);
   this.graphics.beginBitmapFill(bitmapData, null, false, true);
   this.graphics.drawTriangles(vertices, indices, uvtData);
  }

 }

 class Vert extends Vector3D {

  private var vx:Number = 0;
  private var vy:Number = 0; 

  private var orgX:Number;
  private var orgY:Number;  

  public function Vert(xx:int, yy:int, zz:int){
   x = orgX = xx;
   y = orgY = yy;
   z = zz;
  }

  public function update( mousePoint:Point ):void {
   var vertPoint:Point = new Point(x, y);
   var distance:Number = Point.distance(mousePoint, vertPoint);

   var dx:Number;
   var dy:Number;

   if (distance < 225) {
    var diff:Number = -distance * (225 - distance) / 225;
    var radian:Number = Math.atan2(mousePoint.y - orgY, mousePoint.x - orgX);
    var diffPoint:Point = Point.polar(diff*2, radian);
    dx = orgX + diffPoint.x;
    dy= orgY + diffPoint.y;
   } else {
    dx = orgX;
    dy = orgY;
   }

   vx += (dx - x) * 0.02;
   vy += (dy - y) * 0.02;
   vx *= 0.9;
   vy *= 0.9;

   x += vx;
   y += vy;
  }

 }


Flash has a nifty utility function Utils3D.projectVectors(). You pass it all your data and it calculates the missing values for the uvt coordinates. Here's that the the docs say:

A vector of Numbers, where every three Numbers represent the u, v, and t elements of the uvt data. The u and v are the texture coordinate for each projected point. The t value is the projection depth value, the distance from the eye to the Vector3D object in the view or eye space. You should pre-allocate the Vector and specify the u and v values. The projectVectors method fills the t value for each projected point.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Utils3D.html#projectVectors()

0

精彩评论

暂无评论...
验证码 换一张
取 消