Radix.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. module Sandbox {
  2. import Color3 = BABYLON.Color3;
  3. import StandardMaterial = BABYLON.StandardMaterial;
  4. import Scene = BABYLON.Scene;
  5. import Matrix = BABYLON.Matrix;
  6. import Mesh = BABYLON.Mesh;
  7. import Vector3 = BABYLON.Vector3;
  8. import Quaternion = BABYLON.Quaternion;
  9. import VertexData = BABYLON.VertexData;
  10. import LinesMesh = BABYLON.LinesMesh;
  11. import PointLight = BABYLON.PointLight;
  12. import AbstractMesh = BABYLON.AbstractMesh;
  13. import Ray = BABYLON.Ray;
  14. import Vector2 = BABYLON.Vector2;
  15. export const enum RadixFeatures {
  16. None = 0,
  17. /**
  18. * Display the Arrow that follows the X Axis
  19. */
  20. ArrowX = 0x0001,
  21. /**
  22. * Display the Arrow that follows the Y Axis
  23. */
  24. ArrowY = 0x0002,
  25. /**
  26. * Display the Arrow that follows the Z Axis
  27. */
  28. ArrowZ = 0x0004,
  29. /**
  30. * Display the Arrow that follows the XYZ Axis
  31. */
  32. ArrowsXYZ = 0x0007,
  33. /**
  34. * Display the anchor that allow XY plane manipulation
  35. */
  36. PlaneSelectionXY = 0x0010,
  37. /**
  38. * Display the anchor that allow XZ plane manipulation
  39. */
  40. PlaneSelectionXZ = 0x0020,
  41. /**
  42. * Display the anchor that allow YZ plane manipulation
  43. */
  44. PlaneSelectionYZ = 0x0040,
  45. /**
  46. * Display all the anchors that allow plane manipulation
  47. */
  48. AllPlanesSelection = 0x0070,
  49. /**
  50. * Display the rotation cylinder that allows rotation manipulation along the X Axis
  51. */
  52. RotationX = 0x0100,
  53. /**
  54. * Display the rotation cylinder that allows rotation manipulation along the Y Axis
  55. */
  56. RotationY = 0x0200,
  57. /**
  58. * Display the rotation cylinder that allows rotation manipulation along the A Axis
  59. */
  60. RotationZ = 0x0400,
  61. /**
  62. * Display all rotation cylinders
  63. */
  64. Rotations = 0x0700,
  65. //CenterSquare = 0x1000 NOT SUPPORTED RIGHT NOW
  66. }
  67. /**
  68. * This class create the visual geometry to display a manipulation radix in a viewport.
  69. * It also implements the logic to handler intersection, hover on feature.
  70. */
  71. export class Radix {
  72. private static pc = 0.6;
  73. private static sc = 0.2;
  74. /**
  75. * Set/get the Wire Selection Threshold, set a bigger value to improve tolerance while picking a wire mesh
  76. */
  77. get wireSelectionThreshold(): number {
  78. return this._wireSelectionThreshold;
  79. }
  80. set wireSelectionThreshold(value: number) {
  81. this._wireSelectionThreshold = value;
  82. let meshes = this._rootMesh.getChildMeshes(true, m => m instanceof LinesMesh);
  83. for (var mesh of meshes) {
  84. var lm = <LinesMesh>mesh;
  85. if (lm) {
  86. lm.intersectionThreshold = value;
  87. }
  88. }
  89. }
  90. /**
  91. * Get/set the colors of the X Arrow
  92. */
  93. get xArrowColor(): Color3 {
  94. return this._xArrowColor;
  95. }
  96. set xArrowColor(value: Color3) {
  97. this._xArrowColor = value;
  98. this.updateMaterial("arrowX", value);
  99. this.updateMaterial("rotationX", value);
  100. }
  101. /**
  102. * Get/set the colors of the Y Arrow
  103. */
  104. get yArrowColor(): Color3 {
  105. return this._yArrowColor;
  106. }
  107. set yArrowColor(value: Color3) {
  108. this._yArrowColor = value;
  109. this.updateMaterial("arrowY", value);
  110. this.updateMaterial("rotationY", value);
  111. }
  112. /**
  113. * Get/set the colors of the Z Arrow
  114. */
  115. get zArrowColor(): Color3 {
  116. return this._zArrowColor;
  117. }
  118. set zArrowColor(value: Color3) {
  119. this._zArrowColor = value;
  120. this.updateMaterial("arrowZ", value);
  121. this.updateMaterial("rotationZ", value);
  122. }
  123. /**
  124. * Get/set the colors of the XY Plane selection anchor
  125. */
  126. get xyPlaneSelectionColor(): Color3 {
  127. return this._xyPlaneSelectionColor;
  128. }
  129. set xyPlaneSelectionColor(value: Color3) {
  130. this._xyPlaneSelectionColor = value;
  131. }
  132. /**
  133. * Get/set the colors of the XZ Plane selection anchor
  134. */
  135. get xzPlaneSelectionColor(): Color3 {
  136. return this._xzPlaneSelectionColor;
  137. }
  138. set xzPlaneSelectionColor(value: Color3) {
  139. this._xzPlaneSelectionColor = value;
  140. }
  141. /**
  142. * Get/set the colors of the YZ Plane selection anchor
  143. */
  144. get yzPlaneSelectionColor(): Color3 {
  145. return this._yzPlaneSelectionColor;
  146. }
  147. set yzPlaneSelectionColor(value: Color3) {
  148. this._yzPlaneSelectionColor = value;
  149. }
  150. /**
  151. * Get/set the feature of the Radix that are/must be highlighted
  152. * @returns {}
  153. */
  154. get highlighted(): RadixFeatures {
  155. return this._highlighted;
  156. }
  157. set highlighted(value: RadixFeatures) {
  158. this.updateMaterialFromHighlighted(RadixFeatures.ArrowX, value, "arrowX");
  159. this.updateMaterialFromHighlighted(RadixFeatures.ArrowY, value, "arrowY");
  160. this.updateMaterialFromHighlighted(RadixFeatures.ArrowZ, value, "arrowZ");
  161. this.updateMaterialFromHighlighted(RadixFeatures.PlaneSelectionXY, value, "planeXY");
  162. this.updateMaterialFromHighlighted(RadixFeatures.PlaneSelectionXZ, value, "planeXZ");
  163. this.updateMaterialFromHighlighted(RadixFeatures.PlaneSelectionYZ, value, "planeYZ");
  164. this.updateMaterialFromHighlighted(RadixFeatures.RotationX, value, "rotationX");
  165. this.updateMaterialFromHighlighted(RadixFeatures.RotationY, value, "rotationY");
  166. this.updateMaterialFromHighlighted(RadixFeatures.RotationZ, value, "rotationZ");
  167. this._highlighted = value;
  168. }
  169. /**
  170. * Get the Radix Features that were selected upon creation
  171. */
  172. get features(): RadixFeatures {
  173. return this._features;
  174. }
  175. /**
  176. * Create a new Radix instance. The length/radius members are optionals and the default value should suit most cases
  177. * @param scene the owner Scene
  178. * @param features the feature the radix must display
  179. * @param arrowLength the length of a row of an axis, include the rotation cylinder (if any), but always exclude the arrow cone
  180. * @param coneLength the length of the arrow cone. this is also the length taken for the rotation cylinder (if any)
  181. * @param coneRadius the radius of the arrow cone
  182. * @param planeSelectionLength the length of the selection plane
  183. */
  184. constructor(scene: Scene, features: RadixFeatures = RadixFeatures.ArrowsXYZ | RadixFeatures.AllPlanesSelection | RadixFeatures.Rotations, arrowLength?: number, coneLength?: number, coneRadius?: number, planeSelectionLength?: number) {
  185. this._scene = scene;
  186. this._arrowLength = arrowLength ? arrowLength : 1;
  187. this._coneLength = coneLength ? coneLength : 0.2;
  188. this._coneRadius = coneRadius ? coneRadius : 0.1;
  189. this._planeSelectionLength = planeSelectionLength ? planeSelectionLength : (this._arrowLength / 5.0);
  190. this._wireSelectionThreshold = 0.05;
  191. this._light1 = new BABYLON.PointLight("ManipulatorLight", new BABYLON.Vector3(50, 50, 70), this._scene);
  192. this._light1.id = "***SceneManipulatorLight***";
  193. this._light2 = new BABYLON.PointLight("ManipulatorLight", new BABYLON.Vector3(-50, -50, -70), this._scene);
  194. this._light2.id = "***SceneManipulatorLight***";
  195. this._xArrowColor = new Color3(Radix.pc, Radix.sc / 2, Radix.sc);
  196. this._yArrowColor = new Color3(Radix.sc, Radix.pc, Radix.sc / 2);
  197. this._zArrowColor = new Color3(Radix.sc / 2, Radix.sc, Radix.pc);
  198. this._xyPlaneSelectionColor = new Color3(Radix.pc / 1.1, Radix.pc / 1.3, Radix.sc);
  199. this._xzPlaneSelectionColor = new Color3(Radix.pc / 1.1, Radix.sc, Radix.pc / 1.3);
  200. this._yzPlaneSelectionColor = new Color3(Radix.sc, Radix.pc / 1.3, Radix.pc / 1.1);
  201. var materials = [];
  202. materials.push({ name: "arrowX", color: this.xArrowColor });
  203. materials.push({ name: "arrowY", color: this.yArrowColor });
  204. materials.push({ name: "arrowZ", color: this.zArrowColor });
  205. materials.push({ name: "planeXY", color: this.xyPlaneSelectionColor });
  206. materials.push({ name: "planeXZ", color: this.xzPlaneSelectionColor });
  207. materials.push({ name: "planeYZ", color: this.yzPlaneSelectionColor });
  208. materials.push({ name: "rotationX", color: this.xArrowColor.clone() });
  209. materials.push({ name: "rotationY", color: this.yArrowColor.clone() });
  210. materials.push({ name: "rotationZ", color: this.zArrowColor.clone() });
  211. this._materials = [];
  212. for (var matData of materials) {
  213. var mtl = new StandardMaterial(matData.name + "RadixMaterial", this._scene);
  214. mtl.diffuseColor = matData.color;
  215. this._materials[matData.name] = mtl;
  216. }
  217. this._features = features;
  218. this._rootMesh = new Mesh("radixRoot", this._scene);
  219. this._rootMesh.renderingGroupId = 1;
  220. this.constructGraphicalObjects();
  221. }
  222. /**
  223. * make an intersection test between a point position in the viwport and the Radix, return the feature that is intersected, if any.
  224. * only the closer Radix Feature is picked.
  225. * @param pos the viewport position to create the picking ray from.
  226. */
  227. intersect(pos: Vector2): RadixFeatures {
  228. let hit = RadixFeatures.None;
  229. let closest = Number.MAX_VALUE;
  230. // Arrows
  231. if (this.hasFeature(RadixFeatures.ArrowX)) {
  232. let dist = this.intersectMeshes(pos, "arrowX", closest);
  233. if (dist < closest) {
  234. closest = dist;
  235. hit = RadixFeatures.ArrowX;
  236. }
  237. }
  238. if (this.hasFeature(RadixFeatures.ArrowY)) {
  239. let dist = this.intersectMeshes(pos, "arrowY", closest);
  240. if (dist < closest) {
  241. closest = dist;
  242. hit = RadixFeatures.ArrowY;
  243. }
  244. }
  245. if (this.hasFeature(RadixFeatures.ArrowZ)) {
  246. let dist = this.intersectMeshes(pos, "arrowZ", closest);
  247. if (dist < closest) {
  248. closest = dist;
  249. hit = RadixFeatures.ArrowZ;
  250. }
  251. }
  252. // Planes
  253. if (this.hasFeature(RadixFeatures.PlaneSelectionXY)) {
  254. let dist = this.intersectMeshes(pos, "planeXY", closest);
  255. if (dist < closest) {
  256. closest = dist;
  257. hit = RadixFeatures.PlaneSelectionXY;
  258. }
  259. }
  260. if (this.hasFeature(RadixFeatures.PlaneSelectionXZ)) {
  261. let dist = this.intersectMeshes(pos, "planeXZ", closest);
  262. if (dist < closest) {
  263. closest = dist;
  264. hit = RadixFeatures.PlaneSelectionXZ;
  265. }
  266. }
  267. if (this.hasFeature(RadixFeatures.PlaneSelectionYZ)) {
  268. let dist = this.intersectMeshes(pos, "planeYZ", closest);
  269. if (dist < closest) {
  270. closest = dist;
  271. hit = RadixFeatures.PlaneSelectionYZ;
  272. }
  273. }
  274. // Rotation
  275. if (this.hasFeature(RadixFeatures.RotationX)) {
  276. let dist = this.intersectMeshes(pos, "rotationX", closest);
  277. if (dist < closest) {
  278. closest = dist;
  279. hit = RadixFeatures.RotationX;
  280. }
  281. }
  282. if (this.hasFeature(RadixFeatures.RotationY)) {
  283. let dist = this.intersectMeshes(pos, "rotationY", closest);
  284. if (dist < closest) {
  285. closest = dist;
  286. hit = RadixFeatures.RotationY;
  287. }
  288. }
  289. if (this.hasFeature(RadixFeatures.RotationZ)) {
  290. let dist = this.intersectMeshes(pos, "rotationZ", closest);
  291. if (dist < closest) {
  292. closest = dist;
  293. hit = RadixFeatures.RotationZ;
  294. }
  295. }
  296. return hit;
  297. }
  298. /**
  299. * Set the world coordinate of where the Axis should be displayed
  300. * @param position the position
  301. * @param rotation the rotation quaternion
  302. * @param scale the scale (should be uniform)
  303. */
  304. setWorld(position: Vector3, rotation: Quaternion, scale: Vector3) {
  305. this._rootMesh.position = position;
  306. this._rootMesh.rotationQuaternion = rotation;
  307. this._rootMesh.scaling = scale;
  308. }
  309. /**
  310. * Display the Radix on screen
  311. */
  312. show() {
  313. this.setVisibleState(this._rootMesh, true);
  314. }
  315. /**
  316. * Hide the Radix from the screen
  317. */
  318. hide() {
  319. this.setVisibleState(this._rootMesh, false);
  320. }
  321. private setVisibleState(mesh: AbstractMesh, state: boolean) {
  322. mesh.isVisible = state;
  323. mesh.getChildMeshes(true).forEach(m => this.setVisibleState(m, state));
  324. }
  325. private intersectMeshes(pos: Vector2, startName: string, currentClosest: number): number {
  326. let meshes = this._rootMesh.getChildMeshes(true, m => m.name.indexOf(startName) === 0);
  327. for (var mesh of meshes) {
  328. var ray = this._scene.createPickingRay(pos.x, pos.y, mesh.getWorldMatrix(), this._scene.activeCamera);
  329. var pi = mesh.intersects(ray, false);
  330. if (pi.hit && pi.distance < currentClosest) {
  331. currentClosest = pi.distance;
  332. }
  333. }
  334. return currentClosest;
  335. }
  336. private constructGraphicalObjects() {
  337. var hp = Math.PI / 2;
  338. if (this.hasFeature(RadixFeatures.ArrowX)) {
  339. this.constructArrow(RadixFeatures.ArrowX, "arrowX", Matrix.RotationZ(-hp));
  340. }
  341. if (this.hasFeature(RadixFeatures.ArrowY)) {
  342. this.constructArrow(RadixFeatures.ArrowY, "arrowY", Matrix.Identity());
  343. }
  344. if (this.hasFeature(RadixFeatures.ArrowZ)) {
  345. this.constructArrow(RadixFeatures.ArrowZ, "arrowZ", Matrix.RotationX(hp));
  346. }
  347. if (this.hasFeature(RadixFeatures.PlaneSelectionXY)) {
  348. this.constructPlaneSelection(RadixFeatures.PlaneSelectionXY, "planeXY", Matrix.Identity());
  349. }
  350. if (this.hasFeature(RadixFeatures.PlaneSelectionXZ)) {
  351. this.constructPlaneSelection(RadixFeatures.PlaneSelectionXZ, "planeXZ", Matrix.RotationX(hp));
  352. }
  353. if (this.hasFeature(RadixFeatures.PlaneSelectionYZ)) {
  354. this.constructPlaneSelection(RadixFeatures.PlaneSelectionYZ, "planeYZ", Matrix.RotationY(-hp));
  355. }
  356. if (this.hasFeature(RadixFeatures.RotationX)) {
  357. this.constructRotation(RadixFeatures.RotationX, "rotationX", Matrix.RotationZ(-hp));
  358. }
  359. if (this.hasFeature(RadixFeatures.RotationY)) {
  360. this.constructRotation(RadixFeatures.RotationY, "rotationY", Matrix.Identity());
  361. }
  362. if (this.hasFeature(RadixFeatures.RotationZ)) {
  363. this.constructRotation(RadixFeatures.RotationZ, "rotationZ", Matrix.RotationX(hp));
  364. }
  365. }
  366. private constructArrow(feature: RadixFeatures, name: string, transform: Matrix) {
  367. let mtl = this.getMaterial(name);
  368. let hasRot;
  369. switch (feature) {
  370. case RadixFeatures.ArrowX:
  371. hasRot = this.hasFeature(RadixFeatures.RotationX);
  372. break;
  373. case RadixFeatures.ArrowY:
  374. hasRot = this.hasFeature(RadixFeatures.RotationY);
  375. break;
  376. case RadixFeatures.ArrowZ:
  377. hasRot = this.hasFeature(RadixFeatures.RotationZ);
  378. break;
  379. }
  380. let rotation = Quaternion.FromRotationMatrix(transform);
  381. let points = new Array<number>();
  382. points.push(0, hasRot ? this._coneLength : 0, 0);
  383. points.push(0, this._arrowLength - this._coneLength, 0);
  384. let wireMesh = new LinesMesh(name + "Wire", this._scene);
  385. wireMesh.rotationQuaternion = rotation;
  386. wireMesh.parent = this._rootMesh;
  387. wireMesh.color = mtl.diffuseColor;
  388. wireMesh.renderingGroupId = 1;
  389. wireMesh.intersectionThreshold = this.wireSelectionThreshold;
  390. wireMesh.isPickable = false;
  391. var vd = new VertexData();
  392. vd.positions = points;
  393. vd.indices = [0, 1];
  394. vd.applyToMesh(wireMesh);
  395. let arrow = Mesh.CreateCylinder(name + "Cone", this._coneLength, 0, this._coneRadius, 18, 1, this._scene, false);
  396. arrow.position = Vector3.TransformCoordinates(new Vector3(0, this._arrowLength - (this._coneLength / 2), 0), transform);
  397. arrow.rotationQuaternion = rotation;
  398. arrow.material = mtl;
  399. arrow.parent = this._rootMesh;
  400. arrow.renderingGroupId = 1;
  401. arrow.isPickable = false;
  402. this.addSymbolicMeshToLit(arrow);
  403. }
  404. private constructPlaneSelection(feature: RadixFeatures, name: string, transform: Matrix) {
  405. let mtl = this.getMaterial(name);
  406. let points = new Array<Vector3>();
  407. points.push(new Vector3(this._arrowLength - this._planeSelectionLength, this._arrowLength, 0));
  408. points.push(new Vector3(this._arrowLength, this._arrowLength, 0));
  409. points.push(new Vector3(this._arrowLength, this._arrowLength - this._planeSelectionLength, 0));
  410. let wireMesh = Mesh.CreateLines(name + "Plane", points, this._scene);
  411. wireMesh.parent = this._rootMesh;
  412. wireMesh.color = mtl.diffuseColor;
  413. wireMesh.rotationQuaternion = Quaternion.FromRotationMatrix(transform);
  414. wireMesh.renderingGroupId = 1;
  415. wireMesh.intersectionThreshold = this.wireSelectionThreshold;
  416. wireMesh.isPickable = false;
  417. }
  418. private constructRotation(feature: RadixFeatures, name: string, transform: Matrix) {
  419. let mtl = this.getMaterial(name);
  420. var rotCyl = Mesh.CreateCylinder(name + "Cylinder", this._coneLength, this._coneRadius, this._coneRadius, 18, 1, this._scene, false);
  421. rotCyl.material = mtl;
  422. rotCyl.position = Vector3.TransformCoordinates(new Vector3(0, this._coneLength / 2, 0), transform);
  423. rotCyl.rotationQuaternion = Quaternion.FromRotationMatrix(transform);
  424. rotCyl.parent = this._rootMesh;
  425. rotCyl.renderingGroupId = 1;
  426. rotCyl.isPickable = false;
  427. this.addSymbolicMeshToLit(rotCyl);
  428. }
  429. private addSymbolicMeshToLit(mesh: AbstractMesh) {
  430. this._light1.includedOnlyMeshes.push(mesh);
  431. this._light2.includedOnlyMeshes.push(mesh);
  432. this._scene.lights.map(l => { if ((l !== this._light1) && (l !== this._light2)) l.excludedMeshes.push(mesh) });
  433. }
  434. private hasFeature(value: RadixFeatures): boolean {
  435. return (this._features & value) !== 0;
  436. }
  437. private hasHighlightedFeature(value: RadixFeatures): boolean {
  438. return (this._highlighted & value) !== 0;
  439. }
  440. private updateMaterial(name: string, color: Color3) {
  441. let mtl = this.getMaterial(name);
  442. if (mtl) {
  443. mtl.diffuseColor = color;
  444. }
  445. }
  446. private updateMaterialFromHighlighted(feature: RadixFeatures, highlighted: RadixFeatures, name: string) {
  447. if (!this.hasFeature(feature)) {
  448. return;
  449. }
  450. if ((this._highlighted & feature) !== (highlighted & feature)) {
  451. let mtl = this.getMaterial(name);
  452. if ((highlighted&feature) !== 0) {
  453. mtl.diffuseColor.r *= 1.8;
  454. mtl.diffuseColor.g *= 1.8;
  455. mtl.diffuseColor.b *= 1.8;
  456. } else {
  457. mtl.diffuseColor.r /= 1.8;
  458. mtl.diffuseColor.g /= 1.8;
  459. mtl.diffuseColor.b /= 1.8;
  460. }
  461. }
  462. }
  463. private getMaterial(name: string): StandardMaterial {
  464. var mtl = <StandardMaterial>this._materials[name];
  465. return mtl;
  466. }
  467. private _arrowLength: number;
  468. private _coneLength: number;
  469. private _coneRadius: number;
  470. private _planeSelectionLength: number;
  471. private _light1: PointLight;
  472. private _light2: PointLight;
  473. private _rootMesh: Mesh;
  474. private _features: RadixFeatures;
  475. private _scene: Scene;
  476. private _materials;
  477. private _wireSelectionThreshold: number;
  478. private _xArrowColor: Color3;
  479. private _yArrowColor: Color3;
  480. private _zArrowColor: Color3;
  481. private _xyPlaneSelectionColor: Color3;
  482. private _xzPlaneSelectionColor: Color3;
  483. private _yzPlaneSelectionColor: Color3;
  484. private _highlighted: RadixFeatures;
  485. }
  486. }