textureLineComponent.tsx 7.7 KB

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