renderGridPropertyGridComponent.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import * as React from "react";
  2. import { Scene, AbstractMesh, Nullable, UtilityLayerRenderer, Tools, Mesh, Color3, Texture } from "babylonjs";
  3. import { CheckBoxLineComponent } from "../../lines/checkBoxLineComponent";
  4. interface IRenderGridPropertyGridComponentProps {
  5. scene: Scene;
  6. }
  7. export class RenderGridPropertyGridComponent extends React.Component<IRenderGridPropertyGridComponentProps, { isEnabled: boolean }> {
  8. private _gridMesh: Nullable<AbstractMesh>;
  9. constructor(props: IRenderGridPropertyGridComponentProps) {
  10. super(props);
  11. this.state = { isEnabled: false };
  12. }
  13. componentWillMount() {
  14. const scene = UtilityLayerRenderer.DefaultKeepDepthUtilityLayer.utilityLayerScene;
  15. for (var mesh of scene.meshes) {
  16. if (mesh.reservedDataStore && mesh.reservedDataStore.isInspectorGrid) {
  17. this._gridMesh = mesh;
  18. this.setState({ isEnabled: true });
  19. return;
  20. }
  21. }
  22. }
  23. addOrRemoveGrid() {
  24. const scene = UtilityLayerRenderer.DefaultKeepDepthUtilityLayer.utilityLayerScene;
  25. if (!(BABYLON as any).GridMaterial) {
  26. this.setState({ isEnabled: true });
  27. Tools.LoadScript("https://preview.babylonjs.com/materialsLibrary/babylonjs.materials.min.js", () => {
  28. this.addOrRemoveGrid();
  29. });
  30. return;
  31. }
  32. if (!this._gridMesh) {
  33. var extend = this.props.scene.getWorldExtends();
  34. var width = (extend.max.x - extend.min.x) * 5.0;
  35. var depth = (extend.max.z - extend.min.z) * 5.0;
  36. this._gridMesh = Mesh.CreateGround("grid", 1.0, 1.0, 1, scene);
  37. if (!this._gridMesh.reservedDataStore) {
  38. this._gridMesh.reservedDataStore = {};
  39. }
  40. this._gridMesh.scaling.x = Math.max(width, depth);
  41. this._gridMesh.scaling.z = this._gridMesh.scaling.x;
  42. this._gridMesh.reservedDataStore.isInspectorGrid = true;
  43. this._gridMesh.isPickable = false;
  44. var groundMaterial = new (BABYLON as any).GridMaterial("GridMaterial", scene);
  45. groundMaterial.majorUnitFrequency = 10;
  46. groundMaterial.minorUnitVisibility = 0.3;
  47. groundMaterial.gridRatio = 0.01;
  48. groundMaterial.backFaceCulling = false;
  49. groundMaterial.mainColor = new Color3(1, 1, 1);
  50. groundMaterial.lineColor = new Color3(1.0, 1.0, 1.0);
  51. groundMaterial.opacity = 0.8;
  52. groundMaterial.zOffset = 1.0;
  53. groundMaterial.opacityTexture = new Texture("https://assets.babylonjs.com/environments/backgroundGround.png", scene);
  54. this._gridMesh.material = groundMaterial;
  55. this.setState({ isEnabled: true });
  56. return;
  57. }
  58. this.setState({ isEnabled: !this.state.isEnabled });
  59. this._gridMesh.dispose(true, true);
  60. this._gridMesh = null;
  61. }
  62. render() {
  63. return (
  64. <div>
  65. <CheckBoxLineComponent label="Render grid" isSelected={() => this.state.isEnabled} onSelect={() => this.addOrRemoveGrid()} />
  66. </div>
  67. );
  68. }
  69. }