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