textureLineComponent.tsx 9.1 KB

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