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

[BETA] ESM Grid Renderer Script #7079

Merged
merged 40 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
49e69c8
WIP: Ported grid from supersplat
kpal81xd Oct 30, 2024
9882d09
Converted grid to script and added to gizmo examples
kpal81xd Oct 30, 2024
03d688a
Updated thumbnails
kpal81xd Oct 30, 2024
800a797
Added ability to change color of grid axis lines
kpal81xd Oct 30, 2024
ffcad5d
Added attribute flag
kpal81xd Oct 30, 2024
c0860ca
Updated grid to use attach/detach methods
kpal81xd Oct 31, 2024
0d7afb6
Removed adding grid to camera
kpal81xd Oct 31, 2024
ad1da2e
Added grid to root entity
kpal81xd Oct 31, 2024
57f655b
Added half extents to grid
kpal81xd Oct 31, 2024
3493c6a
Renamed infinite grid to grid
kpal81xd Oct 31, 2024
fe77d48
Added grid color and size to controls
kpal81xd Oct 31, 2024
0fb5f87
Merge branch 'main' into grid
kpal81xd Nov 11, 2024
c23fccd
Moved everything inside camera pre render layer
kpal81xd Nov 11, 2024
9975da6
Renamed grid to mjs
kpal81xd Nov 11, 2024
3442794
WIP: added new layer for grid
kpal81xd Nov 11, 2024
d613c7c
Added resolution option to grid
kpal81xd Nov 12, 2024
f4af3e7
Updated resolution type
kpal81xd Nov 12, 2024
e958145
minor refactor
kpal81xd Nov 12, 2024
c570f6b
Added grid at correct layer index
kpal81xd Nov 13, 2024
1621d09
fixes for webgpu
slimbuck Nov 13, 2024
64ef6a1
Merge remote-tracking branch 'upstream/grid' into grid
slimbuck Nov 13, 2024
d7c601d
Merge branch 'main' into grid
kpal81xd Nov 13, 2024
34c2753
Merge branch 'main' into grid
kpal81xd Nov 21, 2024
d339eca
Fixed linting and import of grid
kpal81xd Nov 21, 2024
242fcfb
Updated grid name and script attachment back to camera
kpal81xd Nov 21, 2024
8f4f93a
Merge branch 'main' into grid
kpal81xd Nov 21, 2024
601f4b8
Merge branch 'main' into grid
kpal81xd Nov 27, 2024
5aa2f60
Updated grid sizing so when set to 0 scale is infinite
kpal81xd Nov 27, 2024
fa0cf62
Merge branch 'main' into grid
kpal81xd Dec 2, 2024
3d64e3b
Moved grid to ESM scripts folder
kpal81xd Dec 2, 2024
fd190a1
Fixed lint issue
kpal81xd Dec 9, 2024
a6ce7cd
Removed grid dependency being on entity with camera
kpal81xd Dec 9, 2024
c6dcde5
Merge branch 'main' into grid
kpal81xd Dec 9, 2024
59c78bf
Removed extra log
kpal81xd Dec 9, 2024
da1198b
Merge branch 'grid' of https://github.com/playcanvas/engine into grid
kpal81xd Dec 9, 2024
7729782
Fixed broken observer data
kpal81xd Dec 9, 2024
54d2ebd
Added a grid transform based on entity transform
kpal81xd Dec 9, 2024
8f49c95
refactor
kpal81xd Dec 9, 2024
5f608db
Set camera layer in update loop
kpal81xd Dec 9, 2024
c2d9e35
Merge branch 'main' into grid
kpal81xd Dec 17, 2024
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
32 changes: 13 additions & 19 deletions examples/src/examples/gizmos/transform-rotate.example.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { data } from 'examples/observer';
import { deviceType, rootPath } from 'examples/utils';
import { deviceType, fileImport, rootPath } from 'examples/utils';
import * as pc from 'playcanvas';

const { GridRenderer } = await fileImport(`${rootPath}/static/scripts/esm/grid-renderer.mjs`);

const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById('application-canvas'));
window.focus();

Expand Down Expand Up @@ -82,6 +84,16 @@ data.set('camera', {
fov: camera.camera.fov
});

// create grid
const gridEntity = new pc.Entity('grid');
gridEntity.addComponent('script');
gridEntity.script.create(GridRenderer, {
attributes: {
halfExtents: new pc.Vec2(2, 2)
}
});
app.root.addChild(gridEntity);

