textureLineComponent.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import * as React from "react";
  2. import { Constants } from "babylonjs/Engines/constants";
  3. import { BaseTexture } from "babylonjs/Materials/Textures/baseTexture";
  4. import { Texture } from "babylonjs/Materials/Textures/texture";
  5. import { RenderTargetTexture } from "babylonjs/Materials/Textures/renderTargetTexture";
  6. import { PostProcess } from "babylonjs/PostProcesses/postProcess";
  7. import { PassPostProcess, PassCubePostProcess } from "babylonjs/PostProcesses/passPostProcess";
  8. import { GlobalState } from "../../../components/globalState";
  9. import { ButtonLineComponent } from './buttonLineComponent';
  10. interface ITextureLineComponentProps {
  11. texture: BaseTexture;
  12. width: number;
  13. height: number;
  14. globalState?: GlobalState;
  15. hideChannelSelect?: boolean;
  16. }
  17. enum ChannelToDisplay {
  18. R,
  19. G,
  20. B,
  21. A,
  22. All
  23. }
  24. export class TextureLineComponent extends React.Component<ITextureLineComponentProps, { channel: ChannelToDisplay, face: number }> {
  25. private canvasRef: React.RefObject<HTMLCanvasElement>;
  26. constructor(props: ITextureLineComponentProps) {
  27. super(props);
  28. this.state = {
  29. channel: ChannelToDisplay.All,
  30. face: 0
  31. };
  32. this.canvasRef = React.createRef();
  33. }
  34. shouldComponentUpdate(nextProps: ITextureLineComponentProps, nextState: { channel: ChannelToDisplay, face: number }): boolean {
  35. return (nextProps.texture !== this.props.texture || nextState.channel !== this.state.channel || nextState.face !== this.state.face);
  36. }
  37. componentDidMount() {
  38. this.updatePreview();
  39. }
  40. componentDidUpdate() {
  41. this.updatePreview();
  42. }
  43. updatePreview() {
  44. var texture = this.props.texture;
  45. if (!texture.isReady() && texture._texture) {
  46. texture._texture.onLoadedObservable.addOnce(() => {
  47. this.updatePreview();
  48. })
  49. }
  50. var scene = texture.getScene()!;
  51. var engine = scene.getEngine();
  52. var size = texture.getSize();
  53. var ratio = size.width / size.height;
  54. var width = this.props.width;
  55. var height = (width / ratio) | 1;
  56. let passPostProcess: PostProcess;
  57. if (!texture.isCube) {
  58. passPostProcess = new PassPostProcess("pass", 1, null, Texture.NEAREST_SAMPLINGMODE, engine, false, Constants.TEXTURETYPE_UNSIGNED_INT);
  59. } else {
  60. var passCubePostProcess = new PassCubePostProcess("pass", 1, null, Texture.NEAREST_SAMPLINGMODE, engine, false, Constants.TEXTURETYPE_UNSIGNED_INT);
  61. passCubePostProcess.face = this.state.face;
  62. passPostProcess = passCubePostProcess;
  63. }
  64. if (!passPostProcess.getEffect().isReady()) {
  65. // Try again later
  66. passPostProcess.dispose();
  67. setTimeout(() => this.updatePreview(), 250);
  68. return;
  69. }
  70. const previewCanvas = this.canvasRef.current as HTMLCanvasElement;
  71. if (this.props.globalState) {
  72. this.props.globalState.blockMutationUpdates = true;
  73. }
  74. let rtt = new RenderTargetTexture(
  75. "temp",
  76. { width: width, height: height },
  77. scene, false);
  78. passPostProcess.onApply = function(effect) {
  79. effect.setTexture("textureSampler", texture);
  80. };
  81. let internalTexture = rtt.getInternalTexture();
  82. if (internalTexture) {
  83. scene.postProcessManager.directRender([passPostProcess], internalTexture);
  84. // Read the contents of the framebuffer
  85. var numberOfChannelsByLine = width * 4;
  86. var halfHeight = height / 2;
  87. //Reading datas from WebGL
  88. var data = engine.readPixels(0, 0, width, height);
  89. if (!texture.isCube) {
  90. if (this.state.channel != ChannelToDisplay.All) {
  91. for (var i = 0; i < width * height * 4; i += 4) {
  92. switch (this.state.channel) {
  93. case ChannelToDisplay.R:
  94. data[i + 1] = data[i];
  95. data[i + 2] = data[i];
  96. data[i + 3] = 255;
  97. break;
  98. case ChannelToDisplay.G:
  99. data[i] = data[i + 1];
  100. data[i + 2] = data[i];
  101. data[i + 3] = 255;
  102. break;
  103. case ChannelToDisplay.B:
  104. data[i] = data[i + 2];
  105. data[i + 1] = data[i + 2];
  106. data[i + 3] = 255;
  107. break;
  108. case ChannelToDisplay.A:
  109. data[i] = data[i + 3];
  110. data[i + 1] = data[i + 3];
  111. data[i + 2] = data[i + 3];
  112. data[i + 3] = 255;
  113. break;
  114. }
  115. }
  116. }
  117. }
  118. //To flip image on Y axis.
  119. if ((texture as Texture).invertY || texture.isCube) {
  120. for (var i = 0; i < halfHeight; i++) {
  121. for (var j = 0; j < numberOfChannelsByLine; j++) {
  122. var currentCell = j + i * numberOfChannelsByLine;
  123. var targetLine = height - i - 1;
  124. var targetCell = j + targetLine * numberOfChannelsByLine;
  125. var temp = data[currentCell];
  126. data[currentCell] = data[targetCell];
  127. data[targetCell] = temp;
  128. }
  129. }
  130. }
  131. previewCanvas.width = width;
  132. previewCanvas.height = height;
  133. var context = previewCanvas.getContext('2d');
  134. if (context) {
  135. // Copy the pixels to the preview canvas
  136. var imageData = context.createImageData(width, height);
  137. var castData = imageData.data;
  138. castData.set(data);
  139. context.putImageData(imageData, 0, 0);
  140. }
  141. // Unbind
  142. engine.unBindFramebuffer(internalTexture);
  143. }
  144. rtt.dispose();
  145. passPostProcess.dispose();
  146. previewCanvas.style.height = height + "px";
  147. if (this.props.globalState) {
  148. this.props.globalState.blockMutationUpdates = false;
  149. }
  150. }
  151. render() {
  152. var texture = this.props.texture;
  153. return (
  154. <>
  155. <div className="textureLine">
  156. {
  157. !this.props.hideChannelSelect && texture.isCube &&
  158. <div className="control3D">
  159. <button className={this.state.face === 0 ? "px command selected" : "px command"} onClick={() => this.setState({ face: 0 })}>PX</button>
  160. <button className={this.state.face === 1 ? "nx command selected" : "nx command"} onClick={() => this.setState({ face: 1 })}>NX</button>
  161. <button className={this.state.face === 2 ? "py command selected" : "py command"} onClick={() => this.setState({ face: 2 })}>PY</button>
  162. <button className={this.state.face === 3 ? "ny command selected" : "ny command"} onClick={() => this.setState({ face: 3 })}>NY</button>
  163. <button className={this.state.face === 4 ? "pz command selected" : "pz command"} onClick={() => this.setState({ face: 4 })}>PZ</button>
  164. <button className={this.state.face === 5 ? "nz command selected" : "nz command"} onClick={() => this.setState({ face: 5 })}>NZ</button>
  165. </div>
  166. }
  167. {
  168. !this.props.hideChannelSelect && !texture.isCube &&
  169. <div className="control">
  170. <button className={this.state.channel === ChannelToDisplay.R ? "red command selected" : "red command"} onClick={() => this.setState({ channel: ChannelToDisplay.R })}>R</button>
  171. <button className={this.state.channel === ChannelToDisplay.G ? "green command selected" : "green command"} onClick={() => this.setState({ channel: ChannelToDisplay.G })}>G</button>
  172. <button className={this.state.channel === ChannelToDisplay.B ? "blue command selected" : "blue command"} onClick={() => this.setState({ channel: ChannelToDisplay.B })}>B</button>
  173. <button className={this.state.channel === ChannelToDisplay.A ? "alpha command selected" : "alpha command"} onClick={() => this.setState({ channel: ChannelToDisplay.A })}>A</button>
  174. <button className={this.state.channel === ChannelToDisplay.All ? "all command selected" : "all command"} onClick={() => this.setState({ channel: ChannelToDisplay.All })}>ALL</button>
  175. </div>
  176. }
  177. <canvas ref={this.canvasRef} className="preview" />
  178. </div>
  179. {
  180. texture.isRenderTarget &&
  181. <ButtonLineComponent label="Refresh" onClick={() => {
  182. this.updatePreview();
  183. }} />
  184. }
  185. </>
  186. );
  187. }
  188. }