Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/camera preview #425

Merged
merged 30 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a0b650b
feat: add support of camera preview
julien-moreau Feb 7, 2023
c425327
feat: add support of "None" for node list in inspector
julien-moreau Mar 1, 2023
b7d117b
feat: keep camera preview opened
julien-moreau Mar 1, 2023
74294bc
feat: add support of doubled size for camera preview
julien-moreau Mar 1, 2023
ab11ffc
feat: add export of "editor" in index.ts so scripts and plugins can i…
julien-moreau Mar 1, 2023
ee53137
style: fix var to const
julien-moreau Mar 2, 2023
7902d89
feat: add support of loading phases for "appendScene" method in tools.ts
julien-moreau Mar 18, 2023
7573b7c
feat: add "scaling" tool type to thin instances painting tool
julien-moreau Mar 22, 2023
283361c
feat: add support of "use last camera" shortcut
julien-moreau Mar 23, 2023
d98f3ca
chore: update to latest Babylon.JS version
julien-moreau Mar 26, 2023
db6fd09
fix: rename wrong whatsnew
julien-moreau Mar 27, 2023
ca60600
fix: clean render targets before saving project to handle null case
julien-moreau Apr 3, 2023
87480d9
fix: limit height for suggestion lists in inspecetor and enable overflow
julien-moreau Apr 4, 2023
b6becff
feat: adding support of video textures
julien-moreau Apr 6, 2023
7464606
feat: improve camera preview sub-panel
julien-moreau Apr 8, 2023
d0f22ab
fix: handle mutli cameras only when editor camera is active
julien-moreau Apr 9, 2023
174bae8
feat: add missing properties to cascaded shadow maps
julien-moreau Apr 9, 2023
1b48980
feat: restore sub-meshes menu in graph context menu
julien-moreau Apr 10, 2023
094400c
fix: SSR and MB post-processes in editor preview
julien-moreau Apr 10, 2023
08c94c7
fix: use plugins from file system
julien-moreau Apr 10, 2023
391624c
feat: handle textures with loading errors in assets panel
julien-moreau Apr 12, 2023
986a1bc
feat: add support of JS script editor
julien-moreau Apr 13, 2023
d490665
feat: add support of Compute Range Excluded Meshes for lights
julien-moreau Apr 13, 2023
ca3e59a
feat: improve saving speed for geometries
julien-moreau Apr 16, 2023
fdb734a
feat: add support of SSR rendering pipeline
julien-moreau Apr 16, 2023
f69cc7a
feat: add support of JS files in assets
julien-moreau Apr 17, 2023
62cb12d
Fixed #424: prevent default and unfocus keymap buttons to allow "spac…
julien-moreau Apr 18, 2023
2e145ca
fix: don't reset pipeline on camera selected in preview
julien-moreau Apr 18, 2023
98ce48f
feat: add support of "Update Dependencies" menu in toolbar
julien-moreau Apr 18, 2023
631101e
chore: update to Babylon.JS v6
julien-moreau Apr 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add "scaling" tool type to thin instances painting tool
  • Loading branch information
julien-moreau committed Apr 22, 2023
commit 7573b7c705747b9f0528e220ac5c0d76fa3c68e7
86 changes: 82 additions & 4 deletions src/renderer/editor/painting/foliage/foliage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ import { Decal } from "../tools/decal";

import { AbstractPaintingTool } from "../abstract-tool";

/**
* Defines the possible types for the painting tool.
* - `add`: allows to add/remove painted thin instances.
* - `scale`: allows to rescale existing thin instances.
*/
export type FoliageToolType = "add" | "scale";

const vectorTen = new Vector3(10, 10, 10);

const meshScale = Vector3.Zero();
Expand All @@ -27,9 +34,9 @@ const targetScaling = Vector3.Zero();
const targetPosition = Vector3.Zero();
const targetRotation = Quaternion.Identity();

// const scaling = Vector3.Zero();
// const position = Vector3.Zero();
// const rotation = Quaternion.Identity();
const scaling = Vector3.Zero();
const position = Vector3.Zero();
const rotation = Quaternion.Identity();

