-
Notifications
You must be signed in to change notification settings - Fork 16
/
area-light.js
66 lines (57 loc) · 1.71 KB
/
area-light.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const Signal = require('signals')
const AreaLightsData = require('./area-light-data')
function AreaLight(opts) {
this.type = 'AreaLight'
this.enabled = true
this.changed = new Signal()
this.color = [1, 1, 1, 1]
this.intensity = 1
this.castShadows = false
this.set(opts)
const ctx = opts.ctx
// TODO: area light textures
if (!AreaLight.areaLightTexturesRefs) {
AreaLight.ltc_mat_texture = ctx.texture2D({
data: AreaLightsData.mat,
width: 64,
height: 64,
pixelFormat: ctx.PixelFormat.RGBA32F,
encoding: ctx.Encoding.Linear,
min: ctx.Filter.Linear,
mag: ctx.Filter.Linear
})
AreaLight.ltc_mag_texture = ctx.texture2D({
data: AreaLightsData.mag,
width: 64,
height: 64,
pixelFormat: ctx.PixelFormat.R32F,
encoding: ctx.Encoding.Linear,
min: ctx.Filter.Linear,
mag: ctx.Filter.Linear
})
}
AreaLight.areaLightTexturesRefs = (AreaLight.areaLightTexturesRefs || 0) + 1
this.ltc_mat_texture = AreaLight.ltc_mat_texture
this.ltc_mag_texture = AreaLight.ltc_mag_texture
}
AreaLight.prototype.init = function(entity) {
this.entity = entity
}
AreaLight.prototype.set = function(opts) {
Object.assign(this, opts)
Object.keys(opts).forEach((prop) => this.changed.dispatch(prop))
if (opts.color !== undefined || opts.intensity !== undefined) {
this.color[3] = this.intensity
}
}
AreaLight.prototype.dispose = function() {
if (--AreaLight.areaLightTexturesRefs === 0) {
this.ctx.dispose(AreaLight.ltc_mat_texture)
AreaLight.ltc_mat_texture = null
this.ctx.dispose(AreaLight.ltc_mag_texture)
AreaLight.ltc_mag_texture = null
}
}
module.exports = function(opts) {
return new AreaLight(opts)
}