This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathUniformDescriptor.js
112 lines (84 loc) · 2.93 KB
/
UniformDescriptor.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
import * as THREE from 'three';
import invariant from 'fbjs/lib/invariant';
import PropTypes from 'prop-types';
import Uniform from '../../Uniform';
import UniformContainer from '../../UniformContainer';
import ResourceReference from '../../Resources/ResourceReference';
import THREEElementDescriptor from '../THREEElementDescriptor';
class UniformDescriptor extends THREEElementDescriptor {
constructor(react3Instance) {
super(react3Instance);
this.hasProp('type', {
type: PropTypes.string.isRequired,
simple: true,
});
this.hasProp('value', {
type: PropTypes.any,
update(threeObject, value) {
threeObject.setValue(value);
},
default: null,
});
this.hasProp('name', {
type: PropTypes.string.isRequired,
update: this.triggerRemount,
});
}
construct() {
return new Uniform();
}
applyInitialProps(threeObject, props) {
super.applyInitialProps(threeObject, props);
invariant(props.hasOwnProperty('name'), 'The <uniform/> should have a \'name\' property');
threeObject.name = props.name;
threeObject.value = props.value;
}
setParent(threeObject: Uniform, parentObject3D) {
invariant(parentObject3D instanceof UniformContainer,
'Parent is not a Uniform Container (<uniforms/>)');
const name = threeObject.name;
super.setParent(threeObject, parentObject3D);
parentObject3D.uniforms[name] = {
type: threeObject.type,
value: threeObject.value,
};
threeObject.userData._onValueChanged = (newValue) => {
parentObject3D.uniforms[name].value = newValue;
};
threeObject.userData.events.on('valueChanged', threeObject.userData._onValueChanged);
}
addChildren(threeObject, children) {
invariant(children.filter(this._invalidChild).length === 0,
'Uniform children can only be textures or resource references');
}
addChild(threeObject, child) {
this.addChildren(threeObject, [child]);
}
removeChild() {
// do nothing
}
invalidChildInternal(child) {
const invalid = !(child instanceof THREE.Texture || child instanceof ResourceReference);
return invalid;
}
_invalidChild = child => this.invalidChildInternal(child);
unmount(threeObject) {
threeObject.userData.events.removeListener('valueChanged',
threeObject.userData._onValueChanged);
delete threeObject.userData._onValueChanged;
super.unmount(threeObject);
}
highlight(threeObject) {
const parent = threeObject.userData.markup.parentMarkup.threeObject;
parent.userData._descriptor.highlight(parent);
}
getBoundingBoxes(threeObject) {
const parent = threeObject.userData.markup.parentMarkup.threeObject;
return parent.userData._descriptor.getBoundingBoxes(parent);
}
hideHighlight(threeObject) {
const parent = threeObject.userData.markup.parentMarkup.threeObject;
parent.userData._descriptor.hideHighlight(parent);
}
}
module.exports = UniformDescriptor;