Skip to content
Snippets Groups Projects
Commit c631756e authored by Nicolas Leins's avatar Nicolas Leins
Browse files

further implementation of GridMap

Enable/Disable over mainMenu
parent 687aeb1d
No related branches found
No related tags found
No related merge requests found
Source diff could not be displayed: it is stored in LFS. Options to address this: view the blob.
Source diff could not be displayed: it is stored in LFS. Options to address this: view the blob.
......@@ -4,12 +4,13 @@ using UnityEngine;
public class Grid
{
private int width;
private int height;
private float cellSize;
private readonly int width;
private readonly int height;
private readonly float cellSize;
private int[,] gridArray;
private Vector3 originPosition;
// for debug purpose
private TextMesh[,] debugTextArray;
public Grid(int height, int width, float cellSize, Vector3 originPosition)
......@@ -39,7 +40,7 @@ public class Grid
if (x >= 0 && y >= 0 && x < width && y < height)
{
gridArray[x, y] = value;
//debugTextArray[x, y].text = gridArray[x, y].ToString();
// debugTextArray[x, y].text = gridArray[x, y].ToString();
}
}
......@@ -55,10 +56,12 @@ public class Grid
SetValue(x, y, GetValue(x, y) + value);
}
public void AddValue(Vector3 worldPosition, int value, int fullValueRange, int totalRange)
public void AddValue(Vector3 worldPosition, int value, int fullValueRange, int totalRange, int yShift = 0, bool cutAtZero = false)
{
int lowerValueAmount = Mathf.RoundToInt(value / (totalRange - fullValueRange));
GetXY(worldPosition, out int originX, out int originY);
// diamond shape
for (int x = 0; x < totalRange; x++)
{
for (int y = 0; y < totalRange - x; y++)
......@@ -70,23 +73,96 @@ public class Grid
addValueAmount -= lowerValueAmount * (radius - fullValueRange);
}
AddValue(originX + x, originY + y + yShift, addValueAmount);
if (x != 0)
{
AddValue(originX - x, originY + y + yShift, addValueAmount);
}
if (y != 0)
{
if (!cutAtZero || ((originY - y + yShift) > originY))
{
AddValue(originX + x, originY - y + yShift, addValueAmount);
if (x != 0)
{
AddValue(originX - x, originY - y + yShift, addValueAmount);
}
}
}
}
}
/*
// square shape
for (int x = 0; x < totalRange; x++)
{
for (int y = 0; y < totalRange; y++)
{
int radius = Mathf.Max(x, y);
int addValueAmount = value;
if (radius > fullValueRange)
{
addValueAmount -= lowerValueAmount * (radius - fullValueRange);
}
AddValue(originX + x, originY + y, addValueAmount);
if (x != 0) {
if (x != 0)
{
AddValue(originX - x, originY + y, addValueAmount);
}
if (y != 0)
{
AddValue(originX + x, originY - y, addValueAmount);
AddValue(originX + x, originY - y, addValueAmount);
if (x != 0)
{
AddValue(originX - x, originY - y, addValueAmount);
}
}
}
}*/
//combined
/*for (int x = 0; x < totalRange; x++)
{
for (int y = 0; y < totalRange; y++)
{
int xNew = x;
int yNew = y;
int radius;
if (y < totalRange - x)
{
radius = x + y;
}
else {
radius = Mathf.Max(x, y);
xNew = Mathf.RoundToInt(xNew / 2);
yNew = Mathf.RoundToInt(yNew / 2);
}
int addValueAmount = value;
if (radius > fullValueRange)
{
addValueAmount -= lowerValueAmount * (radius - fullValueRange);
}
AddValue(originX + xNew, originY + yNew, addValueAmount);
if (x != 0)
{
AddValue(originX - xNew, originY + yNew, addValueAmount);
}
if (y != 0)
{
AddValue(originX + xNew, originY - yNew, addValueAmount);
if (x != 0)
{
AddValue(originX - xNew, originY - yNew, addValueAmount);
}
}
}
}
}*/
}
public int GetValue(int x, int y)
......
......@@ -2,44 +2,49 @@
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
public class GridMapTracker : MonoBehaviour
{
private int updateEveryNFrames = 50;
private readonly int updateEveryNFrames = 50;
private int pixelsPerCell;
private int count;
private Grid grid;
private Color[] colors = new Color[6];
private Texture2D texture;
[SerializeField]
private GameObject vrSelf;
private Color[] colors;
private int pixelsPerCell;
// Start is called before the first frame update
void Start()
{
//grid = new Grid(44, 28, 0,5f, new Vector3(-3f, 0, -6f));
//grid = new Grid(220, 140, .1f, new Vector3(-3.5f, 0, -8f));
grid = new Grid(330, 210, .07f, new Vector3(-3.5f, 0, -8f));
count = updateEveryNFrames;
colors = new Color[6];
colors[0] = new Color(1f, 1f, 100/255f);
pixelsPerCell = Mathf.RoundToInt(500f / grid.GetWidth());
/*// yellow to red
colors[0] = new Color(1f, 1f, .4f);
colors[1] = Color.yellow;
colors[2] = new Color(1f, 195 / 255f, 0);
colors[3] = new Color(1f, 80 / 255f, 0);
colors[2] = new Color(1f, .8f, 0);
colors[3] = new Color(1f, .3f, 0);
colors[4] = Color.red;
colors[5] = Color.black;
colors[5] = Color.black;*/
pixelsPerCell = Mathf.RoundToInt(500f / grid.GetWidth());
// transparent to opaque
colors[0] = new Color(1f, 0f, 0f, .1f);
colors[1] = new Color(1f, 0f, 0f, .3f);
colors[2] = new Color(1f, 0f, 0f, .5f);
colors[3] = new Color(1f, 0f, 0f, .8f);
colors[4] = Color.red;
colors[5] = Color.black;
}
private void Update()
{
if (count == 0)
{
grid.AddValue(vrSelf.transform.position, 50, 4, 50);
grid.AddValue(vrSelf.transform.position, 50, 1, 50, 15, true);
count = updateEveryNFrames;
}
count--;
......@@ -47,11 +52,10 @@ public class GridMapTracker : MonoBehaviour
private void OnDisable()
{
Texture2D texture = new Texture2D(grid.GetWidth() * pixelsPerCell, grid.GetHeight() * pixelsPerCell);
texture = new Texture2D(grid.GetWidth() * pixelsPerCell, grid.GetHeight() * pixelsPerCell);
int devideValue = DevideValue(grid);
int gridValue;
int i;
for (int x = 0; x < texture.width; x++)
{
......@@ -60,25 +64,28 @@ public class GridMapTracker : MonoBehaviour
gridValue = grid.GetValue((int)x / pixelsPerCell, (int)y / pixelsPerCell);
if(gridValue == 0)
{
texture.SetPixel(x, y, colors[5]);
texture.SetPixel(x, y, colors[colors.Length - 1]);
}
else
{
i = (int)(gridValue / devideValue);
texture.SetPixel(x, y, colors[i]);
texture.SetPixel(x, y, colors[(int)(gridValue / devideValue)]);
}
}
}
texture.Apply();
SavePNG(texture, Application.dataPath + "/../Assets/TexturePNG.png");
AddMapInformation(texture);
texture.Apply();
SavePNG(texture, Application.dataPath + "/Student_Movement/" + MenuDataHolder.RepetitionCount + "_" + DateTime.Today.Date.Year + " " + DateTime.Today.Date.Month + " " + DateTime.Today.Date.Day + "_" + DateTime.Now.Hour + " " + DateTime.Now.Minute + ".png");
}
private int DevideValue(Grid grid)
{
int highestValue = grid.GetHighestValue();
int devideValue = (int)(highestValue / 5);
int colorSteps = colors.Length - 1;
int devideValue = (int)(highestValue / colorSteps);
if ((highestValue / devideValue) >= 5)
if ((highestValue / devideValue) >= colorSteps)
{
return devideValue + 1;
}
......@@ -90,4 +97,21 @@ public class GridMapTracker : MonoBehaviour
byte[] bytes = texture.EncodeToPNG();
File.WriteAllBytes(path, bytes);
}
private void AddMapInformation(Texture2D texture)
{
int linePixels = Mathf.RoundToInt(texture.width * 0.3f);
int middle = Mathf.RoundToInt(texture.width / 2);
int thickness = 5;
int distanceToBottom = Mathf.RoundToInt(texture.height * 0.05f);
for (int x = middle; x < (middle + linePixels / 2); x++)
{
for (int y = 1; y <= thickness; y++)
{
texture.SetPixel(x, distanceToBottom + y, Color.white);
texture.SetPixel(-x, distanceToBottom + y, Color.white);
}
}
}
}
......@@ -24,6 +24,7 @@ public class ControlMenu : MonoBehaviour
public GameObject toggleNonScriptedBehaviour;
[SerializeField] private GameObject toggleTimer;
[SerializeField] private GameObject inputFieldTimer;
[SerializeField] private GameObject toggleGridMap;
private ExperimentParameters[] experiments;
......@@ -140,6 +141,12 @@ public class ControlMenu : MonoBehaviour
MenuDataHolder.TimerSeconds = input;
}
}
public void SetGripMapping()
{
MenuDataHolder.GridTracking = toggleGridMap.GetComponent<Toggle>().isOn;
}
// Reduce code repetition
private void LoadExperiment(ExperimentParameters experiment)
{
......
......@@ -38,4 +38,6 @@ public static class MenuDataHolder
public static bool TimerActive { get; set; } = false;
public static int TimerSeconds { get; set; } = 0;
public static bool GridTracking = false;
}
......@@ -47,6 +47,11 @@ public class vrselfControl : MonoBehaviour
//float headHeight = camera.transform.localPosition.y;
//float scale = defaultHeight / headHeight;
//transform.localScale = Vector3.one * scale;
if (MenuDataHolder.GridTracking == true)
{
GameObject.Find("Controls").GetComponent<GridMapTracker>().enabled = true;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment