public void Render(RenderContext11 renderContext, float opacity) { if (dirty && !(ISSLayer)) { Reload(); } Matrix3d oldWorld = renderContext.World; Vector3 offset = mesh.BoundingSphere.Center; float unitScale = 1.0f; if (mesh.BoundingSphere.Radius > 0.0f) { unitScale = 1.0f / mesh.BoundingSphere.Radius; } renderContext.World = Matrix3d.Translation(-offset.X, -offset.Y, -offset.Z) * Matrix3d.Scaling(unitScale, unitScale, unitScale) * oldWorld; Matrix3d worldView = renderContext.World * renderContext.View; Vector3d v = worldView.Transform(Vector3d.Empty); double scaleFactor = Math.Sqrt(worldView.M11 * worldView.M11 + worldView.M22 * worldView.M22 + worldView.M33 * worldView.M33) / unitScale; double dist = v.Length(); double radius = scaleFactor; // Calculate pixelsPerUnit which is the number of pixels covered // by an object 1 AU at the distance of the planet center from // the camera. This calculation works regardless of the projection // type. int viewportHeight = (int)renderContext.ViewPort.Height; double p11 = renderContext.Projection.M11; double p34 = renderContext.Projection.M34; double p44 = renderContext.Projection.M44; double w = Math.Abs(p34) * dist + p44; float pixelsPerUnit = (float)(p11 / w) * viewportHeight; float radiusInPixels = (float)(radius * pixelsPerUnit); if (radiusInPixels < 0.5f) { // Too small to be visible; skip rendering return; } // These colors can be modified by shadows, distance from planet, etc. Restore // the original values after rendering. var savedSunlightColor = renderContext.SunlightColor; var savedReflectedColor = renderContext.ReflectedLightColor; var savedHemiColor = renderContext.HemisphereLightColor; if (Properties.Settings.Default.SolarSystemLighting) { SetupLighting(renderContext); renderContext.AmbientLightColor = System.Drawing.Color.FromArgb(11, 11, 11); } else { // No lighting: set ambient light to white and turn off all other light sources renderContext.SunlightColor = System.Drawing.Color.Black; renderContext.ReflectedLightColor = System.Drawing.Color.Black; renderContext.HemisphereLightColor = System.Drawing.Color.Black; renderContext.AmbientLightColor = System.Drawing.Color.White; } SharpDX.Direct3D11.Device device = renderContext.Device; if (mesh == null) { return; } //Object3dLayer.sketch.DrawLines(renderContext, 1.0f, System.Drawing.Color.Red); renderContext.DepthStencilMode = DepthStencilMode.ZReadWrite; renderContext.BlendMode = BlendMode.Alpha; int count = meshMaterials.Count; mesh.beginDrawing(renderContext); if (count > 0) { for (int i = 0; i < meshMaterials.Count; i++) { if (meshMaterials[i].Default) { Material mat = meshMaterials[i]; mat.Diffuse = Color; mat.Ambient = Color; meshMaterials[i] = mat; } // Set the material and texture for this subset renderContext.SetMaterial(meshMaterials[i], meshTextures[i], meshSpecularTextures[i], meshNormalMaps[i], opacity); renderContext.PreDraw(); renderContext.setSamplerState(0, renderContext.WrapSampler); mesh.drawSubset(renderContext, i); } } else { renderContext.PreDraw(); for (int i = 0; i < meshTextures.Count; i++) { var key = new PlanetShaderKey(PlanetSurfaceStyle.Diffuse, false, 0); renderContext.SetupPlanetSurfaceEffect(key, 1.0f); if (meshTextures[i] != null) { renderContext.MainTexture = meshTextures[i]; } renderContext.PreDraw(); mesh.drawSubset(renderContext, i); } } renderContext.setSamplerState(0, renderContext.ClampSampler); renderContext.setRasterizerState(TriangleCullMode.CullClockwise); renderContext.World = oldWorld; renderContext.SunlightColor = savedSunlightColor; renderContext.ReflectedLightColor = savedReflectedColor; renderContext.HemisphereLightColor = savedHemiColor; renderContext.AmbientLightColor = System.Drawing.Color.Black; }