From 048347b25dac34d823ec0276004da3f3cc998735 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Mon, 25 Mar 2019 13:53:39 +0800 Subject: [PATCH] fix luminance (#4094) * fix web audio volume * fix luminance --- cocos/3d/assets/audio/clip.ts | 2 +- cocos/3d/assets/audio/player-dom.ts | 2 +- cocos/3d/assets/audio/player-web.ts | 4 +- cocos/3d/assets/audio/player.ts | 2 +- cocos/3d/framework/audio-source-component.ts | 3 +- cocos/3d/framework/light-component.ts | 5 +- cocos/3d/framework/sphere-light-component.ts | 44 +++++++++++------ cocos/3d/framework/spot-light-component.ts | 51 ++++++++++++++------ cocos/renderer/scene/directional-light.ts | 1 + cocos/renderer/scene/light.ts | 2 + cocos/renderer/scene/sphere-light.ts | 30 ++++++------ cocos/renderer/scene/spot-light.ts | 29 ++++++----- 12 files changed, 107 insertions(+), 68 deletions(-) diff --git a/cocos/3d/assets/audio/clip.ts b/cocos/3d/assets/audio/clip.ts index 8687fc8743..b50e434099 100644 --- a/cocos/3d/assets/audio/clip.ts +++ b/cocos/3d/assets/audio/clip.ts @@ -91,7 +91,7 @@ export class AudioClip extends EventTargetFactory(Asset) { public setCurrentTime (val: number) { if (this._player) { this._player.setCurrentTime(val); } } public getCurrentTime () { if (this._player) { return this._player.getCurrentTime(); } return 0; } public getDuration () { if (this._player) { return this._player.getDuration(); } return this._duration; } - public setVolume (val: number) { if (this._player) { this._player.setVolume(val); } } + public setVolume (val: number, immediate?: boolean) { if (this._player) { this._player.setVolume(val, immediate || false); } } public getVolume () { if (this._player) { return this._player.getVolume(); } return 1; } public setLoop (val: boolean) { if (this._player) { this._player.setLoop(val); } } public getLoop () { if (this._player) { return this._player.getLoop(); } return false; } diff --git a/cocos/3d/assets/audio/player-dom.ts b/cocos/3d/assets/audio/player-dom.ts index c7c1ebe1ec..080034cc7f 100644 --- a/cocos/3d/assets/audio/player-dom.ts +++ b/cocos/3d/assets/audio/player-dom.ts @@ -136,7 +136,7 @@ export class AudioPlayerDOM extends AudioPlayer { return isNaN(this._audio.duration) ? this._duration : this._audio.duration; } - public setVolume (val: number) { + public setVolume (val: number, immediate: boolean) { this._volume = val; /* note this won't work for ios devices, for there is just no way to set HTMLMediaElement's volume */ diff --git a/cocos/3d/assets/audio/player-web.ts b/cocos/3d/assets/audio/player-web.ts index 8fe28e89f7..d608fac30a 100644 --- a/cocos/3d/assets/audio/player-web.ts +++ b/cocos/3d/assets/audio/player-web.ts @@ -147,9 +147,9 @@ export class AudioPlayerWeb extends AudioPlayer { return this._audio ? this._audio.duration : 0; } - public setVolume (val: number) { + public setVolume (val: number, immediate: boolean) { this._volume = val; - if (this._gainNode.gain.setTargetAtTime) { + if (!immediate && this._gainNode.gain.setTargetAtTime) { this._gainNode.gain.setTargetAtTime(val, this._context.currentTime, 0.01); } else { this._gainNode.gain.value = val; diff --git a/cocos/3d/assets/audio/player.ts b/cocos/3d/assets/audio/player.ts index 9e232091eb..e18001dcc9 100644 --- a/cocos/3d/assets/audio/player.ts +++ b/cocos/3d/assets/audio/player.ts @@ -52,7 +52,7 @@ export abstract class AudioPlayer { public abstract setCurrentTime (val: number): void; public abstract getCurrentTime (): number; public abstract getDuration (): number; - public abstract setVolume (val: number): void; + public abstract setVolume (val: number, immediate: boolean): void; public abstract getVolume (): number; public abstract setLoop (val: boolean): void; public abstract getLoop (): boolean; diff --git a/cocos/3d/framework/audio-source-component.ts b/cocos/3d/framework/audio-source-component.ts index 5c05284694..310315e346 100644 --- a/cocos/3d/framework/audio-source-component.ts +++ b/cocos/3d/framework/audio-source-component.ts @@ -142,7 +142,8 @@ export class AudioSourceComponent extends Component { if (!this._clip) { return; } this._clip.setCurrentTime(this._cachedCurrentTime); this._clip.setLoop(this._loop); - this.volume = this._volume; + this._clip.setVolume(this._volume, true); + this._volume = this._clip.getVolume(); } /** diff --git a/cocos/3d/framework/light-component.ts b/cocos/3d/framework/light-component.ts index c0482db200..40f3013dee 100644 --- a/cocos/3d/framework/light-component.ts +++ b/cocos/3d/framework/light-component.ts @@ -79,7 +79,10 @@ export class LightComponent extends Component { * * !#ch 光源色温 */ - @property + @property({ + slide: true, + range: [1000, 15000, 1], + }) get colorTemperature () { return this._colorTemperature; } diff --git a/cocos/3d/framework/sphere-light-component.ts b/cocos/3d/framework/sphere-light-component.ts index 3de08a1aec..f51c2299c2 100644 --- a/cocos/3d/framework/sphere-light-component.ts +++ b/cocos/3d/framework/sphere-light-component.ts @@ -23,7 +23,7 @@ THE SOFTWARE. ****************************************************************************/ import { ccclass, executeInEditMode, menu, property } from '../../core/data/class-decorator'; -import { LightType } from '../../renderer/scene/light'; +import { LightType, nt2lm } from '../../renderer/scene/light'; import { RenderScene } from '../../renderer/scene/render-scene'; import { SphereLight } from '../../renderer/scene/sphere-light'; import { LightComponent } from './light-component'; @@ -34,27 +34,43 @@ import { LightComponent } from './light-component'; export class SphereLightComponent extends LightComponent { @property - protected _intensity = 10000; + protected _size = 0.15; @property - protected _size = 15; + protected _luminance = 1700 / nt2lm(this._size); + @property + protected _useLuminance = false; @property protected _range = 1; protected _type = LightType.SPHERE; protected _light: SphereLight | null = null; - /** - * !#en The light source intensity - * - * !#ch 光源强度 - */ @property - get intensity () { - return this._intensity; + get luminousPower () { + return this._luminance * nt2lm(this._size); } - set intensity (val) { - this._intensity = val; - if (this._light) { this._light.luminousPower = this.intensity; } + set luminousPower (val) { + if (this._useLuminance) { return; } + this._luminance = val / nt2lm(this._size); + if (this._light) { this._light.setLuminousPower(val, this._size); } + } + + @property + get luminance () { + return this._luminance; + } + set luminance (val) { + if (!this._useLuminance) { return; } + this._luminance = val; + if (this._light) { this._light.luminance = val; } + } + + @property + get useLuminance () { + return this._useLuminance; + } + set useLuminance (val) { + this._useLuminance = val; } /** @@ -94,7 +110,7 @@ export class SphereLightComponent extends LightComponent { console.warn('we don\'t support this many lights in forward pipeline.'); return; } - this.intensity = this._intensity; + this.luminance = this._luminance; this.size = this._size; this.range = this._range; super._createLight(scene); diff --git a/cocos/3d/framework/spot-light-component.ts b/cocos/3d/framework/spot-light-component.ts index 3220a8d72b..a6df578c30 100644 --- a/cocos/3d/framework/spot-light-component.ts +++ b/cocos/3d/framework/spot-light-component.ts @@ -24,7 +24,7 @@ ****************************************************************************/ import { ccclass, executeInEditMode, menu, property } from '../../core/data/class-decorator'; import { toRadian } from '../../core/vmath'; -import { LightType } from '../../renderer/scene/light'; +import { LightType, nt2lm } from '../../renderer/scene/light'; import { RenderScene } from '../../renderer/scene/render-scene'; import { SpotLight } from '../../renderer/scene/spot-light'; import { LightComponent } from './light-component'; @@ -35,29 +35,45 @@ import { LightComponent } from './light-component'; export class SpotLightComponent extends LightComponent { @property - protected _intensity = 10000; + protected _size = 0.15; @property - protected _size = 15; + protected _luminance = 1700 / nt2lm(this._size); + @property + protected _useLuminance = false; @property protected _range = 1; @property - protected _spotAngle = Math.PI / 3; + protected _spotAngle = 60; protected _type = LightType.SPOT; protected _light: SpotLight | null = null; - /** - * !#en The light source intensity - * - * !#ch 光源强度 - */ @property - get intensity () { - return this._intensity; + get luminousPower () { + return this._luminance * nt2lm(this._size); } - set intensity (val) { - this._intensity = val; - if (this._light) { this._light.luminousPower = this.intensity; } + set luminousPower (val) { + if (this._useLuminance) { return; } + this._luminance = val / nt2lm(this._size); + if (this._light) { this._light.setLuminousPower(val, this._size); } + } + + @property + get luminance () { + return this._luminance; + } + set luminance (val) { + if (!this._useLuminance) { return; } + this._luminance = val; + if (this._light) { this._light.luminance = val; } + } + + @property + get useLuminance () { + return this._useLuminance; + } + set useLuminance (val) { + this._useLuminance = val; } /** @@ -93,7 +109,10 @@ export class SpotLightComponent extends LightComponent { * * !#ch 聚光灯锥角 */ - @property + @property({ + slide: true, + range: [2, 180, 1], + }) get spotAngle () { return this._spotAngle; } @@ -112,8 +131,8 @@ export class SpotLightComponent extends LightComponent { console.warn('we don\'t support this many lights in forward pipeline.'); return; } - this.intensity = this._intensity; this.size = this._size; + this.luminance = this._luminance; this.range = this._range; this.spotAngle = this._spotAngle; super._createLight(scene); diff --git a/cocos/renderer/scene/directional-light.ts b/cocos/renderer/scene/directional-light.ts index 8f1ce313c8..ef261e8a16 100644 --- a/cocos/renderer/scene/directional-light.ts +++ b/cocos/renderer/scene/directional-light.ts @@ -22,6 +22,7 @@ export class DirectionalLight extends Light { return this._dir; } + // in Lux(lx) set illuminance (illum: number) { this._illum = illum; } diff --git a/cocos/renderer/scene/light.ts b/cocos/renderer/scene/light.ts index a60218b39b..022b4ab6c1 100644 --- a/cocos/renderer/scene/light.ts +++ b/cocos/renderer/scene/light.ts @@ -36,6 +36,8 @@ export enum LightType { UNKNOWN, } +export const nt2lm = (size: number) => 4 * Math.PI * Math.PI * size * size; + export class Light { set enabled (val) { diff --git a/cocos/renderer/scene/sphere-light.ts b/cocos/renderer/scene/sphere-light.ts index ed73118086..2dd203dd61 100644 --- a/cocos/renderer/scene/sphere-light.ts +++ b/cocos/renderer/scene/sphere-light.ts @@ -1,8 +1,7 @@ import { aabb } from '../../3d/geom-utils'; import { Vec3 } from '../../core/value-types'; -import { v3 } from '../../core/value-types/vec3'; import { Node } from '../../scene-graph'; -import { Light, LightType } from './light'; +import { Light, LightType, nt2lm } from './light'; import { RenderScene } from './render-scene'; export class SphereLight extends Light { @@ -27,41 +26,40 @@ export class SphereLight extends Light { return this._range; } + // in Nit(nt) set luminance (lum: number) { this._luminance = lum; - this._luminousPower = this._luminance * (4.0 * this._size * this._size * Math.PI * Math.PI); } get luminance (): number { return this._luminance; } - set luminousPower (lm: number) { - this._luminousPower = lm; - this._luminance = this._luminousPower / (4.0 * this._size * this._size * Math.PI * Math.PI); - } - - get luminousPower (): number { - return this._luminousPower; - } - get aabb () { return this._aabb; } protected _size: number = 0.15; protected _range: number = 1.0; - protected _luminance: number = 0.0; - protected _luminousPower: number = 0.0; + protected _luminance: number = 1700 / nt2lm(this._size); protected _pos: Vec3; protected _aabb: aabb; constructor (scene: RenderScene, name: string, node: Node) { super(scene, name, node); this._type = LightType.SPHERE; - this.luminousPower = 1700.0; this._aabb = aabb.create(); - this._pos = v3(); + this._pos = new Vec3(); + } + + // in Lumen(lm) + public setLuminousPower (power: number, size: number) { + this._size = size; + this._luminance = power / nt2lm(size); + } + + public getLuminousPower (): number { + return this._luminance * nt2lm(this._size); } public update () { diff --git a/cocos/renderer/scene/spot-light.ts b/cocos/renderer/scene/spot-light.ts index 56b66119fe..d7a9393255 100644 --- a/cocos/renderer/scene/spot-light.ts +++ b/cocos/renderer/scene/spot-light.ts @@ -3,7 +3,7 @@ import { Mat4, Quat, Vec3 } from '../../core/value-types'; import { v3 } from '../../core/value-types/vec3'; import { mat4, vec3 } from '../../core/vmath'; import { Node } from '../../scene-graph/node'; -import { Light, LightType } from './light'; +import { Light, LightType, nt2lm } from './light'; import { RenderScene } from './render-scene'; const _forward = new Vec3(0, 0, -1); @@ -18,8 +18,7 @@ export class SpotLight extends Light { protected _dir: Vec3 = new Vec3(1.0, -1.0, -1.0); protected _size: number = 0.15; protected _range: number = 5.0; - protected _luminance: number = 10000.0; - protected _luminousPower: number = 0.0; + protected _luminance: number = 1700 / nt2lm(this._size); protected _spotAngle: number = Math.cos(Math.PI / 6); protected _pos: Vec3; protected _aabb: aabb; @@ -46,24 +45,15 @@ export class SpotLight extends Light { return this._range; } + // in Nit(nt) set luminance (lum: number) { this._luminance = lum; - this._luminousPower = this._luminance * (4.0 * this._size * this._size * Math.PI * Math.PI); } get luminance (): number { return this._luminance; } - set luminousPower (lm: number) { - this._luminousPower = lm; - this._luminance = this._luminousPower / (4.0 * this._size * this._size * Math.PI * Math.PI); - } - - get luminousPower (): number { - return this._luminousPower; - } - get direction (): Vec3 { return this._dir; } @@ -73,7 +63,7 @@ export class SpotLight extends Light { } set spotAngle (val: number) { - this._angle = val / 2; + this._angle = val * 0.5; this._spotAngle = Math.cos(val * 0.5); } @@ -88,12 +78,21 @@ export class SpotLight extends Light { constructor (scene: RenderScene, name: string, node: Node) { super(scene, name, node); this._type = LightType.SPOT; - this.luminousPower = 1700.0; this._aabb = aabb.create(); this._frustum = frustum.create(); this._pos = v3(); } + // in Lumen(lm) + public setLuminousPower (power: number, size: number) { + this._size = size; + this._luminance = power / nt2lm(size); + } + + public getLuminousPower (): number { + return this._luminance * nt2lm(this._size); + } + public update () { if (this._node) { this._node.getWorldPosition(this._pos);