-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathunit3-diffuse-demo.js
147 lines (99 loc) · 3.39 KB
/
unit3-diffuse-demo.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
"use strict"; // good practice - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
////////////////////////////////////////////////////////////////////////////////
// Change angle of light to see how effect decreases with angle
////////////////////////////////////////////////////////////////////////////////
/*global THREE, requestAnimationFrame, dat, window, document*/
var camera, scene, renderer;
var cameraControls;
var ec;
var clock = new THREE.Clock();
var light1, light2, light3;
var ground, lightMesh;
var angle = 0;
function init() {
var canvasWidth = window.innerWidth;
var canvasHeight = window.innerHeight;
// CAMERA
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 80000 );
camera.position.set( 0, 0, 100 );
light1 = new THREE.SpotLight();
light1.color.setRGB(1, 0, 0);
light1.position.set( -100*Math.sin(angle * Math.PI/180), 0, 100*Math.cos(angle * Math.PI/180) );
light1.angle = 0.1;
light1.exponent = 0;
light1.target.position.set( 0, 0, 0 );
var lightMaterial = new THREE.MeshBasicMaterial( { color: 0xffaa00, transparent: true});
var lightSpot = new THREE.SphereGeometry( 2, 32, 16 );
lightMesh = new THREE.Mesh( lightSpot, lightMaterial );
lightMesh.position.x = light1.position.x;
lightMesh.position.z = light1.position.z;
// GROUND
var gg = new THREE.PlaneGeometry( 75, 75, 5, 5 );
var gm = new THREE.MeshPhongMaterial( { color: 0xFFFFFF, side: THREE.DoubleSide} );
gm.specular.setRGB(0,0,0);
var wire = new THREE.MeshBasicMaterial({ color: 0x555555, wireframe: true });
ground = new THREE.SceneUtils.createMultiMaterialObject(gg, [gm, wire]);
//ground = new THREE.Mesh( gg, gm );
ground.position.y = -0.1;
//ground.add(new THREE.AxisHelper(100));
// RENDERER
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( canvasWidth, canvasHeight );
renderer.setClearColorHex( 0xFFFFFF, 1.0 );
var container = document.getElementById('container');
container.appendChild( renderer.domElement );
renderer.gammaInput = true;
renderer.gammaOutput = true;
// EVENTS
window.addEventListener( 'resize', onWindowResize, false );
// CONTROLS
cameraControls = new THREE.OrbitAndPanControls( camera, renderer.domElement );
cameraControls.target.set(0, 0, 0);
fillScene();
// GUI
setupGui();
}
// EVENT HANDLERS
function onWindowResize() {
var canvasWidth = window.innerWidth;
var canvasHeight = window.innerHeight;
renderer.setSize( canvasWidth, canvasHeight );
camera.aspect = canvasWidth/ canvasHeight;
camera.updateProjectionMatrix();
}
function setupGui() {
ec = {
angle: angle
};
var gui = new dat.GUI();
var element = gui.add( ec, "angle", 0.0, 90.0 ).step(0.1);
element.name("Light angle");
}
//
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
var delta = clock.getDelta();
cameraControls.update( delta );
if ( ec.angle !== 0)
{
light1.position.set( -100*Math.sin(ec.angle * Math.PI/180), 0, 100*Math.cos(ec.angle * Math.PI/180) );
lightMesh.position.x = light1.position.x;
lightMesh.position.z = light1.position.z;
}
renderer.render( scene, camera );
}
function fillScene() {
scene = new THREE.Scene();
// LIGHTS
scene.add( light1 );
scene.add( light2 );
scene.add( light3 );
scene.add( ground );
scene.add(lightMesh);
//Coordinates.drawGrid({size:75,scale:0.1, orientation:"z"});
}
init();
animate();