babylonjs-editcontrol 3.2.4 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,10 @@ import {
17
17
  Space,
18
18
  StandardMaterial,
19
19
  Vector3,
20
- TransformNode
20
+ TransformNode,
21
+ Engine,
22
+ DeepImmutableObject,
23
+ UtilityLayerRenderer
21
24
  }
22
25
  from 'babylonjs';
23
26
 
@@ -48,6 +51,7 @@ export class EditControl {
48
51
 
49
52
  private _canvas: HTMLCanvasElement;
50
53
  private _scene: Scene;
54
+ private _utilLayer: UtilityLayerRenderer;
51
55
  private _mainCamera: Camera;
52
56
  //root of the edit control
53
57
  private _ecRoot: Mesh;
@@ -76,6 +80,7 @@ export class EditControl {
76
80
  private _pointerdown: EventListener;
77
81
  private _pointerup: EventListener;
78
82
  private _pointermove: EventListener;
83
+
79
84
  //axes visibility
80
85
  private _visibility: number = 0.75;
81
86
 
@@ -103,7 +108,11 @@ export class EditControl {
103
108
  this._pickWidth = pickWidth;
104
109
  }
105
110
 
106
- this._scene = mesh.getScene();
111
+
112
+ this._utilLayer = UtilityLayerRenderer.DefaultUtilityLayer;
113
+ this._utilLayer.onlyCheckPointerDownEvents = false;
114
+ this._scene = this._utilLayer.utilityLayerScene;
115
+
107
116
  this._actHist = new ActHist(mesh, 10);
108
117
 
109
118
  mesh.computeWorldMatrix(true);
@@ -133,7 +142,7 @@ export class EditControl {
133
142
 
134
143
  //use canvas rather than scene to handle pointer events
135
144
  //scene cannot have mutiple eventlisteners for an event
136
- //with canvas one will have to do ones own pickinfo generattion.
145
+ //with canvas one will have to do ones own pickinfo generation.
137
146
 
138
147
  canvas.addEventListener("pointerdown", this._pointerdown, false);
139
148
  canvas.addEventListener("pointerup", this._pointerup, false);
@@ -261,7 +270,7 @@ export class EditControl {
261
270
  private _cameraNormal: Vector3 = new Vector3(0, 0, 0);
262
271
  private _setECScale() {
263
272
  this._ecRoot.position.subtractToRef(this._mainCamera.position, this._cameraTOec);
264
- Vector3.FromFloatArrayToRef(this._mainCamera.getWorldMatrix().asArray(), 8, this._cameraNormal);
273
+ Vector3.FromArrayToRef(<DeepImmutableObject<Float32Array>>this._mainCamera.getWorldMatrix().asArray(), 8, this._cameraNormal);
265
274
 
266
275
  //get distance of edit control from the camera plane
267
276
  //project "camera to edit control" vector onto the camera normal
@@ -488,9 +497,10 @@ export class EditControl {
488
497
  evt.preventDefault();
489
498
  this._pDown = true;
490
499
  if ((<PointerEvent>evt).button != 0) return;
491
- //TODO: do we really need to do a pick here?
492
- //onPointerOver() has already done this.
493
- let pickResult: PickingInfo = this._scene.pick(this._scene.pointerX, this._scene.pointerY, (mesh) => {
500
+ let engine: Engine = this._scene.getEngine();
501
+ let x = (engine.isPointerLock) ? this._canvas.width * 0.5 : this._scene.pointerX;
502
+ let y = (engine.isPointerLock) ? this._canvas.height * 0.5 : this._scene.pointerY;
503
+ let pickResult: PickingInfo = this._scene.pick(x, y, (mesh) => {
494
504
  if (this._transEnabled) {
495
505
  if ((mesh == this._tX) || (mesh == this._tY) || (mesh == this._tZ) || (mesh == this._tXZ) || (mesh == this._tZY) || (mesh == this._tYX) || (mesh == this._tAll)) return true;
496
506
  } else if ((this._rotEnabled)) {
@@ -499,7 +509,7 @@ export class EditControl {
499
509
  if ((mesh == this._sX) || (mesh == this._sY) || (mesh == this._sZ) || (mesh == this._sXZ) || (mesh == this._sZY) || (mesh == this._sYX) || (mesh == this._sAll)) return true;
500
510
  }
501
511
  return false;
502
- }, null, this._mainCamera);
512
+ }, false, this._mainCamera);
503
513
 
504
514
  if (pickResult.hit) {
505
515
  //this.setAxesVisiblity(0);
@@ -563,7 +573,10 @@ export class EditControl {
563
573
  private _detachCamera(cam: Object, can: Object) {
564
574
  let camera: Camera = <Camera>cam;
565
575
  let canvas: HTMLCanvasElement = <HTMLCanvasElement>can;
566
- camera.detachControl(canvas);
576
+ let engine: Engine = this._scene.getEngine();
577
+ if (!engine.isPointerLock) {
578
+ camera.detachControl(canvas)
579
+ }
567
580
  }
568
581
 
569
582
  private _prevOverMesh: Mesh;
@@ -577,7 +590,10 @@ export class EditControl {
577
590
  private _savedCol: Color3;
578
591
  private _onPointerOver() {
579
592
  //if(this.pDown) return;
580
- let pickResult: PickingInfo = this._scene.pick(this._scene.pointerX, this._scene.pointerY, (mesh) => {
593
+ let engine: Engine = this._scene.getEngine();
594
+ let x = (engine.isPointerLock) ? this._canvas.width * 0.5 : this._scene.pointerX;
595
+ let y = (engine.isPointerLock) ? this._canvas.height * 0.5 : this._scene.pointerY;
596
+ let pickResult: PickingInfo = this._scene.pick(x, y, (mesh) => {
581
597
  if (this._transEnabled) {
582
598
  if ((mesh == this._tX) || (mesh == this._tY) || (mesh == this._tZ) || (mesh == this._tXZ) || (mesh == this._tZY) || (mesh == this._tYX) || (mesh == this._tAll)) return true;
583
599
  } else if ((this._rotEnabled)) {
@@ -586,7 +602,7 @@ export class EditControl {
586
602
  if ((mesh == this._sX) || (mesh == this._sY) || (mesh == this._sZ) || (mesh == this._sXZ) || (mesh == this._sZY) || (mesh == this._sYX) || (mesh == this._sAll)) return true;
587
603
  }
588
604
  return false;
589
- }, null, this._mainCamera);
605
+ }, false, this._mainCamera);
590
606
  if (pickResult.hit) {
591
607
  //if we are still over the same axis mesh then don't do anything
592
608
  if (<Mesh>pickResult.pickedMesh != this._prevOverMesh) {
@@ -662,7 +678,10 @@ export class EditControl {
662
678
  private _onPointerUp(evt: Event) {
663
679
  this._pDown = false;
664
680
  if (this._editing) {
665
- this._mainCamera.attachControl(this._canvas);
681
+ let engine: Engine = this._scene.getEngine();
682
+ if (!engine.isPointerLock) {
683
+ this._mainCamera.attachControl(true);
684
+ }
666
685
  this._setEditing(false);
667
686
  //this.setAxesVisiblity(1);
668
687
  this._hideBaxis();
@@ -1024,9 +1043,9 @@ export class EditControl {
1024
1043
  */
1025
1044
  private _setLocalAxes(mesh: Node) {
1026
1045
  let meshMatrix: Matrix = mesh.getWorldMatrix();
1027
- Vector3.FromFloatArrayToRef(meshMatrix.m, 0, this._localX);
1028
- Vector3.FromFloatArrayToRef(meshMatrix.m, 4, this._localY);
1029
- Vector3.FromFloatArrayToRef(meshMatrix.m, 8, this._localZ);
1046
+ Vector3.FromArrayToRef(<DeepImmutableObject<Float32Array>>meshMatrix.m, 0, this._localX);
1047
+ Vector3.FromArrayToRef(<DeepImmutableObject<Float32Array>>meshMatrix.m, 4, this._localY);
1048
+ Vector3.FromArrayToRef(<DeepImmutableObject<Float32Array>>meshMatrix.m, 8, this._localZ);
1030
1049
  }
1031
1050
 
1032
1051
 
@@ -1141,7 +1160,10 @@ export class EditControl {
1141
1160
  }
1142
1161
 
1143
1162
  private _getPosOnPickPlane(): Vector3 {
1144
- let pickinfo: PickingInfo = this._scene.pick(this._scene.pointerX, this._scene.pointerY, (mesh) => {
1163
+ let engine: Engine = this._scene.getEngine();
1164
+ let x = (engine.isPointerLock) ? this._canvas.width * 0.5 : this._scene.pointerX;
1165
+ let y = (engine.isPointerLock) ? this._canvas.height * 0.5 : this._scene.pointerY;
1166
+ let pickinfo: PickingInfo = this._scene.pick(x, y, (mesh) => {
1145
1167
  return mesh == this._pickedPlane;
1146
1168
  }, null, this._mainCamera);
1147
1169
 
@@ -1343,9 +1365,9 @@ export class EditControl {
1343
1365
  let guideAxes: Mesh = new Mesh("", this._scene);
1344
1366
 
1345
1367
  //the big axes, shown when an axis is selected
1346
- this._bXaxis = Mesh.CreateLines("", [new Vector3(-100, 0, 0), new Vector3(100, 0, 0)], this._scene);
1347
- this._bYaxis = Mesh.CreateLines("", [new Vector3(0, -100, 0), new Vector3(0, 100, 0)], this._scene);
1348
- this._bZaxis = Mesh.CreateLines("", [new Vector3(0, 0, -100), new Vector3(0, 0, 100)], this._scene);
1368
+ this._bXaxis = MeshBuilder.CreateLines("", { points: [new Vector3(-100, 0, 0), new Vector3(100, 0, 0)] }, this._scene);
1369
+ this._bYaxis = MeshBuilder.CreateLines("", { points: [new Vector3(0, -100, 0), new Vector3(0, 100, 0)] }, this._scene);
1370
+ this._bZaxis = MeshBuilder.CreateLines("", { points: [new Vector3(0, 0, -100), new Vector3(0, 0, 100)] }, this._scene);
1349
1371
 
1350
1372
  //lines are now pickable too
1351
1373
  this._bXaxis.isPickable = false;
@@ -1362,9 +1384,9 @@ export class EditControl {
1362
1384
 
1363
1385
  //the small axis
1364
1386
  let al: number = this._axesLen * this._axesScale * 0.75;
1365
- this._xaxis = Mesh.CreateLines("", [new Vector3(0, 0, 0), new Vector3(al, 0, 0)], this._scene);
1366
- this._yaxis = Mesh.CreateLines("", [new Vector3(0, 0, 0), new Vector3(0, al, 0)], this._scene);
1367
- this._zaxis = Mesh.CreateLines("", [new Vector3(0, 0, 0), new Vector3(0, 0, al)], this._scene);
1387
+ this._xaxis = MeshBuilder.CreateLines("", { points: [new Vector3(0, 0, 0), new Vector3(al, 0, 0)] }, this._scene);
1388
+ this._yaxis = MeshBuilder.CreateLines("", { points: [new Vector3(0, 0, 0), new Vector3(0, al, 0)] }, this._scene);
1389
+ this._zaxis = MeshBuilder.CreateLines("", { points: [new Vector3(0, 0, 0), new Vector3(0, 0, al)] }, this._scene);
1368
1390
 
1369
1391
  //lines are now pickable too
1370
1392
  this._xaxis.isPickable = false;
@@ -1392,10 +1414,10 @@ export class EditControl {
1392
1414
  private _pYX: Mesh;
1393
1415
 
1394
1416
  private _createPickPlanes() {
1395
- this._pALL = Mesh.CreatePlane("", 5, this._scene);
1396
- this._pXZ = Mesh.CreatePlane("", 5, this._scene);
1397
- this._pZY = Mesh.CreatePlane("", 5, this._scene);
1398
- this._pYX = Mesh.CreatePlane("", 5, this._scene);
1417
+ this._pALL = MeshBuilder.CreatePlane("", { size: 5 }, this._scene);
1418
+ this._pXZ = MeshBuilder.CreatePlane("", { size: 5 }, this._scene);
1419
+ this._pZY = MeshBuilder.CreatePlane("", { size: 5 }, this._scene);
1420
+ this._pYX = MeshBuilder.CreatePlane("", { size: 5 }, this._scene);
1399
1421
 
1400
1422
  this._pALL.isPickable = false;
1401
1423
  this._pXZ.isPickable = false;
@@ -1454,22 +1476,33 @@ export class EditControl {
1454
1476
 
1455
1477
  this._tCtl = new Mesh("", this._scene);
1456
1478
 
1457
- /*pickable invisible boxes around axes lines*/
1479
+ // pickable invisible boxes around axes lines
1458
1480
  this._createPickableTrans(r, l, this._tCtl, this._scene);
1459
1481
 
1460
1482
  //non pickable but visible cones at end of axes lines
1461
1483
  this._createNonPickableTrans(r, l, this._scene);
1462
1484
  }
1463
1485
 
1486
+ /**
1487
+ * pickable but invisible
1488
+ * a) 3 boxes around each of the 3 small axes lines
1489
+ * b) 3 small planes near origin for movement along a plane
1490
+ * @param r
1491
+ * @param l
1492
+ * @param tCtl
1493
+ * @param scene
1494
+ */
1495
+
1464
1496
  private _createPickableTrans(r: number, l: number, tCtl: Mesh, scene: Scene) {
1465
1497
  let tX = this._extrudeBox(r / 2, l);
1466
1498
  tX.name = "X";
1467
1499
  let tY = tX.clone("Y");
1468
1500
  let tZ = tX.clone("Z");
1469
1501
 
1470
- let tXZ = MeshBuilder.CreatePlane("XZ", { size: r * 2 }, scene);
1471
- let tZY = MeshBuilder.CreatePlane("ZY", { size: r * 2 }, scene);
1472
- let tYX = MeshBuilder.CreatePlane("YX", { size: r * 2 }, scene);
1502
+ let s = r * 2;
1503
+ let tXZ = MeshBuilder.CreatePlane("XZ", { size: s }, scene);
1504
+ let tZY = MeshBuilder.CreatePlane("ZY", { size: s }, scene);
1505
+ let tYX = MeshBuilder.CreatePlane("YX", { size: s }, scene);
1473
1506
 
1474
1507
  tXZ.rotation.x = 1.57;
1475
1508
  tZY.rotation.y = -1.57;
@@ -1487,7 +1520,7 @@ export class EditControl {
1487
1520
  tZY.bakeCurrentTransformIntoVertices();
1488
1521
  tYX.bakeCurrentTransformIntoVertices();
1489
1522
 
1490
- let tAll = Mesh.CreateBox("ALL", r * 2, scene);
1523
+ let tAll = MeshBuilder.CreateBox("ALL", { size: r * 2 }, scene);
1491
1524
 
1492
1525
  tX.parent = tCtl;
1493
1526
  tY.parent = tCtl;
@@ -1515,12 +1548,15 @@ export class EditControl {
1515
1548
  this._setPickableFalse(this._all_t)
1516
1549
  }
1517
1550
 
1551
+ //non pickable but visible
1552
+ // a) 3 cones at end of the 3 small axes lines
1553
+ // b) 3 small planes near origin for movement along a plane
1518
1554
  private _createNonPickableTrans(r: number, l: number, scene: Scene) {
1519
1555
  //cone length
1520
1556
  let cl: number = l / 5;
1521
1557
  //cone base radius
1522
1558
  //let cr: number = r;
1523
- let tEndX = Mesh.CreateCylinder("", cl, 0, r, 6, 1, scene);
1559
+ let tEndX = MeshBuilder.CreateCylinder("", { height: cl, diameterTop: 0, diameterBottom: r, tessellation: 6, subdivisions: 1 }, scene);
1524
1560
  let tEndY = tEndX.clone("");
1525
1561
  let tEndZ = tEndX.clone("");
1526
1562
 
@@ -1530,7 +1566,7 @@ export class EditControl {
1530
1566
  let tEndZY = MeshBuilder.CreatePlane("ZY", { size: s }, scene);
1531
1567
  let tEndYX = MeshBuilder.CreatePlane("YX", { size: s }, scene);
1532
1568
 
1533
- let tEndAll = Mesh.CreateBox("ALL", r, scene);
1569
+ let tEndAll = MeshBuilder.CreateBox("ALL", { size: r }, scene);
1534
1570
 
1535
1571
  tEndX.rotation.x = 1.57;
1536
1572
  tEndY.rotation.x = 1.57;
@@ -1709,7 +1745,7 @@ export class EditControl {
1709
1745
  private _extrudeBox(w: number, l: number): Mesh {
1710
1746
  let shape: Vector3[] = [new Vector3(w, w, 0), new Vector3(-w, w, 0), new Vector3(-w, -w, 0), new Vector3(w, -w, 0), new Vector3(w, w, 0)];
1711
1747
  let path: Vector3[] = [new Vector3(0, 0, 0), new Vector3(0, 0, l)];
1712
- let box: Mesh = Mesh.ExtrudeShape("", shape, path, 1, 0, 2, this._scene);
1748
+ let box: Mesh = MeshBuilder.ExtrudeShape("", { shape: shape, path: path, scale: 1, rotation: 0, cap: 2 }, this._scene);
1713
1749
  return box;
1714
1750
  }
1715
1751
 
@@ -1735,7 +1771,7 @@ export class EditControl {
1735
1771
  p++;
1736
1772
  }
1737
1773
  }
1738
- let circle: LinesMesh = Mesh.CreateLines("", points, this._scene);
1774
+ let circle: LinesMesh = MeshBuilder.CreateLines("", { points: points }, this._scene);
1739
1775
  return circle;
1740
1776
  }
1741
1777
 
@@ -1752,7 +1788,7 @@ export class EditControl {
1752
1788
  points[p] = new Vector3(x, 0, z);
1753
1789
  p++;
1754
1790
  }
1755
- let tube: Mesh = Mesh.CreateTube("", points, this._pickWidth * this._axesScale * 2, 3, null, Mesh.NO_CAP, this._scene);
1791
+ let tube: Mesh = MeshBuilder.CreateTube("", { path: points, radius: this._pickWidth * this._axesScale * 2, tessellation: 3, cap: Mesh.NO_CAP }, this._scene);
1756
1792
  return tube;
1757
1793
  }
1758
1794
 
@@ -1820,7 +1856,7 @@ export class EditControl {
1820
1856
  sZY.bakeCurrentTransformIntoVertices();
1821
1857
  sYX.bakeCurrentTransformIntoVertices();
1822
1858
 
1823
- let sAll: Mesh = Mesh.CreateBox("ALL", 2 * r, this._scene);
1859
+ let sAll: Mesh = MeshBuilder.CreateBox("ALL", { size: 2 * r }, this._scene);
1824
1860
 
1825
1861
  sX.parent = sCtl;
1826
1862
  sY.parent = sCtl;
@@ -1850,7 +1886,7 @@ export class EditControl {
1850
1886
 
1851
1887
  private _createNonPickableScale(r: number, l: number) {
1852
1888
 
1853
- let sEndX = Mesh.CreateBox("", r, this._scene);
1889
+ let sEndX = MeshBuilder.CreateBox("", { size: r }, this._scene);
1854
1890
  let sEndY = sEndX.clone("");
1855
1891
  let sEndZ = sEndX.clone("");
1856
1892
 
@@ -1860,7 +1896,7 @@ export class EditControl {
1860
1896
  let sEndYX = MeshBuilder.CreatePlane("YX", { size: s }, this._scene);
1861
1897
 
1862
1898
 
1863
- let sEndAll = Mesh.CreateBox("ALL", r, this._scene);
1899
+ let sEndAll = MeshBuilder.CreateBox("ALL", { size: r }, this._scene);
1864
1900
 
1865
1901
  sEndXZ.rotation.x = 1.57;
1866
1902
  sEndZY.rotation.y = -1.57;
@@ -1949,31 +1985,46 @@ export class EditControl {
1949
1985
  this._snapT = s;
1950
1986
  }
1951
1987
 
1988
+ public isTransSnap(): boolean {
1989
+ return this._snapT;
1990
+ }
1991
+
1952
1992
  public setRotSnap(s: boolean) {
1953
1993
  this._snapR = s;
1954
1994
  }
1995
+ public isRotSnap(): boolean {
1996
+ return this._snapR;
1997
+ }
1955
1998
 
1956
1999
  public setScaleSnap(s: boolean) {
1957
2000
  this._snapS = s;
1958
2001
  }
2002
+ public isScaleSnap(): boolean {
2003
+ return this._snapS;
2004
+ }
1959
2005
 
1960
2006
  private _tSnap: Vector3 = new Vector3(this._transSnap, this._transSnap, this._transSnap);
1961
2007
  public setTransSnapValue(t: number) {
1962
2008
  this._tSnap.copyFromFloats(t, t, t);
1963
2009
  this._transSnap = t;
1964
2010
  }
2011
+ public getTransSnapValue(): number {
2012
+ return this._transSnap;
2013
+ }
1965
2014
 
1966
2015
  public setRotSnapValue(r: number) {
1967
2016
  this._rotSnap = r;
1968
2017
  }
2018
+ public getRotSnapValue(): number {
2019
+ return this._rotSnap;
2020
+ }
1969
2021
 
1970
- /**
1971
- * use this to set the scale snap value
1972
- */
1973
2022
  public setScaleSnapValue(r: number) {
1974
2023
  this._scaleSnap = r;
1975
2024
  }
1976
-
2025
+ public getScaleSnapValue(): number {
2026
+ return this._scaleSnap;
2027
+ }
1977
2028
  //few temp vectors & matrix
1978
2029
  private _tv1: Vector3 = new Vector3(0, 0, 0);
1979
2030
  private _tv2: Vector3 = new Vector3(0, 0, 0);
@@ -2064,6 +2115,14 @@ export class EditControl {
2064
2115
  return angle;
2065
2116
  }
2066
2117
 
2118
+ private static _getStandardMaterial(col: Color3, scene: Scene): StandardMaterial {
2119
+ let mat: StandardMaterial = new StandardMaterial("", scene);
2120
+ mat.emissiveColor = col;
2121
+ mat.diffuseColor = Color3.Black();
2122
+ mat.specularColor = Color3.Black();
2123
+ mat.backFaceCulling = false;
2124
+ return mat;
2125
+ }
2067
2126
 
2068
2127
  private _createMaterials(scene: Scene) {
2069
2128
  this._redMat = EditControl._getStandardMaterial(this._redCol, scene);
@@ -2081,14 +2140,7 @@ export class EditControl {
2081
2140
  this._yellowMat.dispose();
2082
2141
  }
2083
2142
 
2084
- private static _getStandardMaterial(col: Color3, scene: Scene): StandardMaterial {
2085
- let mat: StandardMaterial = new StandardMaterial("", scene);
2086
- mat.emissiveColor = col;
2087
- mat.diffuseColor = Color3.Black();
2088
- mat.specularColor = Color3.Black();
2089
- mat.backFaceCulling = false;
2090
- return mat;
2091
- }
2143
+
2092
2144
  }
2093
2145
 
2094
2146
  class ActHist {