textureLineComponent.tsx 9.3 KB

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