-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathViro3DObject.tsx
194 lines (178 loc) · 6.01 KB
/
Viro3DObject.tsx
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
192
193
194
import * as React from "react";
import {
findNodeHandle,
Image,
ImageSourcePropType,
NativeModules,
NativeSyntheticEvent,
requireNativeComponent,
} from "react-native";
import {
ViroErrorEvent,
ViroLoadEndEvent,
ViroLoadStartEvent,
} from "./Types/ViroEvents";
import { ViroNativeRef } from "./Types/ViroUtils";
import { checkMisnamedProps } from "./Utilities/ViroProps";
import { ViroBase } from "./ViroBase";
const { resolveAssetSource } = Image;
type Props = {
type: "OBJ" | "VRX" | "GLTF" | "GLB";
/**
* The model file, which is required
*/
source: ImageSourcePropType;
/**
* Additional resource files for various model formats
*/
resources?: ImageSourcePropType[];
/**
* TODO: what does this do?
*/
morphTargets?: Array<{
target?: string;
weight?: number;
}>;
onLoadStart?: (event: NativeSyntheticEvent<ViroLoadStartEvent>) => void;
onLoadEnd?: (event: NativeSyntheticEvent<ViroLoadEndEvent>) => void;
onError?: (event: NativeSyntheticEvent<ViroErrorEvent>) => void;
};
/**
* Viro3DObject is a component that is used to render 3D models in the scene.
*/
export class Viro3DObject extends ViroBase<Props> {
render() {
checkMisnamedProps("Viro3DObject", this.props);
const modelsrc = resolveAssetSource(this.props.source);
const resources = this.props.resources?.map((resource) =>
resolveAssetSource(resource)
);
// Since materials and transformBehaviors can be either a string or an array, convert the string to a 1-element array.
const materials =
typeof this.props.materials === "string"
? [this.props.materials]
: this.props.materials;
const transformBehaviors =
typeof this.props.transformBehaviors === "string"
? [this.props.transformBehaviors]
: this.props.transformBehaviors;
// @ts-ignore since onFuse could be a function, but this should be fine either way.
const timeToFuse = this.props.onFuse?.timeToFuse;
// Always autogenerate a compound shape for 3DObjects if no shape is defined.
const newPhysicsBody = this.props.physicsBody && {
...this.props.physicsBody,
shape: this.props.physicsBody.shape || { type: "compound" },
};
let highAccuracyEvents = this.props.highAccuracyEvents;
if (
this.props.highAccuracyEvents == undefined &&
this.props.highAccuracyGaze != undefined
) {
console.warn(
"**DEPRECATION WARNING** highAccuracyGaze has been deprecated/renamed to highAccuracyEvents"
);
highAccuracyEvents = this.props.highAccuracyGaze;
}
// Called from native on the event a positional change has occured
// for the underlying control within the renderer.
const transformDelegate =
this.props.onTransformUpdate != undefined
? this._onNativeTransformUpdate
: undefined;
return (
<VRT3DObject
{...this.props}
ref={(component: ViroNativeRef) => {
this._component = component;
}}
highAccuracyEvents={highAccuracyEvents}
onNativeTransformDelegateViro={transformDelegate}
hasTransformDelegate={this.props.onTransformUpdate != undefined}
physicsBody={newPhysicsBody}
source={modelsrc}
resources={resources}
materials={materials}
transformBehaviors={transformBehaviors}
canHover={this.props.onHover != undefined}
canClick={
this.props.onClick != undefined ||
this.props.onClickState != undefined
}
canTouch={this.props.onTouch != undefined}
canScroll={this.props.onScroll != undefined}
canSwipe={this.props.onSwipe != undefined}
canDrag={this.props.onDrag != undefined}
canFuse={this.props.onFuse != undefined}
canPinch={this.props.onPinch != undefined}
canRotate={this.props.onRotate != undefined}
onHoverViro={this._onHover}
onClickViro={this._onClickState}
onTouchViro={this._onTouch}
onScrollViro={this._onScroll}
onSwipeViro={this._onSwipe}
onDragViro={this._onDrag}
onFuseViro={this._onFuse}
onPinchViro={this._onPinch}
onRotateViro={this._onRotate}
onLoadStartViro={this._onLoadStart}
onLoadEndViro={this._onLoadEnd}
onErrorViro={this._onError}
onAnimationStartViro={this._onAnimationStart}
onAnimationFinishViro={this._onAnimationFinish}
timeToFuse={timeToFuse}
canCollide={this.props.onCollision != undefined}
onCollisionViro={this._onCollision}
/>
);
}
_onLoadStart = (event: NativeSyntheticEvent<ViroLoadStartEvent>) => {
this.props.onLoadStart && this.props.onLoadStart(event);
};
_onLoadEnd = (event: NativeSyntheticEvent<ViroLoadEndEvent>) => {
this.props.onLoadEnd && this.props.onLoadEnd(event);
};
getBoundingBoxAsync = (): Promise<any> => {
return NativeModules.VRTNodeModule.getBoundingBox(findNodeHandle(this));
};
getMorphTargets = (): Promise<any> => {
return NativeModules.VRTNodeModule.getMorphTargets(findNodeHandle(this));
};
}
// We can probably give a better type for this, but it's not exposed ouside this file so not urgent
const VRT3DObject = requireNativeComponent<any>(
"VRT3DObject",
// @ts-ignore type signature incorrect, or extra arguments are ignored?
Viro3DObject,
{
nativeOnly: {
canHover: true,
canClick: true,
canTouch: true,
canScroll: true,
canSwipe: true,
canDrag: true,
canFuse: true,
canPinch: true,
canRotate: true,
onHoverViro: true,
onClickViro: true,
onTouchViro: true,
onScrollViro: true,
onPinchViro: true,
onRotateViro: true,
onSwipeViro: true,
onDragViro: true,
onLoadStartViro: true,
onLoadEndViro: true,
onErrorViro: true,
onFuseViro: true,
timeToFuse: true,
canCollide: true,
onCollisionViro: true,
onNativeTransformDelegateViro: true,
hasTransformDelegate: true,
onAnimationStartViro: true,
onAnimationFinishViro: true,
},
}
);