Skip to content

Commit 049d02a

Browse files
authored
Expose gamma and tonemap on pc-camera (#69)
* Expose gamma and tonemap on pc-camera * Lint fixes
1 parent 3abde17 commit 049d02a

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

src/components/camera-component.ts

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
import { PROJECTION_ORTHOGRAPHIC, PROJECTION_PERSPECTIVE, CameraComponent, Color, Vec4, XRTYPE_VR } from 'playcanvas';
1+
import { CameraComponent, Color, Vec4, GAMMA_NONE, GAMMA_SRGB, PROJECTION_ORTHOGRAPHIC, PROJECTION_PERSPECTIVE, TONEMAP_LINEAR, TONEMAP_FILMIC, TONEMAP_NEUTRAL, TONEMAP_ACES2, TONEMAP_ACES, TONEMAP_HEJL, TONEMAP_NONE, XRTYPE_VR } from 'playcanvas';
22

33
import { ComponentElement } from './component';
44
import { parseColor, parseVec4 } from '../utils';
55

6+
const tonemaps = new Map([
7+
['none', TONEMAP_NONE],
8+
['linear', TONEMAP_LINEAR],
9+
['filmic', TONEMAP_FILMIC],
10+
['hejl', TONEMAP_HEJL],
11+
['aces', TONEMAP_ACES],
12+
['aces2', TONEMAP_ACES2],
13+
['neutral', TONEMAP_NEUTRAL]
14+
]);
15+
616
/**
717
* The CameraComponentElement interface provides properties and methods for manipulating
818
* `<pc-camera>` elements. The CameraComponentElement interface also inherits the properties and
@@ -29,6 +39,8 @@ class CameraComponentElement extends ComponentElement {
2939

3040
private _frustumCulling = true;
3141

42+
private _gamma: 'none' | 'srgb' = 'srgb';
43+
3244
private _nearClip = 0.1;
3345

3446
private _orthographic = false;
@@ -41,6 +53,8 @@ class CameraComponentElement extends ComponentElement {
4153

4254
private _scissorRect = new Vec4(0, 0, 1, 1);
4355

56+
private _tonemap: 'none' | 'linear' | 'filmic' | 'hejl' | 'aces' | 'aces2' | 'neutral' = 'none';
57+
4458
/** @ignore */
4559
constructor() {
4660
super('camera');
@@ -57,12 +71,14 @@ class CameraComponentElement extends ComponentElement {
5771
flipFaces: this._flipFaces,
5872
fov: this._fov,
5973
frustumCulling: this._frustumCulling,
74+
gammaCorrection: this._gamma === 'srgb' ? GAMMA_SRGB : GAMMA_NONE,
6075
nearClip: this._nearClip,
6176
orthographic: this._orthographic,
6277
orthoHeight: this._orthoHeight,
6378
priority: this._priority,
6479
rect: this._rect,
65-
scissorRect: this._scissorRect
80+
scissorRect: this._scissorRect,
81+
toneMapping: tonemaps.get(this._tonemap)
6682
};
6783
}
6884

@@ -266,6 +282,25 @@ class CameraComponentElement extends ComponentElement {
266282
return this._frustumCulling;
267283
}
268284

285+
/**
286+
* Sets the gamma correction of the camera.
287+
* @param value - The gamma correction.
288+
*/
289+
set gamma(value: 'none' | 'srgb') {
290+
this._gamma = value;
291+
if (this.component) {
292+
this.component.gammaCorrection = value === 'srgb' ? GAMMA_SRGB : GAMMA_NONE;
293+
}
294+
}
295+
296+
/**
297+
* Gets the gamma correction of the camera.
298+
* @returns The gamma correction.
299+
*/
300+
get gamma(): 'none' | 'srgb' {
301+
return this._gamma;
302+
}
303+
269304
/**
270305
* Sets the near clip distance of the camera.
271306
* @param value - The near clip distance.
@@ -380,6 +415,25 @@ class CameraComponentElement extends ComponentElement {
380415
return this._scissorRect;
381416
}
382417

418+
/**
419+
* Sets the tone mapping of the camera.
420+
* @param value - The tone mapping.
421+
*/
422+
set tonemap(value: 'none' | 'linear' | 'filmic' | 'hejl' | 'aces' | 'aces2' | 'neutral') {
423+
this._tonemap = value;
424+
if (this.component) {
425+
this.component.toneMapping = tonemaps.get(value) ?? TONEMAP_NONE;
426+
}
427+
}
428+
429+
/**
430+
* Gets the tone mapping of the camera.
431+
* @returns The tone mapping.
432+
*/
433+
get tonemap(): 'none' | 'linear' | 'filmic' | 'hejl' | 'aces' | 'aces2' | 'neutral' {
434+
return this._tonemap;
435+
}
436+
383437
static get observedAttributes() {
384438
return [
385439
...super.observedAttributes,
@@ -392,12 +446,14 @@ class CameraComponentElement extends ComponentElement {
392446
'flip-faces',
393447
'fov',
394448
'frustum-culling',
449+
'gamma',
395450
'near-clip',
396451
'orthographic',
397452
'ortho-height',
398453
'priority',
399454
'rect',
400-
'scissor-rect'
455+
'scissor-rect',
456+
'tonemap'
401457
];
402458
}
403459

@@ -432,6 +488,9 @@ class CameraComponentElement extends ComponentElement {
432488
case 'frustum-culling':
433489
this.frustumCulling = newValue !== 'false';
434490
break;
491+
case 'gamma':
492+
this.gamma = newValue as 'none' | 'srgb';
493+
break;
435494
case 'near-clip':
436495
this.nearClip = parseFloat(newValue);
437496
break;
@@ -450,6 +509,9 @@ class CameraComponentElement extends ComponentElement {
450509
case 'scissor-rect':
451510
this.scissorRect = parseVec4(newValue);
452511
break;
512+
case 'tonemap':
513+
this.tonemap = newValue as 'none' | 'linear' | 'filmic' | 'hejl' | 'aces' | 'aces2' | 'neutral';
514+
break;
453515
}
454516
}
455517
}

0 commit comments

Comments
 (0)