-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpointLight.js
More file actions
182 lines (136 loc) · 6.3 KB
/
pointLight.js
File metadata and controls
182 lines (136 loc) · 6.3 KB
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
/**
* @module pointLight
* @description A light that gets emitted from a single point in all directions.
* @see [PointLight]{@link https://threejs.org/docs/index.html?q=pointLight#api/en/lights/PointLight}
*
* @author [Andrej Hristoliubov]{@link https://github.com/anhr}
*
* @copyright 2011 Data Arts Team, Google Creative Lab
*
* @license under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
import three from './three.js'
import MyPoints from './myPoints/myPoints.js';
import Options from './Options.js'
class pointLight {
/**
* A light that gets emitted from a single point in all directions.
* @see [PointLight]{@link https://threejs.org/docs/index.html?q=pointLight#api/en/lights/PointLight}
* @param {THREE.Group|THREE.Scene} scene [Group]{@link https://threejs.org/docs/index.html?q=gr#api/en/objects/Group} or [Scene]{@link https://threejs.org/docs/index.html?q=sce#api/en/scenes/Scene} of the <b>pointLight</b>.
* @param {object} [settings={}] the following settings are available
* @param {Options} [settings.options=new Options()] <a href="../../jsdoc/Options/Options.html" target="_blank">Options</a> instance. The following options are available.
* See <b>options</b> parameter of <a href="../../myThree/jsdoc/module-MyThree-MyThree.html" target="_blank">MyThree</a> class.
* @param {boolean} [settings.options.pointLight] false - do not use <b>pointLight</b>.
* @param {object} [settings.options.dat] use dat-gui JavaScript Controller Library. [dat.gui]{@link https://github.com/dataarts/dat.gui}.
* See <b>options.dat</b> parameter of <a href="../../myThree/jsdoc/module-MyThree-MyThree.html" target="_blank">MyThree</a> class.
* @param {object} [settings.options.dat.pointLightGui] false - do not adds a <b>pointLight</b> folder. Have effect only if <b>options.pointLight !== false</b>.
* @param {number} [settings.options.scale] is 1
* @param {THREE.Vector3} [settings.position] light position. Default is zero position.
*/
constructor( scene, settings = {} ) {
const options = new Options( settings.options );
if ( options.pointLight === false ) {
this.add = function (){ }
this.controls = function (){ }
return;
}
const dat = three.dat,
THREE = three.THREE,
strLight = 'mathBoxLight',
controllers = {},
multiplier = 2 * options.scale,//options.scale is 1
position = settings.position || new THREE.Vector3();// 0.5 * options.scale, 0.5 * options.scale, 0.5 * options.scale );
const light = new THREE.PointLight( 0xffffff, 1 );
light.position.copy( position );
light.name = strLight;
scene.add( light );
/**
* Adds the folder of the light settings into [gui]{@link https://github.com/anhr/dat.gui}.
* @param {object} [guiParams={}] Followed parameters is allowed.
* @param {THREE.Group|THREE.Scene} [guiParams.group] [Group]{@link https://threejs.org/docs/index.html?q=gr#api/en/objects/Group} or [Scene]{@link https://threejs.org/docs/index.html?q=sce#api/en/scenes/Scene} of the light point.
* @param {GUI} [guiParams.folder] parent folder
* @param {string} [guiParams.folderName] folder name
*/
this.controls = function ( guiParams = {} ) {
const group = guiParams.group || scene, folder = guiParams.folder || options.dat.gui, folderName = guiParams.folderName;
if ( folder === undefined || ( options.dat.pointLightGui === false ) )
return;
//Localization
const lang = {
light: 'Light',
displayLight: 'Display',
displayLightTitle: 'Display or hide the light source.',
defaultButton: 'Default',
restoreLightTitle: 'Restore position of the light source',
};
switch ( options.getLanguageCode() ) {
case 'ru'://Russian language
lang.light = 'Свет';
lang.displayLight = 'Показать';
lang.displayLightTitle = 'Показать или скрыть источник света.';
lang.defaultButton = 'Восстановить';
lang.restoreLightTitle = 'Восстановить положение источника света';
break;
default://Custom language
if ( ( options.lang === undefined ) || ( options.lang.languageCode != _languageCode ) )
break;
Object.keys( options.lang ).forEach( function ( key ) {
if ( lang[key] === undefined )
return;
lang[key] = options.lang[key];
} );
}
const scales = options.scales, fLight = folder.addFolder( folderName || lang.light );
var lightSource;
//displayLight
dat.controllerNameAndTitle( fLight.add( { display: false }, 'display' ).onChange( function ( value ) {
if ( value ) {
new MyPoints( light.position, group, { pointsOptions: {
onReady( points ) { lightSource = points; }
} } );
} else {
group.remove( lightSource );
//delete lightSource;//Parsing error: Deleting local variable in strict mode
lightSource = undefined;
}
} ), lang.displayLight, lang.displayLightTitle );
//move light
function guiLightAxis( axesName ) {
const scale = scales[axesName];
if ( !scale )
return;
controllers[axesName] =
fLight.add( light.position, axesName, scale.min * multiplier, scale.max * multiplier )
.onChange( function ( value ) {
if ( lightSource === undefined )
return;
const i = 0,
itemSize = lightSource.geometry.attributes.position.itemSize,
point = new THREE.Vector3().fromArray( lightSource.geometry.attributes.position.array, i * itemSize );
point[axesName] = value;
point.toArray( lightSource.geometry.attributes.position.array, i * itemSize );
// lightSource.geometry.attributes.position.array[axesId] = value;
lightSource.geometry.attributes.position.needsUpdate = true;
} );
dat.controllerNameAndTitle( controllers[axesName], scale.name );
}
guiLightAxis( 'x' );
guiLightAxis( 'y' );
guiLightAxis( 'z' );
var restore = {
restore: function () {
controllers.x.setValue( position.x );
controllers.y.setValue( position.y );
controllers.z.setValue( position.z );
}
};
dat.controllerNameAndTitle( fLight.add( restore, 'restore' ), lang.defaultButton, lang.restoreLightTitle );
};
return this;
}
}
export default pointLight;