public void NewGame() { WorldData = JsonUtility.FromJson <WorldData>(WorldDataAsset.text); WorldData.Init(); _worldGenData = JsonUtility.FromJson <WorldGenData>(WorldGenAsset.text); Icosphere = Instantiate(IcospherePrefab, transform); Icosphere.Init(Subdivisions); StaticState = new StaticState(); StaticState.Init(_worldGenData.Radius, Icosphere, WorldData); _simulation = new SimTick(StaticState); _tempState = new TempState(StaticState); int height = 3; for (int i = 0; i < _simStates.Length; i++) { _simStates[i] = new SimState(); _simStates[i].Init(StaticState); } WorldGen.Generate(StaticState.Count, height, _worldGenData, _simStates[_curSimStateIndex], StaticState); _initialized = true; NewGameEvent?.Invoke(_simStates[_curSimStateIndex]); }
public void CopyFrom(SimState from) { Planet = from.Planet; Temperature.CopyFrom(from.Temperature); LandMass.CopyFrom(from.LandMass); VaporMass.CopyFrom(from.VaporMass); CloudMass.CopyFrom(from.CloudMass); IceMass.CopyFrom(from.IceMass); WaterMass.CopyFrom(from.WaterMass); SaltMass.CopyFrom(from.SaltMass); MineralMass.CopyFrom(from.MineralMass); CarbonDioxideMass.CopyFrom(from.CarbonDioxideMass); OxygenMass.CopyFrom(from.OxygenMass); NitrogenMass.CopyFrom(from.NitrogenMass); OrganicMass.CopyFrom(from.OrganicMass); Dirt.CopyFrom(from.Dirt); Sand.CopyFrom(from.Sand); Vegetation.CopyFrom(from.Vegetation); Current.CopyFrom(from.Current); Flow.CopyFrom(from.Flow); Elevation.CopyFrom(from.Elevation); WaterDepth.CopyFrom(from.WaterDepth); Explored.CopyFrom(from.Explored); AnimalPosition.CopyFrom(from.AnimalPosition); AnimalSpecies.CopyFrom(from.AnimalSpecies); }
void OnSimTick(SimState simState) { _lastViewState = _curViewState; _nextViewState = (_nextViewState + 1) % 3; _curViewState = (_curViewState + 1) % 3; var jobHandle = CreateViewStateFromSimState(simState, _viewStates[_nextViewState], Sim.WorldData, Sim.StaticState, TerrainScale, default); jobHandle.Complete(); }
void OnNewGame(SimState simState) { Init(); var jobHandle = CreateViewStateFromSimState(simState, _viewStates[_nextViewState], Sim.WorldData, Sim.StaticState, TerrainScale, default); UpdateMeshes(TerrainMesh.GetComponent <MeshFilter>().sharedMesh, WaterMesh.GetComponent <MeshFilter>().sharedMesh, WaterBackfaceMesh.GetComponent <MeshFilter>().sharedMesh, OverlayMesh.GetComponent <MeshFilter>().sharedMesh, _viewStates[_nextViewState], jobHandle); TerrainMesh.GetComponent <MeshCollider>().sharedMesh = null; TerrainMesh.GetComponent <MeshCollider>().sharedMesh = TerrainMesh.GetComponent <MeshFilter>().sharedMesh; }
private void SelectCells(int posIndex, CellSelectionOptions options, SimState state, List <Tuple <int, float> > selected) { var center = HUD.Sim.StaticState.SphericalPosition[posIndex]; for (int i = 0; i < HUD.Sim.StaticState.Count; i++) { var p = HUD.Sim.StaticState.SphericalPosition[i]; float dist = math.distance(p, center) * HUD.Sim.StaticState.PlanetRadius / HUD.Sim.StaticState.CellRadius; if (dist <= options.BrushSize) { float elevation = state.Elevation[i]; if (elevation >= options.MaskBottom && elevation <= options.MaskTop) { float strength = (1 - options.NoiseStrength) + (noise.snoise(p * options.NoisePeriod) * 0.5f + 0.5f) * options.NoiseStrength; selected.Add(new Tuple <int, float>(i, strength * math.pow(1.0f - math.saturate(dist / options.BrushSize), options.Falloff))); } } } }
public JobHandle CreateViewStateFromSimState(SimState from, ViewState to, WorldData worldData, StaticState staticState, float terrainScale, JobHandle dependency) { //to.Ticks = from.PlanetState.Ticks; //to.Position = from.PlanetState.Position; //to.Rotation = math.degrees(from.PlanetState.Rotation); MeshOverlayData meshOverlay; bool useMeshOverlay = GetMeshOverlayData(ActiveOverlay, from, staticState, worldData, out meshOverlay); var buildRenderStateJobHandle = _perCellJobHelper.Schedule( true, 1, dependency, new CreateViewStateJob() { TerrainColor = to.TerrainColor, TerrainElevation = to.TerrainElevation, WaterColor = to.WaterColor, WaterElevation = to.WaterElevation, OverlayColor = to.OverlayColor, TerrainState = to.TerrainState, Elevation = from.Elevation, WaterDepth = from.WaterDepth, Dirt = from.Dirt, Sand = from.Sand, Vegetation = from.Vegetation, Ice = from.IceMass, Explored = from.Explored, MeshOverlayMin = meshOverlay.Colors.Min, MeshOverlayInverseRange = meshOverlay.InverseRange, MeshOverlayData = meshOverlay.Values, MeshOverlayColors = meshOverlay.Colors.ColorValuePairs, MeshOverlayActive = ActiveOverlay != MeshOverlay.None, PlanetRadius = staticState.PlanetRadius, TerrainScale = terrainScale, }); return(buildRenderStateJobHandle); }
static public void Generate(int columns, int height, WorldGenData worldGenData, SimState state, StaticState staticState) { state.Planet.Gravity = worldGenData.Gravity; state.Planet.DistanceToSun = worldGenData.DistanceToSun; state.Planet.Rotation = math.radians(math.float3(worldGenData.TiltAngle, 0, 0)); state.Planet.Position = math.float3(1, 0, 0) * worldGenData.DistanceToSun; state.Planet.SpinSpeed = math.PI * 2 / (worldGenData.SpinTime * 60 * 60); state.Planet.OrbitSpeed = math.PI * 2 / worldGenData.OrbitTime; state.Planet.AngularSpeed = math.PI * 2 / (worldGenData.SpinTime * 60 * 60); state.Planet.GeothermalHeat = worldGenData.GeothermalHeat; state.Planet.SolarRadiation = worldGenData.SolarRadiation; for (int i = 0; i < columns; i++) { var pos = staticState.SphericalPosition[i]; state.Elevation[i] = (0.5f * noise.snoise(pos) + 0.4f * noise.snoise(pos * 3) + 0.1f * noise.snoise(pos * 9)) * 12000f - 1000f; state.WaterDepth[i] = math.max(0, -state.Elevation[i]); state.Dirt[i] = math.max(0, -state.Elevation[i]); state.Sand[i] = math.max(0, -state.Elevation[i]); state.Vegetation[i] = math.max(0, -state.Elevation[i]); state.IceMass[i] = noise.snoise(pos) * (math.saturate(state.Elevation[i]) + 1000) * math.abs(staticState.Coordinate[i].y) / 1000; state.Dirt[i] = math.saturate(noise.snoise(pos)); state.Sand[i] = math.saturate(noise.snoise(pos + new float3(6567))); state.Vegetation[i] = math.saturate(noise.snoise(pos + new float3(543252))); state.Temperature[i] = WorldData.FreezingTemperature; state.Explored[i] = pos.x > 0.2f ? 1 : 0; } }
public JobHandle Tick(SimState lastState, SimState nextState, StaticState staticState, TempState tempState, WorldData worldData, JobHandle dependency) { float coriolisTerm = 2 * lastState.Planet.SpinSpeed; nextState.CarbonDioxideMass.CopyFrom(lastState.CarbonDioxideMass); nextState.CloudMass.CopyFrom(lastState.CloudMass); nextState.Current.CopyFrom(lastState.Current); nextState.IceMass.CopyFrom(lastState.IceMass); nextState.LandMass.CopyFrom(lastState.LandMass); nextState.MineralMass.CopyFrom(lastState.MineralMass); nextState.NitrogenMass.CopyFrom(lastState.NitrogenMass); nextState.OrganicMass.CopyFrom(lastState.OrganicMass); nextState.OxygenMass.CopyFrom(lastState.OxygenMass); nextState.SaltMass.CopyFrom(lastState.SaltMass); nextState.Temperature.CopyFrom(lastState.Temperature); nextState.VaporMass.CopyFrom(lastState.VaporMass); nextState.WaterMass.CopyFrom(lastState.WaterMass); nextState.WaterDepth.CopyFrom(lastState.WaterDepth); nextState.Flow.CopyFrom(lastState.Flow); nextState.Vegetation.CopyFrom(lastState.Vegetation); nextState.Sand.CopyFrom(lastState.Sand); nextState.Dirt.CopyFrom(lastState.Dirt); nextState.OrganicMass.CopyFrom(lastState.OrganicMass); nextState.Elevation.CopyFrom(lastState.Elevation); nextState.Explored.CopyFrom(lastState.Explored); nextState.AnimalSpecies.CopyFrom(lastState.AnimalSpecies); nextState.AnimalPosition.CopyFrom(lastState.AnimalPosition); nextState.Planet = lastState.Planet; bool sync = false; dependency = _columnJobHelper.Schedule(sync, 1, dependency, new UpdateSurfaceElevationJob() { SurfaceElevation = tempState.SurfaceElevation, WaterDepth = nextState.WaterDepth, Elevation = nextState.Elevation }); dependency = _neighborJobHelper.Schedule(sync, 1, dependency, new UpdateFlowVelocityJob() { Flow = nextState.Flow, LastFlow = lastState.Flow, SurfaceElevation = tempState.SurfaceElevation, WaterDepth = nextState.WaterDepth, NeighborDistInverse = staticState.NeighborDistInverse, Neighbors = staticState.Neighbors, SecondsPerTick = worldData.SecondsPerTick, Gravity = nextState.Planet.Gravity, Damping = worldData.SurfaceWaterFlowDamping, ViscosityInverse = 1.0f - worldData.WaterViscosity }); dependency = _columnJobHelper.Schedule(sync, 1, dependency, new SumOutgoingFlowJob() { OutgoingFlow = tempState.OutgoingFlow, Flow = nextState.Flow, }); dependency = _neighborJobHelper.Schedule(sync, 1, dependency, new LimitOutgoingFlowJob() { Flow = nextState.Flow, FlowPercent = tempState.FlowPercent, OutgoingFlow = tempState.OutgoingFlow, WaterDepth = nextState.WaterDepth, Neighbors = staticState.Neighbors, }); dependency = _columnJobHelper.Schedule(sync, 1, dependency, new ApplyFlowWaterJob() { Delta = tempState.WaterDelta, Depth = nextState.WaterDepth, Positions = staticState.SphericalPosition, Neighbors = staticState.Neighbors, ReverseNeighbors = staticState.ReverseNeighbors, FlowPercent = tempState.FlowPercent, CoriolisMultiplier = staticState.CoriolisMultiplier, CoriolisTerm = coriolisTerm, SecondsPerTick = worldData.SecondsPerTick }); dependency = _columnJobHelper.Schedule(sync, 1, dependency, new ApplyWaterDeltaJob() { Depth = nextState.WaterDepth, Delta = tempState.WaterDelta, }); dependency = _animalJobHelper.Schedule(sync, dependency, new UpdateExplorationJob() { Exploration = nextState.Explored, AnimalSpecies = nextState.AnimalSpecies, AnimalPositions = nextState.AnimalPosition, }); return(dependency); }