Skip to content

Commit e5a6249

Browse files
mvaligurskyMartin Valigursky
andauthored
Engine example Shadow Cascades has HUD controls (playcanvas#3444)
* Engine example Shadow Cascades has HUD controls * name change Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
1 parent 3d8c6f4 commit e5a6249

File tree

1 file changed

+58
-21
lines changed

1 file changed

+58
-21
lines changed

examples/src/examples/graphics/shadow-cascades.tsx

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ import React from 'react';
22
import * as pc from 'playcanvas/build/playcanvas.js';
33
import { AssetLoader } from '../../app/helpers/loader';
44
import Example from '../../app/example';
5+
// @ts-ignore: library file import
6+
import Panel from '@playcanvas/pcui/Panel/component';
7+
// @ts-ignore: library file import
8+
import SliderInput from '@playcanvas/pcui/SliderInput/component';
9+
// @ts-ignore: library file import
10+
import LabelGroup from '@playcanvas/pcui/LabelGroup/component';
11+
// @ts-ignore: library file import
12+
import BindingTwoWay from '@playcanvas/pcui/BindingTwoWay';
13+
// @ts-ignore: library file import
14+
import { Observer } from '@playcanvas/observer';
515

616
class ShadowCascadesExample extends Example {
717
static CATEGORY = 'Graphics';
@@ -14,12 +24,36 @@ class ShadowCascadesExample extends Example {
1424
}
1525

1626
// @ts-ignore: override class function
17-
example(canvas: HTMLCanvasElement): void {
27+
controls(data: Observer) {
28+
return <>
29+
<Panel headerText='Shadow Cascade Settings'>
30+
<LabelGroup text='Count'>
31+
<SliderInput binding={new BindingTwoWay()} link={{ observer: data, path: 'settings.light.numCascades' }} min={1} max={4} precision={0}/>
32+
</LabelGroup>
33+
<LabelGroup text='Resolution'>
34+
<SliderInput binding={new BindingTwoWay()} link={{ observer: data, path: 'settings.light.shadowResolution' }} min={128} max={2048} precision={0}/>
35+
</LabelGroup>
36+
<LabelGroup text='Distribution'>
37+
<SliderInput binding={new BindingTwoWay()} link={{ observer: data, path: 'settings.light.cascadeDistribution' }} min={0} max={1} precision={2}/>
38+
</LabelGroup>
39+
</Panel>
40+
</>;
41+
}
1842

19-
const app = new pc.Application(canvas, {});
43+
// @ts-ignore: override class function
44+
example(canvas: HTMLCanvasElement, assets: any, data:any): void {
2045

46+
const app = new pc.Application(canvas, {});
2147
app.start();
2248

49+
data.set('settings', {
50+
light: {
51+
numCascades: 4, // number of cascades
52+
shadowResolution: 2048, // shadow map resolution storing 4 cascades
53+
cascadeDistribution: 0.7 // distribution of cascade distances to prefer sharpness closer to the camera
54+
}
55+
});
56+
2357
// Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
2458
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
2559
app.setCanvasResolution(pc.RESOLUTION_AUTO);
@@ -88,28 +122,31 @@ class ShadowCascadesExample extends Example {
88122
// Create a directional light casting cascaded shadows
89123
const dirLight = new pc.Entity();
90124
dirLight.addComponent("light", {
91-
type: "directional",
92-
color: pc.Color.WHITE,
93-
shadowBias: 0.3,
94-
normalOffsetBias: 0.2,
95-
intensity: 1.0,
96-
97-
// enable shadow casting
98-
castShadows: true,
99-
shadowDistance: 1000,
100-
101-
// shadow map resolution storing 4 cascades
102-
shadowResolution: 2048,
103-
numCascades: 4,
104-
105-
// distribution of cascade distances to prefer sharpness closer to the camera
106-
cascadeDistribution: 0.7,
107-
108-
// shadow filtering
109-
shadowType: pc.SHADOW_PCF3
125+
...{
126+
type: "directional",
127+
color: pc.Color.WHITE,
128+
shadowBias: 0.3,
129+
normalOffsetBias: 0.2,
130+
intensity: 1.0,
131+
132+
// enable shadow casting
133+
castShadows: true,
134+
shadowDistance: 1000,
135+
136+
// shadow filtering
137+
shadowType: pc.SHADOW_PCF3
138+
},
139+
...data.get('settings.light')
110140
});
111141
app.root.addChild(dirLight);
112142
dirLight.setLocalEulerAngles(45, -20, 20);
143+
144+
// handle HUD changes - update properties on the light
145+
data.on('*:set', (path: string, value: any) => {
146+
const pathArray = path.split('.');
147+
// @ts-ignore
148+
dirLight.light[pathArray[2]] = value;
149+
});
113150
}
114151
}
115152

0 commit comments

Comments
 (0)