meshPropertyGridComponent.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import * as React from "react";
  2. import { Mesh, Observable } from "babylonjs";
  3. import { PropertyChangedEvent } from "../../../../propertyChangedEvent";
  4. import { LineContainerComponent } from "../../../lineContainerComponent";
  5. import { TextLineComponent } from "../../../lines/textLineComponent";
  6. import { CheckBoxLineComponent } from "../../../lines/checkBoxLineComponent";
  7. import { Vector3LineComponent } from "../../../lines/vector3LineComponent";
  8. import { SliderLineComponent } from "../../../lines/sliderLineComponent";
  9. import { QuaternionLineComponent } from "../../../lines/quaternionLineComponent";
  10. import { AxesViewerComponent } from "./axesViewerComponent";
  11. import { FloatLineComponent } from "../../../lines/floatLineComponent";
  12. interface IMeshPropertyGridComponentProps {
  13. mesh: Mesh,
  14. onSelectionChangedObservable?: Observable<any>,
  15. onPropertyChangedObservable?: Observable<PropertyChangedEvent>
  16. }
  17. export class MeshPropertyGridComponent extends React.Component<IMeshPropertyGridComponentProps, { displayNormals: boolean, renderNormalVectors: boolean }> {
  18. constructor(props: IMeshPropertyGridComponentProps) {
  19. super(props);
  20. const mesh = this.props.mesh;
  21. this.state = { displayNormals: false, renderNormalVectors: mesh.metadata && mesh.metadata.normalLines }
  22. }
  23. renderNormalVectors() {
  24. const mesh = this.props.mesh;
  25. const scene = mesh.getScene();
  26. if (mesh.metadata && mesh.metadata.normalLines) {
  27. mesh.metadata.normalLines.dispose();
  28. mesh.metadata.normalLines = null;
  29. this.setState({ renderNormalVectors: false });
  30. return;
  31. }
  32. var normals = mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  33. var positions = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  34. const color = BABYLON.Color3.White();
  35. const size = mesh.getBoundingInfo().diagonalLength * 0.05;
  36. var lines = [];
  37. for (var i = 0; i < normals!.length; i += 3) {
  38. var v1 = BABYLON.Vector3.FromArray(positions!, i);
  39. var v2 = v1.add(BABYLON.Vector3.FromArray(normals!, i).scaleInPlace(size));
  40. lines.push([v1, v2]);
  41. }
  42. var normalLines = BABYLON.MeshBuilder.CreateLineSystem("normalLines", { lines: lines }, scene);
  43. normalLines.color = color;
  44. normalLines.parent = mesh;
  45. if (!mesh.metadata) {
  46. mesh.metadata = {};
  47. }
  48. mesh.metadata.normalLines = normalLines;
  49. this.setState({ renderNormalVectors: true });
  50. }
  51. displayNormals() {
  52. const mesh = this.props.mesh;
  53. const scene = mesh.getScene();
  54. if (!mesh.material) {
  55. return;
  56. }
  57. if (mesh.material.getClassName() === "NormalMaterial") {
  58. mesh.material.dispose();
  59. mesh.material = mesh.metadata.originalMaterial;
  60. mesh.metadata.originalMaterial = null;
  61. this.setState({ displayNormals: false });
  62. } else {
  63. if (!(BABYLON as any).NormalMaterial) {
  64. this.setState({ displayNormals: true });
  65. BABYLON.Tools.LoadScript("https://preview.babylonjs.com/materialsLibrary/babylonjs.materials.js", () => {
  66. this.displayNormals();
  67. });
  68. return;
  69. }
  70. if (!mesh.metadata) {
  71. mesh.metadata = {};
  72. }
  73. mesh.metadata.originalMaterial = mesh.material;
  74. const normalMaterial = new (BABYLON as any).NormalMaterial("normalMaterial", scene);
  75. normalMaterial.disableLighting = true;
  76. normalMaterial.sideOrientation = mesh.material.sideOrientation;
  77. normalMaterial.metadata = { hidden: true };
  78. mesh.material = normalMaterial;
  79. this.setState({ displayNormals: true });
  80. }
  81. }
  82. onMaterialLink() {
  83. if (!this.props.onSelectionChangedObservable) {
  84. return;
  85. }
  86. const mesh = this.props.mesh;
  87. this.props.onSelectionChangedObservable.notifyObservers(mesh.material)
  88. }
  89. convertPhysicsTypeToString(): string {
  90. const mesh = this.props.mesh;
  91. switch (mesh.physicsImpostor!.type) {
  92. case BABYLON.PhysicsImpostor.NoImpostor:
  93. return "No impostor";
  94. case BABYLON.PhysicsImpostor.SphereImpostor:
  95. return "Sphere";
  96. case BABYLON.PhysicsImpostor.BoxImpostor:
  97. return "Box";
  98. case BABYLON.PhysicsImpostor.PlaneImpostor:
  99. return "Plane";
  100. case BABYLON.PhysicsImpostor.MeshImpostor:
  101. return "Mesh";
  102. case BABYLON.PhysicsImpostor.CylinderImpostor:
  103. return "Cylinder";
  104. case BABYLON.PhysicsImpostor.ParticleImpostor:
  105. return "Particle";
  106. case BABYLON.PhysicsImpostor.HeightmapImpostor:
  107. return "Heightmap";
  108. }
  109. return "Unknown";
  110. }
  111. render() {
  112. const mesh = this.props.mesh;
  113. const scene = mesh.getScene();
  114. const displayNormals = mesh.material != null && mesh.material.getClassName() === "NormalMaterial";
  115. const renderNormalVectors = (mesh.metadata && mesh.metadata.normalLines) ? true : false;
  116. return (
  117. <div className="pane">
  118. <LineContainerComponent title="GENERAL">
  119. <TextLineComponent label="ID" value={mesh.id} />
  120. <TextLineComponent label="Unique ID" value={mesh.uniqueId.toString()} />
  121. <TextLineComponent label="Class" value={mesh.getClassName()} />
  122. <TextLineComponent label="Vertices" value={mesh.getTotalVertices().toString()} />
  123. <TextLineComponent label="Faces" value={(mesh.getTotalIndices() / 3).toFixed(0)} />
  124. <TextLineComponent label="Sub-meshes" value={mesh.subMeshes ? mesh.subMeshes.length.toString() : "0"} />
  125. <TextLineComponent label="Has skeleton" value={mesh.skeleton ? "Yes" : "No"} />
  126. <CheckBoxLineComponent label="IsEnabled" isSelected={() => mesh.isEnabled()} onSelect={(value) => mesh.setEnabled(value)} />
  127. <CheckBoxLineComponent label="IsPickable" target={mesh} propertyName="isPickable" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  128. {
  129. mesh.material &&
  130. <TextLineComponent label="Material" value={mesh.material.name} onLink={() => this.onMaterialLink()} />
  131. }
  132. </LineContainerComponent>
  133. <LineContainerComponent title="TRANSFORMS">
  134. <Vector3LineComponent label="Position" target={mesh} propertyName="position" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  135. {
  136. !mesh.rotationQuaternion &&
  137. <Vector3LineComponent label="Rotation" target={mesh} propertyName="rotation" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  138. }
  139. {
  140. mesh.rotationQuaternion &&
  141. <QuaternionLineComponent label="Rotation" target={mesh} propertyName="rotationQuaternion" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  142. }
  143. <Vector3LineComponent label="Scaling" target={mesh} propertyName="scaling" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  144. </LineContainerComponent>
  145. <LineContainerComponent title="DISPLAY" closed={true}>
  146. <SliderLineComponent label="Visibility" target={mesh} propertyName="visibility" minimum={0} maximum={1} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  147. <FloatLineComponent label="Alpha index" target={mesh} propertyName="alphaIndex" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  148. <CheckBoxLineComponent label="Receive shadows" target={mesh} propertyName="receiveShadows" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  149. {
  150. mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind) &&
  151. <CheckBoxLineComponent label="Use vertex colors" target={mesh} propertyName="useVertexColors" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  152. }
  153. {
  154. scene.fogMode !== BABYLON.Scene.FOGMODE_NONE &&
  155. <CheckBoxLineComponent label="Apply fog" target={mesh} propertyName="applyFog" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  156. }
  157. {
  158. !mesh.parent &&
  159. <CheckBoxLineComponent label="Infinite distance" target={mesh} propertyName="infiniteDistance" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  160. }
  161. </LineContainerComponent>
  162. <LineContainerComponent title="ADVANCED" closed={true}>
  163. {
  164. mesh.useBones &&
  165. <CheckBoxLineComponent label="Compute bones using shaders" target={mesh} propertyName="computeBonesUsingShaders" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  166. }
  167. <CheckBoxLineComponent label="Collisions" target={mesh} propertyName="checkCollisions" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  168. <TextLineComponent label="Has normals" value={mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind) ? "Yes" : "No"} />
  169. <TextLineComponent label="Has vertex colors" value={mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind) ? "Yes" : "No"} />
  170. <TextLineComponent label="has UV set 0" value={mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind) ? "Yes" : "No"} />
  171. <TextLineComponent label="has UV set 1" value={mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind) ? "Yes" : "No"} />
  172. <TextLineComponent label="has UV set 2" value={mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV3Kind) ? "Yes" : "No"} />
  173. <TextLineComponent label="has UV set 3" value={mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV4Kind) ? "Yes" : "No"} />
  174. <TextLineComponent label="has tangents" value={mesh.isVerticesDataPresent(BABYLON.VertexBuffer.TangentKind) ? "Yes" : "No"} />
  175. <TextLineComponent label="has matrix weights" value={mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind) ? "Yes" : "No"} />
  176. <TextLineComponent label="has matrix indices" value={mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) ? "Yes" : "No"} />
  177. </LineContainerComponent>
  178. {
  179. mesh.physicsImpostor != null &&
  180. <LineContainerComponent title="PHYSICS" closed={true}>
  181. <FloatLineComponent label="Mass" target={mesh.physicsImpostor} propertyName="mass" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  182. <FloatLineComponent label="Friction" target={mesh.physicsImpostor} propertyName="friction" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  183. <FloatLineComponent label="Restitution" target={mesh.physicsImpostor} propertyName="restitution" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  184. <TextLineComponent label="Type" value={this.convertPhysicsTypeToString()} />
  185. </LineContainerComponent>
  186. }
  187. <LineContainerComponent title="DEBUG" closed={true}>
  188. <CheckBoxLineComponent label="Show bounding box" target={mesh} propertyName="showBoundingBox" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  189. {
  190. mesh.material &&
  191. <CheckBoxLineComponent label="Display normals" isSelected={() => displayNormals} onSelect={() => this.displayNormals()} />
  192. }
  193. {
  194. mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind) &&
  195. <CheckBoxLineComponent label="Render vertex normals" isSelected={() => renderNormalVectors} onSelect={() => this.renderNormalVectors()} />
  196. }
  197. <AxesViewerComponent node={mesh} />
  198. </LineContainerComponent>
  199. </div>
  200. );
  201. }
  202. }