From c1d2ceb4cf3cc10d0fb1ab62df31e614cc4aa3e4 Mon Sep 17 00:00:00 2001 From: liteng <930372551@qq.com> Date: Sun, 10 Feb 2019 20:45:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=8C=E6=AC=A1=E8=B4=9D?= =?UTF-8?q?=E5=A1=9E=E5=B0=94=E6=9B=B2=E7=BA=BF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/editor/menubar/GeometryMenu.js | 17 ++++-- .../src/object/line/QuadraticBezierCurve.js | 55 +++++++++++++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 ShadowEditor.Web/src/object/line/QuadraticBezierCurve.js diff --git a/ShadowEditor.Web/src/editor/menubar/GeometryMenu.js b/ShadowEditor.Web/src/editor/menubar/GeometryMenu.js index 2c53965b..05eb4bed 100644 --- a/ShadowEditor.Web/src/editor/menubar/GeometryMenu.js +++ b/ShadowEditor.Web/src/editor/menubar/GeometryMenu.js @@ -17,6 +17,7 @@ import Text from '../../object/geometry/Text'; import LineCurve from '../../object/line/LineCurve'; import CatmullRomCurve from '../../object/line/CatmullRomCurve'; +import QuadraticBezierCurve from '../../object/line/QuadraticBezierCurve'; import CubicBezierCurve from '../../object/line/CubicBezierCurve'; import EllipseCurve from '../../object/line/EllipseCurve'; @@ -127,6 +128,11 @@ GeometryMenu.prototype.render = function () { html: 'CatmullRom曲线', cls: 'option', onClick: this.addCatmullRomCurve.bind(this) + }, { + xtype: 'div', + html: '二次贝塞尔曲线', + cls: 'option', + onClick: this.addQuadraticBezierCurve.bind(this) }, { xtype: 'div', html: '三次贝塞尔曲线', @@ -137,10 +143,6 @@ GeometryMenu.prototype.render = function () { html: '椭圆曲线', cls: 'option', onClick: this.addEllipseCurve.bind(this) - }, { - xtype: 'div', - html: '四次贝塞尔曲线', - cls: 'option', }, { xtype: 'div', html: 'Spline曲线', @@ -253,6 +255,13 @@ GeometryMenu.prototype.addCatmullRomCurve = function () { this.app.editor.addRawHelper(helper); }; +// ----------------------- 二次贝塞尔曲线 --------------------------------- + +GeometryMenu.prototype.addQuadraticBezierCurve = function () { + var line = new QuadraticBezierCurve(); + this.app.editor.execute(new AddObjectCommand(line)); +}; + // ----------------------- 三次贝塞尔曲线 --------------------------------- GeometryMenu.prototype.addCubicBezierCurve = function () { diff --git a/ShadowEditor.Web/src/object/line/QuadraticBezierCurve.js b/ShadowEditor.Web/src/object/line/QuadraticBezierCurve.js new file mode 100644 index 00000000..3de0f27d --- /dev/null +++ b/ShadowEditor.Web/src/object/line/QuadraticBezierCurve.js @@ -0,0 +1,55 @@ +var ARC_SEGMENTS = 200; + +/** + * 二次贝塞尔曲线 + * @param {*} options + */ +function QuadraticBezierCurve(options = {}) { + var geometry = new THREE.BufferGeometry(); + geometry.addAttribute('position', new THREE.BufferAttribute(new Float32Array(ARC_SEGMENTS * 3), 3)); + + var material = new THREE.LineBasicMaterial({ + color: 0xff0000, + opacity: 0.35 + }); + + THREE.Line.call(this, geometry, material); + + this.name = '二次贝塞尔曲线'; + + this.castShadow = true; + + Object.assign(this.userData, { + type: 'QuadraticBezierCurve', + v0: new THREE.Vector3(-10, 0, 0), + v1: new THREE.Vector3(20, 15, 0), + v2: new THREE.Vector3(10, 0, 0) + }); + + this.update(); +} + +QuadraticBezierCurve.prototype = Object.create(THREE.Line.prototype); +QuadraticBezierCurve.prototype.constructor = QuadraticBezierCurve; + +QuadraticBezierCurve.prototype.update = function () { + var curve = new THREE.QuadraticBezierCurve3( + this.userData.v0, + this.userData.v1, + this.userData.v2 + ); + + var position = this.geometry.attributes.position; + + var point = new THREE.Vector3(); + + for (var i = 0; i < ARC_SEGMENTS; i++) { + var t = i / (ARC_SEGMENTS - 1); + curve.getPoint(t, point); + position.setXYZ(i, point.x, point.y, point.z); + } + + position.needsUpdate = true; +}; + +export default QuadraticBezierCurve; \ No newline at end of file