Skip to content

Commit 170b978

Browse files
mvaligurskyMartin Valigursky
andauthored
Converted few classes in scene folder to use let/const (playcanvas#3437)
Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
1 parent e5a6249 commit 170b978

File tree

6 files changed

+136
-155
lines changed

6 files changed

+136
-155
lines changed

src/scene/camera.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import {
1111
} from './constants.js';
1212

1313
// pre-allocated temp variables
14-
var _deviceCoord = new Vec3();
15-
var _halfSize = new Vec3();
16-
var _point = new Vec3();
17-
var _invViewProjMat = new Mat4();
14+
const _deviceCoord = new Vec3();
15+
const _halfSize = new Vec3();
16+
const _point = new Vec3();
17+
const _invViewProjMat = new Mat4();
1818

1919
/**
2020
* @private
@@ -294,7 +294,7 @@ class Camera {
294294

295295
get viewMatrix() {
296296
if (this._viewMatDirty) {
297-
var wtm = this._node.getWorldTransform();
297+
const wtm = this._node.getWorldTransform();
298298
this._viewMat.copy(wtm).invert();
299299
this._viewMatDirty = false;
300300
}
@@ -362,9 +362,7 @@ class Camera {
362362

363363
_updateViewProjMat() {
364364
if (this._projMatDirty || this._viewMatDirty || this._viewProjMatDirty) {
365-
var projMat = this.projectionMatrix;
366-
var viewMat = this.viewMatrix;
367-
this._viewProjMat.mul2(projMat, viewMat);
365+
this._viewProjMat.mul2(this.projectionMatrix, this.viewMatrix);
368366
this._viewProjMatDirty = false;
369367
}
370368
}
@@ -385,8 +383,8 @@ class Camera {
385383
this._viewProjMat.transformPoint(worldCoord, screenCoord);
386384

387385
// calculate w co-coord
388-
var vpm = this._viewProjMat.data;
389-
var w = worldCoord.x * vpm[3] +
386+
const vpm = this._viewProjMat.data;
387+
const w = worldCoord.x * vpm[3] +
390388
worldCoord.y * vpm[7] +
391389
worldCoord.z * vpm[11] +
392390
1 * vpm[15];
@@ -413,7 +411,7 @@ class Camera {
413411
screenToWorld(x, y, z, cw, ch, worldCoord = new Vec3()) {
414412

415413
// Calculate the screen click as a point on the far plane of the normalized device coordinate 'box' (z=1)
416-
var range = this._farClip - this._nearClip;
414+
const range = this._farClip - this._nearClip;
417415
_deviceCoord.set(x / cw, (ch - y) / ch, z / range);
418416
_deviceCoord.mulScalar(2);
419417
_deviceCoord.sub(Vec3.ONE);
@@ -428,12 +426,12 @@ class Camera {
428426
_halfSize.y *= _deviceCoord.y;
429427

430428
// transform to world space
431-
var invView = this._node.getWorldTransform();
429+
const invView = this._node.getWorldTransform();
432430
_halfSize.z = -this._nearClip;
433431
invView.transformPoint(_halfSize, _point);
434432

435433
// point along camera->_point ray at distance z from the camera
436-
var cameraPos = this._node.getPosition();
434+
const cameraPos = this._node.getPosition();
437435
worldCoord.sub2(_point, cameraPos);
438436
worldCoord.normalize();
439437
worldCoord.mulScalar(z);
@@ -457,8 +455,8 @@ class Camera {
457455
this._projMat.setPerspective(this._fov, this._aspectRatio, this._nearClip, this._farClip, this._horizontalFov);
458456
this._projMatSkybox.copy(this._projMat);
459457
} else {
460-
var y = this._orthoHeight;
461-
var x = y * this._aspectRatio;
458+
const y = this._orthoHeight;
459+
const x = y * this._aspectRatio;
462460
this._projMat.setOrtho(-x, x, -y, y, this._nearClip, this._farClip);
463461
this._projMatSkybox.setPerspective(this._fov, this._aspectRatio, this._nearClip, this._farClip);
464462
}

src/scene/light.js

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import {
1515
} from './constants.js';
1616
import { ShadowRenderer } from './renderer/shadow-renderer.js';
1717

18-
var spotCenter = new Vec3();
19-
var spotEndPoint = new Vec3();
20-
var tmpVec = new Vec3();
18+
const spotCenter = new Vec3();
19+
const spotEndPoint = new Vec3();
20+
const tmpVec = new Vec3();
2121

22-
var chanId = { r: 0, g: 1, b: 2, a: 3 };
22+
const chanId = { r: 0, g: 1, b: 2, a: 3 };
2323

2424
// viewport in shadows map for cascades for directional light
2525
const directionalCascades = [
@@ -129,7 +129,7 @@ class Light {
129129

130130
// Cache of light property data in a format more friendly for shader uniforms
131131
this._finalColor = new Float32Array([0.8, 0.8, 0.8]);
132-
var c = Math.pow(this._finalColor[0], 2.2);
132+
const c = Math.pow(this._finalColor[0], 2.2);
133133
this._linearFinalColor = new Float32Array([c, c, c]);
134134

135135
this._position = new Vec3(0, 0, 0);
@@ -213,7 +213,7 @@ class Light {
213213
* @returns {Light} A cloned Light.
214214
*/
215215
clone() {
216-
var clone = new Light(this.device);
216+
const clone = new Light(this.device);
217217

218218
// Clone Light properties
219219
clone.type = this._type;
@@ -292,10 +292,10 @@ class Light {
292292

293293
getBoundingSphere(sphere) {
294294
if (this._type === LIGHTTYPE_SPOT) {
295-
var range = this.attenuationEnd;
296-
var angle = this._outerConeAngle;
297-
var f = Math.cos(angle * math.DEG_TO_RAD);
298-
var node = this._node;
295+
const range = this.attenuationEnd;
296+
const angle = this._outerConeAngle;
297+
const f = Math.cos(angle * math.DEG_TO_RAD);
298+
const node = this._node;
299299

300300
spotCenter.copy(node.up);
301301
spotCenter.mulScalar(-range * 0.5 * f);
@@ -319,11 +319,11 @@ class Light {
319319

320320
getBoundingBox(box) {
321321
if (this._type === LIGHTTYPE_SPOT) {
322-
var range = this.attenuationEnd;
323-
var angle = this._outerConeAngle;
324-
var node = this._node;
322+
const range = this.attenuationEnd;
323+
const angle = this._outerConeAngle;
324+
const node = this._node;
325325

326-
var scl = Math.abs(Math.sin(angle * math.DEG_TO_RAD) * range);
326+
const scl = Math.abs(Math.sin(angle * math.DEG_TO_RAD) * range);
327327

328328
box.center.set(0, -range * 0.5, 0);
329329
box.halfExtents.set(scl, range * 0.5, scl);
@@ -337,15 +337,15 @@ class Light {
337337
}
338338

339339
_updateFinalColor() {
340-
var color = this._color;
341-
var r = color.r;
342-
var g = color.g;
343-
var b = color.b;
340+
const color = this._color;
341+
const r = color.r;
342+
const g = color.g;
343+
const b = color.b;
344344

345-
var i = this._intensity;
345+
const i = this._intensity;
346346

347-
var finalColor = this._finalColor;
348-
var linearFinalColor = this._linearFinalColor;
347+
const finalColor = this._finalColor;
348+
const linearFinalColor = this._linearFinalColor;
349349

350350
finalColor[0] = r * i;
351351
finalColor[1] = g * i;
@@ -362,19 +362,12 @@ class Light {
362362
}
363363

364364
setColor() {
365-
var r, g, b;
366365
if (arguments.length === 1) {
367-
r = arguments[0].r;
368-
g = arguments[0].g;
369-
b = arguments[0].b;
366+
this._color.set(arguments[0].r, arguments[0].g, arguments[0].b);
370367
} else if (arguments.length === 3) {
371-
r = arguments[0];
372-
g = arguments[1];
373-
b = arguments[2];
368+
this._color.set(arguments[0], arguments[1], arguments[2]);
374369
}
375370

376-
this._color.set(r, g, b);
377-
378371
this._updateFinalColor();
379372
}
380373

@@ -407,7 +400,7 @@ class Light {
407400
// 12 : cookie transform
408401
// 10 - 11 : light source shape
409402
// 8 - 9 : light num cascades
410-
var key =
403+
let key =
411404
(this._type << 29) |
412405
((this._castShadows ? 1 : 0) << 28) |
413406
(this._shadowType << 25) |
@@ -446,7 +439,7 @@ class Light {
446439
this._destroyShadowMap();
447440
this.updateKey();
448441

449-
var stype = this._shadowType;
442+
const stype = this._shadowType;
450443
this._shadowType = null;
451444
this.shadowType = stype; // refresh shadow type; switching from direct/spot to omni and back may change it
452445
}
@@ -463,7 +456,7 @@ class Light {
463456
this._destroyShadowMap();
464457
this.updateKey();
465458

466-
var stype = this._shadowType;
459+
const stype = this._shadowType;
467460
this._shadowType = null;
468461
this.shadowType = stype; // refresh shadow type; switching shape and back may change it
469462
}
@@ -476,7 +469,7 @@ class Light {
476469
if (this._shadowType === value)
477470
return;
478471

479-
var device = this.device;
472+
const device = this.device;
480473

481474
if (this._type === LIGHTTYPE_OMNI)
482475
value = SHADOW_PCF3; // VSM or HW PCF for omni lights is not supported yet
@@ -652,9 +645,9 @@ class Light {
652645
return;
653646

654647
if (value.length < 3) {
655-
var chr = value.charAt(value.length - 1);
656-
var addLen = 3 - value.length;
657-
for (var i = 0; i < addLen; i++)
648+
const chr = value.charAt(value.length - 1);
649+
const addLen = 3 - value.length;
650+
for (let i = 0; i < addLen; i++)
658651
value += chr;
659652
}
660653
this._cookieChannel = value;
@@ -686,7 +679,7 @@ class Light {
686679
if (this._cookieOffset === value)
687680
return;
688681

689-
var xformNew = !!(this._cookieTransformSet || value);
682+
const xformNew = !!(this._cookieTransformSet || value);
690683
if (xformNew && !value && this._cookieOffset) {
691684
this._cookieOffset.set(0, 0);
692685
} else {

0 commit comments

Comments
 (0)