Optimise weather rendering, part 1.

This commit is contained in:
UnknownShadow200 2016-07-18 18:21:37 +10:00
parent 9e90ef6ed5
commit cbfcc01bbf
2 changed files with 47 additions and 43 deletions

View File

@ -52,20 +52,46 @@ namespace ClassicalSharp.Renderers {
int index = 0;
FastColour col = game.World.Env.Sunlight;
for( int dx = -extent; dx <= extent; dx++ ) {
for( int dz = -extent; dz <= extent; dz++ ) {
float rainY = GetRainHeight( pos.X + dx, pos.Z + dz );
float height = Math.Max( game.World.Height, pos.Y + 64 ) - rainY;
if( height <= 0 ) continue;
if( particles && (rainAcc >= 0.25 || moved) )
game.ParticleManager.AddRainParticle( new Vector3( pos.X + dx, rainY, pos.Z + dz ) );
float alpha = AlphaAt( dx * dx + dz * dz );
Utils.Clamp( ref alpha, 0, 0xFF );
col.A = (byte)alpha;
MakeRainForSquare( pos.X + dx, rainY, height, pos.Z + dz, col.Pack(), ref index );
}
VertexP3fT2fC4b v = default(VertexP3fT2fC4b);
for( int dx = -extent; dx <= extent; dx++ )
for( int dz = -extent; dz <= extent; dz++ )
{
int x = pos.X + dx, z = pos.Z + dz;
float y = GetRainHeight( x, z );
float height = Math.Max( game.World.Height, pos.Y + 64 ) - y;
if( height <= 0 ) continue;
if( particles && (rainAcc >= 0.25 || moved) )
game.ParticleManager.AddRainParticle( new Vector3( x, y, z ) );
float alpha = AlphaAt( dx * dx + dz * dz );
Utils.Clamp( ref alpha, 0, 0xFF );
col.A = (byte)alpha;
// NOTE: Making vertex is inlined since this is called millions of times.
v.Colour = col.Pack();
float worldV = vOffset + (z & 1) / 2f - (x & 0x0F) / 16f;
float v1 = y / 6f + worldV, v2 = (y + height) / 6f + worldV;
v.X = x; v.Y = y; v.Z = z; v.U = 0; v.V = v1; vertices[index++] = v;
// (x, y, z) (0, v1)
v.Y = y + height; v.V = v2; vertices[index++] = v;
// (x, y + height, z) (0, v2)
v.X = x + 1; v.Z = z + 1; v.U = 1; vertices[index++] = v;
// (x + 1, y + height, z + 1) (1, v2)
v.Y = y; v.V = v1; vertices[index++] = v;
// (x + 1, y, z + 1) (1, v1)
v.Z = z; vertices[index++] = v;
// (x + 1, y, z) (1, v1)
v.Y = y + height; v.V = v2; vertices[index++] = v;
// (x + 1, y + height, z) (1, v2)
v.X = x; v.Z = z + 1; v.U = 0; vertices[index++] = v;
// (x, y + height, z + 1) (0, v2)
v.Y = y; v.V = v1; vertices[index++] = v;
// (x y, z + 1) (0, v1)
}
if( particles && (rainAcc >= 0.25 || moved) )
rainAcc = 0;
@ -88,22 +114,6 @@ namespace ClassicalSharp.Renderers {
float falloff = 0.05f * x * x - 7 * x;
return 178 + falloff * game.World.Env.WeatherFade;
}
void MakeRainForSquare( int x, float y, float height, int z, int col, ref int index ) {
float worldV = vOffset + (z & 1) / 2f - (x & 0x0F) / 16f;
float v1 = y / 6f + worldV;
float v2 = (y + height) / 6f + worldV;
vertices[index++] = new VertexP3fT2fC4b( x, y, z, 0, v1, col );
vertices[index++] = new VertexP3fT2fC4b( x, y + height, z, 0, v2, col );
vertices[index++] = new VertexP3fT2fC4b( x + 1, y + height, z + 1, 1, v2, col );
vertices[index++] = new VertexP3fT2fC4b( x + 1, y, z + 1, 1, v1, col );
vertices[index++] = new VertexP3fT2fC4b( x + 1, y, z, 1, v1, col );
vertices[index++] = new VertexP3fT2fC4b( x + 1, y + height, z, 1, v2, col );
vertices[index++] = new VertexP3fT2fC4b( x, y + height, z + 1, 0, v2, col );
vertices[index++] = new VertexP3fT2fC4b( x, y, z + 1, 0, v1, col );
}
int length, width, maxY, oneY;
public void Ready( Game game ) { }

View File

@ -279,20 +279,14 @@ namespace ClassicalSharp {
public static void CalcBillboardPoints( Vector2 size, Vector3 position, ref Matrix4 view, out Vector3 p111,
out Vector3 p121, out Vector3 p212, out Vector3 p222 ) {
Vector3 centre = position; centre.Y += size.Y / 2;
Vector3 right = new Vector3( view.Row0.X, view.Row1.X, view.Row2.X );
Vector3 up = new Vector3( view.Row0.Y, view.Row1.Y, view.Row2.Y );
Vector3 a = new Vector3( view.Row0.X * size.X, view.Row1.X * size.X, view.Row2.X * size.X ); // right * size.X
Vector3 b = new Vector3( view.Row0.Y * size.Y, view.Row1.Y * size.Y, view.Row2.Y * size.Y ); // up * size.Y
p111 = Transform( -0.5f, -0.5f, ref size, ref centre, ref up, ref right );
p121 = Transform( -0.5f, 0.5f, ref size, ref centre, ref up, ref right );
p212 = Transform( 0.5f, -0.5f, ref size, ref centre, ref up, ref right );
p222 = Transform( 0.5f, 0.5f, ref size, ref centre, ref up, ref right );
}
static Vector3 Transform( float x, float y, ref Vector2 size,
ref Vector3 centre, ref Vector3 up, ref Vector3 right ) {
return centre + right * x * size.X + up * y * size.Y;
}
p111 = centre + a * -0.5f + b * -0.5f;
p121 = centre + a * -0.5f + b * 0.5f;
p212 = centre + a * 0.5f + b * -0.5f;
p222 = centre + a * 0.5f + b * 0.5f;
}
/// <summary> Linearly interpolates between a given angle range, adjusting if necessary. </summary>
public static float LerpAngle( float leftAngle, float rightAngle, float t ) {