forked from rlamana/a-aviator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shadows.js
68 lines (62 loc) · 2.04 KB
/
shadows.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
/*!
* a-aviator <https://github.com/rlamana/a-aviator>
*
* Copyright (c) 2017, Ramon Lamana.
* Based on Karim Maaloul's code for 'The Aviator':
* <https://github.com/yakudoo/TheAviator>
*
* Released under the MIT License.
*/
/**
* Enables shadowMap in renderer.
*/
AFRAME.registerComponent('shadows', {
schema: { default: true },
init() {
if (this.el.tagName === 'A-SCENE' && this.data) {
this.el.renderer.shadowMap.enabled = true;
this.el.renderer.antialias = true;
this.el.renderer.alpha = true;
}
}
});
/**
* Sets entities's castShadow and receiveShadow properties.
* Works on entities with 'geometry' and/or 'light' components.
*/
AFRAME.registerComponent('cast-shadow', {
dependencies: ['geometry'],
schema: {
cast: { default: false },
receive: { default: false },
cameraLeft: { type: 'number' },
cameraRight: { type: 'number' },
cameraTop: { type: 'number' },
cameraBottom: { type: 'number' },
cameraNear: { type: 'number' },
cameraFar: { type: 'number' },
mapWidth: { type: 'number' },
mapHeight: { type: 'number' }
},
update() {
let object;
if (this.el.hasAttribute('light')) {
const light = this.el.getObject3D('light');
light.shadow.camera.left = this.data['cameraLeft'] || -400;
light.shadow.camera.right = this.data['cameraRight'] || 400;
light.shadow.camera.top = this.data['cameraTop'] || 400;
light.shadow.camera.bottom = this.data['cameraBottom'] || -400;
light.shadow.camera.near = this.data['cameraNear'] || 1;
light.shadow.camera.far = this.data['cameraFar'] || 1000;
light.shadow.mapSize.width = this.data['mapWidth'] || 2048;
light.shadow.mapSize.height = this.data['mapHeight'] || 2048;
object = light;
} else if (this.el.hasAttribute('geometry')) {
object = this.el.getOrCreateObject3D('mesh', THREE.Mesh);
}
if (object) {
object.castShadow = this.data.cast;
object.receiveShadow = this.data.receive;
}
}
});