3d中的镜头
代码写出来了,只是理解得不够深。先计算镜头的显示坐标,再计算各点与镜头的距离,就是在镜头中的坐标。
package
{
import flash.display.GraphicsTrianglePath;
import flash.display.Sprite;
import flash.display.TriangleCulling;
import flash.events.Event;
import flash.geom.Vector3D;
/**
* @author:Gaara
* 2012-3-14
*
**/
[SWF(width="800", height="600" ,frameRate="30", backgroundColor="#FFFFFF")]
public class TestCamer extends Sprite
{
//定义路径类
private var triangPath:GraphicsTrianglePath;
private var sprite:Sprite = new Sprite;
private var indices:Vector.<int>;
private var uvtData:Vector.<Number>;
private var vertices:Vector.<Vector3D>;
private var _Cam:Object = { x:0, y:0, z:0, br:300, angle_x:0, angle_y:0, angle_z:0 } //设计一个镜头
public function TestCamer()
{
super();
sprite.x = this.stage.stageWidth / 2;
sprite.y = this.stage.stageHeight /2;
addChild(sprite);
indices = new Vector.<int>();
indices.push(0,1,2);
indices.push(0,2,3);
indices.push(4,5,6);
indices.push(4,6,7);
indices.push(8,9,10);
indices.push(8,10,11);
indices.push(12,13,14);
indices.push(12,14,15);
uvtData = new Vector.<Number>();
uvtData.push(0,0,1);
uvtData.push(1,0,1);
uvtData.push(1,1,1);
uvtData.push(0,1,1);
uvtData.push(0,0,1);
uvtData.push(1,0,1);
uvtData.push(1,1,1);
uvtData.push(0,1,1);
uvtData.push(0,0,1);
uvtData.push(1,0,1);
uvtData.push(1,1,1);
uvtData.push(0,1,1);
uvtData.push(0,0,1);
uvtData.push(1,0,1);
uvtData.push(1,1,1);
uvtData.push(0,1,1);
vertices = new Vector.<Vector3D>();
vertices.push(new Vector3D(-50,-50,-50));
vertices.push(new Vector3D(50,-50,-50));
vertices.push(new Vector3D(50,50,-50));
vertices.push(new Vector3D(-50,50,-50));
vertices.push(new Vector3D(50,-50,-50));
vertices.push(new Vector3D(50,-50,50));
vertices.push(new Vector3D(50,50,50));
vertices.push(new Vector3D(50,50,-50));
vertices.push(new Vector3D(50,-50,50));
vertices.push(new Vector3D(-50,-50,50));
vertices.push(new Vector3D(-50,50,50));
vertices.push(new Vector3D(50,50,50));
vertices.push(new Vector3D(-50,-50,50));
vertices.push(new Vector3D(-50,-50,-50));
vertices.push(new Vector3D(-50,50,-50));
vertices.push(new Vector3D(-50,50,50));
addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
protected function onEnterFrame(event:Event):void
{
_Cam.angle_y++;
_catch_cam(_Cam);
up_data() ;
}
private function _catch_cam(_Cam:Object):void {
var rx:Number = 0;
var ry:Number = 0;
var rz:Number = 100;
var view_angle_y:Number = _Cam.angle_y-180
var tmp_angle_y:Number = view_angle_y * Math.PI / 180;
_Cam.x = (Math.cos(tmp_angle_y) * rx + Math.sin(tmp_angle_y) * rz);
_Cam.z = (Math.sin(tmp_angle_y) * rx + Math.cos(tmp_angle_y) * rz);
}
public function up_data():void {
var radian:Number = _Cam.angle_y * Math.PI/180;
var newVertices:Vector.<Number> = new Vector.<Number>;
for (var i:int = 0; i < vertices.length; i++)
{
var vec3d:Vector3D = vertices[i]
var rx:Number = vec3d.x -_Cam.x
var ry:Number = vec3d.y -_Cam.y
var rz:Number = vec3d.z -_Cam.z
var newX:Number = Math.cos(radian) * rx - Math.sin(radian) * rz;
var newZ:Number = Math.sin(radian) * rx + Math.cos(radian) * rz ;
var scale:Number = _Cam.br/(_Cam.br+newZ);
uvtData[i*3+2] = _Cam.br/(_Cam.br+newZ);
newVertices.push(newX*scale);
newVertices.push(ry*scale);
}
sprite.graphics.clear();
sprite.graphics.beginBitmapFill(ResConfig.myLpBmp.bitmapData);
// sprite.graphics.lineStyle(1,0xFF0000);
sprite.graphics.drawTriangles(newVertices,indices,uvtData,TriangleCulling.NEGATIVE);
sprite.graphics.endFill();
}
}
}

浙公网安备 33010602011771号