const translation = Vector3.Zero();
const targetScaledPosition = Vector3.Zero();
Expand Down Expand Up @@ -78,6 +85,18 @@ export class FoliagePainter extends AbstractPaintingTool {
*/
public randomRotationMax: Vector3 = new Vector3(0, Math.PI, 0);

/**
* Defines the type of tool used when painting thin instances.
* - `add`: allows to add/remove painted thin instances.
* - `scale`: allows to rescale existing thin instances.
*/
public toolType: FoliageToolType = "add";

/**
* Defines the vector applied on the scale of the existing thin instances in the radius of
* the tool in case the tool type is equal to `scale`.
*/
public rescaleValue: Vector3 = new Vector3(0.01, 0.01, 0.01);

/** @hidden */
public _selectedMeshes: Mesh[] = [];
Expand Down Expand Up @@ -279,6 +298,59 @@ export class FoliagePainter extends AbstractPaintingTool {
* Paints the thin instance at the current position of the cloned mesh.
*/
private _paint(): void {
if (this.toolType === "add") {
return this._addOrRemove();
}

if (this.toolType === "scale") {
return this._rescale();
}
}

/**
* Rescales the thin instances. Called on the tool type is equal to `scale`.
*/
private _rescale(): void {
const radius = this._size * 0.5;

this._selectedMeshes.forEach((m) => {
if (!m.thinInstanceCount) {
return;
}

const targetMatrix = this._getFinalMatrix(m, this._pick!.pickedPoint!, Vector3.Up());
targetMatrix.decompose(targetScaling, targetRotation, targetPosition);

const matrices = this._existingWorldMatrices.get(m)!;

for (let i = 0, len = matrices.length; i < len; ++i) {
const matrix = matrices[i];

matrix.getTranslationToRef(translation);
targetScaledPosition.copyFrom(targetPosition);

if (Vector3.Distance(targetScaledPosition.multiplyInPlace(m.scaling), translation.multiplyInPlace(m.scaling)) < radius) {
matrix.decompose(scaling, rotation, position);

if (this._removing) {
scaling.subtractInPlace(this.rescaleValue);
} else {
scaling.addInPlace(this.rescaleValue);
}

Matrix.ComposeToRef(scaling, rotation, position, matrix);
}
}

this._configureMeshMatrices(m, matrices);
});
}

/**
* Called when the user tries to add or removes instances.
* Called on the tool type is equal to `add`.
*/
private _addOrRemove(): void {
// Remove
if (this._removing) {
return this._selectedMeshes.forEach((m) => {
Expand Down Expand Up @@ -311,6 +383,9 @@ export class FoliagePainter extends AbstractPaintingTool {
});
}

/**
* Adds a thin instance at the given position.
*/
private _add(center: Vector3, map: Map<Mesh, Matrix[]>): unknown {
const mesh = this._selectedMeshes[(this._selectedMeshes.length * Math.random()) >> 0];

Expand Down Expand Up @@ -386,6 +461,9 @@ export class FoliagePainter extends AbstractPaintingTool {
existingWorldMatrices.push(targetMatrix);
}

/**
* Called on the user wants to remove existing thin instances.
*/
private _remove(mesh: Mesh, absolutePosition: Vector3): void {
const targetMatrix = this._getFinalMatrix(mesh, absolutePosition, Vector3.Up());
targetMatrix.decompose(targetScaling, targetRotation, targetPosition);
Expand All @@ -402,7 +480,7 @@ export class FoliagePainter extends AbstractPaintingTool {
for (let i = 0, len = matrices.length; i < len; ++i) {
const matrix = matrices[i];

matrix.getTranslationToRef(translation)
matrix.getTranslationToRef(translation);
targetScaledPosition.copyFrom(targetPosition);

if (Vector3.Distance(targetScaledPosition.multiplyInPlace(mesh.scaling), translation.multiplyInPlace(mesh.scaling)) < radius) {
Expand Down
58 changes: 56 additions & 2 deletions src/renderer/tools/painting/foliage/inspector.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Nullable } from "../../../../shared/types";

import * as React from "react";
import { Divider, H4, NonIdealState } from "@blueprintjs/core";
import { Divider, H4, NonIdealState, Tab, Tabs } from "@blueprintjs/core";

import { Mesh } from "babylonjs";

Expand All @@ -12,14 +12,19 @@ import { InspectorBoolean } from "../../../editor/gui/inspector/fields/boolean";

import { Tools } from "../../../editor/tools/tools";

import { FoliagePainter } from "../../../editor/painting/foliage/foliage";
import { FoliagePainter, FoliageToolType } from "../../../editor/painting/foliage/foliage";

import { IObjectInspectorProps } from "../../../editor/components/inspector";
import { AbstractInspector } from "../../../editor/components/inspectors/abstract-inspector";

import { FoliageAssetItem } from "./item";

export interface IFoliagePainterState {
/**
* Defines the current id of the selected tab.
*/
selectedTabId: FoliageToolType;

/**
* Defines the preview of the mesh.
*/
Expand Down Expand Up @@ -49,6 +54,8 @@ export class FoliagePainterInspector extends AbstractInspector<FoliagePainter, I
}

this.state = {
selectedTabId: "add",

assetPreviews: [],
selectedMeshes: [],
};
Expand All @@ -71,6 +78,36 @@ export class FoliagePainterInspector extends AbstractInspector<FoliagePainter, I
</div>
</InspectorSection>

<div style={{ display: "flex", justifyContent: "center" }}>
<Tabs
animate
selectedTabId={this.state.selectedTabId}
onChange={(id) => {
this.selectedObject.toolType = id as FoliageToolType;
this.setState({ selectedTabId: id as FoliageToolType });
}}
>
<Tab id="add" title="Add" />
<Tab id="scale" title="Scale" />
</Tabs>
</div>

{this.getAddInspector()}
{this.getScaleInspector()}
</>
);
}

/**
* Returns the inspector used to add / remove instances.
*/
protected getAddInspector(): React.ReactNode {
if (this.state.selectedTabId !== "add") {
return undefined;
}

return (
<>
<InspectorSection title="Random Rotation">
<InspectorVector3 object={this.selectedObject} property="randomRotationMin" label="Random Min Rotation" step={0.01} />
<InspectorVector3 object={this.selectedObject} property="randomRotationMax" label="Random Max Rotation" step={0.01} />
Expand All @@ -95,6 +132,23 @@ export class FoliagePainterInspector extends AbstractInspector<FoliagePainter, I
);
}

/**
* Returns the inspector used to scale existing instances.
*/
protected getScaleInspector(): React.ReactNode {
if (this.state.selectedTabId !== "scale") {
return undefined;
}

return (
<>
<InspectorSection title="Value">
<InspectorVector3 object={this.selectedObject} property="rescaleValue" label="Value" step={0.01} />
</InspectorSection>
</>
);
}

/**
* Called on the component did mount.
*/
Expand Down