📄 mesh.as
字号:
package away3d.core.base{ import away3d.core.*; import away3d.core.draw.*; import away3d.core.math.*; import away3d.core.render.*; import away3d.core.utils.*; import away3d.events.*; import away3d.materials.*; import away3d.primitives.*; import flash.utils.Dictionary; /** * 3d object containing face and segment elements */ public class Mesh extends BaseMesh implements IPrimitiveProvider { use namespace arcane; /** @private */ arcane function getFacesByVertex(vertex:Vertex):Array { if (_vertfacesDirty) findVertFaces(); return _vertfaces[vertex]; } /** @private */ arcane function getVertexNormal(vertex:Vertex):Number3D { if (_vertfacesDirty) findVertFaces(); if (_vertnormalsDirty) findVertNormals(); return _vertnormals[vertex]; } /** @private */ arcane function getSceneVertexNormal(vertex:Vertex):Number3D { if (_vertfacesDirty) findVertFaces(); if (_vertnormalsDirty) findVertNormals(); if (_scenevertnormalsDirty) findSceneVertNormals(); return _scenevertnormals[vertex]; } /** @private */ arcane function neighbour01(face:Face):Face { if (_neighboursDirty) findNeighbours(); return _neighbour01[face]; } /** @private */ arcane function neighbour12(face:Face):Face { if (_neighboursDirty) findNeighbours(); return _neighbour12[face]; } /** @private */ arcane function neighbour20(face:Face):Face { if (_neighboursDirty) findNeighbours(); return _neighbour20[face]; } /** @private */ arcane function recalcNeighbours():void { if (!_neighboursDirty) { _neighboursDirty = true; findNeighbours(); /* var sn01:Dictionary = _neighbour01; var sn12:Dictionary = _neighbour12; var sn20:Dictionary = _neighbour20; for each (var f:Face in faces) { if (sn01[f] != _neighbour01[f]) throw new Error("Got you!"); if (sn12[f] != _neighbour12[f]) throw new Error("Got you!"); if (sn20[f] != _neighbour20[f]) throw new Error("Got you!"); } */ } } /** @private */ arcane function createDrawTriangle(face:Face, material:ITriangleMaterial, projection:Projection, v0:ScreenVertex, v1:ScreenVertex, v2:ScreenVertex, uv0:UV, uv1:UV, uv2:UV):DrawTriangle { if (_dtStore.length) { _dtActive.push(_tri = _dtStore.pop()); _tri.texturemapping = null; _tri.create = createDrawTriangle; } else { _dtActive.push(_tri = new DrawTriangle()); _tri.source = this; _tri.create = createDrawTriangle; } _tri.face = face; _tri.material = material; _tri.projection = projection; _tri.v0 = v0; _tri.v1 = v1; _tri.v2 = v2; _tri.uv0 = uv0; _tri.uv1 = uv1; _tri.uv2 = uv2; _tri.calc(); return _tri; } private var _faces:Array = []; private var _material:ITriangleMaterial; private var _neighboursDirty:Boolean = true; private var _neighbour01:Dictionary; private var _neighbour12:Dictionary; private var _neighbour20:Dictionary; private var _vertfacesDirty:Boolean = true; private var _vertfaces:Dictionary; private var _vertnormalsDirty:Boolean = true; private var _vertnormals:Dictionary; private var _scenevertnormalsDirty:Boolean = true; private var _scenevertnormals:Dictionary; private var _fNormal:Number3D; private var _fAngle:Number; private var _fVectors:Array; private var _n01:Face; private var _n12:Face; private var _n20:Face; private var _debugboundingbox:WireCube; private var _tri:DrawTriangle; private var _backmat:ITriangleMaterial; private var _backface:Boolean; private var _uvmaterial:Boolean; private var _vt:ScreenVertex; private var _dtStore:Array = new Array(); private var _dtActive:Array = new Array(); private function onMaterialResize(event:MaterialEvent):void { for each (var face:Face in _faces) if (face._material == null) face._dt.texturemapping = null; } private function findVertFaces():void { if (!_vertfacesDirty) return; _vertfaces = new Dictionary(); for each (var face:Face in faces) { var v0:Vertex = face.v0; if (_vertfaces[v0] == null) _vertfaces[v0] = [face]; else _vertfaces[v0].push(face); var v1:Vertex = face.v1; if (_vertfaces[v1] == null) _vertfaces[v1] = [face]; else _vertfaces[v1].push(face); var v2:Vertex = face.v2; if (_vertfaces[v2] == null) _vertfaces[v2] = [face]; else _vertfaces[v2].push(face); } _vertfacesDirty = false; } private function findVertNormals():void { if (!_vertnormalsDirty) return; _vertnormals = new Dictionary(); for each (var v:Vertex in vertices) { var vF:Array = _vertfaces[v]; var nX:Number = 0; var nY:Number = 0; var nZ:Number = 0; for each (var f:Face in vF) { _fNormal = f.normal; _fVectors = new Array(); for each (var fV:Vertex in f.vertices) if (fV != v) _fVectors.push(new Number3D(fV.x - v.x, fV.y - v.y, fV.z - v.z, true)); _fAngle = Math.acos((_fVectors[0] as Number3D).dot(_fVectors[1] as Number3D)); nX += _fNormal.x*_fAngle; nY += _fNormal.y*_fAngle; nZ += _fNormal.z*_fAngle; } var vertNormal:Number3D = new Number3D(nX, nY, nZ); vertNormal.normalize(); _vertnormals[v] = vertNormal; } _vertnormalsDirty = false; } private function findSceneVertNormals():void { if (!_scenevertnormalsDirty) return; _scenevertnormals = new Dictionary(); //TODO: refresh scene normals /* for each (var v:Vertex in vertices) { } */ _scenevertnormalsDirty = false; } private function findNeighbours():void { if (!_neighboursDirty) return; _neighbour01 = new Dictionary(); _neighbour12 = new Dictionary(); _neighbour20 = new Dictionary(); for each (var face:Face in _faces) { var skip:Boolean = true; for each (var another:Face in _faces) { if (skip) { if (face == another) skip = false; continue; } if ((face._v0 == another._v2) && (face._v1 == another._v1)) { _neighbour01[face] = another; _neighbour12[another] = face; } if ((face._v0 == another._v0) && (face._v1 == another._v2)) { _neighbour01[face] = another; _neighbour20[another] = face; } if ((face._v0 == another._v1) && (face._v1 == another._v0)) { _neighbour01[face] = another; _neighbour01[another] = face; } if ((face._v1 == another._v2) && (face._v2 == another._v1)) { _neighbour12[face] = another; _neighbour12[another] = face; } if ((face._v1 == another._v0) && (face._v2 == another._v2)) { _neighbour12[face] = another; _neighbour20[another] = face; } if ((face._v1 == another._v1) && (face._v2 == another._v0)) { _neighbour12[face] = another; _neighbour01[another] = face; } if ((face._v2 == another._v2) && (face._v0 == another._v1)) { _neighbour20[face] = another; _neighbour12[another] = face; } if ((face._v2 == another._v0) && (face._v0 == another._v2)) { _neighbour20[face] = another; _neighbour20[another] = face; } if ((face._v2 == another._v1) && (face._v0 == another._v0)) { _neighbour20[face] = another; _neighbour01[another] = face; } } } _neighboursDirty = false; } private function rememberFaceNeighbours(face:Face):void { if (_neighboursDirty) return; for each (var another:Face in _faces) { if (face == another) continue; if ((face._v0 == another._v2) && (face._v1 == another._v1)) { _neighbour01[face] = another; _neighbour12[another] = face; } if ((face._v0 == another._v0) && (face._v1 == another._v2)) { _neighbour01[face] = another; _neighbour20[another] = face; } if ((face._v0 == another._v1) && (face._v1 == another._v0)) { _neighbour01[face] = another; _neighbour01[another] = face; } if ((face._v1 == another._v2) && (face._v2 == another._v1)) { _neighbour12[face] = another; _neighbour12[another] = face; } if ((face._v1 == another._v0) && (face._v2 == another._v2)) { _neighbour12[face] = another; _neighbour20[another] = face; } if ((face._v1 == another._v1) && (face._v2 == another._v0)) { _neighbour12[face] = another; _neighbour01[another] = face; } if ((face._v2 == another._v2) && (face._v0 == another._v1)) { _neighbour20[face] = another; _neighbour12[another] = face; } if ((face._v2 == another._v0) && (face._v0 == another._v2)) { _neighbour20[face] = another; _neighbour20[another] = face; } if ((face._v2 == another._v1) && (face._v0 == another._v0)) { _neighbour20[face] = another; _neighbour01[another] = face; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -