import * as React from "react"; import { Texture, PostProcess } from "babylonjs"; interface ITextureLineComponentProps { texture: Texture, width: number, height: number } export class TextureLineComponent extends React.Component { constructor(props: ITextureLineComponentProps) { super(props); this.state = { displayRed: true, displayGreen: true, displayBlue: true, displayAlpha: true, face: 0 } } componentDidMount() { this.updatePreview(); } componentDidUpdate() { this.updatePreview(); } updatePreview() { var texture = this.props.texture; var scene = texture.getScene()!; var engine = scene.getEngine(); var size = texture.getSize(); var ratio = size.width / size.height var width = this.props.width; var height = (width / ratio) | 0; let passPostProcess: PostProcess; if (!texture.isCube) { passPostProcess = new BABYLON.PassPostProcess("pass", 1, null, BABYLON.Texture.NEAREST_SAMPLINGMODE, engine, false, BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT); } else { var passCubePostProcess = new BABYLON.PassCubePostProcess("pass", 1, null, BABYLON.Texture.NEAREST_SAMPLINGMODE, engine, false, BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT); passCubePostProcess.face = this.state.face; passPostProcess = passCubePostProcess; } if (!passPostProcess.getEffect().isReady()) { // Try again later passPostProcess.dispose(); setTimeout(() => this.updatePreview(), 250); return; } const previewCanvas = this.refs.canvas as HTMLCanvasElement; let rtt = new BABYLON.RenderTargetTexture( "temp", { width: width, height: height }, scene, false); passPostProcess.onApply = function(effect) { effect.setTexture("textureSampler", texture); }; let internalTexture = rtt.getInternalTexture(); if (internalTexture) { scene.postProcessManager.directRender([passPostProcess], internalTexture); // Read the contents of the framebuffer var numberOfChannelsByLine = width * 4; var halfHeight = height / 2; //Reading datas from WebGL var data = engine.readPixels(0, 0, width, height); if (!texture.isCube) { if (!this.state.displayRed || !this.state.displayGreen || !this.state.displayBlue) { for (var i = 0; i < width * height * 4; i += 4) { if (!this.state.displayRed) { data[i] = 0; } if (!this.state.displayGreen) { data[i + 1] = 0; } if (!this.state.displayBlue) { data[i + 2] = 0; } if (this.state.displayAlpha) { var alpha = data[i + 2]; data[i] = alpha; data[i + 1] = alpha; data[i + 2] = alpha; data[i + 2] = 0; } } } } //To flip image on Y axis. if (texture.invertY || texture.isCube) { for (var i = 0; i < halfHeight; i++) { for (var j = 0; j < numberOfChannelsByLine; j++) { var currentCell = j + i * numberOfChannelsByLine; var targetLine = height - i - 1; var targetCell = j + targetLine * numberOfChannelsByLine; var temp = data[currentCell]; data[currentCell] = data[targetCell]; data[targetCell] = temp; } } } previewCanvas.width = width; previewCanvas.height = height; var context = previewCanvas.getContext('2d'); if (context) { // Copy the pixels to the preview canvas var imageData = context.createImageData(width, height); var castData = imageData.data; castData.set(data); context.putImageData(imageData, 0, 0); } // Unbind engine.unBindFramebuffer(internalTexture); } rtt.dispose(); passPostProcess.dispose(); previewCanvas.style.height = height + "px"; } render() { var texture = this.props.texture; return (
{ texture.isCube &&
} { !texture.isCube &&
}
); } }