public int Compare(FogNode x, FogNode y) { if (x.Priority < y.Priority) { return(+1); } if (x.Priority > y.Priority) { return(-1); } return(y.SortTag.CompareTo(x.SortTag)); }
// OnLoad() is called when the GameObject is added to the IGameObjectService. protected override void OnLoad() { FogNode = new FogNode(new Fog()) { IsEnabled = false, Name = "Fog", }; AddFogNodeToScene(); // Add GUI controls to the Options window. var sampleFramework = _services.GetInstance<SampleFramework>(); var optionsPanel = sampleFramework.AddOptions("Game Objects"); var panel = SampleHelper.AddGroupBox(optionsPanel, "FogObject"); SampleHelper.AddCheckBox( panel, "Enable fog", FogNode.IsEnabled, isChecked => FogNode.IsEnabled = isChecked); SampleHelper.AddCheckBox( panel, "Attach to camera", AttachToCamera, isChecked => AttachToCamera = isChecked); SampleHelper.AddSlider( panel, "Fog ramp start", "F2", 0, 1000, FogNode.Fog.Start, value => FogNode.Fog.Start = value); SampleHelper.AddSlider( panel, "Fog ramp end", "F2", 0, 5000, FogNode.Fog.End, value => FogNode.Fog.End = value); SampleHelper.AddSlider( panel, "Density", "F2", 0.0f, 2, FogNode.Fog.Density, value => FogNode.Fog.Density = value); SampleHelper.AddSlider( panel, "Height falloff", "F2", -1, 1, FogNode.Fog.HeightFalloff, value => FogNode.Fog.HeightFalloff = value); SampleHelper.AddSlider( panel, "Height Y", "F2", -100, 100, FogNode.PoseWorld.Position.Y, value => { var pose = FogNode.PoseWorld; pose.Position.Y = value; FogNode.PoseWorld = pose; }); }
/// <summary> /// Called to compute the fog intensity for <see cref="GetIntensity"/>. /// </summary> /// <param name="fogNode">The fog node. (Is never <see langword="null"/>.)</param> /// <param name="cameraNode">The camera node. (Is never <see langword="null"/>.)</param> /// <param name="targetPosition">The target position.</param> /// <returns>The fog intensity (0 = no fog; 1 = full fog, nothing else visible).</returns> /*protected virtual*/ private float OnGetIntensity(FogNode fogNode, CameraNode cameraNode, Vector3F targetPosition) { // These computations are the same as in FogRenderer and the Fog shader files. if (Numeric.IsZero(Density)) return 0; Vector3F cameraToPosition = targetPosition - cameraNode.PoseWorld.Position; float distance = cameraToPosition.Length; // The distance traveled inside the fog. Vector3F cameraToPositionDirection = cameraToPosition / distance; // Compute a value that is 0 at Start and 1 at End. float ramp = (distance - Start) / (End - Start); // Smoothstep distance fog float smoothRamp = InterpolationHelper.HermiteSmoothStep(ramp); // Exponential Fog float referenceHeight = cameraNode.PoseWorld.Position.Y - fogNode.PoseWorld.Position.Y + cameraToPositionDirection.Y * Start; float distanceInFog = distance - Start; var fogDirection = cameraToPositionDirection; if (HeightFalloff * fogDirection.Y < 0) { referenceHeight += fogDirection.Y * distanceInFog; fogDirection = -fogDirection; } float referenceDensity = Density * (float)Math.Pow(2, -HeightFalloff * referenceHeight); float opticalLength = GetOpticalLengthInHeightFog(distanceInFog, referenceDensity, fogDirection * distanceInFog, HeightFalloff); float heightFog = (1 - (float)Math.Pow(2, -opticalLength * 1)); // Get alpha from BottomColor and TopColor. // (Note: We have to avoid division by zero.) float height = targetPosition.Y - fogNode.PoseWorld.Position.Y; float p = MathHelper.Clamp((height - Height0) / Math.Max(Height1 - Height0, 0.0001f), 0, 1); float alpha = InterpolationHelper.Lerp(Color0.W, Color1.W, p); return smoothRamp * heightFog * alpha; }
/// <summary> /// Gets the fog intensity at the specified target position. /// </summary> /// <param name="fogNode">The fog node.</param> /// <param name="cameraNode">The camera node.</param> /// <param name="targetPosition">The target position.</param> /// <returns>The fog intensity (0 = no fog; 1 = full fog, nothing else visible).</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="fogNode"/> or <paramref name="cameraNode"/> is <see langword="null"/>. /// </exception> public float GetIntensity(FogNode fogNode, CameraNode cameraNode, Vector3F targetPosition) { if (fogNode == null) throw new ArgumentNullException("fogNode"); if (cameraNode == null) throw new ArgumentNullException("cameraNode"); return OnGetIntensity(fogNode, cameraNode, targetPosition); }