World Position to Grid Position

HexGridSystem
Sascha 2023-06-20 19:29:56 +07:00
parent 3a1c0c8308
commit 3319159ccd
13 changed files with 304 additions and 35 deletions

@ -30,7 +30,7 @@ Material:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 2800000, guid: ab269c538609ee34d8bd8e2ff5168c86, type: 3}
m_Texture: {fileID: 2800000, guid: c09c0c8a31a20941f8b6695e435c1199, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BumpMap:
@ -54,7 +54,7 @@ Material:
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: ab269c538609ee34d8bd8e2ff5168c86, type: 3}
m_Texture: {fileID: 2800000, guid: c09c0c8a31a20941f8b6695e435c1199, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
@ -110,7 +110,7 @@ Material:
- _Parallax: 0.005
- _QueueOffset: 0
- _ReceiveShadows: 1
- _Smoothness: 1
- _Smoothness: 0.552
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
@ -119,8 +119,8 @@ Material:
- _WorkflowMode: 1
- _ZWrite: 0
m_Colors:
- _BaseColor: {r: 0, g: 0.7246165, b: 1, a: 1}
- _Color: {r: 0, g: 0.7246165, b: 1, a: 1}
- _BaseColor: {r: 1, g: 0, b: 0, a: 1}
- _Color: {r: 1, g: 0, b: 0, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
m_BuildTextureStacks: []

@ -30,7 +30,7 @@ Material:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 2800000, guid: ab269c538609ee34d8bd8e2ff5168c86, type: 3}
m_Texture: {fileID: 2800000, guid: c09c0c8a31a20941f8b6695e435c1199, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BumpMap:
@ -54,7 +54,7 @@ Material:
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: ab269c538609ee34d8bd8e2ff5168c86, type: 3}
m_Texture: {fileID: 2800000, guid: c09c0c8a31a20941f8b6695e435c1199, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
@ -110,7 +110,7 @@ Material:
- _Parallax: 0.005
- _QueueOffset: 0
- _ReceiveShadows: 1
- _Smoothness: 1
- _Smoothness: 0.552
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1

@ -30,7 +30,7 @@ Material:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 2800000, guid: ab269c538609ee34d8bd8e2ff5168c86, type: 3}
m_Texture: {fileID: 2800000, guid: c09c0c8a31a20941f8b6695e435c1199, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BumpMap:
@ -54,7 +54,7 @@ Material:
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: ab269c538609ee34d8bd8e2ff5168c86, type: 3}
m_Texture: {fileID: 2800000, guid: c09c0c8a31a20941f8b6695e435c1199, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
@ -110,7 +110,7 @@ Material:
- _Parallax: 0.005
- _QueueOffset: 0
- _ReceiveShadows: 1
- _Smoothness: 0.5
- _Smoothness: 0.552
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
@ -119,8 +119,8 @@ Material:
- _WorkflowMode: 1
- _ZWrite: 0
m_Colors:
- _BaseColor: {r: 1, g: 0, b: 0, a: 0.19607843}
- _Color: {r: 1, g: 0, b: 0, a: 0.19607843}
- _BaseColor: {r: 1, g: 0, b: 0, a: 1}
- _Color: {r: 1, g: 0, b: 0, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
m_BuildTextureStacks: []

@ -30,7 +30,7 @@ Material:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 2800000, guid: ab269c538609ee34d8bd8e2ff5168c86, type: 3}
m_Texture: {fileID: 2800000, guid: c09c0c8a31a20941f8b6695e435c1199, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BumpMap:
@ -54,7 +54,7 @@ Material:
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: ab269c538609ee34d8bd8e2ff5168c86, type: 3}
m_Texture: {fileID: 2800000, guid: c09c0c8a31a20941f8b6695e435c1199, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
@ -110,7 +110,7 @@ Material:
- _Parallax: 0.005
- _QueueOffset: 0
- _ReceiveShadows: 1
- _Smoothness: 1
- _Smoothness: 0.552
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
@ -119,8 +119,8 @@ Material:
- _WorkflowMode: 1
- _ZWrite: 0
m_Colors:
- _BaseColor: {r: 1, g: 0.6862745, b: 0, a: 1}
- _Color: {r: 1, g: 0.68627447, b: 0, a: 1}
- _BaseColor: {r: 1, g: 0, b: 0, a: 1}
- _Color: {r: 1, g: 0, b: 0, a: 1}
- _EmissionColor: {r: 1, g: 0.42869055, b: 0, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
m_BuildTextureStacks: []

@ -0,0 +1,140 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: HexGridGreen
m_Shader: {fileID: 4800000, guid: 933532a4fcc9baf4fa0491de14d08ed7, type: 3}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords:
- _EMISSION
- _SURFACE_TYPE_TRANSPARENT
m_InvalidKeywords: []
m_LightmapFlags: 1
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: 3000
stringTagMap:
RenderType: Transparent
disabledShaderPasses:
- MOTIONVECTORS
- DepthOnly
- SHADOWCASTER
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 2800000, guid: c09c0c8a31a20941f8b6695e435c1199, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 2800000, guid: c09c0c8a31a20941f8b6695e435c1199, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: c09c0c8a31a20941f8b6695e435c1199, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SpecGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_Lightmaps:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_LightmapsInd:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_ShadowMasks:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _AddPrecomputedVelocity: 0
- _AlphaClip: 0
- _AlphaToMask: 0
- _Blend: 0
- _BlendModePreserveSpecular: 0
- _BumpScale: 1
- _ClearCoatMask: 0
- _ClearCoatSmoothness: 0
- _Cull: 2
- _Cutoff: 0.5
- _DetailAlbedoMapScale: 1
- _DetailNormalMapScale: 1
- _DstBlend: 10
- _DstBlendAlpha: 10
- _EnvironmentReflections: 1
- _GlossMapScale: 0
- _Glossiness: 0
- _GlossyReflections: 0
- _Metallic: 0
- _OcclusionStrength: 1
- _Parallax: 0.005
- _QueueOffset: 0
- _ReceiveShadows: 1
- _Smoothness: 0.552
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 5
- _SrcBlendAlpha: 1
- _Surface: 1
- _WorkflowMode: 1
- _ZWrite: 0
m_Colors:
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0.45140845, g: 6.158499, b: 0, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
m_BuildTextureStacks: []
m_AllowLocking: 1
--- !u!114 &1589063840277063233
MonoBehaviour:
m_ObjectHideFlags: 11
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 9

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5a03792a4237ee6a880b29486efe7f07
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

@ -7,12 +7,11 @@ Material:
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: GridWhite
m_Name: HexGridWhite
m_Shader: {fileID: 4800000, guid: 933532a4fcc9baf4fa0491de14d08ed7, type: 3}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords:
- _ALPHAPREMULTIPLY_ON
- _SURFACE_TYPE_TRANSPARENT
m_InvalidKeywords: []
m_LightmapFlags: 4
@ -91,7 +90,7 @@ Material:
- _AlphaClip: 0
- _AlphaToMask: 0
- _Blend: 0
- _BlendModePreserveSpecular: 1
- _BlendModePreserveSpecular: 0
- _BumpScale: 1
- _ClearCoatMask: 0
- _ClearCoatSmoothness: 0
@ -110,10 +109,10 @@ Material:
- _Parallax: 0.005
- _QueueOffset: 0
- _ReceiveShadows: 1
- _Smoothness: 1
- _Smoothness: 0.552
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _SrcBlend: 5
- _SrcBlendAlpha: 1
- _Surface: 1
- _WorkflowMode: 1

@ -31,6 +31,7 @@ Transform:
m_ConstrainProportionsScale: 1
m_Children:
- {fileID: 8988725203248466747}
- {fileID: 5042683358940564155}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &3475492525256023353
@ -46,6 +47,92 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
meshRenderer: {fileID: 8582009238152053183}
selectedGameObject: {fileID: 6439026718077445406}
--- !u!1 &6439026718077445406
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 5042683358940564155}
- component: {fileID: 985454775846429915}
- component: {fileID: 6683619282588536226}
m_Layer: 0
m_Name: Selected
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!4 &5042683358940564155
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6439026718077445406}
serializedVersion: 2
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
m_LocalPosition: {x: 0, y: 0.04, z: 0}
m_LocalScale: {x: 2, y: 2, z: 2}
m_ConstrainProportionsScale: 1
m_Children: []
m_Father: {fileID: 7003346701217803257}
m_LocalEulerAnglesHint: {x: 90, y: 0, z: 0}
--- !u!33 &985454775846429915
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6439026718077445406}
m_Mesh: {fileID: 10210, guid: 0000000000000000e000000000000000, type: 0}
--- !u!23 &6683619282588536226
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6439026718077445406}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RayTracingAccelStructBuildFlagsOverride: 0
m_RayTracingAccelStructBuildFlags: 1
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: 5a03792a4237ee6a880b29486efe7f07, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!1 &7129222469917164262
GameObject:
m_ObjectHideFlags: 0

@ -10633,7 +10633,7 @@ PrefabInstance:
- target: {fileID: 4785646500396285758, guid: 1c577f9fe0c6219a992900cb942290f8,
type: 3}
propertyPath: m_LocalPosition.x
value: -13
value: -12
objectReference: {fileID: 0}
- target: {fileID: 4785646500396285758, guid: 1c577f9fe0c6219a992900cb942290f8,
type: 3}
@ -10643,7 +10643,7 @@ PrefabInstance:
- target: {fileID: 4785646500396285758, guid: 1c577f9fe0c6219a992900cb942290f8,
type: 3}
propertyPath: m_LocalPosition.z
value: 14
value: 12
objectReference: {fileID: 0}
- target: {fileID: 4785646500396285758, guid: 1c577f9fe0c6219a992900cb942290f8,
type: 3}
@ -14576,7 +14576,7 @@ PrefabInstance:
- target: {fileID: 4785646500396285758, guid: 1c577f9fe0c6219a992900cb942290f8,
type: 3}
propertyPath: m_LocalPosition.x
value: 8
value: -8
objectReference: {fileID: 0}
- target: {fileID: 4785646500396285758, guid: 1c577f9fe0c6219a992900cb942290f8,
type: 3}
@ -14586,7 +14586,7 @@ PrefabInstance:
- target: {fileID: 4785646500396285758, guid: 1c577f9fe0c6219a992900cb942290f8,
type: 3}
propertyPath: m_LocalPosition.z
value: 22
value: 9
objectReference: {fileID: 0}
- target: {fileID: 4785646500396285758, guid: 1c577f9fe0c6219a992900cb942290f8,
type: 3}

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
@ -29,9 +30,30 @@ namespace Grid {
}
public GridPosition GetGridPosition(Vector3 worldPosition) {
int x = Mathf.RoundToInt(worldPosition.x / cellSize);
int z = Mathf.RoundToInt(worldPosition.z / cellSize);
return new(x, z);
//Get the rough GridPosition
GridPosition roughXZ = new(Mathf.RoundToInt(worldPosition.x / cellSize), Mathf.RoundToInt(worldPosition.z / cellSize / hexVerticalOffsetMultiplier));
//Get all neighbours
bool oddRow = roughXZ.Z % 2 == 1;
List<GridPosition> neighbourGridPositionList = new() {
roughXZ + new GridPosition(-1, 0),
roughXZ + new GridPosition(+1, 0),
roughXZ + new GridPosition(0, +1),
roughXZ + new GridPosition(0, -1),
roughXZ + new GridPosition(oddRow ? +1 : -1, +1),
roughXZ + new GridPosition(oddRow ? +1 : -1, -1),
};
//Find the closest neighbour
GridPosition closestGridPosition = roughXZ;
foreach (GridPosition neighbourGridPosition in neighbourGridPositionList) {
if (Vector3.Distance(worldPosition, GetWorldPosition(neighbourGridPosition)) < Vector3.Distance(worldPosition, GetWorldPosition(closestGridPosition)))
closestGridPosition = neighbourGridPosition;
}
return closestGridPosition;
}
public void CreateDebugObjects(Transform debugPrefab) {

@ -22,6 +22,7 @@ namespace Grid {
private List<GridVisualTypeMaterial> gridVisualTypeMaterialList;
private GridSystemVisualSingle[,] gridSystemVisualSingleArray;
private GridSystemVisualSingle lastSelectedGridSystemVisualSingle;
private static GridSystemVisual Instance { get; set; }
private void Awake() {
@ -34,6 +35,15 @@ namespace Grid {
Instance = this;
}
// private void Update() {
// if (lastSelectedGridSystemVisualSingle is not null) lastSelectedGridSystemVisualSingle.SetSelected(false); //Deselected last one
// GridPosition gridPosition = LevelGrid.Instance.GetGridPosition(MouseWorld.GetPosition());
// if (LevelGrid.Instance.IsValidGridPosition(gridPosition)) {
// lastSelectedGridSystemVisualSingle = gridSystemVisualSingleArray[gridPosition.X, gridPosition.Z];
// }
// if (lastSelectedGridSystemVisualSingle is not null) lastSelectedGridSystemVisualSingle.SetSelected(true); //Select current one
// }
private void Start() {
int width = LevelGrid.Instance.GetWidth();
int height = LevelGrid.Instance.GetHeight();
@ -50,11 +60,11 @@ namespace Grid {
LevelGrid.Instance.OnAnyUnitMovedGridPosition += LevelGrid_OnAnyUnitMovedGridPosition;
UpdateGridVisual();
for (int x = 0; x < LevelGrid.Instance.GetWidth(); x++) {
for (int z = 0; z < LevelGrid.Instance.GetHeight(); z++) {
gridSystemVisualSingleArray[x, z].Show(GetGridVisualTypeMaterial(GridVisualType.White));
}
}
// for (int x = 0; x < LevelGrid.Instance.GetWidth(); x++) {
// for (int z = 0; z < LevelGrid.Instance.GetHeight(); z++) {
// gridSystemVisualSingleArray[x, z].Show(GetGridVisualTypeMaterial(GridVisualType.White));
// }
// }
}
private void LevelGrid_OnAnyUnitMovedGridPosition(object sender, EventArgs e) => UpdateGridVisual();

@ -3,6 +3,7 @@ using UnityEngine;
namespace Grid {
public class GridSystemVisualSingle : MonoBehaviour {
[SerializeField] private MeshRenderer meshRenderer;
// [SerializeField] private GameObject selectedGameObject;
public void Show(Material material) {
meshRenderer.material = material;
@ -10,5 +11,7 @@ namespace Grid {
}
public void Hide() => meshRenderer.enabled = false;
// public void SetSelected(bool selected) => selectedGameObject.SetActive(selected);
}
}