import * as React from "react"; import { Mesh, Observable } from "babylonjs"; import { PropertyChangedEvent } from "../../../../propertyChangedEvent"; import { LineContainerComponent } from "../../../lineContainerComponent"; import { TextLineComponent } from "../../../lines/textLineComponent"; import { CheckBoxLineComponent } from "../../../lines/checkBoxLineComponent"; import { Vector3LineComponent } from "../../../lines/vector3LineComponent"; import { SliderLineComponent } from "../../../lines/sliderLineComponent"; import { QuaternionLineComponent } from "../../../lines/quaternionLineComponent"; import { AxesViewerComponent } from "./axesViewerComponent"; import { FloatLineComponent } from "../../../lines/floatLineComponent"; import { LockObject } from "../lockObject"; interface IMeshPropertyGridComponentProps { mesh: Mesh, lockObject: LockObject, onSelectionChangedObservable?: Observable, onPropertyChangedObservable?: Observable } export class MeshPropertyGridComponent extends React.Component { constructor(props: IMeshPropertyGridComponentProps) { super(props); const mesh = this.props.mesh; this.state = { displayNormals: false, renderNormalVectors: mesh.metadata && mesh.metadata.normalLines } } renderNormalVectors() { const mesh = this.props.mesh; const scene = mesh.getScene(); if (mesh.metadata && mesh.metadata.normalLines) { mesh.metadata.normalLines.dispose(); mesh.metadata.normalLines = null; this.setState({ renderNormalVectors: false }); return; } var normals = mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind); var positions = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind); const color = BABYLON.Color3.White(); const size = mesh.getBoundingInfo().diagonalLength * 0.05; var lines = []; for (var i = 0; i < normals!.length; i += 3) { var v1 = BABYLON.Vector3.FromArray(positions!, i); var v2 = v1.add(BABYLON.Vector3.FromArray(normals!, i).scaleInPlace(size)); lines.push([v1, v2]); } var normalLines = BABYLON.MeshBuilder.CreateLineSystem("normalLines", { lines: lines }, scene); normalLines.color = color; normalLines.parent = mesh; if (!mesh.metadata) { mesh.metadata = {}; } mesh.metadata.normalLines = normalLines; this.setState({ renderNormalVectors: true }); } displayNormals() { const mesh = this.props.mesh; const scene = mesh.getScene(); if (!mesh.material) { return; } if (mesh.material.getClassName() === "NormalMaterial") { mesh.material.dispose(); mesh.material = mesh.metadata.originalMaterial; mesh.metadata.originalMaterial = null; this.setState({ displayNormals: false }); } else { if (!(BABYLON as any).NormalMaterial) { this.setState({ displayNormals: true }); BABYLON.Tools.LoadScript("https://preview.babylonjs.com/materialsLibrary/babylonjs.materials.js", () => { this.displayNormals(); }); return; } if (!mesh.metadata) { mesh.metadata = {}; } mesh.metadata.originalMaterial = mesh.material; const normalMaterial = new (BABYLON as any).NormalMaterial("normalMaterial", scene); normalMaterial.disableLighting = true; normalMaterial.sideOrientation = mesh.material.sideOrientation; normalMaterial.metadata = { hidden: true }; mesh.material = normalMaterial; this.setState({ displayNormals: true }); } } onMaterialLink() { if (!this.props.onSelectionChangedObservable) { return; } const mesh = this.props.mesh; this.props.onSelectionChangedObservable.notifyObservers(mesh.material) } convertPhysicsTypeToString(): string { const mesh = this.props.mesh; switch (mesh.physicsImpostor!.type) { case BABYLON.PhysicsImpostor.NoImpostor: return "No impostor"; case BABYLON.PhysicsImpostor.SphereImpostor: return "Sphere"; case BABYLON.PhysicsImpostor.BoxImpostor: return "Box"; case BABYLON.PhysicsImpostor.PlaneImpostor: return "Plane"; case BABYLON.PhysicsImpostor.MeshImpostor: return "Mesh"; case BABYLON.PhysicsImpostor.CylinderImpostor: return "Cylinder"; case BABYLON.PhysicsImpostor.ParticleImpostor: return "Particle"; case BABYLON.PhysicsImpostor.HeightmapImpostor: return "Heightmap"; } return "Unknown"; } render() { const mesh = this.props.mesh; const scene = mesh.getScene(); const displayNormals = mesh.material != null && mesh.material.getClassName() === "NormalMaterial"; const renderNormalVectors = (mesh.metadata && mesh.metadata.normalLines) ? true : false; return (
mesh.isEnabled()} onSelect={(value) => mesh.setEnabled(value)} /> { mesh.material && this.onMaterialLink()} /> } { !mesh.rotationQuaternion && } { mesh.rotationQuaternion && } { mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind) && } { scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && } { !mesh.parent && } { mesh.useBones && } { mesh.physicsImpostor != null && } { mesh.material && displayNormals} onSelect={() => this.displayNormals()} /> } { mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind) && renderNormalVectors} onSelect={() => this.renderNormalVectors()} /> }
); } }