-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
191 lines (166 loc) · 5.02 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* global AFRAME */
/* global THREE */
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.')
}
if (typeof THREE === 'undefined') {
throw new Error('Component attempted to register before THREE was available.')
}
//https://github.com/mrdoob/three.js/blob/master/examples/js/objects/Lensflare.js
require('./Lensflare')
/**
* A-Frame Lensflare Component component for A-Frame.
*/
AFRAME.registerComponent('lensflare', {
schema: {
src: {
type: 'asset'
},
createLight: {
type: 'boolean',
default: true
},
position: {
type: 'vec3'
},
target: {
type: 'string'
},
intensity: {
type: 'number',
default: 5
},
relative: {
type: 'boolean',
default: true
},
size: {
type: 'number',
default: 500
},
lightColor: {
type: 'string',
default: 'rgb(255, 255, 255)'
},
lightDistance: {
type: 'number',
default: 4.0,
},
lightAngle: {
type: 'number',
default: Math.PI / 3,
},
lightPenumbra: {
type: 'number',
default: 0.077,
},
lightDecay: {
type: 'number',
default: 1,
},
lightType: {
default: 'spot',
oneOf: ['directional', 'point', 'spot']
}
},
/**
* Set if component needs multiple instancing.
*/
multiple: true,
/**
* setLightType - Create a light based on lightType
*
* @param {String} type Type of the light, supplied as a string.
* @param {Object} settings Additional settings to pass to the light. E.g. angle and decay
* @return {THREE.Light} A THREE.JS light object
*/
setLightType: function (type, settings) {
switch (type) {
case 'spot':
return new THREE.SpotLight(new THREE.Color(settings.lightColor), settings.intensity, settings.lightDistance, settings.lightAngle, settings.lightPenumbra, settings.lightDecay)
case 'point':
return new THREE.PointLight(new THREE.Color(settings.lightColor), settings.intensity, settings.lightDistance, settings.lightDecay)
case 'directional':
return new THREE.DirectionalLight(new THREE.Color(settings.lightColor), settings.intensity)
}
},
/**
* Called once when component is attached. Generally for initial setup.
*/
init: function () {
const scene = document.querySelector('a-scene').object3D;
const parentEl = this.el.object3D
const sceneEl = this.el.sceneEl.object3D
//Determine positioning
const position = this.data.relative ? new THREE.Vector3(0, 0, 0) : this.data.position
//Load texture (Three r84 upward doesn't support progress)
const textureLoader = new THREE.TextureLoader()
const textureFlare = textureLoader.load(this.data.src.currentSrc,
function (texture) {
return texture
},
undefined,
function (error) {
throw new Error('An error occured loading the Flare texture')
}
)
this.lensFlare = new THREE.Lensflare()
this.lensFlareElement = new THREE.LensflareElement(textureFlare, this.data.size, 0.0, new THREE.Color(this.data.lightColor))
this.lensFlare.addElement(this.lensFlareElement);
this.lensFlare.position.copy(position)
//Determine if the user wants a light
if (this.data.createLight) {
const light = this.setLightType(this.data.lightType.toLowerCase(), this.data)
//Has a target been supplied?
const hasTarget = this.data.target ? this.data.target : false
//Set light target.
if (hasTarget) {
light.target = document.querySelector(this.data.target).object3D
sceneEl.add(light.target)
sceneEl.updateMatrixWorld()
}
light.position.set(position.x, position.y, position.z)
//If relative, we want to attach the lensflare, and the light as child objects and call updateMatrixWorld once.
if (this.data.relative) {
light.add(this.lensFlare)
parentEl.add(light)
sceneEl.updateMatrixWorld()
} else {
scene.add(light)
}
} else {
//If relative, we want to attach the lensflare as a child object. This is so our lensflare works with animation updates.
if (this.data.relative) {
parentEl.add(this.lensFlare)
sceneEl.updateMatrixWorld()
} else {
scene.add(this.lensFlare)
}
}
},
/**
* Called when component is attached and when component data changes.
* Generally modifies the entity based on the data.
*/
update: function (oldData) {
},
/**
* Called when a component is removed (e.g., via removeAttribute).
* Generally undoes all modifications to the entity.
*/
remove: function () { },
/**
* Called on each scene tick.
*/
// tick: function (t) { },
/**
* Called when entity pauses.
* Use to stop or remove any dynamic or background behavior such as events.
*/
pause: function () { },
/**
* Called when entity resumes.
* Use to continue or add any dynamic or background behavior such as events.
*/
play: function () { }
});