void CalculateAsteroidsTask() { cubeCount = 0; Vector3 position; BoundingFrustum frustum; lock (_camera) { position = _camera.Position; frustum = _camera.Frustum; } var close = AsteroidFieldShared.GetCloseCube(cameraPos, field.CubeSize); int amountCubes = (int)Math.Floor((field.FillDist / field.CubeSize)) + 1; for (int x = -amountCubes; x <= amountCubes; x++) { for (int y = -amountCubes; y <= amountCubes; y++) { for (int z = -amountCubes; z <= amountCubes; z++) { var center = close + new Vector3(x, y, z) * field.CubeSize; var closestDistance = (Vector3.Distance(center, position) - cubeRadius); if (closestDistance >= field.FillDist || closestDistance >= lastFog) { continue; } if (!field.Zone.Shape.ContainsPoint(center)) { continue; } var cubeSphere = new BoundingSphere(center, cubeRadius); if (!frustum.Intersects(cubeSphere)) { continue; } float tval; if (!AsteroidFieldShared.CubeExists(center, field.EmptyCubeFrequency, out tval)) { continue; } if (GetExclusionZone(center) != null) { continue; } cubes[cubeCount++] = new CalculatedCube(center, field.CubeRotation.GetRotation(tval) * Matrix4x4.CreateTranslation(center)); } } } }
void CalculateAsteroids() { Vector3 position; BoundingFrustum frustum; lock (_camera) { position = _camera.Position; frustum = _camera.Frustum; } ZfrustumCulled = ZexistCulled = ZexcludeCulled = ZshapeCulled = 0; var close = AsteroidFieldShared.GetCloseCube(cameraPos, field.CubeSize); var cubeRad = new Vector3(field.CubeSize) * 0.5f; int amountCubes = (int)Math.Floor((field.FillDist / field.CubeSize)) + 1; for (int x = -amountCubes; x <= amountCubes; x++) { for (int y = -amountCubes; y <= amountCubes; y++) { for (int z = -amountCubes; z <= amountCubes; z++) { var center = close + new Vector3(x * field.CubeSize, y * field.CubeSize, z * field.CubeSize); if (!field.Zone.Shape.ContainsPoint(center)) { continue; } //var cubeBox = new BoundingBox(center - cubeRad, center + cubeRad); //if (!frustum.Intersects(cubeBox)) //continue; var cubeSphere = new BoundingSphere(center, field.CubeSize * 0.5f); if (!frustum.Intersects(cubeSphere)) { continue; } float tval; if (!AsteroidFieldShared.CubeExists(center, field.EmptyCubeFrequency, out tval)) { continue; } if (GetExclusionZone(center) != null) { continue; } cubes[cubeCount++] = new CalculatedCube(center, Matrix4.CreateTranslation(center) * field.CubeRotation.GetRotation(tval)); } } } _asteroidsCalculated = true; }
public AsteroidFieldRenderer(AsteroidField field, SystemRenderer sys) { this.field = field; this.sys = sys; //Set up renderDistSq float rdist = 0f; if (field.Zone.Shape is ZoneSphere) { rdist = ((ZoneSphere)field.Zone.Shape).Radius; } else if (field.Zone.Shape is ZoneEllipsoid) { var s = ((ZoneEllipsoid)field.Zone.Shape).Size; rdist = Math.Max(Math.Max(s.X, s.Y), s.Z); } else if (field.Zone.Shape is ZoneBox) { var s = ((ZoneEllipsoid)field.Zone.Shape).Size; rdist = Math.Max(Math.Max(s.X, s.Y), s.Z); } if (field.BillboardCount != -1) { astbillboards = new AsteroidBillboard[field.BillboardCount]; } rdist += field.FillDist; renderDistSq = rdist * rdist; cubes = new CalculatedCube[1000]; _asteroidsCalculation = CalculateAsteroids; if (field.Cube.Count > 0) { CreateBufferObject(); } //Set up band if (field.Band == null) { return; } if (bandShader == null) { bandShader = ShaderCache.Get("AsteroidBand.vs", "AsteroidBand.frag"); _bsTexture = bandShader.Shader.GetLocation("Texture"); _bsCameraPosition = bandShader.Shader.GetLocation("CameraPosition"); _bsColorShift = bandShader.Shader.GetLocation("ColorShift"); _bsTextureAspect = bandShader.Shader.GetLocation("TextureAspect"); } Vector3 sz; if (field.Zone.Shape is ZoneSphere) { sz = new Vector3(((ZoneSphere)field.Zone.Shape).Radius); } else if (field.Zone.Shape is ZoneEllipsoid) { sz = ((ZoneEllipsoid)field.Zone.Shape).Size; } else { return; } sz.Xz -= new Vector2(field.Band.OffsetDistance); lightingRadius = Math.Max(sz.X, sz.Z); renderBand = true; bandTransform = ( Matrix4.CreateScale(sz.X, field.Band.Height / 2, sz.Z) * field.Zone.RotationMatrix * Matrix4.CreateTranslation(field.Zone.Position) ); bandCylinder = new OpenCylinder(SIDES); bandNormal = bandTransform; bandNormal.Invert(); bandNormal.Transpose(); }
public AsteroidFieldRenderer(AsteroidField field, SystemRenderer sys) { this.field = field; this.sys = sys; //Set up renderDistSq float rdist = 0f; if (field.Zone.Shape is ZoneSphere) { rdist = ((ZoneSphere)field.Zone.Shape).Radius; } else if (field.Zone.Shape is ZoneEllipsoid) { var s = ((ZoneEllipsoid)field.Zone.Shape).Size; rdist = Math.Max(Math.Max(s.X, s.Y), s.Z); } else if (field.Zone.Shape is ZoneBox) { var s = ((ZoneEllipsoid)field.Zone.Shape).Size; rdist = Math.Max(Math.Max(s.X, s.Y), s.Z); } if (field.BillboardCount != -1) { billboardCube = new AsteroidBillboard[field.BillboardCount]; for (int i = 0; i < field.BillboardCount; i++) { billboardCube[i].Spawn(this); } calculatedBillboards = new AsteroidBillboard[field.BillboardCount]; } rdist += field.FillDist; renderDistSq = rdist * rdist; cubes = new CalculatedCube[4000]; _asteroidsCalculation = CalculateAsteroidsTask; if (field.Cube.Count > 0) { CreateBufferObject(); } //Set up band if (field.Band == null) { return; } if (bandShader == null) { bandShader = Shaders.AsteroidBand.Get(); _bsTexture = bandShader.Shader.GetLocation("Texture"); _bsCameraPosition = bandShader.Shader.GetLocation("CameraPosition"); _bsColorShift = bandShader.Shader.GetLocation("ColorShift"); _bsTextureAspect = bandShader.Shader.GetLocation("TextureAspect"); } Vector3 sz; if (field.Zone.Shape is ZoneSphere) { sz = new Vector3(((ZoneSphere)field.Zone.Shape).Radius); } else if (field.Zone.Shape is ZoneEllipsoid) { sz = ((ZoneEllipsoid)field.Zone.Shape).Size; } else { return; } sz.X -= field.Band.OffsetDistance; sz.Z -= field.Band.OffsetDistance; lightingRadius = Math.Max(sz.X, sz.Z); renderBand = true; bandTransform = ( Matrix4x4.CreateScale(sz.X, field.Band.Height / 2, sz.Z) * field.Zone.RotationMatrix * Matrix4x4.CreateTranslation(field.Zone.Position) ); bandCylinder = sys.ResourceManager.GetOpenCylinder(SIDES); }