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