fix luminance (#4094)

* fix web audio volume

* fix luminance
This commit is contained in:
Yun Hsiao Wu
2019-03-25 13:53:39 +08:00
committed by Weiliang Wang
parent cc0fb16a99
commit 048347b25d
12 changed files with 107 additions and 68 deletions
+1 -1
View File
@@ -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; }
+1 -1
View File
@@ -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 */
+2 -2
View File
@@ -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;
+1 -1
View File
@@ -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;
+2 -1
View File
@@ -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();
}
/**
+4 -1
View File
@@ -79,7 +79,10 @@ export class LightComponent extends Component {
*
* !#ch 光源色温
*/
@property
@property({
slide: true,
range: [1000, 15000, 1],
})
get colorTemperature () {
return this._colorTemperature;
}
+30 -14
View File
@@ -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);
+35 -16
View File
@@ -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);
@@ -22,6 +22,7 @@ export class DirectionalLight extends Light {
return this._dir;
}
// in Lux(lx)
set illuminance (illum: number) {
this._illum = illum;
}
+2
View File
@@ -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) {
+14 -16
View File
@@ -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 () {
+14 -15
View File
@@ -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);