textureLineComponent.tsx 8.7 KB

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