textureLineComponent.tsx 9.4 KB

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