forked from DarkArtsAndSciences/OculusRiftSceneKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avatar.mm
183 lines (152 loc) · 5.12 KB
/
avatar.mm
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
#import "avatar.h"
#import "OculusRiftSceneKitView.h"
#import <Extras/OVR_Math.h>
using namespace OVR;
@implementation AvatarHead
@synthesize leftEye;
@synthesize rightEye;
-(id)initWithPivotToEyes:(CGFloat)pivotToEyes
{
self = [super init];
if (self == nil) return nil;
// create nodes for eye cameras and head sensors
OculusRiftDevice *hmd = [OculusRiftDevice getDevice];
SCNNode *(^nodeForEye)(ovrEyeType) = ^(ovrEyeType eye)
{
Vector3f displace = [hmd renderDescForEye: eye].HmdToEyeViewOffset;
FovPort fov = [hmd renderDescForEye: eye].Fov;
// TODO: read these from the HMD?
SCNCamera *camera = [SCNCamera camera];
camera.xFov = fov.GetHorizontalFovDegrees();
camera.yFov = fov.GetVerticalFovDegrees();
camera.zNear = 0.01;
camera.zFar = 1000;
SCNNode *node = [SCNNode node];
node.camera = camera;
// obviously the when we tilt our head, we should have a shift in eye position as well
// here I move eyes up by an IPD, but I am not sure if this the the best way
node.position = SCNVector3Make(-displace.x, pivotToEyes, -0.05+displace.z);
return node;
};
leftEye = nodeForEye(ovrEye_Left);
rightEye = nodeForEye(ovrEye_Right);
[self addChildNode:leftEye];
[self addChildNode:rightEye];
return self;
}
@end
@implementation Avatar
{
NSTimeInterval eventTimeStamp;
NSPoint lastMousePosition;
}
@synthesize velocity;
@synthesize angularVelocity;
@synthesize head;
#pragma mark - Initialization
- (SCNVector3) facing {
Vector3f vec = Vector3f(0, 0, -1);
SCNVector4 dir = self.orientation;
Quatf rot = Quatf(dir.x, dir.y, dir.z, dir.w);
vec = rot.Rotate(vec);
return SCNVector3Make(vec.x, vec.y, vec.z);
}
- (id)initWithEyeHeight:(CGFloat)eyeHeight
pivotToEyes:(CGFloat)pivotToEyes
{
if (!(self = [super init])) return nil;
// default speed
velocity = SCNVector3Zero;
eventTimeStamp = [NSDate timeIntervalSinceReferenceDate];
// head node
head = [self makeHeadWithPivotToEyes:pivotToEyes];
head.position = SCNVector3Make(0, eyeHeight-pivotToEyes, 0);
[self addChildNode:head];
[self load];
return self;
}
#pragma mark - Avatar head rotation
- (AvatarHead*)makeHeadWithPivotToEyes:(CGFloat)pivotToEyes
{
return [[AvatarHead alloc] initWithPivotToEyes:pivotToEyes];
}
- (void) load
{
// load file
// TODO: code works, avatar.dae is broken?
//NSURL *url = [NSURL URLWithString:@"file:///Path/to/file.dae"];
//NSURL *url = [NSURL URLWithString:@"http://www.path.to/file.dae"];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"avatar" withExtension:@"dae"];
if (url == nil) return;
SCNSceneSource *sceneSource = [[SCNSceneSource alloc]initWithURL:url options:nil];
SCNScene *scene = [sceneSource sceneWithOptions: [NSDictionary dictionary] error: nil];
SCNNode *avatar = nil;
if (scene == nil) {
NSLog(@" No nodes in DAE file");
avatar = [SCNNode node];
} else {
avatar = [scene.rootNode clone];
//NSLog(@"using node %@", node);
}
avatar.position = SCNVector3Zero;
[self addChildNode:avatar];
}
#pragma mark - Avatar movement
// TODO: 2D turning, 3D movement (flying instead of walking)
// TODO: add diagonal 2D movement (add and normalize vectors)
// MAYBE: add XY WASD movement (locked to world, not direction facing)
- (void)tick
{
NSTimeInterval time = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval dt = time - eventTimeStamp;
self.position = SCNVector3Make(self.position.x + velocity.x * dt,
self.position.y + velocity.y * dt,
self.position.z + velocity.z * dt);
[self rotateY: angularVelocity * dt];
eventTimeStamp = time;
}
#pragma mark - Convenience functions for creating lights and objects
// Make a spotlight that automatically points wherever the user looks.
- (SCNLight*)makeAvatarSpotlight
{
SCNLight *avatarLight = [SCNLight light];
avatarLight.type = SCNLightTypeSpot;
avatarLight.castsShadow = YES;
SCNNode *avatarLightNode = [SCNNode node];
avatarLightNode.light = avatarLight;
[head addChildNode:avatarLightNode];
return avatarLight; // caller can set light color, etc.
}
// Make an omnilight that automatically follows the user.
- (SCNLight*)makeAvatarOmnilight
{
SCNLight *avatarLight = [SCNLight light];
avatarLight.type = SCNLightTypeOmni;
SCNNode *avatarLightNode = [SCNNode node];
avatarLightNode.light = avatarLight;
[head addChildNode: avatarLightNode];
return avatarLight; // caller can set light color, etc.
}
#pragma mark - Standard control schemes
//- (void)addEventHandlersForBothMouseDownMove // TODO: both buttons at once
// TODO: QE turning
// TODO: defaults for space, return, esc?
// MAYBE: jump
#pragma mark - Event handlers
SCNVector3 rotateY(SCNVector3 direction, CGFloat angle)
{
CGFloat x=direction.x, y=direction.y;
SCNVector3 dir = SCNVector3Make(cos(angle)*x-sin(angle)*y,
sin(angle)*x+cos(angle)*y,
0);
return dir;
}
- (void) rotateY: (CGFloat) angle
{
SCNVector4 orientation = self.orientation;
Quatf quat = Quatf(orientation.x, orientation.y, orientation.z, orientation.w);
Quatf rot = Quatf(Vector3f(0, 1, 0), angle);
quat = Quatf(Matrix4f(rot)*Matrix4f(quat));
self.orientation = SCNVector4Make(quat.x, quat.y, quat.z, quat.w);
}
@end