protected internal override bool Init() { Vertexes = new Vertex[12]; for (int i = 0; i < 12; i++) { int Height = Program.Configuration.Height; Vertexes[i].Position.X = ((i & 1) == 1) ^ ((i & 2) == 2) ? Program.Configuration.Width : 0; if (i > 2) { Vertexes[i].Position.Y = Height; } else { Vertexes[i].Position.Y = 0; } Vertexes[i].TextureCoords.X = 0.0f; Vertexes[i].TextureCoords.Y = 0.0f; } Texture = LoadTexture("Textures/White.png"); Sun = new SunCalculator(Settings.Default.Lng, Settings.Default.Lat, 0, TimeZoneInfo.Local.IsDaylightSavingTime(DateTime.Now)); ZIndex = 0; return(true); }
public void SetupLights(DrawArgs drawArgs) { Device device = drawArgs.device; CameraBase camera = drawArgs.WorldCamera; if (World.Settings.EnableSunShading) { // Set up material Material material = new Material(); material.Diffuse = Color.White; material.Ambient = Color.White; material.Specular = Color.White; material.SpecularSharpness = 50.0f; // thin highlight (smaller = larger) device.Material = material; // Use a white, directional light device.Lights[0].Diffuse = Color.White; device.Lights[0].Specular = Color.FromArgb(0x80, 0x80, 0x80); // soft highlight device.Lights[0].Type = LightType.Directional; // Reading current WorldWind Sun position Point3d sunPosition = SunCalculator.GetGeocentricPosition(TimeKeeper.CurrentTimeUtc); // Setup light direction device.Lights[0].Direction = new Vector3((float)sunPosition.X, (float)sunPosition.Y, (float)sunPosition.Z); device.Lights[0].Update(); device.Lights[0].Enabled = true; device.RenderState.SpecularEnable = true; } else { // Disale lighting device.RenderState.Lighting = false; device.RenderState.Ambient = World.Settings.StandardAmbientColor; device.RenderState.SpecularEnable = false; } }
// Updates vLight and vLightDirection according to Sun position private void UpdateLightVector() { System.DateTime currentTime = TimeKeeper.CurrentTimeUtc; Vector3d sunPosition = SunCalculator.GetGeocentricPosition(currentTime); Vector3 sunVector = new Vector3((float)-sunPosition.X, (float)-sunPosition.Y, (float)-sunPosition.Z); m_vLight = sunVector * 100000000f; m_vLightDirection = new Vector3(m_vLight.X / m_vLight.Length(), m_vLight.Y / m_vLight.Length(), m_vLight.Z / m_vLight.Length()); }
private void Awake() { // Panelrenderer cachen und Event binden _UIRenderer = GetComponent <PanelRenderer>(); _UIRenderer.postUxmlReload = X_BindPanel; // GameManager diesen UI Handler zuweisen GameManager.Instance.MenuChanged += X_MenuChanged; GameManager.Instance.UIController = this; // Lightcalculator cachen _sunCalculator = FindObjectOfType <SunCalculator>(); #if UNITY_IOS // iOS: AR Session cachen _session = FindObjectOfType <ARSession>(); #endif }
public void Foo() { var toWork = DateTime.Now.Date.AddHours(8).AddMinutes(10); var fromWork = DateTime.Now.Date.AddHours(17).AddMinutes(30); var journeyTime = 30 / 60.0; //var sc = new SunCalculator(-0.1, 51.52); var sc = new SunCalculator(3.189, 55.95); // Edinburgh var data = new List<string>(); var toWorkTime = TimeToMinutes(toWork); var fromWorkTime = TimeToMinutes(fromWork); for (var date = new DateTime(2013, 1, 1); date < new DateTime(2014, 1, 1); date = date.AddDays(1)) { var sunRise = sc.CalculateSunRise(date, "GMT Standard Time"); var sunSet = sc.CalculateSunSet(date, "GMT Standard Time"); var sunRiseTime = TimeToMinutes(sunRise); var sunSetTime = TimeToMinutes(sunSet); for (var hour = 0.0; hour < 24; hour++) { GetDataRow(data.Add, date, hour, sunRiseTime, sunSetTime, toWorkTime, fromWorkTime, journeyTime, false); } // Sunrise infill GetInfillDataRows(data.Add, date, sunRiseTime, sunRiseTime, sunSetTime, toWorkTime, fromWorkTime, journeyTime); // Sunset infill GetInfillDataRows(data.Add, date, sunSetTime, sunRiseTime, sunSetTime, toWorkTime, fromWorkTime, journeyTime); //GetInfillDataRows(data.Add, date, toWorkTime, toWorkTime, toWorkTime + journeyTime, toWorkTime, fromWorkTime, journeyTime); //GetInfillDataRows(data.Add, date, fromWorkTime, sunRiseTime, sunSetTime, toWorkTime, fromWorkTime, journeyTime); } System.Diagnostics.Debug.WriteLine(string.Join("\n", data)); }
/// <summary> /// Draws the layer /// </summary> public override void Render(DrawArgs drawArgs) { if (downloadThread != null && downloadThread.IsAlive) { RenderProgress(drawArgs); } if (!this.isInitialized) { return; } try { if (texture == null || m_SurfaceImage != null) { return; } drawArgs.device.SetTexture(0, this.texture); if (this._disableZbuffer) { if (drawArgs.device.RenderState.ZBufferEnable) { drawArgs.device.RenderState.ZBufferEnable = false; } } else { if (!drawArgs.device.RenderState.ZBufferEnable) { drawArgs.device.RenderState.ZBufferEnable = true; } } drawArgs.device.RenderState.ZBufferEnable = true; drawArgs.device.Clear(ClearFlags.ZBuffer, 0, 1.0f, 0); /* if (m_opacity < 255 && device.DeviceCaps.DestinationBlendCaps.SupportsBlendFactor) * { * // Blend * device.RenderState.AlphaBlendEnable = true; * device.RenderState.SourceBlend = m_sourceBlend; * device.RenderState.DestinationBlend = m_destinationBlend; * // Set Red, Green and Blue = opacity * device.RenderState.BlendFactorColor = (m_opacity << 16) | (m_opacity << 8) | m_opacity; * }*/ // else if (EnableColorKeying && device.DeviceCaps.TextureCaps.SupportsAlpha) // { // device.RenderState.AlphaBlendEnable = true; // device.RenderState.SourceBlend = Blend.SourceAlpha; // device.RenderState.DestinationBlend = Blend.InvSourceAlpha; // } drawArgs.device.Transform.World = Matrix.Translation( (float)-drawArgs.WorldCamera.ReferenceCenter.X, (float)-drawArgs.WorldCamera.ReferenceCenter.Y, (float)-drawArgs.WorldCamera.ReferenceCenter.Z ); device.VertexFormat = CustomVertex.PositionNormalTextured.Format; if (!RenderGrayscale || (device.DeviceCaps.PixelShaderVersion.Major < 1)) { if (World.Settings.EnableSunShading) { Point3d sunPosition = SunCalculator.GetGeocentricPosition(TimeKeeper.CurrentTimeUtc); Vector3 sunVector = new Vector3( (float)sunPosition.X, (float)sunPosition.Y, (float)sunPosition.Z); device.RenderState.Lighting = true; Material material = new Material(); material.Diffuse = System.Drawing.Color.White; material.Ambient = System.Drawing.Color.White; device.Material = material; device.RenderState.AmbientColor = World.Settings.ShadingAmbientColor.ToArgb(); device.RenderState.NormalizeNormals = true; device.RenderState.AlphaBlendEnable = true; device.Lights[0].Enabled = true; device.Lights[0].Type = LightType.Directional; device.Lights[0].Diffuse = System.Drawing.Color.White; device.Lights[0].Direction = sunVector; device.TextureState[0].ColorOperation = TextureOperation.Modulate; device.TextureState[0].ColorArgument1 = TextureArgument.Diffuse; device.TextureState[0].ColorArgument2 = TextureArgument.TextureColor; } else { device.RenderState.Lighting = false; device.RenderState.Ambient = World.Settings.StandardAmbientColor; drawArgs.device.TextureState[0].ColorOperation = TextureOperation.SelectArg1; drawArgs.device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor; } device.RenderState.TextureFactor = System.Drawing.Color.FromArgb(m_opacity, 0, 0, 0).ToArgb(); device.TextureState[0].AlphaOperation = TextureOperation.BlendFactorAlpha; device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor; device.TextureState[0].AlphaArgument2 = TextureArgument.TFactor; drawArgs.device.VertexFormat = CustomVertex.PositionNormalTextured.Format; drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, vertices.Length, indices.Length / 3, indices, true, vertices); } else { if (grayscaleEffect == null) { device.DeviceReset += new EventHandler(device_DeviceReset); device_DeviceReset(device, null); } grayscaleEffect.Technique = "RenderGrayscaleBrightness"; grayscaleEffect.SetValue("WorldViewProj", Matrix.Multiply(device.Transform.World, Matrix.Multiply(device.Transform.View, device.Transform.Projection))); grayscaleEffect.SetValue("Tex0", texture); grayscaleEffect.SetValue("Brightness", GrayscaleBrightness); float opacity = (float)m_opacity / 255.0f; grayscaleEffect.SetValue("Opacity", opacity); int numPasses = grayscaleEffect.Begin(0); for (int i = 0; i < numPasses; i++) { grayscaleEffect.BeginPass(i); drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, vertices.Length, indices.Length / 3, indices, true, vertices); grayscaleEffect.EndPass(); } grayscaleEffect.End(); } drawArgs.device.Transform.World = drawArgs.WorldCamera.WorldMatrix; } finally { if (m_opacity < 255) { // Restore alpha blend state device.RenderState.SourceBlend = Blend.SourceAlpha; device.RenderState.DestinationBlend = Blend.InvSourceAlpha; } if (this._disableZbuffer) { drawArgs.device.RenderState.ZBufferEnable = true; } } }
private void RenderFlag(DrawArgs drawArgs, double offset) { if (m_effect == null) { string outerrors = ""; System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); Stream effectStream = assembly.GetManifestResourceStream("WorldWind.Shaders.flag.fx"); m_effect = Effect.FromStream( drawArgs.device, effectStream, null, null, ShaderFlags.None, null, out outerrors); if (outerrors != null && outerrors.Length > 0) { Log.Write(Log.Levels.Error, outerrors); } } if (m_vertexBuffer == null) { drawArgs.device.DeviceReset += new EventHandler(device_DeviceReset); device_DeviceReset(drawArgs.device, null); } if (m_flagPoleVertices == null) { CreateFlagPole(drawArgs.device); } Vector3 pos = MathEngine.SphericalToCartesian(m_latitude, m_longitude, World.EquatorialRadius + World.Settings.VerticalExaggeration * ScaleZ + offset); Vector3 surfacePos = MathEngine.SphericalToCartesian(m_latitude, m_longitude, World.EquatorialRadius + offset); Vector3 rc = new Vector3( (float)drawArgs.WorldCamera.ReferenceCenter.X, (float)drawArgs.WorldCamera.ReferenceCenter.Y, (float)drawArgs.WorldCamera.ReferenceCenter.Z ); drawArgs.device.Transform.World = Matrix.Scaling(World.Settings.VerticalExaggeration * ScaleX * 0.01f, World.Settings.VerticalExaggeration * ScaleY * 0.01f, -World.Settings.VerticalExaggeration * 2 * ScaleZ); drawArgs.device.Transform.World *= Matrix.RotationY((float)-MathEngine.DegreesToRadians(90)); drawArgs.device.Transform.World *= Matrix.RotationY((float)-MathEngine.DegreesToRadians(m_latitude)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(m_longitude)); drawArgs.device.Transform.World *= Matrix.Translation(surfacePos - rc); drawArgs.device.VertexFormat = CustomVertex.PositionColored.Format; drawArgs.device.TextureState[0].ColorOperation = TextureOperation.SelectArg1; drawArgs.device.TextureState[0].ColorArgument1 = TextureArgument.Diffuse; drawArgs.device.TextureState[0].AlphaArgument1 = TextureArgument.Diffuse; drawArgs.device.TextureState[0].AlphaOperation = TextureOperation.SelectArg1; drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, m_flagPoleVertices.Length, m_flagPoleIndices.Length / 3, m_flagPoleIndices, true, m_flagPoleVertices); drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.LineList, 0, m_outlineFlagPoleVertices.Length, m_outlineFlagPoleIndices.Length / 2, m_outlineFlagPoleIndices, true, m_outlineFlagPoleVertices); m_angle += .04f; if (m_angle > 360) { m_angle = 0; } drawArgs.device.VertexFormat = CustomVertex.PositionNormalTextured.Format; drawArgs.device.Transform.World = Matrix.Scaling(World.Settings.VerticalExaggeration * ScaleX, World.Settings.VerticalExaggeration * ScaleY, World.Settings.VerticalExaggeration * ScaleZ); drawArgs.device.Transform.World *= Matrix.RotationY((float)-MathEngine.DegreesToRadians(m_latitude)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(m_longitude)); drawArgs.device.Transform.World *= Matrix.Translation(pos - rc); Matrix worldViewProj = drawArgs.device.Transform.World * drawArgs.device.Transform.View * drawArgs.device.Transform.Projection; System.DateTime currentTime = TimeKeeper.CurrentTimeUtc; Point3d sunPosition = SunCalculator.GetGeocentricPosition(currentTime); Vector3 sunVector = new Vector3( (float)-sunPosition.X, (float)-sunPosition.Y, (float)-sunPosition.Z); m_effect.Technique = "VertexAndPixelShader"; m_effect.SetValue("angle", (float)m_angle); m_effect.SetValue("attentuation", Attentuation); m_effect.SetValue("World", drawArgs.device.Transform.World); m_effect.SetValue("View", drawArgs.device.Transform.View); m_effect.SetValue("Projection", drawArgs.device.Transform.Projection); m_effect.SetValue("Tex0", m_texture); m_effect.SetValue("lightDir", new Vector4(sunVector.X, sunVector.Y, sunVector.Z, 0)); drawArgs.device.Indices = m_indexBuffer; drawArgs.device.SetStreamSource(0, m_vertexBuffer, 0); int numPasses = m_effect.Begin(0); for (int i = 0; i < numPasses; i++) { m_effect.BeginPass(i); drawArgs.device.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, m_vertices.Length, 0, m_indices.Length / 3); m_effect.EndPass(); } m_effect.End(); drawArgs.device.Indices = null; drawArgs.device.Transform.World = drawArgs.WorldCamera.WorldMatrix; drawArgs.device.Transform.View = drawArgs.WorldCamera.ViewMatrix; }
void OnValidate() { latitude = Math.Min(Math.Max(latitude, -90.0f), 90.0f); longitude = Math.Min(Math.Max(longitude, -180.0f), 180.0f); year = Mathf.Clamp(year, 1, 9999); month = (month - 1) % 12 + 1; day = Mathf.Clamp(day, 1, DateTime.DaysInMonth(year, month)); dayOfTheYear = (dayOfTheYear - 1) % (DateTime.IsLeapYear(year) ? 366 : 365) + 1; if (dayOfTheYear != _DayOfTheYear) { int d = dayOfTheYear; for (int i = 1; i <= 12; i++) { int days = DateTime.DaysInMonth(year, i); if (d <= days) { day = d; month = i; break; } d -= days; } } else { dayOfTheYear = new DateTime(year, month, day).DayOfYear; } _DayOfTheYear = dayOfTheYear; timeOfTheDay = timeOfTheDay % 24f; if (timeOfTheDay != _TimeOfTheDay) { hour = Mathf.FloorToInt(timeOfTheDay); minute = Mathf.FloorToInt((timeOfTheDay % 1) * 60f); } else { timeOfTheDay = hour + minute / 60f; } _TimeOfTheDay = timeOfTheDay; if (method != _method || sunCalculator == null) { _method = method; switch (method) { case Method.Approximation: sunCalculator = new ApproximationSunCalculator(); break; case Method.Accurate: sunCalculator = new AccurateSunCalculator(); break; } } sunCalculator.GetElevationAzimuth(new DateTime(year, month, day, hour, minute, 0), latitude, longitude, timeZone, ref elevation, ref azimuth); transform.localRotation = Quaternion.Euler(elevation, -azimuth, 0); transform.localPosition = -transform.forward * domeRadius; if (showDayTrajectory && material && !Application.isPlaying) { UpdateSunForTheDay(); } }
/// <summary> /// 1.绘制太阳 /// </summary> /// <param name="drawArgs"></param> private void RenderSun(DrawArgs drawArgs) { //根据时间计算太阳的位置 Point3d sunPosition = -SunCalculator.GetGeocentricPosition(TimeKeeper.CurrentTimeUtc); Point3d sunSpherical = MathEngine.CartesianToSphericalD(sunPosition.X, sunPosition.Y, sunPosition.Z); sunPosition = MathEngine.SphericalToCartesianD( Angle.FromRadians(sunSpherical.Y), Angle.FromRadians(sunSpherical.Z), 150000000000); Vector3 sunVector = new Vector3((float)sunPosition.X, (float)sunPosition.Y, (float)sunPosition.Z); Frustum viewFrustum = new Frustum(); float aspectRatio = (float)drawArgs.WorldCamera.Viewport.Width / drawArgs.WorldCamera.Viewport.Height; Matrix projectionMatrix = Matrix.PerspectiveFovRH((float)drawArgs.WorldCamera.Fov.Radians, aspectRatio, 1.0f, 300000000000); viewFrustum.Update( Matrix.Multiply(drawArgs.WorldCamera.AbsoluteWorldMatrix, Matrix.Multiply(drawArgs.WorldCamera.AbsoluteViewMatrix, projectionMatrix))); if (!viewFrustum.ContainsPoint(sunVector)) { return; } Vector3 translationVector = new Vector3( (float)(sunPosition.X - drawArgs.WorldCamera.ReferenceCenter.X), (float)(sunPosition.Y - drawArgs.WorldCamera.ReferenceCenter.Y), (float)(sunPosition.Z - drawArgs.WorldCamera.ReferenceCenter.Z)); Vector3 projectedPoint = drawArgs.WorldCamera.Project(translationVector); if (m_sunTexture == null) { m_sunTexture = ImageHelper.LoadTexture(Path.Combine(((QRSTWorldGlobeControl)drawArgs.parentControl).DataDirectory, @"Space\sun.dds")); m_sunSurfaceDescription = m_sunTexture.GetLevelDescription(0); } if (m_sprite == null) { m_sprite = new Sprite(drawArgs.device); } m_sprite.Begin(SpriteFlags.AlphaBlend); // Render icon float xscale = (float)m_sunWidth / m_sunSurfaceDescription.Width; float yscale = (float)m_sunHeight / m_sunSurfaceDescription.Height; m_sprite.Transform = Matrix.Scaling(xscale, yscale, 0); m_sprite.Transform *= Matrix.Translation(projectedPoint.X, projectedPoint.Y, 0); m_sprite.Draw(m_sunTexture, new Vector3(m_sunSurfaceDescription.Width >> 1, m_sunSurfaceDescription.Height >> 1, 0), Vector3.Empty, System.Drawing.Color.FromArgb(253, 253, 200).ToArgb()); // Reset transform to prepare for text rendering later m_sprite.Transform = Matrix.Identity; m_sprite.End(); }
public override void Initialize(DrawArgs drawArgs) { if (_effectStored == null || _effectStored.Disposed) { string outerrors = null; string fileName = System.IO.Path.Combine(WorldManager.SystemSettings.RootPath, "Shaders\\3DTextureShader.fx"); StreamReader tr = new StreamReader(fileName); string ef = tr.ReadToEnd(); tr.Close(); try { _effectStored = Effect.FromString( drawArgs.device, ef, null, null, null, ShaderFlags.EnableBackwardsCompatibility, null, out outerrors); } catch (Exception e) { } //string fileName = System.IO.Path.Combine(WorldManager.SystemSettings.BinPath, "Shaders\\TextureBinary.fxo"); //FileStream fsr = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read); //BinaryReader br = new BinaryReader(fsr); //byte[] buff = new byte[fsr.Length]; //br.Read(buff, 0, (int)fsr.Length); //br.Close(); //fsr.Close(); //try //{ // _effectStored = Effect.FromMemory(drawArgs.device, // buff, // null, // null, // null, // ShaderFlags.EnableBackwardsCompatibility, // null, // out outerrors); //} //catch (Exception e) //{ //} if (outerrors != null && outerrors.Length > 0) { throw new Exception(outerrors); } } _effect = _effectStored; GMVector3D sunPosition = SunCalculator.GetGeocentricPosition(TimeKeeper.CurrentTimeUtc); Vector4 sunVec = new Vector4((float)sunPosition.X, (float)sunPosition.Y, (float)sunPosition.Z, 0.0f); _effect.SetValue("lightDirection", sunVec); _effect.SetValue("dimensions", new Vector4(100, 100, 20, 0)); _effect.SetTexture("TransferMap", _transferMap); }