toolsTabComponent.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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, TransformNode, PBRMaterial, StandardMaterial, BackgroundMaterial, EnvironmentTextureTools, CubeTexture } from "babylonjs";
  6. import { GLTFComponent } from "./tools/gltfComponent";
  7. import { GLTFData } from "babylonjs-serializers";
  8. export class ToolsTabComponent extends PaneComponent {
  9. private _videoRecorder: Nullable<VideoRecorder>;
  10. constructor(props: IPaneComponentProps) {
  11. super(props);
  12. this.state = { tag: "Record video" };
  13. }
  14. componentWillMount() {
  15. if (!(BABYLON as any).GLTF2Export) {
  16. BABYLON.Tools.LoadScript("https://preview.babylonjs.com/serializers/babylonjs.serializers.min.js", () => {
  17. });
  18. return;
  19. }
  20. }
  21. componentWillUnmount() {
  22. if (this._videoRecorder) {
  23. this._videoRecorder.stopRecording();
  24. this._videoRecorder.dispose();
  25. this._videoRecorder = null;
  26. }
  27. }
  28. captureScreenshot() {
  29. const scene = this.props.scene;
  30. if (scene.activeCamera) {
  31. BABYLON.Tools.CreateScreenshotUsingRenderTarget(scene.getEngine(), scene.activeCamera, { precision: 1.0 }, undefined, undefined, 4, true);
  32. }
  33. }
  34. recordVideo() {
  35. if (this._videoRecorder && this._videoRecorder.isRecording) {
  36. this._videoRecorder.stopRecording();
  37. return;
  38. }
  39. const scene = this.props.scene;
  40. if (!this._videoRecorder) {
  41. this._videoRecorder = new BABYLON.VideoRecorder(scene.getEngine());
  42. }
  43. this._videoRecorder.startRecording().then(() => {
  44. this.setState({ tag: "Record video" })
  45. });
  46. this.setState({ tag: "Stop recording" })
  47. }
  48. shouldExport(transformNode: TransformNode): boolean {
  49. // No skybox
  50. if (transformNode instanceof BABYLON.Mesh) {
  51. if (transformNode.material) {
  52. const material = transformNode.material as PBRMaterial | StandardMaterial | BackgroundMaterial;
  53. const reflectionTexture = material.reflectionTexture;
  54. if (reflectionTexture && reflectionTexture.coordinatesMode === BABYLON.Texture.SKYBOX_MODE) {
  55. return false;
  56. }
  57. }
  58. }
  59. return true;
  60. }
  61. exportGLTF() {
  62. const scene = this.props.scene;
  63. BABYLON.GLTF2Export.GLBAsync(scene, "scene", {
  64. shouldExportTransformNode: (transformNode) => this.shouldExport(transformNode)
  65. }).then((glb: GLTFData) => {
  66. glb.downloadFiles();
  67. });
  68. }
  69. exportBabylon() {
  70. const scene = this.props.scene;
  71. var strScene = JSON.stringify(BABYLON.SceneSerializer.Serialize(scene));
  72. var blob = new Blob([strScene], { type: "octet/stream" });
  73. BABYLON.Tools.Download(blob, "scene.babylon")
  74. }
  75. createEnvTexture() {
  76. const scene = this.props.scene;
  77. EnvironmentTextureTools.CreateEnvTextureAsync(scene.environmentTexture as CubeTexture)
  78. .then((buffer: ArrayBuffer) => {
  79. var blob = new Blob([buffer], { type: "octet/stream" });
  80. BABYLON.Tools.Download(blob, "environment.env");
  81. })
  82. .catch((error: any) => {
  83. console.error(error);
  84. });
  85. }
  86. render() {
  87. const scene = this.props.scene;
  88. if (!scene) {
  89. return null;
  90. }
  91. return (
  92. <div className="pane">
  93. <LineContainerComponent title="CAPTURE">
  94. <ButtonLineComponent label="Screenshot" onClick={() => this.captureScreenshot()} />
  95. <ButtonLineComponent label={this.state.tag} onClick={() => this.recordVideo()} />
  96. </LineContainerComponent>
  97. <LineContainerComponent title="SCENE EXPORT">
  98. <ButtonLineComponent label="Export to GLB" onClick={() => this.exportGLTF()} />
  99. <ButtonLineComponent label="Export to Babylon" onClick={() => this.exportBabylon()} />
  100. {
  101. !scene.getEngine().premultipliedAlpha && scene.environmentTexture && scene.activeCamera &&
  102. <ButtonLineComponent label="Generate .env texture" onClick={() => this.createEnvTexture()} />
  103. }
  104. </LineContainerComponent>
  105. <GLTFComponent scene={scene} globalState={this.props.globalState!} />
  106. </div>
  107. );
  108. }
  109. }