Skip to content

Commit 9b0e75a

Browse files
committed
feat: auto select picked material from preview when available in MultiMaterial inspector
1 parent 749fe5d commit 9b0e75a

4 files changed

Lines changed: 24 additions & 2 deletions

File tree

editor/src/editor/layout/inspector/material/multi.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { loadImportedMaterial } from "../../preview/import/import";
1111

1212
import { registerUndoRedo } from "../../../../tools/undoredo";
1313

14+
import { Editor } from "../../../main";
15+
1416
import { EditorInspectorSectionField } from "../fields/section";
1517

1618
import { EditorSkyMaterialInspector } from "./sky";
@@ -26,6 +28,7 @@ import { EditorStandardMaterialInspector } from "./standard";
2628
import { EditorTriPlanarMaterialInspector } from "./tri-planar";
2729

2830
export interface IEditorPBRMaterialInspectorProps {
31+
editor: Editor;
2932
material: MultiMaterial;
3033
}
3134

@@ -37,8 +40,10 @@ export class EditorMultiMaterialInspector extends Component<IEditorPBRMaterialIn
3740
public constructor(props: IEditorPBRMaterialInspectorProps) {
3841
super(props);
3942

43+
const subMeshId = props.editor.layout.preview.lastPickingInfo?.subMeshId ?? 0;
44+
4045
this.state = {
41-
material: props.material.subMaterials[0] ?? null,
46+
material: props.material.subMaterials[subMeshId] ?? null,
4247
};
4348
}
4449

editor/src/editor/layout/inspector/mesh/mesh.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ export class EditorMeshInspector extends Component<IEditorInspectorImplementatio
413413
return <EditorNodeMaterialInspector mesh={this.props.object} material={this.props.object.material as NodeMaterial} />;
414414

415415
case "MultiMaterial":
416-
return <EditorMultiMaterialInspector material={this.props.object.material as MultiMaterial} />;
416+
return <EditorMultiMaterialInspector editor={this.props.editor} material={this.props.object.material as MultiMaterial} />;
417417

418418
case "SkyMaterial":
419419
return <EditorSkyMaterialInspector mesh={this.props.object} material={this.props.object.material as SkyMaterial} />;

editor/src/editor/layout/preview.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ export class EditorPreview extends Component<IEditorPreviewProps, IEditorPreview
170170
*/
171171
public canvas: HTMLCanvasElement | null = null;
172172

173+
/**
174+
* Defines the reference to the last picking info processed in the preview.
175+
*/
176+
public lastPickingInfo: PickingInfo | null = null;
177+
173178
private _renderScene: boolean = true;
174179
private _mouseDownPosition: Vector2 = Vector2.Zero();
175180

@@ -613,12 +618,15 @@ export class EditorPreview extends Component<IEditorPreviewProps, IEditorPreview
613618
/** @internal */
614619
public _handleMouseLeave(): void {
615620
this._restoreCurrentMeshUnderPointer();
621+
this.lastPickingInfo = null;
616622
this._objectUnderPointer = null;
617623
}
618624

619625
private _mouseMoveTimeoutId: number = -1;
620626

621627
private _handleMouseMove(x: number, y: number): void {
628+
this.lastPickingInfo = null;
629+
622630
if (!this.state.pickingEnabled) {
623631
return;
624632
}
@@ -649,6 +657,8 @@ export class EditorPreview extends Component<IEditorPreviewProps, IEditorPreview
649657
}
650658

651659
private _handleMouseDown(event: MouseEvent<HTMLCanvasElement, globalThis.MouseEvent>): void {
660+
this.lastPickingInfo = null;
661+
652662
if (!this.state.pickingEnabled) {
653663
return;
654664
}
@@ -672,6 +682,8 @@ export class EditorPreview extends Component<IEditorPreviewProps, IEditorPreview
672682
}
673683

674684
private _handleDoubleClick(_event: MouseEvent<HTMLCanvasElement, globalThis.MouseEvent>): void {
685+
this.lastPickingInfo = null;
686+
675687
if (!this.state.pickingEnabled || this.axis._axisMeshUnderPointer) {
676688
return;
677689
}
@@ -683,6 +695,8 @@ export class EditorPreview extends Component<IEditorPreviewProps, IEditorPreview
683695
}
684696

685697
private _handleMouseUp(event: MouseEvent<HTMLCanvasElement, globalThis.MouseEvent>): void {
698+
this.lastPickingInfo = null;
699+
686700
if (!this.state.pickingEnabled) {
687701
return;
688702
}
@@ -711,6 +725,8 @@ export class EditorPreview extends Component<IEditorPreviewProps, IEditorPreview
711725
}
712726
}
713727

728+
this.lastPickingInfo = pickingInfo;
729+
714730
if (effectivePickedObject) {
715731
if (event.shiftKey) {
716732
this.props.editor.layout.graph.addToSelectedNodes(effectivePickedObject);

editor/src/editor/layout/preview/play.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ export class EditorPreviewPlayComponent extends Component<IEditorPreviewPlayComp
305305

306306
return true;
307307
} catch (e) {
308+
console.error("Failed to compile play scripts:", e);
308309
if (e instanceof Error) {
309310
this.props.editor.layout.console.error(`Failed to compile play scripts:\n${e.message}`);
310311
}

0 commit comments

Comments
 (0)