質問編集履歴

1

コードを増やしました。

2018/10/05 06:22

投稿

ehuronkon35
ehuronkon35

スコア33

test CHANGED
File without changes
test CHANGED
@@ -96,179 +96,545 @@
96
96
 
97
97
 
98
98
 
99
+
100
+
99
101
  ```C#
100
102
 
101
103
  コード
102
104
 
105
+ using UnityEngine;
106
+
103
- using System.Collections;
107
+ using System;
104
108
 
105
109
  using System.Collections.Generic;
106
110
 
107
- using UnityEngine;
108
-
109
-
110
-
111
- public class FOVELookSample : MonoBehaviour
112
-
113
- {
114
-
115
- public Light attachedLight;
116
-
117
- public FoveInterfaceBase foveInterface;
118
-
119
-
120
-
121
- private Collider my_collider;
122
-
123
- private Material material;
124
-
125
- private bool light_attached = false;
126
-
127
- // Update is called once per frame
128
-
129
- public float GazingTime = 0.2f; // 見つめ時間...何秒見つめ続けると反応するかを設定する
130
-
131
- private float currentGazingTime; // 見つめ時間カウント用のフィールド
132
-
133
- private bool lightEnabled; // 現在ライトが点灯中かどうかを示すフィールド
134
-
135
- // Use this for initialization
136
-
137
- void Start()
138
-
139
- {
140
-
141
- my_collider = GetComponent<Collider>();
142
-
143
-
144
-
145
-
146
-
147
- if (attachedLight)
148
-
149
- {
150
-
151
- light_attached = true;
152
-
153
- attachedLight.enabled = false;
154
-
155
- }
156
-
157
- material = gameObject.GetComponent<Renderer>().material;
158
-
159
-
160
-
161
- if (material == null)
162
-
163
- gameObject.SetActive(false);
164
-
165
- }
166
-
167
-
168
-
169
-
170
-
171
- void Update()
172
-
173
- {
174
-
175
- if (lightEnabled)
176
-
177
- {
178
-
179
- // もしライト点灯中の状態で...
180
-
181
- /* if (!foveInterface.Gazecast(my_collider))
182
-
183
- {
184
-
185
- // Gazecastがヒットしなかった場合、視線が外れたのでライトを消灯
186
-
187
- lightEnabled = false;
188
-
189
- currentGazingTime = GazingTime;
190
-
191
- material.DisableKeyword("_EMISSION");
192
-
193
-
194
-
195
- if (light_attached)
196
-
197
- {
198
-
199
- material.SetColor("_EmissionColor", Color.black);
200
-
201
- attachedLight.enabled = false;
202
-
203
- DynamicGI.SetEmissive(GetComponent<Renderer>(), Color.black);
204
-
205
- }
206
-
207
- }*/
208
-
209
- }
210
-
211
- else
212
-
213
- {
214
-
215
- // もしライト消灯中の状態で...
216
-
217
- if (foveInterface.Gazecast(my_collider))
218
-
219
- {
220
-
221
- // Gazecastがヒットした場合...
222
-
223
-
224
-
225
- // currentGazingTimeを経過時間分だけ減らす
226
-
227
- currentGazingTime -= Time.deltaTime;
228
-
229
-
230
-
231
- if (currentGazingTime <= 0.0f)
232
-
233
- {
234
-
235
- // currentGazingTimeが0以下になったら、見つめ続けてGazingTime秒以上経過したので、ライトを点灯する
236
-
237
- lightEnabled = true;
238
-
239
- material.EnableKeyword("_EMISSION");
240
-
241
-
242
-
243
- if (light_attached)
244
-
245
- {
246
-
247
- material.SetColor("_EmissionColor", attachedLight.color);
248
-
249
- attachedLight.enabled = false; //ライトが点く
250
-
251
- DynamicGI.SetEmissive(GetComponent<Renderer>(), attachedLight.color);
252
-
253
- }
254
-
255
- }
256
-
257
- }
258
-
259
- else
260
-
261
- {
262
-
263
- // Gazecastがヒットしなかった場合、視線が外れたのでcurrentGazingTimeをGazingTimeにリセット
264
-
265
- currentGazingTime = GazingTime;
266
-
267
- }
268
-
269
- }
270
-
271
- }
111
+ using System.Runtime.Remoting.Messaging;
112
+
113
+ using Fove.Managed;
114
+
115
+
116
+
117
+ [RequireComponent(typeof(Camera))]
118
+
119
+ [AddComponentMenu("")] // hide FoveEyeCamera from mono behaviour menus to discourage people adding it by hand
120
+
121
+ public class FoveEyeCamera : MonoBehaviour {
122
+
123
+
124
+
125
+ [SerializeField]
126
+
127
+ public EFVR_Eye whichEye = EFVR_Eye.Left;
128
+
129
+
130
+
131
+ [SerializeField]
132
+
133
+ public bool suppressProjectionUpdates = true;
134
+
135
+
136
+
137
+ [SerializeField]
138
+
139
+ [Range(0.1f, 4f)]
140
+
141
+ public float resolutionScale = 1.4f;
142
+
143
+ public int antiAliasing = 1;
144
+
145
+
146
+
147
+ private float _lastScale;
148
+
149
+ private int _lastAA;
150
+
151
+
152
+
153
+ private FVRHeadset _headset = null;
154
+
155
+ private Camera _cam = null;
156
+
157
+ private FVRCompositor _compositor;
158
+
159
+ public FoveInterfaceBase foveInterfaceBase;
160
+
161
+
162
+
163
+ private SFVR_CompositorLayerSubmitInfo _layerSubmitInfo;
164
+
165
+
166
+
167
+ private static bool couldUseNewMatrices;
168
+
169
+ private static Matrix4x4 leftProjectionMatrix;
170
+
171
+ private static Matrix4x4 rightProjectionMatrix;
172
+
173
+
174
+
175
+ private class FoveCameraPair
176
+
177
+ {
178
+
179
+ public SFVR_CompositorLayer layer;
180
+
181
+
182
+
183
+ public FoveEyeCamera left;
184
+
185
+ public FoveEyeCamera right;
186
+
187
+
188
+
189
+ // Private so nobody can make one this way
190
+
191
+ private FoveCameraPair() {}
192
+
193
+
194
+
195
+ public FoveCameraPair(FVRCompositor compositor, FoveInterfaceBase xface)
196
+
197
+ {
198
+
199
+ left = null;
200
+
201
+ right = null;
202
+
203
+
204
+
205
+ var createInfo = new SFVR_CompositorLayerCreateInfo
206
+
207
+ {
208
+
209
+ alphaMode = EFVR_AlphaMode.Auto,
210
+
211
+ disableDistortion = xface.DistortionDisabled,
212
+
213
+ disableTimewarp = xface.TimewarpDisabled,
214
+
215
+ disableFading = xface.FadingDisabled,
216
+
217
+ type = xface.LayerType
218
+
219
+ };
220
+
221
+
222
+
223
+ var err = compositor.CreateLayer(createInfo, out layer);
224
+
225
+ if (err != EFVR_ErrorCode.None)
226
+
227
+ {
228
+
229
+ Debug.LogError("[FOVE] Error getting layer: " + err);
230
+
231
+ }
232
+
233
+
234
+
235
+ Debug.Log("[FOVE] Layer requested no distortion? " + createInfo.disableDistortion);
236
+
237
+ }
238
+
239
+
240
+
241
+ public void SetCamera(EFVR_Eye whichEye, FoveEyeCamera cam)
242
+
243
+ {
244
+
245
+ switch (whichEye)
246
+
247
+ {
248
+
249
+ case EFVR_Eye.Left:
250
+
251
+ left = cam;
252
+
253
+ break;
254
+
255
+ case EFVR_Eye.Right:
256
+
257
+ right = cam;
258
+
259
+ break;
260
+
261
+ }
262
+
263
+ }
264
+
265
+
266
+
267
+ public bool CanUseCamera(EFVR_Eye whichEye, FoveEyeCamera cam)
268
+
269
+ {
270
+
271
+ switch (whichEye)
272
+
273
+ {
274
+
275
+ case EFVR_Eye.Left:
276
+
277
+ return left == null || left == cam;
278
+
279
+ case EFVR_Eye.Right:
280
+
281
+ return right == null || right == cam;
282
+
283
+ }
284
+
285
+
286
+
287
+ return false;
288
+
289
+ }
290
+
291
+ }
292
+
293
+
294
+
295
+ private static List<FoveCameraPair> _layerCameraPairs = new List<FoveCameraPair>();
296
+
297
+
298
+
299
+ private static FoveCameraPair GetNextLayerPair(EFVR_Eye whichEye, FoveEyeCamera cam)
300
+
301
+ {
302
+
303
+ if (whichEye != EFVR_Eye.Left && whichEye != EFVR_Eye.Right)
304
+
305
+ return null;
306
+
307
+
308
+
309
+ foreach (var pair in _layerCameraPairs)
310
+
311
+ {
312
+
313
+ if (pair.CanUseCamera(whichEye, cam))
314
+
315
+ return pair;
316
+
317
+ }
318
+
319
+
320
+
321
+ var p = new FoveCameraPair(cam._compositor, cam.foveInterfaceBase);
322
+
323
+ _layerCameraPairs.Add(p);
324
+
325
+
326
+
327
+ return p;
328
+
329
+ }
330
+
331
+
332
+
333
+ public FVRCompositor Compositor
334
+
335
+ {
336
+
337
+ set
338
+
339
+ {
340
+
341
+ _compositor = value;
342
+
343
+ }
344
+
345
+ }
346
+
347
+
348
+
349
+ private static bool _isProjectionErrorFree;
350
+
351
+
352
+
353
+ private void UpdateProjectionMatrix()
354
+
355
+ {
356
+
357
+ // Get the projection matrix from the FOVE SDK and apply it to the camera
358
+
359
+ if (!_isProjectionErrorFree || couldUseNewMatrices)
360
+
361
+ {
362
+
363
+ SFVR_Matrix44 fove_mx_left, fove_mx_right;
364
+
365
+ var err = _headset.GetProjectionMatricesRH(_cam.nearClipPlane, _cam.farClipPlane, out fove_mx_left,
366
+
367
+ out fove_mx_right);
368
+
369
+ if (err != EFVR_ErrorCode.None)
370
+
371
+ {
372
+
373
+ _isProjectionErrorFree = false;
374
+
375
+ }
376
+
377
+ else
378
+
379
+ {
380
+
381
+ _isProjectionErrorFree = true;
382
+
383
+ leftProjectionMatrix = FoveUnityUtils.GetUnityMx(fove_mx_left);
384
+
385
+ rightProjectionMatrix = FoveUnityUtils.GetUnityMx(fove_mx_right);
386
+
387
+
388
+
389
+ couldUseNewMatrices = false;
390
+
391
+ }
392
+
393
+ }
394
+
395
+
396
+
397
+ switch (whichEye)
398
+
399
+ {
400
+
401
+ case EFVR_Eye.Left:
402
+
403
+ _cam.projectionMatrix = leftProjectionMatrix;
404
+
405
+ break;
406
+
407
+ case EFVR_Eye.Right:
408
+
409
+ _cam.projectionMatrix = rightProjectionMatrix;
410
+
411
+ break;
412
+
413
+ default:
414
+
415
+ Debug.Log("Fove eye camera not set to left or right");
416
+
417
+ break;
418
+
419
+ }
420
+
421
+
422
+
423
+ }
424
+
425
+
426
+
427
+ private void SetSubmitBounds(ref SFVR_CompositorLayerEyeSubmitInfo info)
428
+
429
+ {
430
+
431
+ info.bounds.left = 0.0f;
432
+
433
+ info.bounds.bottom = 0.0f;
434
+
435
+ info.bounds.right = 1.0f;
436
+
437
+ info.bounds.top = 1.0f;
438
+
439
+ }
440
+
441
+
442
+
443
+ void Start() {
444
+
445
+ _headset = FoveInterfaceBase.GetFVRHeadset();
446
+
447
+
448
+
449
+ _lastScale = resolutionScale;
450
+
451
+ _lastAA = antiAliasing;
452
+
453
+
454
+
455
+ FoveCameraPair myPair = GetNextLayerPair(whichEye, this);
456
+
457
+ myPair.SetCamera(whichEye, this);
458
+
459
+ SFVR_CompositorLayer layer = myPair.layer;
460
+
461
+ SFVR_Vec2i res = layer.idealResolutionPerEye;
462
+
463
+
464
+
465
+ var rt = new RenderTexture((int)(res.x * resolutionScale), (int)(res.y * resolutionScale), 24);
466
+
467
+ rt.antiAliasing = antiAliasing;
468
+
469
+
470
+
471
+ _cam = gameObject.GetComponent<Camera>();
472
+
473
+ _cam.targetTexture = rt;
474
+
475
+ _cam.enabled = false;
476
+
477
+
478
+
479
+ switch (whichEye)
480
+
481
+ {
482
+
483
+ case EFVR_Eye.Left:
484
+
485
+ SetSubmitBounds(ref _layerSubmitInfo.left);
486
+
487
+ _layerSubmitInfo.left.texInfo.colorSpace = EFVR_ColorSpace.Linear;
488
+
489
+ break;
490
+
491
+ case EFVR_Eye.Right:
492
+
493
+ SetSubmitBounds(ref _layerSubmitInfo.right);
494
+
495
+ _layerSubmitInfo.left.texInfo.colorSpace = EFVR_ColorSpace.Linear;
496
+
497
+ break;
498
+
499
+ }
500
+
501
+
502
+
503
+ _layerSubmitInfo.layerId = myPair.layer.layerId;
504
+
505
+ }
506
+
507
+
508
+
509
+ void Update()
510
+
511
+ {
512
+
513
+ // For live updating of resolution scale and antialiasing settings
514
+
515
+ const float tolerance = 0.0001f;
516
+
517
+ if (Math.Abs(resolutionScale - _lastScale) > tolerance || antiAliasing != _lastAA)
518
+
519
+ {
520
+
521
+ var rt = new RenderTexture((int)(1280 * resolutionScale), (int)(1440 * resolutionScale), 24);
522
+
523
+ rt.antiAliasing = antiAliasing;
524
+
525
+
526
+
527
+ _cam.targetTexture.Release();
528
+
529
+ _cam.targetTexture = rt;
530
+
531
+
532
+
533
+ _lastScale = resolutionScale;
534
+
535
+ _lastAA = antiAliasing;
536
+
537
+ }
538
+
539
+ }
540
+
541
+
542
+
543
+ void OnPreCull() {
544
+
545
+ UpdateProjectionMatrix();
546
+
547
+ }
548
+
549
+
550
+
551
+ void OnRenderImage(RenderTexture source, RenderTexture destination)
552
+
553
+ {
554
+
555
+ Graphics.Blit(source, destination);
556
+
557
+
558
+
559
+ if (_compositor == null)
560
+
561
+ return;
562
+
563
+
564
+
565
+ IntPtr texPtr = source.GetNativeTexturePtr();
566
+
567
+ if (texPtr != IntPtr.Zero)
568
+
569
+ {
570
+
571
+ _layerSubmitInfo.pose = FoveInterface.GetLastPose();
572
+
573
+ switch (whichEye)
574
+
575
+ {
576
+
577
+ case EFVR_Eye.Left:
578
+
579
+ _layerSubmitInfo.left.texInfo.pTexture = texPtr;
580
+
581
+ _layerSubmitInfo.right.texInfo.pTexture = IntPtr.Zero;
582
+
583
+ break;
584
+
585
+ case EFVR_Eye.Right:
586
+
587
+ _layerSubmitInfo.left.texInfo.pTexture = IntPtr.Zero;
588
+
589
+ _layerSubmitInfo.right.texInfo.pTexture = texPtr;
590
+
591
+ break;
592
+
593
+ default:
594
+
595
+ Debug.LogError("[FOVE] Camera set to " + whichEye + " which isn't supported.");
596
+
597
+ return;
598
+
599
+ }
600
+
601
+
602
+
603
+ var result = _compositor.Submit(ref _layerSubmitInfo);
604
+
605
+ if (result != EFVR_ErrorCode.None)
606
+
607
+ {
608
+
609
+ Debug.LogWarning("[FOVE] Submit returned unexpected " + result);
610
+
611
+ }
612
+
613
+
614
+
615
+ GL.Flush();
616
+
617
+ }
618
+
619
+ else
620
+
621
+ {
622
+
623
+ Debug.LogWarning("RenderTexture native pointer is null; cannot submit null texture pointers.");
624
+
625
+ }
626
+
627
+
628
+
629
+ if (!suppressProjectionUpdates)
630
+
631
+ {
632
+
633
+ couldUseNewMatrices = true;
634
+
635
+ }
636
+
637
+ }
272
638
 
273
639
  }
274
640