-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodesnippets.txt
552 lines (444 loc) · 19.5 KB
/
codesnippets.txt
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
This example shows how to select a file using the OpenFileDialog dialog box. OpenFileDialog allows users to select files. It is found in System.Windows.Forms namespace and it displays the standard Windows dialog box.
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Title = "Select A File";
openDialog.Filter = "Text Files (*.txt)|*.txt" + "|" +
"Image Files (*.png;*.jpg)|*.png;*.jpg" + "|" +
"All Files (*.*)|*.*";
if (openDialog.ShowDialog() == DialogResult.OK)
{
string file = openDialog.FileName;
}
Quaternion CalcEleGivenAng(float ang, float dis)
{
///<summary>
/// Given an angle, calculate its position in Unity coordinates
///</summary>
Quaternion pos = Quaternion.AngleAxis(Camera.main.transform.eulerAngles.y + ang, Vector3.up);
Debug.Log("Elevation as quaternion: " + pos.eulerAngles);
return pos;
}
Quaternion CalcAziGivenAng(float ang, float dis)
{
///<summary>
/// Given an angle, calculate its position in Unity coordinates
///</summary>
Quaternion pos = Quaternion.AngleAxis(Camera.main.transform.eulerAngles.x + ang, Vector3.right);
Debug.Log("Azimuth as quaternion: " + pos.eulerAngles);
return pos;
}
float CalcAzimuth(GameObject go1, GameObject go2)
{
///<summary>
/// Given two game objects, calculate their azimuth
/// </summary>
// get x,z coordinates of objects
var vec1 = new Vector2(go1.transform.position.x, go1.transform.position.z);
var vec2 = new Vector2(go2.transform.position.x, go2.transform.position.z);
// get camera offset along the same axes
var vecCam = new Vector2
(Camera.main.transform.position.x, Camera.main.transform.position.z);
// calc angle, removing offset from camera
return Vector2.SignedAngle(vec1 - vecCam, vec2 - vecCam);
}
float CalcElevation(GameObject go1, GameObject go2)
{
///<summary>
/// Given two game objects, calculate their elevation
/// </summary>
// get x,y coordinates of objects
var vec1 = new Vector2(go1.transform.position.z, go1.transform.position.y);
var vec2 = new Vector2(go2.transform.position.z, go2.transform.position.y);
var vecCam = new Vector2
(Camera.main.transform.position.x, Camera.main.transform.position.y);
// calc angle, removing offset from camera
return Vector2.SignedAngle(vec2 - vecCam, vec1 - vecCam);
}
float RandVal(float max, float min)
{
///<summary>
/// Calculate random float given range max and min
///</summary>
System.Random random = new System.Random();
double val = (random.NextDouble() * (max - min) + min);
return (float)val;
}
void InstantiateStimuli(string val1, string val2)
{
///<summary>
/// Instantiates the stimuli object from model mdl and with random x
/// and y values for position (left/right, up/down)
/// </summary>
///
// Range of azimuth and elevation values
//var val1 = CalcAziGivenAng(float.Parse(azi), 0.8f);
//var val2 = CalcEleGivenAng(float.Parse(ele), 0.8f);
stimObj = (GameObject)Instantiate(mdl,
CalcPosGivenAziEle(float.Parse(val1), float.Parse(val2), 0.8f), Quaternion.identity);
//Debug.Log("Distance: " + Vector3.Distance(refObj.transform.position, stimObj.transform.position));
var bounds: Bounds;
// You could also declare these as an array, but they aren't generally
// used in a loop so it usually won't make much difference.
var topFrontLeft: Vector3;
var topFrontRight: Vector3;
var topBackLeft: Vector3;
var topBackRight: Vector3;
var bottomFrontLeft: Vector3;
var bottomFrontRight: Vector3;
var bottomBackLeft: Vector3;
var bottomBackRight: Vector3;
function Start() {
var mf: MeshFilter = GetComponent(MeshFilter);
bounds = mf.mesh.bounds;
corners = new Vector3[8];
}
function UpdateCorners() {
topFrontRight = transform.TransformPoint(bounds.center + bounds.extents);
topFrontLeft = transform.TransformPoint(bounds.center + Vector3.Scale(bounds.extents, Vector3(-1, 1, 1)));
topBackRight = transform.TransformPoint(bounds.center + Vector3.Scale(bounds.extents, Vector3(1, 1, -1)));
topBackLeft = transform.TransformPoint(bounds.center + Vector3.Scale(bounds.extents, Vector3(-1, 1, -1)));
bottomFrontRight = transform.TransformPoint(bounds.center + Vector3.Scale(bounds.extents, Vector3(1, -1, 1)));
bottomFrontLeft = transform.TransformPoint(bounds.center + Vector3.Scale(bounds.extents, Vector3(-1, -1, 1)));
bottomBackRight = transform.TransformPoint(bounds.center + Vector3.Scale(bounds.extents, Vector3(1, -1, -1)));
bottomBackLeft = transform.TransformPoint(bounds.center + Vector3.Scale(bounds.extents, Vector3(-1, -1, -1)));
}
function Update () {
UpdateCorners();
Debug.DrawLine(topFrontLeft, topFrontRight);
Debug.DrawLine(bottomFrontLeft, bottomFrontRight);
Debug.DrawLine(topBackLeft, topBackRight);
Debug.DrawLine(bottomBackLeft, bottomBackRight);
Debug.DrawLine(topFrontLeft, topBackLeft);
Debug.DrawLine(topFrontRight, topBackRight);
Debug.DrawLine(bottomFrontLeft, bottomBackLeft);
Debug.DrawLine(bottomFrontRight, bottomBackRight);
Debug.DrawLine(topFrontLeft, bottomFrontLeft);
Debug.DrawLine(topFrontRight, bottomFrontRight);
Debug.DrawLine(topBackLeft, bottomBackLeft);
Debug.DrawLine(topBackRight, bottomBackRight);
}
<><><><><><><> From 2AFC test
sing System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
public class runExpDemo : MonoBehaviour
{
/// <summary>
/// Main class for the Depth Constancy experiment described by Allison and
/// Wilcox (publication pending.) Code written by Cyan Kuo (2021).
/// </summary>
public GameObject mdl;
public int trialNumber;
public int trialTotal;
public string participantNo;
public GameObject stimObj;
public GameObject refObj;
private LineRenderer stimLnRend;
private LineRenderer refLnRend;
private GameObject selObj;
// Trial duration calculations
public System.TimeSpan trialDuration;
public System.DateTime trialStartTime;
public System.DateTime trialEndTime;
public StreamWriter resultStream;
private float meshSz;
private bool isTrial;
private string folderName;
void Start()
{
///<summary>
/// Start is called before the first frame update and initializes
/// the environment in preparation for the experimental procedure
/// </summary>
// Initialize exp control variables and participant details
trialNumber = 1;
trialTotal = 5;
participantNo = "00";
// Create folder path, create folder in user director
folderName = Application.persistentDataPath + "/results";
System.IO.Directory.CreateDirectory(folderName);
// Put reference and stimuli into environment
InstantiateReference();
InstantiateStimuli();
selObj = refObj;
// Mesh size for calculating absolute measurements
meshSz = mdl.GetComponent<MeshFilter>().sharedMesh.bounds.size.z;
// Start trial and get sys time for trial duration record
trialStartTime = System.DateTime.Now;
isTrial = true;
}
void Update()
{
///<summary>
/// The procedures in this method run every frame, checks for the
/// confirmation button event, in which case the trial ends and the
/// next begins or the script stops if the total trials have been
/// reached. If no confirmation is pressed, check for a length
/// manipulation button event and adjust the length of stimObj
/// accordingly.
///</summary>
// A trial is in session
if (isTrial)
{
// User confirms their manipulation
if (Input.GetKeyDown("space"))
{
OutputTrialResults();
isTrial = false;
Destroy(stimObj);
}
// Toggle
else if (Input.GetKeyDown("right") | Input.GetKeyDown("left"))
{
if (selObj == refObj)
{
refObj.GetComponent<LineRenderer>().enabled = false;
stimObj.GetComponent<LineRenderer>().enabled = true;
DrawBoundingBox(stimObj);
selObj = stimObj;
}
else if (selObj == stimObj)
{
stimObj.GetComponent<LineRenderer>().enabled = false;
refObj.GetComponent<LineRenderer>().enabled = true;
DrawBoundingBox(refObj);
selObj = refObj;
}
}
}
else
{
// Total number of trials completed, quit
if (trialNumber == trialTotal)
{
Application.Quit();
}
// Initialize new trial
else
{
trialStartTime = System.DateTime.Now;
trialNumber++;
InstantiateReference();
InstantiateStimuli();
selObj = refObj;
isTrial = true;
}
}
}
void OutputTrialResults()
{
///<summary>
/// Runs on confirmation event. Output a line to a .csv file with the
/// results of the trial. Independent variables: refLen - the length of
/// the reference object, adjLen - the adjusted length of the stimuli
/// object, azi - the azimuth from reference to stimuli, ele - the
/// elevation from reference to stimuli, and the trial duration
/// </summary>
// dummy variables for testing
float refLen = AbsoluteSize(refObj);
//float adjLen = AbsoluteSize(stimObj);
string sel = null;
if (selObj == stimObj)
{
sel = "eccentric object";
}
else if (selObj == refObj)
{
sel = "midline object";
}
float azi = CalcAzimuth(stimObj,refObj);
float ele = CalcElevation(stimObj, refObj);
// Find trial duration
trialEndTime = System.DateTime.Now;
trialDuration = trialEndTime - trialStartTime;
// Record trial results, output to file
string trialResponses =
(refLen.ToString() + ',' + azi.ToString() + ',' +
ele.ToString() + ',' + trialDuration.ToString() + ',' +
sel + Environment.NewLine);
//using (StreamWriter resultStream = File.AppendText(
// folderName + "/p" + participantNo + "_test.csv"));
//{
//Debug.Log(trialResponses);
resultStream = new StreamWriter( folderName + "/p" + participantNo + "_test.csv", append: true);
resultStream.Write(trialResponses);
resultStream.Close();
//}
}
void InstantiateStimuli()
{
///<summary>
/// Instantiates the stimuli object from model mdl and with random x
/// and y values for position (left/right, up/down)
/// </summary>
// Range of azimuth and elevation values
float stimX = RandVal(500.9f, 499.1f);
float stimY = RandVal(1.2f, 2.0f);
stimObj = (GameObject)Instantiate(mdl,
new Vector3(stimX, stimY, 500.8f), Quaternion.identity);
stimObj.AddComponent<LineRenderer>();
}
void InstantiateReference()
{
///<summary>
/// Instantiates the reference object from model mdl and with random
/// length, and fixed position in front of, and in line with the
/// camera.
/// </summary>
// Reference object is fixed, but changes length
refObj = Instantiate(mdl, new Vector3(500f, 1.5f, 500.8f),
Quaternion.identity);
refObj.transform.localScale = new Vector3(1, 1,
RandVal(0.5f, 1.5f));
refObj.AddComponent<LineRenderer>();
}
float RandVal(float max, float min)
{
///<summary>
/// Calculate random float given range max and min
///</summary>
System.Random random = new System.Random();
double val = (random.NextDouble() * (max - min) + min);
return (float) val;
}
float AbsoluteSize(GameObject go)
{
///<summary>
/// Obtain the absolute size of the unity game object go by multiplying
/// the model's mesh size by the scale factor
///</summary>
var trScl = go.transform.localScale.z;
return meshSz * trScl;
}
void AdjLen(GameObject go, float adj)
{
///<summary>
/// Adjust the length of game object go by adj.
/// Given that absolute size is given by mesh bounds * local scale
/// Adding a 1m in Unity coordinates to the size of an object is
/// 1m/mesh bounds + to the local scale
/// </summary>
float meshSz = go.GetComponent<MeshFilter>().mesh.bounds.size.z;
var trScl = go.transform.localScale.z;
// change its local scale
go.transform.localScale = new Vector3(1, 1, trScl+adj/meshSz);
}
float CalcAzimuth(GameObject go1, GameObject go2)
{
///<summary>
/// Given two game objects, calculate their azimuth
/// </summary>
// get x,z coordinates of objects
var vec1 = new Vector2(go1.transform.position.x, go1.transform.position.z);
var vec2 = new Vector2(go2.transform.position.x, go2.transform.position.z);
// get camera offset along the same axes
var vecCam = new Vector2
(Camera.main.transform.position.x, Camera.main.transform.position.z);
// calc angle, removing offset from camera
return Vector2.SignedAngle(vec1-vecCam,vec2-vecCam);
}
float CalcElevation(GameObject go1, GameObject go2)
{
///<summary>
/// Given two game objects, calculate their elevation
/// </summary>
// get x,y coordinates of objects
var vec1 = new Vector2(go1.transform.position.y, go1.transform.position.z);
var vec2 = new Vector2(go2.transform.position.y, go2.transform.position.z);
// get camera offset along the same axes
var vecCam = new Vector2
(Camera.main.transform.position.y, Camera.main.transform.position.z);
// calc angle, removing offset from camera
return Vector2.SignedAngle(vec1 - vecCam, vec2 - vecCam);
}
void DrawBoundingBox(GameObject go)
{
//mf MeshFilter = mdl.GetComponent<MeshFilter>();
var objBounds = go.GetComponent<Renderer>().bounds;
float s = 3f;
float w = 1.5f;
Vector3 topFrontRight = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(s, s, w)));
Vector3 topFrontLeft = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(-s, s, w)));
Vector3 topBackRight = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(s, s, -w)));
Vector3 topBackLeft = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(-s, s, -w)));
Vector3 bottomFrontRight = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(s, -s, w)));
Vector3 bottomFrontLeft = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(-s, -s, w)));
Vector3 bottomBackRight = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(s, -s, -w)));
Vector3 bottomBackLeft = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(-s, -s, -w)));
LineRenderer lnRend = go.GetComponent<LineRenderer>();
lnRend.SetColors(Color.red, Color.red);
lnRend.startWidth = 0.01f;
lnRend.endWidth = 0.01f;
lnRend.positionCount = 16;
Vector3 test1 = new Vector3(500.9f, 2.0f, 500.8f);
Vector3 test2 = new Vector3(499.1f, 1.2f, 500.8f);
//Debug.DrawLine(test1, test2,Color.red);
Debug.Log(objBounds.center + ", " + objBounds.extents);
Debug.Log(topFrontLeft);
lnRend.SetPosition(0, topFrontLeft);
lnRend.SetPosition(1, bottomFrontLeft);
lnRend.SetPosition(2, bottomFrontRight);
lnRend.SetPosition(3, topFrontRight);
lnRend.SetPosition(4, topFrontLeft);
lnRend.SetPosition(5, topBackLeft);
lnRend.SetPosition(6, bottomBackLeft);
lnRend.SetPosition(7, bottomBackRight);
lnRend.SetPosition(8, topBackRight);
lnRend.SetPosition(9, topBackLeft);
lnRend.SetPosition(10, topBackRight);
lnRend.SetPosition(11, topFrontRight);
lnRend.SetPosition(12, bottomFrontRight);
lnRend.SetPosition(13, bottomBackRight);
lnRend.SetPosition(14, bottomBackLeft);
lnRend.SetPosition(15, bottomFrontLeft);
//lnRend.SetPosition(2, topBackRight);
//lnRend.SetPosition(3, topBackLeft);
//lnRend.SetPosition(4, bottomBackRight);
//lnRend.SetPosition(5, bottomBackLeft);
}
void DrawBoundingBox(GameObject go)
{
//mf MeshFilter = mdl.GetComponent<MeshFilter>();
var objBounds = go.GetComponent<Renderer>().bounds;
float s = 3f;
float w = 1.5f;
Vector3 topFrontRight = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(s, s, w)));
Vector3 topFrontLeft = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(-s, s, w)));
Vector3 topBackRight = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(s, s, -w)));
Vector3 topBackLeft = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(-s, s, -w)));
Vector3 bottomFrontRight = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(s, -s, w)));
Vector3 bottomFrontLeft = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(-s, -s, w)));
Vector3 bottomBackRight = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(s, -s, -w)));
Vector3 bottomBackLeft = (objBounds.center + Vector3.Scale(objBounds.extents, new Vector3(-s, -s, -w)));
LineRenderer lnRend = go.GetComponent<LineRenderer>();
lnRend.SetColors(Color.red, Color.red);
lnRend.startWidth = 0.01f;
lnRend.endWidth = 0.01f;
lnRend.positionCount = 16;
//Debug.DrawLine(test1, test2,Color.red);
Debug.Log(objBounds.center + ", " + objBounds.extents);
Debug.Log(topFrontLeft);
lnRend.SetPosition(0, topFrontLeft);
lnRend.SetPosition(1, bottomFrontLeft);
lnRend.SetPosition(2, bottomFrontRight);
lnRend.SetPosition(3, topFrontRight);
lnRend.SetPosition(4, topFrontLeft);
lnRend.SetPosition(5, topBackLeft);
lnRend.SetPosition(6, bottomBackLeft);
lnRend.SetPosition(7, bottomBackRight);
lnRend.SetPosition(8, topBackRight);
lnRend.SetPosition(9, topBackLeft);
lnRend.SetPosition(10, topBackRight);
lnRend.SetPosition(11, topFrontRight);
lnRend.SetPosition(12, bottomFrontRight);
lnRend.SetPosition(13, bottomBackRight);
lnRend.SetPosition(14, bottomBackLeft);
lnRend.SetPosition(15, bottomFrontLeft);
//lnRend.SetPosition(2, topBackRight);
//lnRend.SetPosition(3, topBackLeft);
//lnRend.SetPosition(4, bottomBackRight);
//lnRend.SetPosition(5, bottomBackLeft);
}
}