/// <summary> /// Clone all the time-dependent components, share the others. /// </summary> /// <returns></returns> public virtual object Clone() { AnimatedRayScene sc = new AnimatedRayScene(); ITimeDependent intersectable = Intersectable as ITimeDependent; sc.Intersectable = (intersectable == null) ? Intersectable : (IIntersectable)intersectable.Clone(); sc.BackgroundColor = (double[])BackgroundColor.Clone(); ITimeDependent camera = Camera as ITimeDependent; sc.Camera = (camera == null) ? Camera : (ICamera)camera.Clone(); ILightSource[] tmp = new ILightSource[Sources.Count]; Sources.CopyTo(tmp, 0); for (int i = 0; i < tmp.Length; i++) { ITimeDependent source = tmp[i] as ITimeDependent; if (source != null) { tmp[i] = (ILightSource)source.Clone(); } } sc.Sources = new LinkedList <ILightSource>(tmp); sc.Start = Start; sc.End = End; sc.setTime(Time); // propagates the current time to all time-dependent components.. return(sc); }
private void HandleAnxiety() { float modifier = 1.0f; Collider[] colliders = Physics.OverlapSphere(transform.position, lightCheckRange, anxietyMask); if (colliders.Length > 0) { foreach (Collider c in colliders) { ILightSource lightSource = c.GetComponent <ILightSource>(); if (lightSource != null) { if (Vector3.Distance(transform.position, c.transform.position) < lightSource.Range) { modifier -= lightSource.Power; } } IRelaxationObject relaxationObject = c.GetComponent <IRelaxationObject>(); if (relaxationObject != null) { anxiety -= relaxationObject.AnxietyDecreaseAmount * Time.deltaTime; } } } modifier *= 1 - (flashlightTransition * (1 - flashlightAnxietyModifier)); if (modifier > 0.0f) { anxiety += (darknessAnxiety * modifier + koboldSpawner.KoboldCount * anxietyPerKobold) * Time.deltaTime; } anxiety = Mathf.Clamp(anxiety, 0, maxAnxiety); }
public FadeInTransition(ILightSource previous, ILightSource lightSource, float totalMs) { this.previous = previous; this.lightSource = lightSource; this.totalMs = totalMs; TotalLength = TimeSpan.FromMilliseconds(totalMs); }
public Color GetColor(ILightSource light, Vector pointOnSurface, Vector cameraVector) { var normal = GetNormal(pointOnSurface); if (normal == Vector.Zero) { throw new Exception("Given point is not on the surface"); } // apply phong light model var effectiveColor = light.Intensity * Material.Albedo; var diffuseColor = Color.Black; var ambientColor = new Color(0.1f, 0.1f, 0.1f); var specularColor = Color.Black; var lightVector = Vector.Normalize(light.Position - Center); var lDotN = Vector.Dot(lightVector, normal); if (lDotN >= 0) { diffuseColor = effectiveColor * Material.Albedo * lDotN; var reflect = Vector.Reflect(lightVector, normal); var rDotC = Vector.Dot(reflect, cameraVector); if (rDotC >= 0) { var value = Math.Pow(rDotC, Material.Shininess); specularColor = light.Intensity * Material.Specular * value; } } return(diffuseColor + specularColor + ambientColor); }
private void Shiny(ILightSource light) { Vector2 point = light.WorldCenter; Vector3 difference = point - _center; float speed = ShinySpeed * Time.deltaTime; bool atDestination = difference.sqrMagnitude < speed * speed; _velocity = (atDestination ? difference : difference.normalized * speed); }
public LinearMovementTransition( ILightSource lightSource, float xMovement, float yMovement, TimeSpan timeSpan) { this.lightSource = lightSource; this.xMovement = xMovement; this.yMovement = yMovement; TotalLength = timeSpan; totalMs = TotalLength.TotalMilliseconds; }
public AngleFilterLightSource( ILightSource baseLightSource, double centerX, double centerY, double angleStart, double degrees) { this.baseLightSource = baseLightSource; this.centerX = centerX; this.centerY = centerY; radiansStart = angleStart * Math.PI / 180D; radiansEnd = (angleStart + degrees) * Math.PI / 180D; }
public static OpenGLLightSource ToGLLight(this ILightSource light) { return(new OpenGLLightSource() { AmbientIntensity = light.AmbientIntensity, AttenuationCoef = light.AttenuationCoef, DiffusePower = light.DiffusePower, ConeAngle = light.ConeAngle, Pos = light.LightType == LightType.Directional ? new Vector4(-light.LightDir.ToGLVector3(), 0.0f) : new Vector4(light.Pos.ToGLVector3(), 1.0f), Color = light.LightColor.ToGLVector3(), ConeDir = light.LightDir.ToGLVector3(), ShadowMapID = -1, FrameBufferID = -1 }); }
public bool IsInShadow(Point3D point, ILightSource lightSource) { var shadowVector = lightSource.Position - point; var distance = shadowVector.Magnitude; var ray = new Ray(point, shadowVector.Normalize()); var intersections = Intersect(ray); var nearestHit = intersections.GetNearestHit() .ValueOr(new Intersection(Double.PositiveInfinity, NullSurface.Instance, ray)); if (nearestHit.Distance < distance) { return(true); } return(false); }
private (ILightSource, float) nearestLight() { float minDist = float.PositiveInfinity; ILightSource minLight = null; foreach (LightSourceHolder light in _lights) { foreach (ILightSource l in light) { float d = (l.WorldCenter - _center).sqrMagnitude; if (d < minDist) { minLight = l; minDist = d; } } } return(minLight, Mathf.Sqrt(minDist)); }
private void Update() { if (_dead) { return; } _center = WorldCenter; Vector2 playerPos = PlayerCenter; (ILightSource, float)lightdist = nearestLight(); ILightSource nearest = lightdist.Item1; Vector2 mag = playerPos - _center; Vector2 scaledMag = new Vector2(mag.x * DiveScale.x, mag.y * DiveScale.y); float playerSqr = scaledMag.sqrMagnitude; float dist = lightdist.Item2; if (!_diving) { if (nearest.TurnedOn && dist < ShinyRange) { Shiny(nearest); } else if (DiveRange * DiveRange > playerSqr && _player.gameObject.activeInHierarchy) { CheckDive(); } if (!_diving) { if (AgroRange * AgroRange > playerSqr) { Agro(); } else { Patrol(); } } UpdatePosition(); } UpdateDirection(); }
public Color Illuminate(Point3D point, Vector3D eyeVector, Vector3D normalVector, ILightSource lightSource, bool isInShadow = false) { if (lightSource is null) { throw new ArgumentNullException(nameof(lightSource)); } var lightVector = GetLightVector(point, lightSource); var illuminationColor = Color * lightSource.Color; var lightDotNormal = lightVector * normalVector; var diffusion = new Color(0, 0, 0); var specularity = new Color(0, 0, 0); if (lightDotNormal > 0) // light source is not behind the surface { diffusion = illuminationColor * _diffusion * lightDotNormal; var reflectionVector = GetReflectionVector(normalVector, lightVector); var reflectionDotEye = reflectionVector * eyeVector; if (reflectionDotEye > 0) { var specularFactor = Pow(reflectionDotEye, _shininess.AsDouble()); specularity = lightSource.Color * _specularity * specularFactor; } } var ambience = illuminationColor * _ambience; if (isInShadow) { return(ambience); } return(ambience + diffusion + specularity); }
private Vector3D GetLightVector(Point3D point, ILightSource lightSource) => (lightSource.Position - point).Normalize();
public Color Illuminate(Intersection intersection, ILightSource lightSource, bool isInShadow = false) => Illuminate(intersection.Position, intersection.EyeVector, intersection.NormalVector, lightSource, isInShadow);
public Scene(IEnumerable <ISurface> surfaces, ILightSource lightSource) : this(surfaces, new[] { lightSource }) { }
public void addSource(ILightSource source, string key) { lightSources.Add(key, source); }
public void AddLight(ILightSource light) { lights.Add(light); }
public PlayerLightSource(ILightSource lightSource, double y, double radius) { lineY = y; this.radius = radius; this.lightSource = lightSource; }
public MainWindow() { InitializeComponent(); // Create scene object Scene scene = new Scene(); #region Populate scene with function surface // Number of sample points per coordinate int resolution = 20; // Boundary values double minX = -2; double maxX = 2; double minZ = -2; double maxZ = 2; // Arrays to hold point coordinate and vertex normals Point3D[][] points = new Point3D[resolution + 1][]; NormalizedVector3D[][] normals = new NormalizedVector3D[resolution + 1][]; // Compute the sample point coordinates and normals for (int x = 0; x <= resolution; x++) { points[x] = new Point3D[resolution + 1]; normals[x] = new NormalizedVector3D[resolution + 1]; // Convert sample index to coordinate double X = minX + x * (maxX - minX) / resolution; for (int z = 0; z <= resolution; z++) { // Convert sample index to coordinate double Z = minZ + z * (maxZ - minZ) / resolution; // Compute function f(x, z) = 4 * x * exp(-(x^2 + z^2)) double funcVal = 4 * X * Math.Exp(-(X * X + Z * Z)); // Scale everything by 100 to avoid underflow points[x][z] = new Point3D(X * 100, -funcVal * 100, Z * 100); // Compute the surface normal using the gradient of the function F(x, y, z) = f(x, z) + y (note that the y axis points down) double normalX = 4 * Math.Exp(-(X * X + Z * Z)) * (1 - 2 * X * X); double normalY = 1; double normalZ = -8 * X * Z * Math.Exp(-(X * X + Z * Z)); NormalizedVector3D normal = new NormalizedVector3D(normalX, normalY, normalZ); normals[x][z] = normal; } } // Create triangles using the previously computed coordinates and normals for (int x = 0; x < resolution; x++) { for (int z = 0; z < resolution; z++) { Triangle3DElement triangle1 = new Triangle3DElement(points[x][z + 1], points[x + 1][z + 1], points[x + 1][z], normals[x][z + 1], normals[x + 1][z + 1], normals[x + 1][z]); triangle1.Fill.Add(new PhongMaterial(Colours.CornflowerBlue) { SpecularReflectionCoefficient = 0 }); Triangle3DElement triangle2 = new Triangle3DElement(points[x][z + 1], points[x + 1][z], points[x][z], normals[x][z + 1], normals[x + 1][z], normals[x][z]); triangle2.Fill.Add(new PhongMaterial(Colours.CornflowerBlue) { SpecularReflectionCoefficient = 0 }); scene.AddElement(triangle1); scene.AddElement(triangle2); // Also add the reversed triangles, otherwise the "bottom" side of the surface would be culled when looking from below // Not reversing the normals, otherwise everything at the it would be dark (unless we also add another light source) Triangle3DElement triangle3 = new Triangle3DElement(points[x][z + 1], points[x + 1][z], points[x + 1][z + 1], normals[x][z + 1], normals[x + 1][z], normals[x + 1][z + 1]); triangle3.Fill.Add(new PhongMaterial(Colours.CornflowerBlue) { SpecularReflectionCoefficient = 0 }); Triangle3DElement triangle4 = new Triangle3DElement(points[x][z + 1], points[x][z], points[x + 1][z], normals[x][z + 1], normals[x][z], normals[x + 1][z]); triangle4.Fill.Add(new PhongMaterial(Colours.CornflowerBlue) { SpecularReflectionCoefficient = 0 }); scene.AddElement(triangle3); scene.AddElement(triangle4); } } #endregion // Create a camera PerspectiveCamera camera = new PerspectiveCamera(new Point3D(-300, -200, -400), new NormalizedVector3D(3, 2, 4), 100, new Size(115, 115), 1); // Generic renderer initialised as a VectorRenderer IRenderer renderer = new VectorRenderer() { DefaultOverFill = 0.05 }; // Main light source ParallelLightSource light = new ParallelLightSource(0.75, new NormalizedVector3D(0.25, 1, 0.1)); // Light source array ILightSource[] lights = new ILightSource[] { new AmbientLightSource(0.1), light }; // Render the scene Page pag = renderer.Render(scene, lights, camera); // Display the rendered scene on the window Avalonia.Controls.Canvas can = pag.PaintToCanvas(); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Children.Add(can); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Width = can.Width; this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Height = can.Height; // Function to recreate the renderer based on the options selected in the UI void updateRenderer() { // VectorRenderer if (this.FindControl <ComboBox>("RendererBox").SelectedIndex == 0) { renderer = new VectorRenderer() { DefaultOverFill = 0.05 }; this.FindControl <TextBlock>("ResolutionBlock").IsVisible = false; this.FindControl <TextBlock>("ResolutionHeightBlock").IsVisible = false; this.FindControl <TextBlock>("ResolutionWidthBlock").IsVisible = false; this.FindControl <NumericUpDown>("ResolutionWidthNUD").IsVisible = false; this.FindControl <NumericUpDown>("ResolutionHeightNUD").IsVisible = false; this.FindControl <CheckBox>("AntialiasingBox").IsVisible = false; } // RasterRenderer else if (this.FindControl <ComboBox>("RendererBox").SelectedIndex == 1) { int width = (int)Math.Round(this.FindControl <NumericUpDown>("ResolutionWidthNUD").Value); int height = (int)Math.Round(this.FindControl <NumericUpDown>("ResolutionHeightNUD").Value); renderer = new RasterRenderer(width, height); this.FindControl <TextBlock>("ResolutionBlock").IsVisible = true; this.FindControl <TextBlock>("ResolutionHeightBlock").IsVisible = true; this.FindControl <TextBlock>("ResolutionWidthBlock").IsVisible = true; this.FindControl <NumericUpDown>("ResolutionWidthNUD").IsVisible = true; this.FindControl <NumericUpDown>("ResolutionHeightNUD").IsVisible = true; this.FindControl <CheckBox>("AntialiasingBox").IsVisible = false; } // RaycastingRenderer else if (this.FindControl <ComboBox>("RendererBox").SelectedIndex == 2) { int width = (int)Math.Round(this.FindControl <NumericUpDown>("ResolutionWidthNUD").Value); int height = (int)Math.Round(this.FindControl <NumericUpDown>("ResolutionHeightNUD").Value); this.FindControl <TextBlock>("ResolutionBlock").IsVisible = true; this.FindControl <TextBlock>("ResolutionHeightBlock").IsVisible = true; this.FindControl <TextBlock>("ResolutionWidthBlock").IsVisible = true; this.FindControl <NumericUpDown>("ResolutionWidthNUD").IsVisible = true; this.FindControl <NumericUpDown>("ResolutionHeightNUD").IsVisible = true; this.FindControl <CheckBox>("AntialiasingBox").IsVisible = true; renderer = new RaycastingRenderer(width, height) { AntiAliasing = this.FindControl <CheckBox>("AntialiasingBox").IsChecked == true ? RaycastingRenderer.AntiAliasings.Bilinear4X : RaycastingRenderer.AntiAliasings.None }; } // Render the scene and display it on the window Avalonia.Controls.Canvas can = renderer.Render(scene, lights, camera).PaintToCanvas(); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Children.Clear(); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Children.Add(can); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Width = can.Width; this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Height = can.Height; } // Add events to controls changing rendering parameters this.FindControl <ComboBox>("RendererBox").SelectionChanged += (s, e) => updateRenderer(); this.FindControl <NumericUpDown>("ResolutionWidthNUD").ValueChanged += (s, e) => updateRenderer(); this.FindControl <NumericUpDown>("ResolutionHeightNUD").ValueChanged += (s, e) => updateRenderer(); this.FindControl <CheckBox>("AntialiasingBox").Click += (s, e) => updateRenderer(); // Camera reset button this.FindControl <Button>("ResetCameraButton").Click += (s, e) => { camera = new PerspectiveCamera(new Point3D(-300, -200, -400), new NormalizedVector3D(3, 2, 4), 100, new Size(115, 115), 1); Avalonia.Controls.Canvas can = renderer.Render(scene, lights, camera).PaintToCanvas(); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Children.Clear(); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Children.Add(can); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Width = can.Width; this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Height = can.Height; }; // Code to handle mouse events moving the camera on the canvas bool isPressed = false; PointerPoint pointerPress = null; double lastTheta = 0; double lastPhi = 0; double lastX = 0; double lastY = 0; // Start drag this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").PointerPressed += (s, e) => { pointerPress = e.GetCurrentPoint(this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas")); lastTheta = 0; lastPhi = 0; lastX = 0; lastY = 0; isPressed = true; }; // Drag end this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").PointerReleased += (s, e) => { isPressed = false; }; this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").PointerMoved += (s, e) => { // While dragging if (isPressed) { PointerPoint point = e.GetCurrentPoint(this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas")); // Left button: orbit if (pointerPress.Properties.PointerUpdateKind == PointerUpdateKind.LeftButtonPressed) { double dx = (point.Position.X - pointerPress.Position.X) / this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Width; double dy = (point.Position.Y - pointerPress.Position.Y) / this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Height; if (dx >= -1 && dx <= 1 && dy >= -1 && dy <= 1) { double theta = Math.Asin(dx) * 2; double phi = Math.Asin(dy) * 2; camera.Orbit(theta - lastTheta, phi - lastPhi); Avalonia.Controls.Canvas can = renderer.Render(scene, lights, camera).PaintToCanvas(); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Children.Clear(); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Children.Add(can); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Width = can.Width; this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Height = can.Height; lastTheta = theta; lastPhi = phi; } } // Right button: pan else if (pointerPress.Properties.PointerUpdateKind == PointerUpdateKind.RightButtonPressed) { double dx = point.Position.X - pointerPress.Position.X; double dy = point.Position.Y - pointerPress.Position.Y; camera.Pan(dx - lastX, dy - lastY); Avalonia.Controls.Canvas can = renderer.Render(scene, lights, camera).PaintToCanvas(); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Children.Clear(); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Children.Add(can); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Width = can.Width; this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Height = can.Height; lastX = dx; lastY = dy; } } }; // Mouse wheel: zoom this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").PointerWheelChanged += (s, e) => { camera.Zoom(e.Delta.Y * 100); Avalonia.Controls.Canvas can = renderer.Render(scene, lights, camera).PaintToCanvas(); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Children.Clear(); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Children.Add(can); this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Width = can.Width; this.FindControl <Avalonia.Controls.Canvas>("ContainerCanvas").Height = can.Height; }; }
private void ApplyLightSource(Mesh entity, BasicEffect effect, ILightSource source) { Vector3 vtfo = (entity.Position - source.Position); float dot = Vector3.Dot(vtfo, source.Direction); }
public FadeInTransition(ILightSource lightSource, double ms) : this(new SolidLightSource(RGB.Black), lightSource, (float)ms) { }
/// <summary> /// Clone all the time-dependent components, share the others. /// </summary> /// <returns></returns> public virtual object Clone() { AnimatedRayScene sc = new AnimatedRayScene(); ITimeDependent intersectable = Intersectable as ITimeDependent; sc.Intersectable = (intersectable == null) ? Intersectable : (IIntersectable)intersectable.Clone(); sc.BackgroundColor = (double[])BackgroundColor.Clone(); ITimeDependent camera = Camera as ITimeDependent; sc.Camera = (camera == null) ? Camera : (ICamera)camera.Clone(); ILightSource[] tmp = new ILightSource[ Sources.Count ]; Sources.CopyTo( tmp, 0 ); for ( int i = 0; i < tmp.Length; i++ ) { ITimeDependent source = tmp[ i ] as ITimeDependent; if ( source != null ) tmp[ i ] = (ILightSource)source.Clone(); } sc.Sources = new LinkedList<ILightSource>( tmp ); sc.Start = Start; sc.End = End; sc.setTime( Time ); // propagates the current time to all time-dependent components.. return sc; }
public Color Illuminate(Intersection intersection, ILightSource lightSource, bool isInShadow = false) => Color.Black;
public Color Illuminate(Point3D point, Vector3D eyeVector, Vector3D reflectionVector, ILightSource lightSource, bool isInShadow = false) => Color.Black;
public Scene(ISurface surface, ILightSource lightSource) : this(new[] { surface }, lightSource) { }