Skip to content

Commit

Permalink
improve texture suport for nme #6012
Browse files Browse the repository at this point in the history
  • Loading branch information
deltakosh committed Sep 30, 2019
1 parent 1ed92f1 commit 347da21
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface ITextureLinkLineComponentProps {
onSelectionChangedObservable?: Observable<any>;
onDebugSelectionChangeObservable?: Observable<BaseTexture>;
propertyName?: string;
onTextureCreated?: (texture: BaseTexture) => void;
customDebugAction?: (state: boolean) => void
}

Expand Down Expand Up @@ -142,7 +143,11 @@ export class TextureLinkLineComponent extends React.Component<ITextureLinkLineCo

let texture = new Texture(url, material.getScene(), false, false);

(material as any)[this.props.propertyName!] = texture;
if (this.props.propertyName) {
(material as any)[this.props.propertyName!] = texture;
} else if (this.props.onTextureCreated) {
this.props.onTextureCreated(texture);
}

this.forceUpdate();

Expand All @@ -153,7 +158,7 @@ export class TextureLinkLineComponent extends React.Component<ITextureLinkLineCo
const texture = this.props.texture;

if (!texture) {
if (this.props.propertyName) {
if (this.props.propertyName || this.props.onTextureCreated) {
return (
<FileButtonLineComponent label={`Add ${this.props.label} texture`} onClick={(file) => this.updateTexture(file)} accept=".jpg, .png, .tga, .dds, .env" />
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ export class NodeMaterialPropertyGridComponent extends React.Component<INodeMate
{
textureBlocks.map((textureBlock, i) => {
return (
<TextureLinkLineComponent label={textureBlock.name} key={textureBlock.texture!.uniqueId + i} texture={textureBlock.texture} material={material} onSelectionChangedObservable={this.props.onSelectionChangedObservable} onDebugSelectionChangeObservable={onDebugSelectionChangeObservable} />
<TextureLinkLineComponent label={textureBlock.name}
key={i}
texture={textureBlock.texture}
material={material}
onTextureCreated={texture => textureBlock.texture = texture}
onSelectionChangedObservable={this.props.onSelectionChangedObservable}
onDebugSelectionChangeObservable={onDebugSelectionChangeObservable} />
)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ export class TexturePropertyTabComponent extends React.Component<ITexturePropert

let texture = this.props.node.texture as BaseTexture;

this.state = {isEmbedded: !texture || texture.name.substring(0, 4) !== "http", loadAsCubeTexture: texture && texture.isCube};
this.state = {isEmbedded: !texture || texture.name.substring(0, 4) === "data", loadAsCubeTexture: texture && texture.isCube};
}

UNSAFE_componentWillUpdate(nextProps: ITexturePropertyTabComponentProps, nextState: {isEmbedded: boolean, loadAsCubeTexture: boolean}) {
if (nextProps.node !== this.props.node) {
let texture = nextProps.node.texture as BaseTexture;

nextState.isEmbedded = !texture || texture.name.substring(0, 4) !== "http";
nextState.isEmbedded = !texture || texture.name.substring(0, 4) === "data";
nextState.loadAsCubeTexture = texture && texture.isCube;
}
}
Expand All @@ -55,6 +55,18 @@ export class TexturePropertyTabComponent extends React.Component<ITexturePropert
this.forceUpdate();
}

removeTexture() {
let texture = this.props.node.texture as BaseTexture;

if (texture) {
texture.dispose();
(texture as any) = null;
this.props.node.texture = null;
}

this.updateAfterTextureLoad();
}

/**
* Replaces the texture of the node
* @param file the file of the texture to use
Expand Down Expand Up @@ -263,7 +275,7 @@ export class TexturePropertyTabComponent extends React.Component<ITexturePropert
}
</LineContainerComponent>
<LineContainerComponent title="SOURCE">
<CheckBoxLineComponent label="Embed texture" isSelected={() => this.state.isEmbedded} onSelect={value => {
<CheckBoxLineComponent label="Embed static texture" isSelected={() => this.state.isEmbedded} onSelect={value => {
this.setState({isEmbedded: value});
this.props.node.texture = null;
this.updateAfterTextureLoad();
Expand All @@ -285,6 +297,10 @@ export class TexturePropertyTabComponent extends React.Component<ITexturePropert
!this.state.isEmbedded && url &&
<ButtonLineComponent label="Refresh" onClick={() => this.replaceTextureWithUrl(url + "?nocache=" + this._generateRandomForCache())}/>
}
{
texture &&
<ButtonLineComponent label="Remove" onClick={() => this.removeTexture()}/>
}
</LineContainerComponent>
</div>
);
Expand Down
2 changes: 2 additions & 0 deletions src/Materials/Node/Blocks/Dual/textureBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ export class TextureBlock extends NodeMaterialBlock {
}

if (!this.texture || !this.texture.getTextureMatrix) {
defines.setValue(this._defineName, false);
defines.setValue(this._mainUVDefineName, true);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Materials/Node/nodeMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ export class NodeMaterial extends PushMaterial {
return [];
}

return this._sharedData.textureBlocks.filter((tb) => tb.texture);
return this._sharedData.textureBlocks;
}

/**
Expand Down

0 comments on commit 347da21

Please sign in to comment.