|
@@ -1,14 +1,10 @@
|
|
import * as React from "react";
|
|
import * as React from "react";
|
|
|
|
|
|
-import { Constants } from "babylonjs/Engines/constants";
|
|
|
|
import { BaseTexture } from "babylonjs/Materials/Textures/baseTexture";
|
|
import { BaseTexture } from "babylonjs/Materials/Textures/baseTexture";
|
|
-import { Texture } from "babylonjs/Materials/Textures/texture";
|
|
|
|
-import { RenderTargetTexture } from "babylonjs/Materials/Textures/renderTargetTexture";
|
|
|
|
-import { PostProcess } from "babylonjs/PostProcesses/postProcess";
|
|
|
|
-import { PassPostProcess, PassCubePostProcess } from "babylonjs/PostProcesses/passPostProcess";
|
|
|
|
|
|
|
|
import { GlobalState } from "../../../components/globalState";
|
|
import { GlobalState } from "../../../components/globalState";
|
|
import { ButtonLineComponent } from './buttonLineComponent';
|
|
import { ButtonLineComponent } from './buttonLineComponent';
|
|
|
|
+import { TextureHelper, TextureChannelToDisplay } from '../../../textureHelper';
|
|
|
|
|
|
interface ITextureLineComponentProps {
|
|
interface ITextureLineComponentProps {
|
|
texture: BaseTexture;
|
|
texture: BaseTexture;
|
|
@@ -18,29 +14,22 @@ interface ITextureLineComponentProps {
|
|
hideChannelSelect?: boolean;
|
|
hideChannelSelect?: boolean;
|
|
}
|
|
}
|
|
|
|
|
|
-enum ChannelToDisplay {
|
|
|
|
- R,
|
|
|
|
- G,
|
|
|
|
- B,
|
|
|
|
- A,
|
|
|
|
- All
|
|
|
|
-}
|
|
|
|
|
|
|
|
-export class TextureLineComponent extends React.Component<ITextureLineComponentProps, { channel: ChannelToDisplay, face: number }> {
|
|
|
|
|
|
+export class TextureLineComponent extends React.Component<ITextureLineComponentProps, { channel: TextureChannelToDisplay, face: number }> {
|
|
private canvasRef: React.RefObject<HTMLCanvasElement>;
|
|
private canvasRef: React.RefObject<HTMLCanvasElement>;
|
|
|
|
|
|
constructor(props: ITextureLineComponentProps) {
|
|
constructor(props: ITextureLineComponentProps) {
|
|
super(props);
|
|
super(props);
|
|
|
|
|
|
this.state = {
|
|
this.state = {
|
|
- channel: ChannelToDisplay.All,
|
|
|
|
|
|
+ channel: TextureChannelToDisplay.All,
|
|
face: 0
|
|
face: 0
|
|
};
|
|
};
|
|
|
|
|
|
this.canvasRef = React.createRef();
|
|
this.canvasRef = React.createRef();
|
|
}
|
|
}
|
|
|
|
|
|
- shouldComponentUpdate(nextProps: ITextureLineComponentProps, nextState: { channel: ChannelToDisplay, face: number }): boolean {
|
|
|
|
|
|
+ shouldComponentUpdate(nextProps: ITextureLineComponentProps, nextState: { channel: TextureChannelToDisplay, face: number }): boolean {
|
|
return (nextProps.texture !== this.props.texture || nextState.channel !== this.state.channel || nextState.face !== this.state.face);
|
|
return (nextProps.texture !== this.props.texture || nextState.channel !== this.state.channel || nextState.face !== this.state.face);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -54,111 +43,13 @@ export class TextureLineComponent extends React.Component<ITextureLineComponentP
|
|
|
|
|
|
updatePreview() {
|
|
updatePreview() {
|
|
var texture = this.props.texture;
|
|
var texture = this.props.texture;
|
|
- if (!texture.isReady() && texture._texture) {
|
|
|
|
- texture._texture.onLoadedObservable.addOnce(() => {
|
|
|
|
- this.updatePreview();
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
- var scene = texture.getScene()!;
|
|
|
|
- var engine = scene.getEngine();
|
|
|
|
var size = texture.getSize();
|
|
var size = texture.getSize();
|
|
var ratio = size.width / size.height;
|
|
var ratio = size.width / size.height;
|
|
var width = this.props.width;
|
|
var width = this.props.width;
|
|
- var height = (width / ratio) | 1;
|
|
|
|
-
|
|
|
|
- let passPostProcess: PostProcess;
|
|
|
|
-
|
|
|
|
- if (!texture.isCube) {
|
|
|
|
- passPostProcess = new PassPostProcess("pass", 1, null, Texture.NEAREST_SAMPLINGMODE, engine, false, Constants.TEXTURETYPE_UNSIGNED_INT);
|
|
|
|
- } else {
|
|
|
|
- var passCubePostProcess = new PassCubePostProcess("pass", 1, null, Texture.NEAREST_SAMPLINGMODE, engine, false, Constants.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.canvasRef.current as HTMLCanvasElement;
|
|
|
|
-
|
|
|
|
- if (this.props.globalState) {
|
|
|
|
- this.props.globalState.blockMutationUpdates = true;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- let rtt = new 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.channel != ChannelToDisplay.All) {
|
|
|
|
- for (var i = 0; i < width * height * 4; i += 4) {
|
|
|
|
-
|
|
|
|
- switch (this.state.channel) {
|
|
|
|
- case ChannelToDisplay.R:
|
|
|
|
- data[i + 1] = data[i];
|
|
|
|
- data[i + 2] = data[i];
|
|
|
|
- data[i + 3] = 255;
|
|
|
|
- break;
|
|
|
|
- case ChannelToDisplay.G:
|
|
|
|
- data[i] = data[i + 1];
|
|
|
|
- data[i + 2] = data[i];
|
|
|
|
- data[i + 3] = 255;
|
|
|
|
- break;
|
|
|
|
- case ChannelToDisplay.B:
|
|
|
|
- data[i] = data[i + 2];
|
|
|
|
- data[i + 1] = data[i + 2];
|
|
|
|
- data[i + 3] = 255;
|
|
|
|
- break;
|
|
|
|
- case ChannelToDisplay.A:
|
|
|
|
- data[i] = data[i + 3];
|
|
|
|
- data[i + 1] = data[i + 3];
|
|
|
|
- data[i + 2] = data[i + 3];
|
|
|
|
- data[i + 3] = 255;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //To flip image on Y axis.
|
|
|
|
- if ((texture as 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;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ var height = (width / ratio) | 1;
|
|
|
|
|
|
|
|
+ TextureHelper.GetTextureDataAsync(texture, width, height, this.state.face, this.state.channel, this.props.globalState).then(data => {
|
|
|
|
+ const previewCanvas = this.canvasRef.current as HTMLCanvasElement;
|
|
previewCanvas.width = width;
|
|
previewCanvas.width = width;
|
|
previewCanvas.height = height;
|
|
previewCanvas.height = height;
|
|
var context = previewCanvas.getContext('2d');
|
|
var context = previewCanvas.getContext('2d');
|
|
@@ -170,19 +61,8 @@ export class TextureLineComponent extends React.Component<ITextureLineComponentP
|
|
castData.set(data);
|
|
castData.set(data);
|
|
context.putImageData(imageData, 0, 0);
|
|
context.putImageData(imageData, 0, 0);
|
|
}
|
|
}
|
|
-
|
|
|
|
- // Unbind
|
|
|
|
- engine.unBindFramebuffer(internalTexture);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- rtt.dispose();
|
|
|
|
- passPostProcess.dispose();
|
|
|
|
-
|
|
|
|
- previewCanvas.style.height = height + "px";
|
|
|
|
- if (this.props.globalState) {
|
|
|
|
- this.props.globalState.blockMutationUpdates = false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ previewCanvas.style.height = height + "px";
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
render() {
|
|
render() {
|
|
@@ -205,11 +85,11 @@ export class TextureLineComponent extends React.Component<ITextureLineComponentP
|
|
{
|
|
{
|
|
!this.props.hideChannelSelect && !texture.isCube &&
|
|
!this.props.hideChannelSelect && !texture.isCube &&
|
|
<div className="control">
|
|
<div className="control">
|
|
- <button className={this.state.channel === ChannelToDisplay.R ? "red command selected" : "red command"} onClick={() => this.setState({ channel: ChannelToDisplay.R })}>R</button>
|
|
|
|
- <button className={this.state.channel === ChannelToDisplay.G ? "green command selected" : "green command"} onClick={() => this.setState({ channel: ChannelToDisplay.G })}>G</button>
|
|
|
|
- <button className={this.state.channel === ChannelToDisplay.B ? "blue command selected" : "blue command"} onClick={() => this.setState({ channel: ChannelToDisplay.B })}>B</button>
|
|
|
|
- <button className={this.state.channel === ChannelToDisplay.A ? "alpha command selected" : "alpha command"} onClick={() => this.setState({ channel: ChannelToDisplay.A })}>A</button>
|
|
|
|
- <button className={this.state.channel === ChannelToDisplay.All ? "all command selected" : "all command"} onClick={() => this.setState({ channel: ChannelToDisplay.All })}>ALL</button>
|
|
|
|
|
|
+ <button className={this.state.channel === TextureChannelToDisplay.R ? "red command selected" : "red command"} onClick={() => this.setState({ channel: TextureChannelToDisplay.R })}>R</button>
|
|
|
|
+ <button className={this.state.channel === TextureChannelToDisplay.G ? "green command selected" : "green command"} onClick={() => this.setState({ channel: TextureChannelToDisplay.G })}>G</button>
|
|
|
|
+ <button className={this.state.channel === TextureChannelToDisplay.B ? "blue command selected" : "blue command"} onClick={() => this.setState({ channel: TextureChannelToDisplay.B })}>B</button>
|
|
|
|
+ <button className={this.state.channel === TextureChannelToDisplay.A ? "alpha command selected" : "alpha command"} onClick={() => this.setState({ channel: TextureChannelToDisplay.A })}>A</button>
|
|
|
|
+ <button className={this.state.channel === TextureChannelToDisplay.All ? "all command selected" : "all command"} onClick={() => this.setState({ channel: TextureChannelToDisplay.All })}>ALL</button>
|
|
</div>
|
|
</div>
|
|
}
|
|
}
|
|
<canvas ref={this.canvasRef} className="preview" />
|
|
<canvas ref={this.canvasRef} className="preview" />
|