toolsTabComponent.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import * as React from "react";
  2. import { PaneComponent, IPaneComponentProps } from "../paneComponent";
  3. import { LineContainerComponent } from "../lineContainerComponent";
  4. import { ButtonLineComponent } from "../lines/buttonLineComponent";
  5. import { VideoRecorder, Nullable } from "babylonjs";
  6. import { GLTFComponent } from "./tools/gltfComponent";
  7. export class ToolsTabComponent extends PaneComponent {
  8. private _videoRecorder: Nullable<VideoRecorder>;
  9. constructor(props: IPaneComponentProps) {
  10. super(props);
  11. this.state = { tag: "Record video" };
  12. }
  13. componentWillMount() {
  14. if (!(BABYLON as any).GLTF2Export) {
  15. BABYLON.Tools.LoadScript("https://preview.babylonjs.com/serializers/babylonjs.serializers.min.js", () => {
  16. });
  17. return;
  18. }
  19. }
  20. componentWillUnmount() {
  21. if (this._videoRecorder) {
  22. this._videoRecorder.stopRecording();
  23. this._videoRecorder.dispose();
  24. this._videoRecorder = null;
  25. }
  26. }
  27. captureScreenshot() {
  28. const scene = this.props.scene;
  29. if (scene.activeCamera) {
  30. BABYLON.Tools.CreateScreenshotUsingRenderTarget(scene.getEngine(), scene.activeCamera, { precision: 1.0 }, undefined, undefined, 4, true);
  31. }
  32. }
  33. recordVideo() {
  34. if (this._videoRecorder && this._videoRecorder.isRecording) {
  35. this._videoRecorder.stopRecording();
  36. return;
  37. }
  38. const scene = this.props.scene;
  39. if (!this._videoRecorder) {
  40. this._videoRecorder = new BABYLON.VideoRecorder(scene.getEngine());
  41. }
  42. this._videoRecorder.startRecording().then(() => {
  43. this.setState({ tag: "Record video" })
  44. });
  45. this.setState({ tag: "Stop recording" })
  46. }
  47. render() {
  48. const scene = this.props.scene;
  49. if (!scene) {
  50. return null;
  51. }
  52. return (
  53. <div className="pane">
  54. <LineContainerComponent title="CAPTURE">
  55. <ButtonLineComponent label="Screenshot" onClick={() => this.captureScreenshot()} />
  56. <ButtonLineComponent label={this.state.tag} onClick={() => this.recordVideo()} />
  57. </LineContainerComponent>
  58. <GLTFComponent scene={scene} globalState={this.props.globalState!}/>
  59. </div>
  60. );
  61. }
  62. }