protected override void OnUpdate() { entityQuery = GetEntityQuery(ComponentType.ReadOnly <CellComponent>()); NativeArray <CellComponent> cellSpriteDataArray = entityQuery.ToComponentDataArray <CellComponent>(Allocator.TempJob); MaterialPropertyBlock materialPropertyBlock = new MaterialPropertyBlock(); Vector4[] uv = new Vector4[1]; Camera mainC = Camera.main; Material SpriteSheetMat = CreateTileMap.GetInstance().SpriteSheetMat; Mesh mesh = CreateTileMap.GetInstance().quadMesh; int shaderPropertyId = Shader.PropertyToID("_MainTex_UV"); //Account for limitations of DrawMeshInstanced int sliceCount = 1023; for (int i = 0; i < cellSpriteDataArray.Length; i += sliceCount) { int sliceSize = math.min(cellSpriteDataArray.Length - i, sliceCount); List <Matrix4x4> matrixList = new List <Matrix4x4>(); List <Vector4> uvList = new List <Vector4>(); for (int j = 0; j < sliceSize; j++) { CellComponent cellComponentData = cellSpriteDataArray[i + j]; matrixList.Add(cellComponentData.matrix); uvList.Add(cellComponentData.uv); } materialPropertyBlock.SetVectorArray(shaderPropertyId, uvList); Graphics.DrawMeshInstanced( mesh, 0, SpriteSheetMat, matrixList, materialPropertyBlock); } cellSpriteDataArray.Dispose(); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { //Get Entity Query of CellComponents entityQuery = GetEntityQuery(ComponentType.ReadOnly <CellComponent>()); //Create Readable Current Array var current = entityQuery.ToComponentDataArray <CellComponent>(Allocator.TempJob); //Create Writable next(Future) Array var next = new NativeArray <CellComponent>(current.Length, Allocator.TempJob); //Set next = current to preserve data next.CopyFrom(current); // Max and min cell liquid values float MaxLiquid = 1.0f; float MinLiquid = 0.005f; // Extra liquid a cell can store than the cell above it float MaxCompression = 0.25f; // Lowest and highest amount of liquids allowed to flow per iteration float MinFlow = 0.005f; float MaxFlow = 4f; // Adjusts flow speed (0.0f - 1.0f) float FlowSpeed = 1f; //Grid Width of Map int GridWidth = CreateTileMap.GetInstance().GridWidth; //Calculate Water Physics inputDeps = new CalculateWaterPhysics() { current = current, next = next, MaxLiquid = MaxLiquid, MinLiquid = MinLiquid, MaxCompression = MaxCompression, MinFlow = MinFlow, MaxFlow = MaxFlow, FlowSpeed = FlowSpeed, GridWidth = GridWidth, deltaTime = Time.DeltaTime //Delta Time is applied to Flow }.Schedule(current.Length, 32); //Complete Physics Job inputDeps.Complete(); //Make Current = Water Physics Job's Next array current.CopyFrom(next); //Apply Water Physics inputDeps = new ApplyWaterPhysics() { current = current, next = next }.Schedule(current.Length, 32); inputDeps.Complete(); //Update Entities entityQuery.CopyFromComponentDataArray(next); //Clean Native Arrays current.Dispose(); next.Dispose(); return(inputDeps); }