// create light entity
const light = new pc.Entity('light');
light.addComponent('light');
Expand Down Expand Up @@ -146,24 +158,6 @@ const resize = () => {
window.addEventListener('resize', resize);
resize();

// grid lines
const createGridLines = (size = 1) => {
const lines = [];
for (let i = -size; i < size + 1; i++) {
lines.push(
new pc.Vec3(-size, 0, i),
new pc.Vec3(size, 0, i),
new pc.Vec3(i, 0, -size),
new pc.Vec3(i, 0, size)
);
}
return lines;
};

const lines = createGridLines(2);
const gridCol = new pc.Color(1, 1, 1, 0.5);
app.on('update', () => app.drawLines(lines, gridCol));

app.on('destroy', () => {
window.removeEventListener('resize', resize);
});
Expand Down
32 changes: 13 additions & 19 deletions examples/src/examples/gizmos/transform-scale.example.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { data } from 'examples/observer';
import { deviceType, rootPath } from 'examples/utils';
import { deviceType, fileImport, rootPath } from 'examples/utils';
import * as pc from 'playcanvas';

const { GridRenderer } = await fileImport(`${rootPath}/static/scripts/esm/grid-renderer.mjs`);

const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById('application-canvas'));
window.focus();

Expand Down Expand Up @@ -82,6 +84,16 @@ data.set('camera', {
fov: camera.camera.fov
});

// create grid
const gridEntity = new pc.Entity('grid');
gridEntity.addComponent('script');
gridEntity.script.create(GridRenderer, {
attributes: {
halfExtents: new pc.Vec2(2, 2)
}
});
app.root.addChild(gridEntity);

// create light entity
const light = new pc.Entity('light');
light.addComponent('light');
Expand Down Expand Up @@ -150,24 +162,6 @@ const resize = () => {
window.addEventListener('resize', resize);
resize();

// grid lines
const createGridLines = (size = 1) => {
const lines = [];
for (let i = -size; i < size + 1; i++) {
lines.push(
new pc.Vec3(-size, 0, i),
new pc.Vec3(size, 0, i),
new pc.Vec3(i, 0, -size),
new pc.Vec3(i, 0, size)
);
}
return lines;
};

const lines = createGridLines(2);
const gridCol = new pc.Color(1, 1, 1, 0.5);
app.on('update', () => app.drawLines(lines, gridCol));

app.on('destroy', () => {
window.removeEventListener('resize', resize);
});
Expand Down
32 changes: 13 additions & 19 deletions examples/src/examples/gizmos/transform-translate.example.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { data } from 'examples/observer';
import { deviceType, rootPath } from 'examples/utils';
import { deviceType, rootPath, fileImport } from 'examples/utils';
import * as pc from 'playcanvas';

const { GridRenderer } = await fileImport(`${rootPath}/static/scripts/esm/grid-renderer.mjs`);

const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById('application-canvas'));
window.focus();

Expand Down Expand Up @@ -82,6 +84,16 @@ data.set('camera', {
fov: camera.camera.fov
});

// create grid
const gridEntity = new pc.Entity('grid');
gridEntity.addComponent('script');
gridEntity.script.create(GridRenderer, {
attributes: {
halfExtents: new pc.Vec2(2, 2)
}
});
app.root.addChild(gridEntity);

// create light entity
const light = new pc.Entity('light');
light.addComponent('light');
Expand Down Expand Up @@ -151,24 +163,6 @@ const resize = () => {
window.addEventListener('resize', resize);
resize();

// grid lines
const createGridLines = (size = 1) => {
const lines = [];
for (let i = -size; i < size + 1; i++) {
lines.push(
new pc.Vec3(-size, 0, i),
new pc.Vec3(size, 0, i),
new pc.Vec3(i, 0, -size),
new pc.Vec3(i, 0, size)
);
}
return lines;
};

const lines = createGridLines(2);
const gridCol = new pc.Color(1, 1, 1, 0.5);
app.on('update', () => app.drawLines(lines, gridCol));

app.on('destroy', () => {
window.removeEventListener('resize', resize);
});
Expand Down
46 changes: 45 additions & 1 deletion examples/src/examples/misc/editor.controls.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as pc from 'playcanvas';
* @returns {JSX.Element} The returned JSX Element.
*/
export const controls = ({ observer, ReactPCUI, React, jsx, fragment }) => {
const { BindingTwoWay, LabelGroup, Panel, SliderInput, SelectInput } = ReactPCUI;
const { BindingTwoWay, LabelGroup, Panel, SliderInput, SelectInput, ColorPicker } = ReactPCUI;
const { useState } = React;

const [type, setType] = useState('translate');
Expand Down Expand Up @@ -87,6 +87,50 @@ export const controls = ({ observer, ReactPCUI, React, jsx, fragment }) => {
})
)
),
jsx(
Panel,
{ headerText: 'Grid' },
jsx(
LabelGroup,
{ text: 'Resolution' },
jsx(SelectInput, {
options: [
{ v: 3, t: 'High' },
{ v: 2, t: 'Medium' },
{ v: 1, t: 'Low' }
],
binding: new BindingTwoWay(),
link: { observer, path: 'grid.resolution' }
})
),
jsx(
LabelGroup,
{ text: 'Size' },
jsx(SliderInput, {
binding: new BindingTwoWay(),
link: { observer, path: 'grid.size' },
min: 0,
max: 10,
precision: 0
})
),
jsx(
LabelGroup,
{ text: 'Color X' },
jsx(ColorPicker, {
binding: new BindingTwoWay(),
link: { observer, path: 'grid.colorX' }
})
),
jsx(
LabelGroup,
{ text: 'Color Z' },
jsx(ColorPicker, {
binding: new BindingTwoWay(),
link: { observer, path: 'grid.colorZ' }
})
)
),
jsx(
Panel,
{ headerText: 'Camera' },
Expand Down
47 changes: 38 additions & 9 deletions examples/src/examples/misc/editor.example.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// @config DESCRIPTION <div style='text-align:center'><div>Translate (1), Rotate (2), Scale (3)</div><div>World/Local (X)</div><div>Perspective (P), Orthographic (O)</div></div>
import { data } from 'examples/observer';
import { deviceType, rootPath, localImport } from 'examples/utils';
import { deviceType, rootPath, localImport, fileImport } from 'examples/utils';
import * as pc from 'playcanvas';

const { GridRenderer } = await fileImport(`${rootPath}/static/scripts/esm/grid-renderer.mjs`);

const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById('application-canvas'));
window.focus();

