Skip to content

Commit 920870a

Browse files
committed
feat: Make clouds respond to average wind speed and direction
1 parent 39e75c4 commit 920870a

File tree

5 files changed

+27
-41
lines changed

5 files changed

+27
-41
lines changed

Source/RunActivity/Content/SkyShader.fx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
// COPYRIGHT 2010, 2011, 2012, 2013 by the Open Rails project.
2-
//
1+
// COPYRIGHT 2009 - 2023 by the Open Rails project.
2+
//
33
// This file is part of Open Rails.
4-
//
4+
//
55
// Open Rails is free software: you can redistribute it and/or modify
66
// it under the terms of the GNU General Public License as published by
77
// the Free Software Foundation, either version 3 of the License, or
88
// (at your option) any later version.
9-
//
9+
//
1010
// Open Rails is distributed in the hope that it will be useful,
1111
// but WITHOUT ANY WARRANTY; without even the implied warranty of
1212
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1313
// GNU General Public License for more details.
14-
//
14+
//
1515
// You should have received a copy of the GNU General Public License
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717

18-
// This file is the responsibility of the 3D & Environment Team.
18+
// This file is the responsibility of the 3D & Environment Team.
1919

2020
////////////////////////////////////////////////////////////////////////////////
2121
// S H A D O W M A P S H A D E R //
@@ -27,7 +27,7 @@ float4x4 WorldViewProjection; // model -> world -> view -> projection
2727
float4 LightVector; // Direction vector to sun, w = 1/length of vector
2828
float Time; // Used for moving textures across the sky
2929
float4 Overcast; // x = alpha, y = contrast, z = brightness, w = !Overcast.y && !Overcast.z
30-
float2 WindDisplacement;
30+
float4 CloudScalePosition;
3131
float3 SkyColor;
3232
float3 FogColor;
3333
float4 Fog;
@@ -255,10 +255,7 @@ float4 PSMoon(VERTEX_OUTPUT In) : COLOR
255255

256256
float4 PSClouds(VERTEX_OUTPUT In) : COLOR
257257
{
258-
// Get the color information for the current pixel
259-
// Cloud map is tiled. Tiling factor: 4
260-
// Move cloud map to suit wind conditions
261-
float2 TexCoord = float2(In.TexCoord.x * 4 + WindDisplacement.x, In.TexCoord.y * 4 + WindDisplacement.y);
258+
float2 TexCoord = In.TexCoord.xy * CloudScalePosition.xy - CloudScalePosition.zw;
262259
float4 cloudColor = tex2D(CloudMapSampler, TexCoord);
263260
float alpha = cloudColor.a;
264261

Source/RunActivity/Viewer3D/MSTSSky.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,7 @@ public override void Render(GraphicsDevice graphicsDevice, IEnumerable<RenderIte
585585
MSTSSkyShader.MoonScale = MSTSSkyConstants.skyRadius / 20;
586586
MSTSSkyShader.Overcast = Viewer.World.MSTSSky.mstsskyovercastFactor;
587587
MSTSSkyShader.SetFog(Viewer.World.MSTSSky.mstsskyfogDistance, ref SharedMaterialManager.FogColor);
588-
MSTSSkyShader.WindSpeed = Viewer.World.MSTSSky.mstsskywindSpeed;
589-
MSTSSkyShader.WindDirection = Viewer.World.MSTSSky.mstsskywindDirection; // Keep setting this after Time and Windspeed. Calculating displacement here.
588+
MSTSSkyShader.CloudScalePosition = Viewer.World.WeatherControl.CloudScalePosition;
590589

591590
for (var i = 0; i < 5; i++)
592591
graphicsDevice.SamplerStates[i] = SamplerState.LinearWrap;

Source/RunActivity/Viewer3D/Shaders.cs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
// COPYRIGHT 2009, 2010, 2011, 2012, 2013 by the Open Rails project.
2-
//
1+
// COPYRIGHT 2009 - 2023 by the Open Rails project.
2+
//
33
// This file is part of Open Rails.
4-
//
4+
//
55
// Open Rails is free software: you can redistribute it and/or modify
66
// it under the terms of the GNU General Public License as published by
77
// the Free Software Foundation, either version 3 of the License, or
88
// (at your option) any later version.
9-
//
9+
//
1010
// Open Rails is distributed in the hope that it will be useful,
1111
// but WITHOUT ANY WARRANTY; without even the implied warranty of
1212
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1313
// GNU General Public License for more details.
14-
//
14+
//
1515
// You should have received a copy of the GNU General Public License
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717

18-
// This file is the responsibility of the 3D & Environment Team.
18+
// This file is the responsibility of the 3D & Environment Team.
1919

2020
using System;
2121
using System.IO;
2222
using Microsoft.Xna.Framework;
2323
using Microsoft.Xna.Framework.Content.Pipeline;
2424
using Microsoft.Xna.Framework.Graphics;
25-
using Orts.Viewer3D.Processes;
2625
using ORTS.Common;
26+
using Orts.Viewer3D.Processes;
2727

2828
namespace Orts.Viewer3D
2929
{
@@ -308,7 +308,7 @@ public class SkyShader : Shader
308308
readonly EffectParameter lightVector;
309309
readonly EffectParameter time;
310310
readonly EffectParameter overcast;
311-
readonly EffectParameter windDisplacement;
311+
readonly EffectParameter cloudScalePosition;
312312
readonly EffectParameter skyColor;
313313
readonly EffectParameter fogColor;
314314
readonly EffectParameter fog;
@@ -382,16 +382,7 @@ public float Overcast
382382
}
383383
}
384384

385-
public float WindSpeed { get; set; }
386-
387-
public float WindDirection
388-
{
389-
set
390-
{
391-
var totalWindDisplacement = 50 * WindSpeed * _time; // This exaggerates the wind speed, but it is necessary to get a visible effect
392-
windDisplacement.SetValue(new Vector2(-(float)Math.Sin(value) * totalWindDisplacement, (float)Math.Cos(value) * totalWindDisplacement));
393-
}
394-
}
385+
public Vector4 CloudScalePosition { set => cloudScalePosition.SetValue(value); }
395386

396387
public float MoonScale { get; set; }
397388

@@ -427,7 +418,7 @@ public SkyShader(GraphicsDevice graphicsDevice)
427418
lightVector = Parameters["LightVector"];
428419
time = Parameters["Time"];
429420
overcast = Parameters["Overcast"];
430-
windDisplacement = Parameters["WindDisplacement"];
421+
cloudScalePosition = Parameters["CloudScalePosition"];
431422
skyColor = Parameters["SkyColor"];
432423
fogColor = Parameters["FogColor"];
433424
fog = Parameters["Fog"];

Source/RunActivity/Viewer3D/Sky.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ namespace Orts.Viewer3D
3131
public class SkyViewer
3232
{
3333
internal readonly SkyPrimitive Primitive;
34-
internal readonly float WindSpeed;
35-
internal readonly float WindDirection;
3634
internal int MoonPhase;
3735
internal Vector3 SolarDirection;
3836
internal Vector3 LunarDirection;
@@ -54,11 +52,6 @@ public SkyViewer(Viewer viewer)
5452

5553
// Instantiate classes
5654
Primitive = new SkyPrimitive(Viewer.RenderProcess);
57-
58-
// Default wind speed and direction
59-
// TODO: We should be using Viewer.Simulator.Weather instead of our own local weather fields
60-
WindSpeed = 5.0f; // m/s (approx 11 mph)
61-
WindDirection = 4.7f; // radians (approx 270 deg, i.e. westerly)
6255
}
6356

6457
public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
@@ -389,8 +382,7 @@ public override void Render(GraphicsDevice graphicsDevice, IEnumerable<RenderIte
389382
SkyShader.MoonScale = SkyPrimitive.RadiusM / 20;
390383
SkyShader.Overcast = Viewer.Simulator.Weather.CloudCoverFactor;
391384
SkyShader.SetFog(Viewer.Simulator.Weather.VisibilityM, ref SharedMaterialManager.FogColor);
392-
SkyShader.WindSpeed = Viewer.World.Sky.WindSpeed;
393-
SkyShader.WindDirection = Viewer.World.Sky.WindDirection; // Keep setting this after Time and Windspeed. Calculating displacement here.
385+
SkyShader.CloudScalePosition = Viewer.World.WeatherControl.CloudScalePosition;
394386

395387
for (var i = 0; i < 5; i++)
396388
{

Source/RunActivity/Viewer3D/Weather.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public class WeatherControl
4747
public readonly List<SoundSourceBase> SnowSound;
4848
public readonly List<SoundSourceBase> WeatherSounds = new List<SoundSourceBase>();
4949

50+
public Vector4 CloudScalePosition { get => new Vector4(CloudScale.X, CloudScale.Y, CloudPositionM.X / SkyPrimitive.RadiusM, CloudPositionM.Y / SkyPrimitive.RadiusM); }
51+
52+
Vector2 CloudScale;
53+
Vector2 CloudPositionM;
5054
WorldLocation CameraWorldLocation;
5155

5256
public bool weatherChangeOn = false;
@@ -186,6 +190,7 @@ public void UpdateWeatherParameters()
186190

187191
Weather.WindAverageDirectionRad = (float)Viewer.Random.NextDouble() * MathHelper.TwoPi;
188192
Weather.WindAverageSpeedMpS = (float)Viewer.Random.NextDouble() * WeatherConstants.WindSpeedBeaufortMpS[WindSpeedBeaufort];
193+
CloudScale = new Vector2(4, 4);
189194
}
190195

191196
void UpdateSoundSources()
@@ -242,6 +247,8 @@ private void UpdateWind(ElapsedTime elapsedTime)
242247
WindUpdateTimer -= WindGustUpdateTimeS;
243248
}
244249

250+
WorldLocation.GetDistance(CameraWorldLocation, Viewer.Camera.CameraWorldLocation).Deconstruct(out var x, out var _, out var y);
251+
CloudPositionM += elapsedTime.ClockSeconds * Weather.WindAverageDirection * Weather.WindAverageSpeedMpS - new Vector2(x, y);
245252
CameraWorldLocation = Viewer.Camera.CameraWorldLocation;
246253
}
247254

0 commit comments

Comments
 (0)