diff --git a/src/Add-on/DrawLine.ts b/src/Add-on/DrawLine.ts index bfcbbcbfa..d42bd8289 100644 --- a/src/Add-on/DrawLine.ts +++ b/src/Add-on/DrawLine.ts @@ -1,5 +1,5 @@ import * as THREE from 'three'; -import { Vector3 } from 'three'; +import { Vector3, Vector2 } from 'three'; import { app } from '../ApplicationServices/Application'; import { Circle } from '../DatabaseServices/Circle'; @@ -8,6 +8,7 @@ import { Command } from '../Editor/CommandMachine'; import { PromptStatus } from '../Editor/PromptResult'; import { RenderType } from '../GraphicsSystem/Enum'; import { Ellipse } from '../DatabaseServices/Ellipse'; +import { Spline } from '../DatabaseServices/Spline'; export class DrawLine implements Command { @@ -281,6 +282,60 @@ export class DrawEllipse implements Command } } +export class DrawSpline implements Command +{ + async exec() + { + app.m_Editor.m_CommandStore.Prompt("请输入一个点:"); + let ptRes = await app.m_Editor.GetPoint({ Msg: "请输入第一个点:" }); + + if (ptRes.Status != PromptStatus.OK) + { + return; + } + let i = 0; + let p1 = ptRes.Value; + + let pts = []; + let ptLast = new Vector2(p1.x, p1.y); + pts.push(ptLast); + let spline = new Spline(pts); + + while (true) + { + app.m_Editor.m_CommandStore.Prompt("请输入点2:"); + ptRes = await app.m_Editor.GetPoint({ + Msg: "请输入点2:", + BasePoint: p1, + AllowDrawRubberBand: true, + KeyWordList: [{ msg: "放弃", key: "U" }], + Callback: (v) => + { + let p = new Vector2(v.x, v.y); + let tmpPts = pts.slice(0); + tmpPts.push(p); + spline.Points = tmpPts; + } + }); + + if (ptRes.Status == PromptStatus.OK) + { + ptLast = new Vector2(ptRes.Value.x, ptRes.Value.y); + app.m_Database.ModelSpace.Append(spline); + pts.push(ptLast); + p1 = ptRes.Value; + spline.Points = pts; + continue; + } + else + { + break; + } + } + + + } +} export class DrawTest implements Command { async exec() diff --git a/src/DatabaseServices/Spline.ts b/src/DatabaseServices/Spline.ts new file mode 100644 index 000000000..335e7f14f --- /dev/null +++ b/src/DatabaseServices/Spline.ts @@ -0,0 +1,52 @@ +import { Entity } from "./Entity"; +import { Vector2, Geometry } from "three"; +import * as THREE from "three"; +import { RenderType } from "../GraphicsSystem/Enum"; +import { ColorMaterial } from "../Common/ColorPalette"; + +export class Spline extends Entity +{ + m_Points: Vector2[]; + constructor(points: Vector2[]) + { + super(); + this.m_Points = points; + } + get Points() + { + return this.m_Points; + } + set Points(v: Vector2[]) + { + this.m_Points = v; + this.Update(); + } + Draw(renderType: RenderType): THREE.Object3D + { + let obj = super.Draw(renderType); + if (obj) return obj; + // let line = new THREE.Line(geo); + let curve = new THREE.SplineCurve(this.Points); + let points = curve.getPoints(1000); + let geometry = new THREE.Geometry().setFromPoints(points); + + // Create the final object to add to the scene + let splineObject = new THREE.Line(geometry, ColorMaterial.GetLineMaterial(this.m_Color)); + this.m_DrawEntity.set(renderType, splineObject); + splineObject.userData = this; + return splineObject; + } + Update() + { + for (let [, obj] of this.m_DrawEntity) + { + let geo = obj["geometry"] as Geometry; + + let curve = new THREE.SplineCurve(this.Points); + geo.setFromPoints(curve.getPoints(1000)); + + geo.verticesNeedUpdate = true; + geo.computeBoundingSphere(); + } + } +} \ No newline at end of file diff --git a/src/Editor/CommandRegister.ts b/src/Editor/CommandRegister.ts index d281914eb..264bef7f4 100644 --- a/src/Editor/CommandRegister.ts +++ b/src/Editor/CommandRegister.ts @@ -1,7 +1,7 @@ // import { Command_DrawBoard } from '../Add-on/DrawBoard'; // import { DrawFloor } from '../Add-on/DrawFloor'; import { DrawGripStretch } from '../Add-on/DrawGripStretch'; -import { DrawCircle, DrawLine, DrawRect, DrawSphere, DrawTest, ZoomE, DrawEllipse } from '../Add-on/DrawLine'; +import { DrawCircle, DrawLine, DrawRect, DrawSphere, DrawTest, ZoomE, DrawEllipse, DrawSpline } from '../Add-on/DrawLine'; import { DrawCircle0 } from '../Add-on/DrawZeroCircle'; import { Command_Erase } from '../Add-on/Erase'; import { Fbx } from '../Add-on/loadfbx'; @@ -28,6 +28,7 @@ export function registerCommand() commandMachine.RegisterCommand("rec", new DrawRect()) commandMachine.RegisterCommand("c", new DrawCircle()) commandMachine.RegisterCommand("el", new DrawEllipse()) + commandMachine.RegisterCommand("spl", new DrawSpline()) commandMachine.RegisterCommand("t", new DrawTest()) commandMachine.RegisterCommand("m", new Command_Move()) diff --git a/src/GraphicsSystem/Viewer.ts b/src/GraphicsSystem/Viewer.ts index ce127466b..d183371de 100644 --- a/src/GraphicsSystem/Viewer.ts +++ b/src/GraphicsSystem/Viewer.ts @@ -10,6 +10,7 @@ import { CameraControl } from './CameraControl'; import { RenderType } from './Enum'; import { GripScene } from './GripScene'; import { PreViewer } from './PreViewer'; +import { Vector3, Geometry } from 'three'; //导入其他js模块 require("three-CopyShader"); @@ -115,6 +116,9 @@ export class Viewer this.m_Composer.addPass(this.m_effectFXAA); this.onSize(); + + + } onSize = (width?, height?) =>