// class for handling gizmo
const { GizmoHandler } = await localImport('gizmo-handler.mjs');
const { Grid } = await localImport('grid.mjs');
const { Selector } = await localImport('selector.mjs');

const gfxOptions = {
Expand Down Expand Up @@ -127,6 +128,22 @@ camera.setPosition(1, 1, 1);
app.root.addChild(camera);
orbitCamera.distance = 5 * camera.camera?.aspectRatio;

// create grid
const gridEntity = new pc.Entity('grid');
gridEntity.addComponent('script');
const grid = /** @type {GridRenderer} */ (gridEntity.script.create(GridRenderer, {
attributes: {
halfExtents: new pc.Vec2(4, 4)
}
}));
app.root.addChild(gridEntity);
data.set('grid', {
resolution: grid.resolution + 1,
size: grid.halfExtents.x,
colorX: Object.values(grid.colorX),
colorZ: Object.values(grid.colorZ)
});

// create light entity
const light = new pc.Entity('light');
light.addComponent('light', {
Expand Down Expand Up @@ -204,6 +221,8 @@ window.addEventListener('keyup', keyup);
window.addEventListener('keypress', keypress);

// gizmo and camera set handler
const tmpVa = new pc.Vec2();
const tmpC1 = new pc.Color();
data.on('*:set', (/** @type {string} */ path, /** @type {any} */ value) => {
const [category, key] = path.split('.');
switch (category) {
Expand All @@ -230,6 +249,23 @@ data.on('*:set', (/** @type {string} */ path, /** @type {any} */ value) => {
gizmoHandler.gizmo[key] = value;
break;
}
case 'grid': {
switch (key) {
case 'resolution':
grid.resolution = value - 1;
break;
case 'size':
grid.halfExtents = tmpVa.set(value, value);
break;
case 'colorX':
grid.colorX = tmpC1.set(value[0], value[1], value[2]);
break;
case 'colorZ':
grid.colorZ = tmpC1.set(value[0], value[1], value[2]);
break;
}
break;
}
}
});

Expand All @@ -246,13 +282,6 @@ selector.on('deselect', () => {
gizmoHandler.clear();
});

// grid
const grid = new Grid();

app.on('update', (/** @type {number} */ dt) => {
grid.draw(app);
});

app.on('destroy', () => {
gizmoHandler.destroy();
selector.destroy();
Expand Down
77 changes: 0 additions & 77 deletions examples/src/examples/misc/editor.grid.mjs

This file was deleted.

Binary file modified examples/thumbnails/gizmos_transform-rotate_large.webp
Binary file not shown.
Binary file modified examples/thumbnails/gizmos_transform-rotate_small.webp
Binary file not shown.
Binary file modified examples/thumbnails/gizmos_transform-scale_large.webp
Binary file not shown.
Binary file modified examples/thumbnails/gizmos_transform-scale_small.webp
Binary file not shown.
Binary file modified examples/thumbnails/gizmos_transform-translate_large.webp
Binary file not shown.
Binary file modified examples/thumbnails/gizmos_transform-translate_small.webp
Binary file not shown.
Binary file modified examples/thumbnails/misc_editor_large.webp
Binary file not shown.
Binary file modified examples/thumbnails/misc_editor_small.webp
Binary file not shown.
Loading
Loading