21 using System.Collections.Generic;
28 public class TableTopGizmo {
30 private string boardMeshAssetPath_LE = MeshAssets.GetPathToGameboardMesh(
GameboardType.GameboardType_LE);
31 private string surfaceMeshChildPath_LE =
"Surface_LE";
32 private string borderMeshChildPath_LE =
"Border_LE";
34 private string boardMeshAssetPath_XE = MeshAssets.GetPathToGameboardMesh(
GameboardType.GameboardType_XE);
35 private string surfaceMeshChildPath_XE =
"Surface_Flat";
36 private string borderMeshChildPath_XE =
"Border_Flat";
38 private string boardMeshAssetPath_XE_Raised = MeshAssets.GetPathToGameboardMesh(
GameboardType.GameboardType_XE_Raised);
39 private string surfaceMeshChildPath_XE_Raised =
"Surface_Raised";
40 private string borderMeshChildPath_XE_Raised =
"Border_Raised";
42 private string logoMeshAssetPath = MeshAssets.GetPathToT5LogoMesh();
43 private string logoBorderChildPath =
"Circle";
44 private string logoLeftCharacterChildPath =
"Tilt";
45 private string logoRightCharacterChildPath =
"Five";
47 private float gizmoAlpha;
48 private ScaleSettings scaleSettings;
49 private GameBoardSettings gameBoardSettings;
50 private float scaleToUWRLD_UGBD => scaleSettings.GetScaleToUWRLD_UGBD(gameBoardSettings.gameBoardScale);
51 private GameBoard.GameboardExtents gameboardExtents =
new GameBoard.GameboardExtents(
52 GameBoard.GameboardExtents.GAMEBOARD_SIZE_LE);
54 private float totalGameBoardWidthInMeters =>
55 usableGameBoardWidthInMeters + 2f * GameBoard.GameboardExtents.BORDER_WIDTH_IN_METERS;
56 private float totalGameBoardLengthInMeters =>
57 usableGameBoardLengthInMeters + 2f * GameBoard.GameboardExtents.BORDER_WIDTH_IN_METERS;
59 private float usableGameBoardWidthInMeters => gameboardExtents.ViewableSpanX.ToMeters;
60 private float usableGameBoardLengthInMeters => gameboardExtents.ViewableSpanZ.ToMeters;
64 private GameObject meshObj_LE;
65 private Mesh meshBorder_LE;
66 private Mesh meshSurface_LE;
68 private GameObject meshObj_XE;
69 private Mesh meshBorder_XE;
70 private Mesh meshSurface_XE;
72 private GameObject meshObj_XE_Raised;
73 private Mesh meshBorder_XE_Raised;
74 private Mesh meshSurface_XE_Raised;
76 private GameObject logoObj;
77 private Mesh meshLogoBorder;
78 private Mesh meshLogoLeftCharacter;
79 private Mesh meshLogoRightCharacter;
81 private Mesh meshRuler;
82 private List<LineSegment> rulerData;
86 private List<LineSegment> xGridLines;
87 private List<LineSegment> zGridLines;
88 private float yGridOffset;
90 private struct LineSegment
92 public LineSegment(Vector3 start, Vector3 end,
int LOD)
104 private void Configure(ScaleSettings scaleSettings, GameBoardSettings gameBoardSettings,
105 float alpha,
float gridOffsetY, GameBoard.GameboardExtents gameboardExtents)
107 this.scaleSettings = scaleSettings;
108 this.gameBoardSettings = gameBoardSettings;
109 this.gizmoAlpha = alpha;
110 this.yGridOffset = gridOffsetY;
111 this.gameboardExtents = gameboardExtents;
113 if (meshObj_LE ==
null || meshBorder_LE ==
null || meshSurface_LE ==
null)
115 LoadGameboardModel(boardMeshAssetPath_LE, surfaceMeshChildPath_LE, borderMeshChildPath_LE,
116 out meshObj_LE, out meshSurface_LE, out meshBorder_LE);
118 if (meshObj_XE ==
null || meshBorder_XE ==
null || meshSurface_XE ==
null)
120 LoadGameboardModel(boardMeshAssetPath_XE, surfaceMeshChildPath_XE, borderMeshChildPath_XE,
121 out meshObj_XE, out meshSurface_XE, out meshBorder_XE);
123 if (meshObj_XE_Raised ==
null || meshBorder_XE_Raised ==
null || meshSurface_XE_Raised ==
null)
125 LoadGameboardModel(boardMeshAssetPath_XE_Raised, surfaceMeshChildPath_XE_Raised, borderMeshChildPath_XE_Raised,
126 out meshObj_XE_Raised, out meshSurface_XE_Raised, out meshBorder_XE_Raised);
131 logoObj = AssetDatabase.LoadAssetAtPath<GameObject>(logoMeshAssetPath);
134 if(
null == meshLogoBorder)
136 var transform = logoObj.transform.Find( logoBorderChildPath );
137 if(transform !=
null)
139 if(transform.TryGetComponent<MeshFilter>(out var meshFilter))
141 meshLogoBorder = meshFilter.sharedMesh;
145 if(
null == meshLogoLeftCharacter)
147 var transform = logoObj.transform.Find( logoLeftCharacterChildPath );
148 if(transform !=
null)
150 if(transform.TryGetComponent<MeshFilter>(out var meshFilter))
152 meshLogoLeftCharacter = meshFilter.sharedMesh;
156 if(
null == meshLogoRightCharacter)
158 var transform = logoObj.transform.Find( logoRightCharacterChildPath );
159 if(transform !=
null)
161 if(transform.TryGetComponent<MeshFilter>(out var meshFilter))
163 meshLogoRightCharacter = meshFilter.sharedMesh;
168 var previousGameboardType = gameboardType;
169 gameboardType = gameBoardSettings.currentGameBoard.GetDisplayedGameboardType(gameBoardSettings);
171 bool gameboardTypeChanged = previousGameboardType != gameboardType;
173 if (
null == xGridLines ||
null == zGridLines || gameboardTypeChanged)
178 if(
null == meshRuler || gameboardTypeChanged)
180 ConstructRulerMesh(gameboardExtents.ViewableSpanX,
"meshRuler", out meshRuler);
184 public void LoadGameboardModel(
string meshAssetPath,
string surfaceMeshChildPath,
string borderMeshChildPath,
185 out GameObject meshObj, out Mesh meshSurface, out Mesh meshBorder)
187 meshObj = AssetDatabase.LoadAssetAtPath<GameObject>(meshAssetPath);
189 var surfaceMeshTransform = meshObj.transform.Find(surfaceMeshChildPath);
190 if (surfaceMeshTransform !=
null)
192 meshSurface = surfaceMeshTransform.TryGetComponent<MeshFilter>(out var meshFilter) ? meshFilter.sharedMesh :
null;
199 var borderMeshTransform = meshObj.transform.Find(borderMeshChildPath);
200 if (borderMeshTransform !=
null)
202 meshBorder = borderMeshTransform.TryGetComponent<MeshFilter>(out var meshFilter) ? meshFilter.sharedMesh :
null;
210 public void ConstructRulerMesh(Length rulerLength,
string meshName, out Mesh meshRuler)
212 var rulerData =
new List<LineSegment>();
214 float oneMillimeterLengthInMeters =
new Length(1,
LengthUnit.Millimeters).ToMeters;
217 for (
int i = 0; i * oneMillimeterLengthInMeters < rulerLength.ToMeters; i++)
219 float currentDistance = i * (oneMillimeterLengthInMeters / rulerLength.ToMeters);
220 float smallestFractionOfBoardWidth = 1f / 150f;
222 float tickMarkLength = smallestFractionOfBoardWidth;
225 lod -= i % 5 == 0 ? 1 : 0;
226 lod -= i % 10 == 0 ? 1 : 0;
228 tickMarkLength += (3 - lod) * smallestFractionOfBoardWidth;
230 rulerData.Add(
new LineSegment(
new Vector3(currentDistance, 0f, 0f),
new Vector3(currentDistance, 0f, tickMarkLength), lod));
233 float oneSixteenthInchLengthInMeters =
new Length(1 / 16f,
LengthUnit.Inches).ToMeters;
236 for (
int i = 0; i * oneSixteenthInchLengthInMeters < rulerLength.ToMeters; i++)
238 float currentDistance = i * (oneSixteenthInchLengthInMeters / rulerLength.ToMeters);
239 float smallestFractionOfBoardWidth = 1f / 300f;
241 float tickMarkLength = smallestFractionOfBoardWidth;
244 lod -= i % 2 == 0 ? 1 : 0;
245 lod -= i % 4 == 0 ? 1 : 0;
246 lod -= i % 8 == 0 ? 1 : 0;
247 lod -= i % 16 == 0 ? 1 : 0;
249 tickMarkLength += (5 - lod) * smallestFractionOfBoardWidth;
251 float offsetFromCentimeterRuler = 1 / 16f;
252 rulerData.Add(
new LineSegment(
253 new Vector3(currentDistance, 0f, offsetFromCentimeterRuler - tickMarkLength),
254 new Vector3(currentDistance, 0f, offsetFromCentimeterRuler),
258 meshRuler =
new Mesh();
259 meshRuler.name = meshName;
261 int vertArraySizeRatio = 4;
262 Vector3[] verts =
new Vector3[rulerData.Count * vertArraySizeRatio];
264 int triArraySizeRatio = 6;
265 int[] triangles =
new int[rulerData.Count * triArraySizeRatio];
267 float lineThickness = 1 / 2800f;
270 var lineThicknessOffset = Vector3.right * (lineThickness / 2f);
272 for (
int i = 0; i < rulerData.Count; i++)
274 var line = rulerData[i];
276 var bottomLeft = line.Start - lineThicknessOffset + Vector3.left / 2 + Vector3.back / 32f;
277 var topLeft = line.End - lineThicknessOffset + Vector3.left / 2 + Vector3.back / 32f;
279 var bottomRight = line.Start + lineThicknessOffset + Vector3.left / 2 + Vector3.back / 32f;
280 var topRight = line.End + lineThicknessOffset + Vector3.left / 2 + Vector3.back / 32f;
282 var vertIndex = i * vertArraySizeRatio;
283 verts[vertIndex] = bottomLeft;
284 verts[vertIndex + 1] = topLeft;
285 verts[vertIndex + 2] = bottomRight;
286 verts[vertIndex + 3] = topRight;
288 var triIndex = i * triArraySizeRatio;
289 triangles[triIndex] = vertIndex;
290 triangles[triIndex + 1] = vertIndex + 1;
291 triangles[triIndex + 2] = vertIndex + 2;
293 triangles[triIndex + 3] = vertIndex + 2;
294 triangles[triIndex + 4] = vertIndex + 1;
295 triangles[triIndex + 5] = vertIndex + 3;
298 meshRuler.vertices = verts;
299 meshRuler.triangles = triangles;
300 meshRuler.RecalculateNormals();
304 public void ResetGrid(ScaleSettings newScaleSettings =
null, GameBoardSettings newGameBoardSettings =
null)
306 if (newScaleSettings !=
null)
308 this.scaleSettings = newScaleSettings;
310 if(newGameBoardSettings !=
null)
312 this.gameBoardSettings = newGameBoardSettings;
315 if(xGridLines ==
null)
317 xGridLines =
new List<LineSegment>();
319 else xGridLines.Clear();
321 if(zGridLines ==
null)
323 zGridLines =
new List<LineSegment>();
325 else zGridLines.Clear();
327 var lineLengthZ = 0.5f;
328 var linePosOffsetZ = lineLengthZ - 0.5f;
329 var lineStartPosZ = lineLengthZ;
330 var lineStopPosZ = -lineLengthZ + linePosOffsetZ;
331 var minDimension = Mathf.Min(usableGameBoardWidthInMeters, usableGameBoardLengthInMeters);
332 var oneUnit = scaleSettings.oneUnitLengthInMeters / minDimension;
334 for (
int i = 0; i * scaleSettings.oneUnitLengthInMeters < usableGameBoardWidthInMeters / 2; i++)
336 float distanceFromCenter = i * (scaleSettings.oneUnitLengthInMeters / minDimension);
339 xGridLines.Add(
new LineSegment(
new Vector3(distanceFromCenter, 0, lineStartPosZ),
new Vector3(distanceFromCenter, 0, lineStopPosZ), lod));
342 if(i < 1) {
continue;}
344 xGridLines.Add(
new LineSegment(
new Vector3(-distanceFromCenter, 0, lineStartPosZ),
new Vector3(-distanceFromCenter, 0, lineStopPosZ), lod));
348 for (
int i = 0; i * scaleSettings.oneUnitLengthInMeters < usableGameBoardWidthInMeters / 2; i++)
350 float distanceFromCenter = i * (scaleSettings.oneUnitLengthInMeters / minDimension);
353 zGridLines.Add(
new LineSegment(
new Vector3(0.5f, 0, distanceFromCenter),
new Vector3(-0.5f, 0, distanceFromCenter), lod));
356 if(i < scaleSettings.oneUnitLengthInMeters) {
continue; }
358 zGridLines.Add(
new LineSegment(
new Vector3(0.5f, 0, -distanceFromCenter),
new Vector3(-0.5f, 0, -distanceFromCenter), lod));
363 public void Draw(ScaleSettings scaleSettings, GameBoardSettings gameBoardSettings,
364 float alpha,
bool showGrid,
float gridOffsetY = 0f)
366 Configure (scaleSettings, gameBoardSettings, alpha, gridOffsetY, gameboardExtents);
368 if (
null == gameBoardSettings) {
return; }
371 Matrix4x4 mtxOrigin = Matrix4x4.TRS( Vector3.zero, Quaternion.identity, Vector3.one );
373 Matrix4x4 mtxWorld = Matrix4x4.TRS( gameBoardSettings.gameBoardCenter,
374 Quaternion.Euler(gameBoardSettings.gameBoardRotation),
375 Vector3.one * scaleToUWRLD_UGBD);
377 Matrix4x4 mtxPreTransform = Matrix4x4.TRS( Vector3.zero, Quaternion.Euler(0.0f, 0.0f, 0.0f), Vector3.one );
379 Matrix4x4 mtxGizmo = mtxOrigin * mtxWorld * mtxPreTransform;
381 Color oldColor = Gizmos.color;
382 Matrix4x4 oldMatrix = Gizmos.matrix;
384 Gizmos.matrix = mtxGizmo;
386 Mesh meshBorder, meshSurface;
388 switch (gameboardType)
391 meshBorder = meshBorder_XE;
392 meshSurface = meshSurface_XE;
395 meshBorder = meshBorder_XE_Raised;
396 meshSurface = meshSurface_XE_Raised;
401 meshBorder = meshBorder_LE;
402 meshSurface = meshSurface_LE;
406 if(meshBorder !=
null)
408 Gizmos.color =
new Color(0.0f, 0.0f, 0.0f, alpha);
409 Gizmos.DrawMesh(meshBorder, 0 );
412 if(meshSurface !=
null)
414 Gizmos.color =
new Color (0.5f, 0.5f, 0.5f, alpha);
415 Gizmos.DrawMesh(meshSurface, 0 );
418 if(meshLogoBorder !=
null && meshLogoLeftCharacter !=
null && meshLogoRightCharacter !=
null)
429 Gizmos.matrix = oldMatrix;
430 Gizmos.color = oldColor;
433 private void DrawLogo()
435 var logoDiameter = 2.5f;
436 var logoRadius = logoDiameter / 2f;
437 var borderThickness =
new Length(GameBoard.GameboardExtents.BORDER_WIDTH_IN_METERS,
LengthUnit.Meters).ToCentimeters;
438 var gameBoardFrontExtent = -gameBoardSettings.currentGameBoard.transform.forward / 2f;
439 var gameBoardRightExtent = gameBoardSettings.currentGameBoard.transform.right / 2f;
442 var logoPosition = (gameBoardRightExtent + gameBoardFrontExtent) * usableGameBoardWidthInMeters;
444 var oneCentimeterLengthInMeters =
new Length(1,
LengthUnit.Centimeters).ToMeters;
445 logoPosition -= gameBoardSettings.currentGameBoard.transform.right * 5f * oneCentimeterLengthInMeters;
446 logoPosition -= gameBoardSettings.currentGameBoard.transform.forward * ((borderThickness / 2) - logoRadius) * oneCentimeterLengthInMeters;
448 var contentScaleFactor = scaleToUWRLD_UGBD;
449 Matrix4x4 mtxOrigin = Matrix4x4.TRS( Vector3.zero, Quaternion.identity, Vector3.one );
450 var mtxWorld = Matrix4x4.TRS( gameBoardSettings.gameBoardCenter + logoPosition * contentScaleFactor,
451 gameBoardSettings.currentGameBoard.rotation,
452 logoDiameter * Vector3.one * contentScaleFactor );
453 Matrix4x4 mtxPreTransform = Matrix4x4.TRS( Vector3.zero, Quaternion.Euler(-90.0f, 180.0f, 0.0f), Vector3.one );
454 Gizmos.matrix = mtxOrigin * mtxWorld * mtxPreTransform;
456 Gizmos.color =
new Color (0.969f, 0.969f, 0.969f, gizmoAlpha);
457 Gizmos.DrawMesh( meshLogoBorder, 0 );
459 Gizmos.color =
new Color (0.945f, 0.349f, 0.133f, gizmoAlpha);
460 Gizmos.DrawMesh( meshLogoLeftCharacter, 0 );
462 Gizmos.color =
new Color (0.376f, 0.392f, 0.439f, gizmoAlpha);
463 Gizmos.DrawMesh( meshLogoRightCharacter, 0 );
466 private void DrawGrid()
468 var contentScaleFactor = scaleToUWRLD_UGBD;
469 float heightOffsetInMeters = yGridOffset * scaleSettings.oneUnitLengthInMeters;
470 float lengthOffsetInMeters = Mathf.Max(usableGameBoardLengthInMeters - usableGameBoardWidthInMeters, 0f) / 2f;
472 var gameBoardTransform = gameBoardSettings.currentGameBoard.transform;
475 Matrix4x4 mtxOrigin = Matrix4x4.TRS( Vector3.zero, Quaternion.identity, Vector3.one );
476 Matrix4x4 mtxPreTransform = Matrix4x4.TRS(
478 Quaternion.Euler(0.0f, 0.0f, 0.0f),
479 Vector3.one * Mathf.Min(usableGameBoardWidthInMeters, usableGameBoardLengthInMeters));
481 Matrix4x4 mtxWorld = Matrix4x4.TRS(
482 gameBoardSettings.gameBoardCenter + (gameBoardTransform.up * heightOffsetInMeters / contentScaleFactor),
483 gameBoardTransform.rotation,
484 Vector3.one / contentScaleFactor );
485 Matrix4x4 mtxGizmo = mtxOrigin * mtxWorld * mtxPreTransform;
486 Gizmos.matrix = mtxGizmo;
489 var gridLinePeriod = scaleSettings.contentScaleUnit ==
LengthUnit.Inches ? 12 : 10;
493 float beginFadeThreshold = 20f;
494 float endFadeThreshold = 10f;
497 TryGetCameraPositionOverGameBoard(out var cameraPositionOverGameBoard);
498 Vector3 testUnitVector = cameraPositionOverGameBoard + Vector3.right * scaleSettings.oneUnitLengthInMeters / contentScaleFactor;
499 Vector3 secondTestUnitVector = cameraPositionOverGameBoard + Vector3.right * gridLinePeriod * scaleSettings.oneUnitLengthInMeters / contentScaleFactor;
501 var screenSpaceDistance = (SceneView.currentDrawingSceneView.camera.WorldToScreenPoint(testUnitVector)
502 - SceneView.currentDrawingSceneView.camera.WorldToScreenPoint(cameraPositionOverGameBoard)).magnitude;
503 var secondScreenSpaceDistance = (SceneView.currentDrawingSceneView.camera.WorldToScreenPoint(secondTestUnitVector)
504 - SceneView.currentDrawingSceneView.camera.WorldToScreenPoint(cameraPositionOverGameBoard)).magnitude;
507 var beginFade = screenSpaceDistance < beginFadeThreshold;
508 var endFade = screenSpaceDistance < endFadeThreshold;
510 var beginSecondFade = secondScreenSpaceDistance < beginFadeThreshold;
511 var endSecondFade = secondScreenSpaceDistance < endFadeThreshold;
513 var fadeFactor = 1f - Mathf.Clamp((beginFadeThreshold - screenSpaceDistance) / endFadeThreshold, 0f, 1f);
514 var secondFadeFactor = 1f - Mathf.Clamp((beginFadeThreshold - secondScreenSpaceDistance) / endFadeThreshold, 0f, 1f);
516 var fadeAlpha = gizmoAlpha * fadeFactor;
517 var secondFadeAlpha = gizmoAlpha * secondFadeFactor;
520 var oldColor = Gizmos.color;
522 DrawGridLines(xGridLines, gridLinePeriod, beginFade, endFade, fadeAlpha, beginSecondFade, endSecondFade, secondFadeAlpha);
523 DrawGridLines(zGridLines, gridLinePeriod, beginFade, endFade, fadeAlpha, beginSecondFade, endSecondFade, secondFadeAlpha);
525 Gizmos.color = oldColor;
528 private void DrawGridLines(List<LineSegment> lines,
int gridLinePeriod,
bool beginFade,
bool endFade,
529 float fadeAlpha,
bool beginSecondFade,
bool endSecondFade,
float secondFadeAlpha)
531 var gridLinePeriodSquared = gridLinePeriod * gridLinePeriod;
532 var mtxGizmo = Gizmos.matrix;
536 float cursorSphereRadius = scaleToUWRLD_UGBD * (scaleSettings.oneUnitLengthInMeters / 2f);
539 cursorSphereRadius *= (gridLinePeriod * gridLinePeriod) / 2f;
543 cursorSphereRadius *= gridLinePeriod / 2f;
546 for(
int i = 0; i < lines.Count; i++)
549 var isPeriodic = i % gridLinePeriod == 0 || i % gridLinePeriod == gridLinePeriod - 1;
550 if(endFade && !isPeriodic) {
continue; }
552 var isDoublePeriodic = i % gridLinePeriodSquared == 0 || i % gridLinePeriodSquared == gridLinePeriodSquared - 1;
553 if(endSecondFade && !isDoublePeriodic) {
continue; }
556 var currentGridLineSegment = lines[i];
557 var transformedLineSegment =
new LineSegment(
558 mtxGizmo.MultiplyPoint(currentGridLineSegment.Start),
559 mtxGizmo.MultiplyPoint(currentGridLineSegment.End), currentGridLineSegment.LOD);
561 var lineCollidesWithCursor = TryGetCursorPosition(out var cursorPosition)
562 && CheckRaySphereCollision(cursorPosition, cursorSphereRadius, transformedLineSegment);
565 var lineAlpha = gizmoAlpha;
566 if(beginSecondFade && !isDoublePeriodic)
568 lineAlpha = secondFadeAlpha;
570 else if(beginFade && !isPeriodic)
572 lineAlpha = fadeAlpha;
575 Gizmos.color = lineCollidesWithCursor
576 ?
new Color(1f, 0f, 0f, gizmoAlpha)
577 : new Color(1f, 1f, 1f, lineAlpha);
580 if(lineCollidesWithCursor)
582 var oldGizmoMatrix = Gizmos.matrix;
583 var xLineExtensionFactor = totalGameBoardWidthInMeters / usableGameBoardWidthInMeters;
584 var zLineExtensionFactor = totalGameBoardLengthInMeters / usableGameBoardLengthInMeters;
587 var lineDifference = currentGridLineSegment.End - currentGridLineSegment.Start;
588 var xAxisAligned = Mathf.Abs(Vector3.Dot(Vector3.right, lineDifference)) == 1;
590 var mtxPreTransform = Matrix4x4.TRS(Vector3.zero, Quaternion.identity,
591 (xAxisAligned ?
new Vector3(xLineExtensionFactor, 1f, 1f) :
new Vector3(1f, 1f, zLineExtensionFactor)));
592 var newMtxWorld = Matrix4x4.TRS(
593 gameBoardSettings.currentGameBoard.transform.position,
594 gameBoardSettings.currentGameBoard.transform.rotation,
595 new Vector3(scaleToUWRLD_UGBD * usableGameBoardWidthInMeters, 0f, scaleToUWRLD_UGBD * usableGameBoardWidthInMeters));
596 Gizmos.matrix = newMtxWorld;
599 Gizmos.DrawLine(currentGridLineSegment.Start, mtxPreTransform.MultiplyPoint(currentGridLineSegment.Start));
600 Gizmos.DrawLine(currentGridLineSegment.End, mtxPreTransform.MultiplyPoint(currentGridLineSegment.End));
601 Gizmos.matrix = oldGizmoMatrix;
603 Gizmos.DrawLine(currentGridLineSegment.Start, currentGridLineSegment.End);
608 private bool TryGetCursorPosition(out Vector3 intersectPoint)
610 var sceneView = UnityEditor.SceneView.currentDrawingSceneView;
611 var sceneViewCamera = sceneView.camera;
612 var mousePos = Event.current.mousePosition - sceneView.position.position;
615 mousePos = GUIUtility.GUIToScreenPoint(mousePos);
618 var frameOffset = Vector2.down * 36f;
619 mousePos.y = (sceneViewCamera.pixelHeight - mousePos.y - frameOffset.y);
621 var ray = sceneViewCamera.ScreenPointToRay(mousePos);
623 return TryGetGridPlanePosition(ray, out intersectPoint);
626 private bool TryGetCameraPositionOverGameBoard(out Vector3 intersectPoint)
628 var ray =
new Ray(UnityEditor.SceneView.currentDrawingSceneView.camera.transform.position, -gameBoardSettings.currentGameBoard.transform.up);
629 return TryGetGridPlanePosition(ray, out intersectPoint);
632 private bool TryGetGridPlanePosition(Ray ray, out Vector3 intersectPoint)
635 var planeOffsetY = (yGridOffset * scaleSettings.oneUnitLengthInMeters) / scaleToUWRLD_UGBD;
636 var gridPlane =
new Plane(
637 gameBoardSettings.currentGameBoard.transform.up,
638 gameBoardSettings.currentGameBoard.transform.localToWorldMatrix.MultiplyPoint(planeOffsetY * Vector3.up));
640 if(gridPlane.Raycast(ray, out var intersectionDistance))
643 intersectPoint = ray.GetPoint(intersectionDistance);
647 intersectPoint = Vector3.zero;
651 private bool CheckRaySphereCollision(Vector3 sphereCenter,
float sphereRadius, LineSegment lineSegment){
653 var ray =
new Ray(lineSegment.Start, lineSegment.End - lineSegment.Start);
655 Vector3 rayToSphereCenterDistance = ray.origin - sphereCenter;
656 float a = Vector3.Dot(ray.direction, ray.direction);
657 float b = 2f * Vector3.Dot(rayToSphereCenterDistance, ray.direction);
658 float c = Vector3.Dot(rayToSphereCenterDistance, rayToSphereCenterDistance) - sphereRadius * sphereRadius;
659 float discriminant = (b * b) - (4*a*c);
660 return discriminant > 0;
663 private void DrawRulers()
665 if(meshRuler ==
null) {
return; }
667 Gizmos.color =
new Color (1f, 0.8320962f, 0.3803922f, gizmoAlpha);
669 var contentScaleFactor = scaleToUWRLD_UGBD;
670 float borderWidthInMeters = GameBoard.GameboardExtents.BORDER_WIDTH_IN_METERS;
671 float lengthOffsetInMeters = Mathf.Max(usableGameBoardLengthInMeters - usableGameBoardWidthInMeters, 0f) / 2f;
672 var gameBoardTransform = gameBoardSettings.currentGameBoard.transform;
673 var gameboardCenter = gameBoardSettings.gameBoardCenter;
675 Matrix4x4 mtxOrigin = Matrix4x4.TRS( Vector3.zero, Quaternion.identity, Vector3.one );
676 Matrix4x4 mtxPreTransform = Matrix4x4.TRS( Vector3.zero, Quaternion.Euler(0.0f, 0.0f, 0.0f),
new Vector3(usableGameBoardWidthInMeters, 1f, usableGameBoardLengthInMeters));
679 Matrix4x4 mtxWorld = Matrix4x4.TRS(gameboardCenter + (gameBoardTransform.forward * lengthOffsetInMeters / contentScaleFactor) + (0.5f * (usableGameBoardLengthInMeters + borderWidthInMeters) * gameBoardTransform.forward) / contentScaleFactor,
680 Quaternion.Euler(gameBoardSettings.gameBoardRotation),
681 Vector3.one / contentScaleFactor );
682 Gizmos.matrix = mtxOrigin * mtxWorld * mtxPreTransform;
683 Gizmos.DrawMesh(meshRuler, 0 );
686 mtxWorld = Matrix4x4.TRS(gameboardCenter - (0.5f * (usableGameBoardWidthInMeters + borderWidthInMeters) * gameBoardTransform.forward) / contentScaleFactor,
687 Quaternion.Euler(gameBoardSettings.gameBoardRotation),
688 Vector3.one / contentScaleFactor );
689 Gizmos.matrix = mtxOrigin * mtxWorld * mtxPreTransform;
690 Gizmos.DrawMesh(meshRuler, 0 );
693 mtxPreTransform = Matrix4x4.TRS( Vector3.zero, Quaternion.Euler(0.0f, 90.0f, 0.0f), Vector3.one * usableGameBoardWidthInMeters);
694 mtxWorld = Matrix4x4.TRS(gameboardCenter + (0.5f * (usableGameBoardWidthInMeters + borderWidthInMeters) * gameBoardTransform.right) / contentScaleFactor,
695 Quaternion.Euler(gameBoardSettings.gameBoardRotation),
696 Vector3.one / contentScaleFactor );
697 Gizmos.matrix = mtxOrigin * mtxWorld * mtxPreTransform;
698 Gizmos.DrawMesh(meshRuler, 0 );
701 mtxWorld = Matrix4x4.TRS(gameboardCenter - (0.5f * (usableGameBoardWidthInMeters + borderWidthInMeters) * gameBoardTransform.right) / contentScaleFactor,
702 Quaternion.Euler(gameBoardSettings.gameBoardRotation),
703 Vector3.one / contentScaleFactor );
704 Gizmos.matrix = mtxOrigin * mtxWorld * mtxPreTransform;
705 Gizmos.DrawMesh(meshRuler, 0 );
GameboardType
The type of Gameboard being tracked by the glasses