textureLinkLineComponent.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import * as React from "react";
  2. import { BaseTexture, Observable, Material, Observer, Nullable } from "babylonjs";
  3. import { TextLineComponent } from "./textLineComponent";
  4. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
  5. import { faWrench } from '@fortawesome/free-solid-svg-icons';
  6. export interface ITextureLinkLineComponentProps {
  7. label: string,
  8. texture: Nullable<BaseTexture>,
  9. material: Material,
  10. onSelectionChangeObservable?: Observable<any>,
  11. onDebugSelectionChangeObservable: Observable<BaseTexture>
  12. }
  13. export class TextureLinkLineComponent extends React.Component<ITextureLinkLineComponentProps, { isDebugSelected: boolean }> {
  14. private _onDebugSelectionChangeObserver: Nullable<Observer<BaseTexture>>;
  15. constructor(props: ITextureLinkLineComponentProps) {
  16. super(props);
  17. const material = this.props.material;
  18. const texture = this.props.texture;
  19. this.state = { isDebugSelected: material.metadata && material.metadata.debugTexture === texture };
  20. }
  21. componentWillMount() {
  22. this._onDebugSelectionChangeObserver = this.props.onDebugSelectionChangeObservable.add((texture) => {
  23. if (this.props.texture !== texture) {
  24. this.setState({ isDebugSelected: false });
  25. }
  26. });
  27. }
  28. componentWillUnmount() {
  29. if (this._onDebugSelectionChangeObserver) {
  30. this.props.onDebugSelectionChangeObservable.remove(this._onDebugSelectionChangeObserver);
  31. }
  32. }
  33. debugTexture() {
  34. const texture = this.props.texture;
  35. const material = this.props.material;
  36. const scene = material.getScene();
  37. if (material.metadata && material.metadata.debugTexture === texture) {
  38. const debugMaterial = material.metadata.debugMaterial;
  39. for (var mesh of scene.meshes) {
  40. if (mesh.material === debugMaterial) {
  41. mesh.material = material;
  42. }
  43. }
  44. debugMaterial.dispose();
  45. material.metadata.debugTexture = null;
  46. material.metadata.debugMaterial = null;
  47. this.setState({ isDebugSelected: false });
  48. return;
  49. }
  50. let checkMaterial = material;
  51. let needToDisposeCheckMaterial = false;
  52. if (material.metadata && material.metadata.debugTexture) {
  53. checkMaterial = material.metadata.debugMaterial;
  54. needToDisposeCheckMaterial = true;
  55. }
  56. var debugMaterial = new BABYLON.StandardMaterial("debugMaterial", scene);
  57. debugMaterial.disableLighting = true;
  58. debugMaterial.sideOrientation = material.sideOrientation;
  59. debugMaterial.emissiveTexture = texture!;
  60. debugMaterial.forceDepthWrite = true;
  61. debugMaterial.metadata = { hidden: true };
  62. for (var mesh of scene.meshes) {
  63. if (mesh.material === checkMaterial) {
  64. mesh.material = debugMaterial;
  65. }
  66. }
  67. if (!material.metadata) {
  68. material.metadata = {};
  69. }
  70. material.metadata.debugTexture = texture;
  71. material.metadata.debugMaterial = debugMaterial;
  72. this.props.onDebugSelectionChangeObservable.notifyObservers(texture!);
  73. if (needToDisposeCheckMaterial) {
  74. checkMaterial.dispose();
  75. }
  76. this.setState({ isDebugSelected: true });
  77. }
  78. onLink() {
  79. if (!this.props.onSelectionChangeObservable) {
  80. return;
  81. }
  82. const texture = this.props.texture;
  83. this.props.onSelectionChangeObservable.notifyObservers(texture!);
  84. }
  85. render() {
  86. const texture = this.props.texture;
  87. if (!texture) {
  88. return null;
  89. }
  90. return (
  91. <div className="textureLinkLine">
  92. {
  93. !texture.isCube &&
  94. <div className={this.state.isDebugSelected ? "debug selected" : "debug"} onClick={() => this.debugTexture()} title="Render as main texture">
  95. <FontAwesomeIcon icon={faWrench} />
  96. </div>
  97. }
  98. <TextLineComponent label={this.props.label} value={texture.name} onLink={() => this.onLink()} />
  99. </div>
  100. );
  101. }
  102. }