private string slicesPath; // path of png slices #endregion Fields #region Methods private RayMarching duplicate(RayMarching original, RayMarching copy) { BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default | BindingFlags.DeclaredOnly; PropertyInfo[] pinfos = original.GetType().GetProperties(flags); foreach (var pinfo in pinfos) { if (pinfo.CanWrite) { try { pinfo.SetValue(copy, pinfo.GetValue(original, null), null); } catch { } // In case of NotImplementedException being thrown. For some reason specifying that exception didn't seem to catch it, so I didn't catch anything specific. } } FieldInfo[] finfos = original.GetType().GetFields(flags); foreach (var finfo in finfos) { finfo.SetValue(copy, finfo.GetValue(original)); } return copy; }
// init function void Start() { this.slicesPath = PlayerPrefs.GetString("SlicesPath"); if (this.slicesPath == "") { Debug.LogError("No path for slices was found"); return; } // get the middlevr camera and assign the raymarching on it GameObject camera = GameObject.Find("Camera0"); if (camera == null) { Debug.LogError("Camera from MiddleVR was not found"); return; } this.middleVRRay = camera.AddComponent<RayMarching>(); this.duplicate(this.RayMarchingPrefab, this.middleVRRay); this.middleVRRay.ClippingPlane = this.clippingPlane; this.middleVRRay.CubeTarget = this.cubeTarget; // load the slices loadSlices(); }
/// <summary> /// release:2020-10-16 17:01:32.824: AABB.RayMarch(10000000) : 0,88M per second /// release:2020-10-21 16:03:01.240: AABB.RayMarch(10000000) : 0,82M per second /// </summary> /// <param name="n"></param> public static void RayMarchingTest(long n = TEST_COUNT) { var space = 1000; var origins = new Vector2f[n]; for (var i = 0; i < n; i++) { origins[i] = new Vector2f( Program.Rng.NextDouble() * space - space / 2, Program.Rng.NextDouble() * space - space / 2); } var directions = new Vector2f[n]; for (var i = 0; i < n; i++) { directions[i] = new Vector2f(Program.Rng.NextDouble() - 0.5, Program.Rng.NextDouble() - 0.5) .Normalize(); } var objects = new AABB[] { new AABB(new Vector2f(space / -8), new Vector2f(space / -8) + new Vector2f(space / 4)) }.Select(a => a as ISdf).ToArray(); var start = Program.Elapsed(); for (var i = 0; i < n; i++) { RayMarching.Distance(objects, origins[i], directions[i], out Vector2f normal); } Log.Add($"AABB.RayMarch({ n }): { (n / (Program.Elapsed() - start) / 1000.0):0.00}M per second"); }
// init function void Start() { this.slicesPath = PlayerPrefs.GetString("SlicesPath"); if (this.slicesPath == "") { Debug.LogError("No path for slices was found"); return; } // get the middlevr camera and assign the raymarching on it GameObject camera = GameObject.Find("Camera0"); if (camera == null) { Debug.LogError("Camera from MiddleVR was not found"); return; } this.middleVRRay = camera.AddComponent <RayMarching>(); this.duplicate(this.RayMarchingPrefab, this.middleVRRay); this.middleVRRay.ClippingPlane = this.clippingPlane; this.middleVRRay.CubeTarget = this.cubeTarget; // load the slices loadSlices(); }
// Use this for initialization void Start() { manager = GetComponent <RayMarching>(); Hashtable ht = iTween.Hash("from", 0, "to", 99.0f, "time", tweenTime, "onupdate", "TweenX", "easetype", iTween.EaseType.easeInOutSine, "looptype", iTween.LoopType.pingPong); iTween.ValueTo(gameObject, ht); }
// Use this for initialization void Start() { UnityEngine.Debug.Log("Start init RayMarching"); RayMarching ray = GameObject.Find("Camera0").GetComponent <RayMarching>(); ray.setShaders(compositeShader, renderFrontDepthShader, renderBackDepthShader, rayMarchShader); ray.setTarget(); SliceMesh s = GameObject.Find("Camera0").GetComponent <SliceMesh> (); s.setSize(mesh); UnityEngine.Debug.Log("End init RayMarching"); }
private RayMarching duplicate(RayMarching original, RayMarching copy) { BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default | BindingFlags.DeclaredOnly; PropertyInfo[] pinfos = original.GetType().GetProperties(flags); foreach (var pinfo in pinfos) { if (pinfo.CanWrite) { try { pinfo.SetValue(copy, pinfo.GetValue(original, null), null); } catch { } // In case of NotImplementedException being thrown. For some reason specifying that exception didn't seem to catch it, so I didn't catch anything specific. } } FieldInfo[] finfos = original.GetType().GetFields(flags); foreach (var finfo in finfos) { finfo.SetValue(copy, finfo.GetValue(original)); } return(copy); }
static void Main(string[] args) { RenderWindow window = new RenderWindow(new VideoMode(1000, 800), "RayMatchingTest"); World world = new World(); RayMarching rayMarching = new RayMarching(world); Texture texture = new Texture("Blank.bmp"); Font font = new Font("Font.ttf"); Text fpsLabel = new Text("", font); Circle o = new Circle { Color = Color.Magenta, Position = new Vector2f(300, 300), Radius = 10 }; Box rect = new Box(0, 0, 200, 50); Random random = new Random(); int actualTick = Environment.TickCount; int actualFps = 0, fpsCounter = 0; window.SetVisible(true); for (int i = 0; i < 10; i++) { switch (random.Next(0, 6)) { case 0: world.Objects.Add(new HorizontalLine((float)random.NextDouble() * window.Size.X, (float)random.NextDouble() * window.Size.Y, random.Next(10, 200))); break; case 1: world.Objects.Add(new VerticalLine((float)random.NextDouble() * window.Size.X, (float)random.NextDouble() * window.Size.Y, random.Next(10, 200))); break; case 2: world.Objects.Add(new Circle((float)random.NextDouble() * window.Size.X, (float)random.NextDouble() * window.Size.Y, random.Next(10, 100))); break; case 3: world.Objects.Add(new Box((float)random.NextDouble() * window.Size.X, (float)random.NextDouble() * window.Size.Y, random.Next(10, 100), random.Next(10, 100))); break; case 4: world.Objects.Add(new RotatedBox((float)random.NextDouble() * window.Size.X, (float)random.NextDouble() * window.Size.Y, random.Next(10, 100), random.Next(10, 100), random.Next(0, 360))); break; case 5: world.Objects.Add(new Line((float)random.NextDouble() * window.Size.X, (float)random.NextDouble() * window.Size.Y, (float)random.NextDouble() * window.Size.X, (float)random.NextDouble() * window.Size.Y)); break; } } rect.Color = Color.Black; fpsLabel.Position = new Vector2f(10, 10); actualTick = Environment.TickCount; while (window.IsOpen) { window.DispatchEvents(); if (Environment.TickCount - actualTick >= 1000) { actualTick = Environment.TickCount; actualFps = fpsCounter; fpsCounter = 0; } fpsCounter++; if (Keyboard.IsKeyPressed(Keyboard.Key.Escape)) { window.Close(); } Vector2i mouse = Mouse.GetPosition(window); if (Mouse.IsButtonPressed(Mouse.Button.Left)) { o.Position = new Vector2f(mouse.X, mouse.Y); window.Clear(); } world.Objects.ForEach(obj => obj.Color = Color.White); window.Clear(); foreach (Body body in world.Objects) { body.Render(window, texture); } MarchingCollision result = rayMarching.CastRay(o.Position, Atan2D(new Vector2f(mouse.X, mouse.Y), o.Position)); if (result.Object != null) { result.Object.Color = Color.Green; } o.Render(window, texture); window.Draw(new Vertex[] { new Vertex { Position = result.Position, Color = Color.Red }, new Vertex { Position = o.Position, Color = o.Color } }, 0, 2, PrimitiveType.Lines, RenderStates.Default); rect.Render(window, texture); fpsLabel.DisplayedString = $"Fps: {actualFps} - {result.TextureCoord}"; window.Draw(fpsLabel); window.Display(); Thread.Sleep(0); } }