mirror of https://gitee.com/cf-fz/WebCAD.git
!1727 新增:方块坐标轴
parent
5401590b3f
commit
5c9b4d9163
@ -0,0 +1,87 @@
|
||||
import { BoxBufferGeometry, CylinderBufferGeometry, Group, Line, LineBasicMaterial, Mesh } from "three";
|
||||
import { ColorMaterial } from "../../Common/ColorPalette";
|
||||
|
||||
export default class AxesCube extends Group
|
||||
{
|
||||
private _Line: Mesh;
|
||||
private _Cone: Mesh;
|
||||
private _Cube: Mesh;
|
||||
|
||||
constructor(
|
||||
private _ConeHeight: number = 0.4,//圆锥高
|
||||
private _ConeRadius: number = 0.1,//圆锥半径
|
||||
private _AxesLength: number = 2, //轴线长度
|
||||
)
|
||||
{
|
||||
super();
|
||||
//直线
|
||||
this._Line = new Mesh(AxesCube.LineGeom, ColorMaterial.GetLineMaterial(1));
|
||||
//方块
|
||||
this._Cube = new Mesh(AxesCube.CubeGeo, ColorMaterial.GetBasicMaterial(1));
|
||||
this.add(this._Line);
|
||||
this.add(this._Cube);
|
||||
}
|
||||
|
||||
|
||||
//坐标顶点是个盒子在这里初始化它
|
||||
private _CubeNeg: Mesh;
|
||||
InitNegCube()
|
||||
{
|
||||
if (this._CubeNeg) return;
|
||||
|
||||
this._CubeNeg = new Mesh(AxesCube.CubeGeo, ColorMaterial.GetBasicMaterial(1));
|
||||
this._CubeNeg.rotateX(Math.PI);
|
||||
this._CubeNeg.matrixWorldNeedsUpdate = true;
|
||||
this._CubeNeg.updateMatrix();
|
||||
|
||||
this.add(this._CubeNeg);
|
||||
}
|
||||
|
||||
|
||||
private static _LineGeom: CylinderBufferGeometry;
|
||||
static get LineGeom()
|
||||
{
|
||||
if (AxesCube._LineGeom) return AxesCube._LineGeom;
|
||||
|
||||
AxesCube._LineGeom = new CylinderBufferGeometry(0.03, 0.03, 2, 3);
|
||||
AxesCube._LineGeom.translate(0, 1, 0);
|
||||
AxesCube._LineGeom.rotateX(Math.PI / 2);
|
||||
return AxesCube._LineGeom;
|
||||
}
|
||||
//坐标是一个盒子的
|
||||
private static _CubeGeo: BoxBufferGeometry;
|
||||
static get CubeGeo()
|
||||
{
|
||||
if (AxesCube._CubeGeo) return AxesCube._CubeGeo;
|
||||
|
||||
AxesCube._CubeGeo = new BoxBufferGeometry(.3, .3, .3);
|
||||
AxesCube._CubeGeo.rotateX(Math.PI / 2);
|
||||
AxesCube._CubeGeo.translate(0, 0, 2);
|
||||
return AxesCube._CubeGeo;
|
||||
}
|
||||
//坐标轴轴面
|
||||
private static _BoxGeo: BoxBufferGeometry;
|
||||
static get BoxGeo()
|
||||
{
|
||||
if (AxesCube._BoxGeo) return AxesCube._BoxGeo;
|
||||
|
||||
AxesCube._BoxGeo = new BoxBufferGeometry(0.65, 0.65, 0.05);
|
||||
AxesCube._BoxGeo.rotateX(Math.PI / 2);
|
||||
return AxesCube._BoxGeo;
|
||||
}
|
||||
|
||||
//默认为0.1
|
||||
set ConeRadius(size: number)
|
||||
{
|
||||
let sc = size / this.ConeRadius;
|
||||
this._Cone.scale.set(sc, sc, sc);
|
||||
}
|
||||
set Material(m: LineBasicMaterial)
|
||||
{
|
||||
for (let obj of this.children)
|
||||
{
|
||||
let o = obj as Mesh | Line;
|
||||
o.material = m;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,24 +1,247 @@
|
||||
import { Group } from "three";
|
||||
import { Group, Mesh, MeshBasicMaterial, OctahedronGeometry, Vector3 } from "three";
|
||||
import { ColorMaterial } from "../../Common/ColorPalette";
|
||||
import { equalv3, ZAxis, ZAxisN } from "../../Geometry/GeUtils";
|
||||
import { Axes } from "./Axes";
|
||||
import AxesCube from "./AxesCube";
|
||||
import { AxisType } from "./CoorAxes";
|
||||
import { IGizmo } from "./IGizmo";
|
||||
|
||||
export class ScaleGizmo extends Group implements IGizmo
|
||||
{
|
||||
protected _Axis_X: AxesCube;
|
||||
protected _Axis_Y: AxesCube;
|
||||
protected _Axis_Z: AxesCube;
|
||||
Picks: Group;
|
||||
AxtiveIndex: AxisType;
|
||||
private _ActiveIndex: AxisType = 0;
|
||||
//拾取坐标中间那个盒子
|
||||
private _OriginFace: Mesh;
|
||||
//拾取坐标中间那个盒子的材质
|
||||
static _OrgMtl = new MeshBasicMaterial({
|
||||
transparent: true,
|
||||
color: ColorMaterial.GetColor(7),
|
||||
opacity: .4,
|
||||
});
|
||||
//拾取坐标中间那个盒子的形状
|
||||
static _OriGeo = new OctahedronGeometry(.3, 0);
|
||||
//隐藏材质
|
||||
private static _MtlHide = new MeshBasicMaterial({
|
||||
visible: false
|
||||
});
|
||||
/**
|
||||
* 轴面材质
|
||||
*/
|
||||
static _XYBoxMtl = new MeshBasicMaterial({
|
||||
transparent: true,
|
||||
color: ColorMaterial.GetColor(5),
|
||||
opacity: 0.5
|
||||
});
|
||||
static _XZBoxMtl = new MeshBasicMaterial({
|
||||
transparent: true,
|
||||
color: ColorMaterial.GetColor(3),
|
||||
opacity: 0.5
|
||||
});
|
||||
static _YZBoxMtl = new MeshBasicMaterial({
|
||||
transparent: true,
|
||||
color: ColorMaterial.GetColor(1),
|
||||
opacity: 0.5
|
||||
});
|
||||
|
||||
constructor()
|
||||
{
|
||||
super();
|
||||
|
||||
/**
|
||||
* 坐标本体
|
||||
*/
|
||||
this._Axis_X = new AxesCube();
|
||||
this._Axis_X.rotateY(Math.PI / 2);
|
||||
this._Axis_X.updateMatrix();
|
||||
this._Axis_X.userData.Axis = AxisType.X;
|
||||
|
||||
this._Axis_Y = new AxesCube();
|
||||
this._Axis_Y.rotateX(Math.PI / -2);
|
||||
this._Axis_Y.Material = ColorMaterial.GetLineMaterial(3);
|
||||
this._Axis_Y.updateMatrix();
|
||||
this._Axis_Y.userData.Axis = AxisType.Y;
|
||||
|
||||
this._Axis_Z = new AxesCube();
|
||||
this._Axis_Z.Material = ColorMaterial.GetLineMaterial(5);
|
||||
this._Axis_Z.updateMatrix();
|
||||
this._Axis_Z.userData.Axis = AxisType.Z;
|
||||
|
||||
this.add(this._Axis_X, this._Axis_Y, this._Axis_Z);
|
||||
|
||||
//TODO:Draw
|
||||
/**
|
||||
* 坐标下面的三个盒子和覆盖坐标线隐藏的六条线
|
||||
*/
|
||||
for (let i = 0; i < 3; i++)
|
||||
{
|
||||
let a = this.children[i] as AxesCube;
|
||||
a.InitNegCube();
|
||||
}
|
||||
this._OriginFace = new Mesh(ScaleGizmo._OriGeo, ScaleGizmo._OrgMtl);
|
||||
this._OriginFace.userData.Axis = AxisType.Origin;
|
||||
this.add(this._OriginFace);
|
||||
|
||||
this.Picks = new Group;
|
||||
|
||||
let Axis_X = new Mesh();
|
||||
Axis_X = new Mesh(Axes.LineGeom, ScaleGizmo._MtlHide);
|
||||
Axis_X.position.x = .4;
|
||||
Axis_X.scale.set(9, 9, 1);
|
||||
Axis_X.rotateY(Math.PI / 2);
|
||||
Axis_X.updateMatrix();
|
||||
Axis_X.userData.Axis = AxisType.X;
|
||||
|
||||
let Axis_Y = new Mesh();
|
||||
Axis_Y = new Mesh(Axes.LineGeom, ScaleGizmo._MtlHide);
|
||||
Axis_Y.position.y = .4;
|
||||
Axis_Y.scale.set(9, 9, 1);
|
||||
Axis_Y.rotateX(Math.PI / -2);
|
||||
Axis_Y.updateMatrix();
|
||||
Axis_Y.userData.Axis = AxisType.Y;
|
||||
|
||||
let Axis_Z = new Mesh();
|
||||
Axis_Z = new Mesh(Axes.LineGeom, ScaleGizmo._MtlHide);
|
||||
Axis_Z.position.z = .4;
|
||||
Axis_Z.scale.set(9, 9, 1);
|
||||
Axis_Z.updateMatrix();
|
||||
Axis_Z.userData.Axis = AxisType.Z;
|
||||
|
||||
let org = new Mesh(ScaleGizmo._OriGeo, ScaleGizmo._MtlHide);
|
||||
org.userData.Axis = AxisType.Origin;
|
||||
org.scale.set(2, 2, 2);
|
||||
org.updateMatrix();
|
||||
|
||||
this.Picks.add(Axis_X, Axis_Y, Axis_Z, org);
|
||||
|
||||
let axn = Axis_X.clone() as Mesh;
|
||||
axn.position.x = -2.4;
|
||||
axn.updateMatrix();
|
||||
this.Picks.add(axn);
|
||||
|
||||
let ayn = Axis_Y.clone() as Mesh;
|
||||
ayn.position.y = -2.4;
|
||||
ayn.updateMatrix();
|
||||
this.Picks.add(ayn);
|
||||
|
||||
let azn = Axis_Z.clone() as Mesh;
|
||||
azn.position.z = -2.4;
|
||||
azn.updateMatrix();
|
||||
this.Picks.add(azn);
|
||||
|
||||
//三个轴面
|
||||
let _Axis_XY = new Mesh(AxesCube.BoxGeo, ScaleGizmo._XYBoxMtl);
|
||||
_Axis_XY.position.x = 0.7;
|
||||
_Axis_XY.position.y = 0.7;
|
||||
_Axis_XY.rotateX(Math.PI / 2);
|
||||
_Axis_XY.updateMatrix();
|
||||
_Axis_XY.userData.Axis = AxisType.XY;
|
||||
|
||||
let _Axis_XZ = new Mesh(AxesCube.BoxGeo, ScaleGizmo._XZBoxMtl);
|
||||
_Axis_XZ.position.x = 0.7;
|
||||
_Axis_XZ.position.z = 0.7;
|
||||
_Axis_XZ.updateMatrix();
|
||||
_Axis_XZ.userData.Axis = AxisType.XZ;
|
||||
|
||||
let _Axis_YZ = new Mesh(AxesCube.BoxGeo, ScaleGizmo._YZBoxMtl);
|
||||
_Axis_YZ.position.z = 0.7;
|
||||
_Axis_YZ.position.y = 0.7;
|
||||
_Axis_YZ.rotateZ(Math.PI / 2);
|
||||
_Axis_YZ.updateMatrix();
|
||||
_Axis_YZ.userData.Axis = AxisType.YZ;
|
||||
|
||||
this.Picks.add(_Axis_YZ, _Axis_XZ, _Axis_XY);
|
||||
|
||||
this.add(this.Picks);
|
||||
}
|
||||
|
||||
set AxtiveIndex(v: AxisType)
|
||||
{
|
||||
this._ActiveIndex = v;
|
||||
if (v < 3)
|
||||
{
|
||||
let axes = this.children[v] as AxesCube;
|
||||
axes.Material = ColorMaterial.GetLineMaterial(2);
|
||||
}
|
||||
else if (v === AxisType.Origin)
|
||||
{
|
||||
let mesh = this.children[v] as Mesh;
|
||||
mesh.material = ColorMaterial.GetLineMaterial(2);
|
||||
}
|
||||
else if (v === AxisType.XY)
|
||||
{
|
||||
let axes1 = this.children[0] as AxesCube;
|
||||
axes1.Material = ColorMaterial.GetLineMaterial(2);
|
||||
let axes2 = this.children[1] as AxesCube;
|
||||
axes2.Material = ColorMaterial.GetLineMaterial(2);
|
||||
|
||||
let face = this.Picks.children[v + 3] as Mesh;
|
||||
face.material = ColorMaterial.GetLineMaterial(2);
|
||||
}
|
||||
else if (v === AxisType.XZ)
|
||||
{
|
||||
let axes1 = this.children[0] as AxesCube;
|
||||
axes1.Material = ColorMaterial.GetLineMaterial(2);
|
||||
let axes2 = this.children[2] as AxesCube;
|
||||
axes2.Material = ColorMaterial.GetLineMaterial(2);
|
||||
|
||||
let face = this.Picks.children[v + 3] as Mesh;
|
||||
face.material = ColorMaterial.GetLineMaterial(2);
|
||||
}
|
||||
else if (v === AxisType.YZ)
|
||||
{
|
||||
let axes1 = this.children[1] as AxesCube;
|
||||
axes1.Material = ColorMaterial.GetLineMaterial(2);
|
||||
let axes2 = this.children[2] as AxesCube;
|
||||
axes2.Material = ColorMaterial.GetLineMaterial(2);
|
||||
|
||||
let face = this.Picks.children[v + 3] as Mesh;
|
||||
face.material = ColorMaterial.GetLineMaterial(2);
|
||||
}
|
||||
}
|
||||
|
||||
get AxtiveIndex()
|
||||
{
|
||||
return this._ActiveIndex;
|
||||
}
|
||||
private __vec__ = new Vector3;
|
||||
|
||||
HideAxes(): void
|
||||
{
|
||||
if (!this.visible) return;
|
||||
|
||||
for (let i = 0; i < 3; i++)
|
||||
{
|
||||
let axes = this.children[i];
|
||||
this.__vec__.setFromMatrixColumn(this.matrix, i).divideScalar(77);
|
||||
axes.visible = !(equalv3(this.__vec__, ZAxis, 0.2) || equalv3(this.__vec__, ZAxisN, 0.2));
|
||||
this.Picks.children[i].visible = axes.visible;
|
||||
this.Picks.children[4 + i].visible = axes.visible;
|
||||
|
||||
let d = this.__vec__.dot(ZAxis);
|
||||
this.Picks.children[7 + i].visible = Math.abs(d) > 0.1;
|
||||
}
|
||||
}
|
||||
Reset(): void
|
||||
{
|
||||
for (let i = 0; i < 3; i++)
|
||||
{
|
||||
let axis = this.children[i] as Axes;
|
||||
axis.Material = ColorMaterial.GetLineMaterial(i * 2 + 1);
|
||||
}
|
||||
this._ActiveIndex = -1;
|
||||
|
||||
this._OriginFace.material = ScaleGizmo._OrgMtl;
|
||||
|
||||
let face1 = this.Picks.children[7] as Mesh;
|
||||
face1.material = ScaleGizmo._YZBoxMtl;
|
||||
|
||||
let face2 = this.Picks.children[8] as Mesh;
|
||||
face2.material = ScaleGizmo._XZBoxMtl;
|
||||
|
||||
let face3 = this.Picks.children[9] as Mesh;
|
||||
face3.material = ScaleGizmo._XYBoxMtl;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue