Skip to content

Commit b11e889

Browse files
author
davidv-unity
committed
updated ECSSamples package dependencies
1 parent d78b0ff commit b11e889

File tree

64 files changed

+1902
-1043
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1902
-1043
lines changed

ECSSamples/Assets/Advanced/GridPath/Scenes/GridCube/Grid.unity

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ MonoBehaviour:
150150
m_Script: {fileID: 11500000, guid: 7238b95470734819b4a9ba11900739a2, type: 3}
151151
m_Name:
152152
m_EditorClassIdentifier:
153-
Version: 4-macton-2
154153
RowCount: 32
155154
FloorPrefab:
156155
- {fileID: 6226756458611596980, guid: fa9e694a0208e45c48a1c607230cd068, type: 3}

ECSSamples/Assets/Advanced/GridPath/Scenes/GridPlane/Grid.unity

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ MonoBehaviour:
150150
m_Script: {fileID: 11500000, guid: d3b7dec172b34d219fd0799d314645e5, type: 3}
151151
m_Name:
152152
m_EditorClassIdentifier:
153-
Version: 4-macton-9
154153
ColumnCount: 128
155154
RowCount: 32
156155
FloorPrefab:
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using Unity.Entities;
3+
using Unity.Mathematics;
4+
5+
// Direction along grid axis
6+
// 0 = N
7+
// 1 = S
8+
// 2 = W
9+
// 3 = E
10+
public struct CartesianGridDirection : IComponentData
11+
{
12+
public byte Value; // 2 bits current direction
13+
}
14+
15+
// Speed of movement in grid-space
16+
// - 6:10 fixed point instead of 32bit float (for size)
17+
public struct CartesianGridSpeed : IComponentData
18+
{
19+
public ushort Value;
20+
}
21+
22+
// Coordinates quantized to the grid
23+
public struct CartesianGridCoordinates : IComponentData, IEquatable<CartesianGridCoordinates>
24+
{
25+
public short x;
26+
public short y;
27+
28+
public CartesianGridCoordinates(float2 pos, int rowCount, int colCount)
29+
{
30+
// Quantized grid coordinates from position in grid space
31+
// - Always positive when on the grid. (Up, Right)
32+
// - Basically just float to int cast.
33+
// - However, in the case of a cube, the translation may go off the grid when travelling around the corner.
34+
// In this case, stretch the one-off grid element to encompass the whole area off the grid.
35+
// e.g. if it's off to the left, grid x value is always -1.
36+
x = (short)math.clamp(((int)(pos.x + 1.0f)) - 1, -1, colCount);
37+
y = (short)math.clamp(((int)(pos.y + 1.0f)) - 1, -1, rowCount);
38+
}
39+
40+
public bool Equals(CartesianGridCoordinates other)
41+
{
42+
return ((other.x == x) && (other.y == y));
43+
}
44+
}

ECSSamples/Assets/Advanced/GridPath/Scripts/CartesianGrid.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
using Unity.Burst;
2+
using Unity.Collections;
3+
using Unity.Collections.LowLevel.Unsafe;
4+
using Unity.Entities;
5+
using Unity.Jobs;
6+
using Unity.Mathematics;
7+
using Unity.Transforms;
8+
using UnityEngine;
9+
10+
public static class CartesianGridGeneratorUtility
11+
{
12+
public enum WallFlags : byte
13+
{
14+
SouthWall = 1,
15+
WestWall = 2,
16+
}
17+
18+
public static unsafe void CreateGridPath(int rowCount, int columnCount, byte* gridWalls, float wallSProbability, float wallWProbability, bool outerWalls)
19+
{
20+
var buildGridPathJob = new BuildGridPath
21+
{
22+
GridWalls = gridWalls,
23+
RowCount = rowCount,
24+
ColumnCount = columnCount,
25+
WallSProbability = wallSProbability,
26+
WallWProbability = wallWProbability,
27+
OuterWalls = outerWalls
28+
};
29+
buildGridPathJob.Run();
30+
}
31+
32+
[BurstCompile]
33+
unsafe struct BuildGridPath : IJob
34+
{
35+
[NativeDisableUnsafePtrRestriction]
36+
public byte* GridWalls;
37+
public int RowCount;
38+
public int ColumnCount;
39+
public float WallSProbability;
40+
public float WallWProbability;
41+
public bool OuterWalls;
42+
43+
public void Execute()
44+
{
45+
var cx = (ColumnCount * 0.5f);
46+
var cz = (RowCount * 0.5f);
47+
48+
// Add additional row/col to create SE walls along outer edge
49+
var GridWallsRowCount = RowCount + 1;
50+
var GridWallsColumnCount = ColumnCount + 1;
51+
52+
// 2 bit (0=None, 1=South Wall, 2=West Wall)
53+
// Temp allocations in jobs auto-disposed
54+
var GridWallsSW = new NativeArray<byte>(GridWallsRowCount * GridWallsColumnCount, Allocator.Temp);
55+
56+
// Populate the grid
57+
for (int y = 0; y < GridWallsRowCount; y++)
58+
for (int x = 0; x < GridWallsColumnCount; x++)
59+
{
60+
// By default place outer walls along the edge of the grid.
61+
if ((y == 0) && (x < (GridWallsColumnCount - 1)))
62+
GridWallsSW[(y * GridWallsColumnCount) + x] |= (byte)WallFlags.SouthWall;
63+
if ((x == 0) && (y < (GridWallsRowCount - 1)))
64+
GridWallsSW[(y * GridWallsColumnCount) + x] |= (byte)WallFlags.WestWall;
65+
if ((y == (GridWallsRowCount - 1)) && (x < (GridWallsColumnCount - 1)))
66+
GridWallsSW[(y * GridWallsColumnCount) + x] |= (byte)WallFlags.SouthWall;
67+
if ((x == (GridWallsColumnCount - 1)) && (y < (GridWallsRowCount - 1)))
68+
GridWallsSW[(y * GridWallsColumnCount) + x] |= (byte)WallFlags.WestWall;
69+
70+
if ((x < (GridWallsColumnCount - 1)) && (y < (GridWallsRowCount - 1)))
71+
{
72+
var tx = ((float)x) - cx;
73+
var tz = ((float)y) - cz;
74+
75+
var n0 = noise.snoise(new float2(tx * 1.2345f, tz * 1.2345f));
76+
var n1 = noise.snoise(new float2(tz * 1.6789f, tx * 1.6789f));
77+
78+
if (((n0 * 0.5f) + 0.5f) < WallSProbability)
79+
GridWallsSW[(y * GridWallsColumnCount) + x] |= (byte)WallFlags.SouthWall;
80+
if (((n1 * 0.5f) + 0.5f) < WallWProbability)
81+
GridWallsSW[(y * GridWallsColumnCount) + x] |= (byte)WallFlags.WestWall;
82+
}
83+
84+
// Make sure there are no outer walls along the edge of the grid
85+
if (!OuterWalls)
86+
{
87+
if (x == 0)
88+
GridWallsSW[(y * GridWallsColumnCount) + x] &= (byte)~WallFlags.WestWall;
89+
if (x == (GridWallsColumnCount - 1))
90+
GridWallsSW[(y * GridWallsColumnCount) + x] &= (byte)~WallFlags.WestWall;
91+
if (y == (GridWallsRowCount - 1))
92+
GridWallsSW[(y * GridWallsColumnCount) + x] &= (byte)~WallFlags.SouthWall;
93+
if (y == 0)
94+
GridWallsSW[(y * GridWallsColumnCount) + x] &= (byte)~WallFlags.SouthWall;
95+
}
96+
}
97+
98+
for (int y = 0; y < RowCount; y++)
99+
for (int x = 0; x < ColumnCount; x++)
100+
{
101+
var wallN = ((GridWallsSW[((y + 1) * GridWallsColumnCount) + x] & (byte)WallFlags.SouthWall) != 0);
102+
var wallS = ((GridWallsSW[(y * GridWallsColumnCount) + x] & (byte)WallFlags.SouthWall) != 0);
103+
var wallW = ((GridWallsSW[(y * GridWallsColumnCount) + x] & (byte)WallFlags.WestWall) != 0);
104+
var wallE = ((GridWallsSW[(y * GridWallsColumnCount) + (x + 1)] & (byte)WallFlags.WestWall) != 0);
105+
var walls = ((wallN) ? 0x01 : 0x00)
106+
| ((wallS) ? 0x02 : 0x00)
107+
| ((wallW) ? 0x04 : 0x00)
108+
| ((wallE) ? 0x08 : 0x00);
109+
110+
var gridWallIndex = (y * ((ColumnCount+1)/2)) + (x / 2);
111+
walls <<= (x & 1) * 4; // odd columns packed into upper 4 bits
112+
walls |= GridWalls[gridWallIndex];
113+
GridWalls[gridWallIndex] = (byte)walls;
114+
}
115+
}
116+
}
117+
118+
public static void CreateFloorPanel(EntityManager dstManager, Entity prefab, float4x4 parentLocalToWorld, float tx, float tz)
119+
{
120+
var pos = new float3(tx + 0.5f, 0.0f, tz + 0.5f);
121+
var childLocalToParent = math.mul(float4x4.Translate(pos), float4x4.Scale(0.98f));
122+
var localToWorld = new LocalToWorld
123+
{
124+
Value = math.mul(parentLocalToWorld,childLocalToParent)
125+
};
126+
127+
CreatePanel(dstManager, prefab, localToWorld);
128+
}
129+
130+
public static void CreateWallS(EntityManager dstManager, Entity prefab, float4x4 parentLocalToWorld, float tx, float tz)
131+
{
132+
var pos = new float3(tx + 0.5f, 1.0f, tz);
133+
var childLocalToParent = math.mul(float4x4.Translate(pos), float4x4.Scale(1.1f, 1.1f, 0.1f));
134+
var localToWorld = new LocalToWorld
135+
{
136+
Value = math.mul(parentLocalToWorld,childLocalToParent)
137+
};
138+
139+
CreatePanel(dstManager, prefab, localToWorld);
140+
}
141+
142+
public static void CreateWallW(EntityManager dstManager, Entity prefab, float4x4 parentLocalToWorld, float tx, float tz)
143+
{
144+
var pos = new float3(tx, 1.0f, tz + 0.5f);
145+
var childLocalToParent = math.mul(float4x4.Translate(pos), float4x4.Scale(0.1f, 1.1f, 1.1f));
146+
var localToWorld = new LocalToWorld
147+
{
148+
Value = math.mul(parentLocalToWorld,childLocalToParent)
149+
};
150+
151+
CreatePanel(dstManager, prefab, localToWorld);
152+
}
153+
154+
public static void CreatePanel(EntityManager dstManager, Entity prefab, LocalToWorld localToWorld)
155+
{
156+
var panelEntity = dstManager.Instantiate(prefab);
157+
158+
dstManager.RemoveComponent<Translation>(panelEntity);
159+
dstManager.RemoveComponent<Rotation>(panelEntity);
160+
dstManager.SetComponentData(panelEntity, localToWorld);
161+
}
162+
}

0 commit comments

Comments
 (0)