コード例 #1
0
        public Vector3f CalculateSpecularReflectionDirection(RayIntersection data, Scene scene)
        {
            Vector3f normal = new Vector3f(data.Normal).Normalize();
            Vector3f eyeVec = new Vector3f(-data.Position).Normalize(); //Eye position is always (0,0,0)

            return Vector3f.ReflectVector(eyeVec, normal).Normalize();
        }
コード例 #2
0
        // returns frame at ray-intersection point, with normal pointing *outwards*
        public static Frame3f GetSphereFrame(float fHUDRadius, float fHorzAngleDeg, float fVertAngleDeg)
        {
            Ray3f r     = VRUtil.MakeRayFromSphereCenter(fHorzAngleDeg, fVertAngleDeg);
            float fRayT = 0.0f;

            RayIntersection.Sphere(r.Origin, r.Direction, Vector3f.Zero, fHUDRadius, out fRayT);
            Vector3f v = r.Origin + fRayT * r.Direction;

            return(new Frame3f(v, v.Normalized));
        }
コード例 #3
0
        internal void UpdateRaycast(Transform rayOrigin, float distance)
        {
            GameObject go;
            RaycastHit hit;

            Raycast(new Ray(rayOrigin.position, rayOrigin.forward), out hit, out go, distance);
            m_RaycastGameObjects[rayOrigin] = new RayIntersection {
                go = go, distance = hit.distance
            };
        }
コード例 #4
0
        // returns frame at ray-intersection point, with normal pointing *outwards*
        public static Frame3f GetCylinderFrameFromAngles(float fHUDRadius, float fHorzAngleDeg, float fVertAngleDeg)
        {
            Ray3f r     = VRUtil.MakeRayFromSphereCenter(fHorzAngleDeg, fVertAngleDeg);
            float fRayT = 0.0f;

            RayIntersection.InfiniteCylinder(r.Origin, r.Direction, Vector3f.Zero, Vector3f.AxisY, fHUDRadius, out fRayT);
            Vector3f v = r.Origin + fRayT * r.Direction;
            Vector3f n = new Vector3f(v[0], 0, v[2]).Normalized;

            return(new Frame3f(v, n));
        }
コード例 #5
0
        public static Frame3f GetCylinderFrameFromAngleHeight(float fHUDRadius, float fHorzAngleDeg, float fVertHeight)
        {
            Ray r = VRUtil.MakeRayFromSphereCenter(fHorzAngleDeg, 0);

            r.direction = fHUDRadius * r.direction + fVertHeight * Vector3.up;
            r.direction.Normalize();
            float fRayT = 0.0f;

            RayIntersection.InfiniteCylinder(r.origin, r.direction, Vector3f.Zero, Vector3f.AxisY, fHUDRadius, out fRayT);
            Vector3 v = r.origin + fRayT * r.direction;
            Vector3 n = new Vector3(v[0], 0, v[2]).normalized;

            return(new Frame3f(v, n));
        }
コード例 #6
0
    public virtual bool Intersect(Transform transform, Ray ray, out RayIntersectionResult result,
                                  int?wantedDepth = null, bool debug = false)
    {
        if (wantedDepth != null && wantedDepth < 0)
        {
            throw new ArgumentOutOfRangeException("wantedDepth", "Wanted depth should not be less than zero.");
        }

        var results = new RayIntersection(transform, this, ray, false, wantedDepth, null, debug).results;

        if (results.Count > 0)
        {
            result = results[0];
            return(true);
        }

        result = new RayIntersectionResult(false);
        return(false);
    }
コード例 #7
0
        private static RayIntersection IntersectSphereWithRay(RenderableEntity entity, Ray ray)
        {
            // Create a line segment between the ray origin and the center of the sphere
            var line = entity.Position - ray.Origin;

            // Use line as a hypotenuse and find the length of the adjacent side
            var adjacent = Hlsl.Dot(line, ray.Direction);

            // Find the length-squared of the opposite side
            var length2 = Hlsl.Dot(line, line) - (adjacent * adjacent);

            // Determine the radius squared
            var radius2 = entity.Radius * entity.Radius;

            // If that length-squared is greater than radius squared, the ray doesn't interact the sphere
            if (length2 > radius2)
            {
                return(new RayIntersection());
            }

            var thc = MathF.Sqrt(radius2 - length2);
            var t0  = adjacent - thc;
            var t1  = adjacent + thc;

            if (t0 < 0.0f && t1 < 0.0f)
            {
                return(new RayIntersection());
            }

            // Determine the intersect distance
            var distance = t0 < t1 ? t0 : t1;

#pragma warning disable IDE0017 // Simplify object initialization
            var rayIntersection = new RayIntersection();
            rayIntersection.Intersecting = true;
            rayIntersection.Distance     = distance;
#pragma warning restore IDE0017 // Simplify object initialization

            return(rayIntersection);
        }
コード例 #8
0
        private static RayIntersection IntersectPlaneWithRay(RenderableEntity entity, Ray ray)
        {
            var denom = Hlsl.Dot(entity.Normal, ray.Direction);

            if (denom > 0.000001f)
            {
                var v        = entity.Position - ray.Origin;
                var distance = Hlsl.Dot(v, entity.Normal) / denom;
                if (distance >= 0.0)
                {
#pragma warning disable IDE0017 // Simplify object initialization
                    var rayIntersection = new RayIntersection();
                    rayIntersection.Intersecting = true;
                    rayIntersection.Distance     = distance;
#pragma warning restore IDE0017 // Simplify object initialization

                    return(rayIntersection);
                }
            }

            return(new RayIntersection());
        }
コード例 #9
0
        internal void UpdateRaycast(Transform rayOrigin, float distance)
        {
            if (!m_RayoriginEnabled.ContainsKey(rayOrigin))
            {
                m_RayoriginEnabled[rayOrigin] = true;
            }

            GameObject go;
            RaycastHit hit;

            Raycast(new Ray(rayOrigin.position, rayOrigin.forward), out hit, out go, distance);

            if (!m_RayoriginEnabled[rayOrigin])
            {
                go           = null;
                hit.distance = 0;
            }

            m_RaycastGameObjects[rayOrigin] = new RayIntersection {
                go = go, distance = hit.distance
            };
        }
コード例 #10
0
        override public bool UpdateCapture(InputEvent e)
        {
            if (in_drag)
            {
                float fRayT = 0.0f;
                if (RayIntersection.Sphere(e.ray.Origin, e.ray.Direction, hitFrame.Origin, IndicatorDistance, out fRayT) == false)
                {
                    return(true);        // should not happen, but I guess it could if user
                }
                // had arms out in some crazy position...
                Vector3f v        = e.ray.Origin + fRayT * e.ray.Direction;
                Vector3f vToHit   = (v - hitFrame.Origin).Normalized;
                Vector3f vToStart = (startHitPos - hitFrame.Origin).Normalized;
                float    deltaUp  = -VRUtil.PlaneAngleSigned(vToHit, vToStart, hitFrame.X);
                cockpit.TiltAngle = MathUtil.Clamp(start_tilt + deltaUp, -30.0f, 20.0f);
                float deltaLR = -VRUtil.PlaneAngleSigned(vToHit, vToStart, hitFrame.Y);
                cockpit.ShiftAngle = start_shift + deltaLR;
                //if ( tracker.IsLocked == false )
                cockpit.ShiftAngle = MathUtil.Clamp(cockpit.ShiftAngle, -10.0f, 10.0f);
            }

            return(true);
        }
コード例 #11
0
        public static bool FindClosestRayIntersection(ISampledCurve3d c, double segRadius, Ray3d ray, out double rayT)
        {
            rayT = double.MaxValue;
            int nNearSegment = -1;
            //double fNearSegT = 0.0;

            int N     = c.VertexCount;
            int iStop = (c.Closed) ? N : N - 1;

            for (int i = 0; i < iStop; ++i)
            {
                DistRay3Segment3 dist = new DistRay3Segment3(ray,
                                                             new Segment3d(c.GetVertex(i), c.GetVertex((i + 1) % N)));

                // raycast to line bounding-sphere first (is this going ot be faster??)
                double fSphereHitT;
                bool   bHitBoundSphere = RayIntersection.SphereSigned(ray.Origin, ray.Direction,
                                                                      dist.Segment.Center, dist.Segment.Extent + segRadius, out fSphereHitT);
                if (bHitBoundSphere == false)
                {
                    continue;
                }

                // find ray/seg min-distance and use ray T
                double dSqr = dist.GetSquared();
                if (dSqr < segRadius * segRadius)
                {
                    if (dist.RayParameter < rayT)
                    {
                        rayT = dist.RayParameter;
                        //fNearSegT = dist.SegmentParameter;
                        nNearSegment = i;
                    }
                }
            }
            return(nNearSegment >= 0);
        }
コード例 #12
0
    public override bool Intersect(Transform transform, Ray ray, out RayIntersectionResult result, int?wantedDepth = null,
                                   bool debugRaycasts = false)
    {
        var subIntersectionResult = new RayIntersectionResult(false);

        var intersection = new RayIntersection(transform, GetOwnerNode().GetTree(), ray, false, null, intersectionResult => {
            var intersectionSuperNode = intersectionResult.node as SuperVoxelTree.Node;
            if (intersectionSuperNode == null)
            {
                return(false);
            }

            var intersectedVoxel = intersectionSuperNode.GetItem();

            if (intersectedVoxel == null)
            {
                return(false);
            }

            if (intersectedVoxel.IntersectInternal(transform, ray, out subIntersectionResult, wantedDepth, debugRaycasts))
            {
                return(true);
            }

            return(false);
        }, debugRaycasts);

        result = subIntersectionResult;

        if (intersection.results.Count > 0)
        {
            return(true);
        }

        return(false);
    }
コード例 #13
0
        public Vector3f CalculateDirectShading(RayIntersection data, Light l)
        {
            Vector3f eyeVec = new Vector3f(-data.Position).Normalize(); //Eye position must be at (0,0,0)

            MaterialData md = data.MaterialData;

            //Vector3f outputColor = new Vector3f();

            //foreach (Light l in scene.Lights) //Foreach light, compute direct diffuse reflection and direct specular highlight
            //{
            Vector3f norm = data.Normal;

            Vector3f lVec = (data.Position - l.Position).Normalize();

            float lambert = (lVec * norm) * 0.3f * md.Diffuse;

            //Specular
            Vector3f reflect = -Vector3f.ReflectVector(lVec, norm);
            float spec = md.Specular * MathUtil.Pow(MathUtil.Max(reflect * eyeVec, 0.0f), 1);

            //outputColor = (spec * md.SpecularColor.AsVector3()) + (lambert * md.DiffuseColor.AsVector3());
            //}
            return new Vector3f((spec * md.SpecularColor.AsVector3()) + (lambert * md.DiffuseColor.AsVector3()));
        }
コード例 #14
0
    // Use this for initialization
    public override void Awake()
    {
        // if we need to auto-configure Rift vs Vive vs (?) VR, we need
        // to do this before any other F3 setup, because MainCamera will change
        // and we are caching that in a lot of places...
        if (AutoConfigVR)
        {
            VRCameraRig = gs.VRPlatform.AutoConfigureVR();
        }

        // restore any settings
        SceneGraphConfig.RestorePreferences();

        // set up some defaults
        // this will move the ground plane down, but the bunnies will be floating...
        //SceneGraphConfig.InitialSceneTranslate = -4.0f * Vector3f.AxisY;
        SceneGraphConfig.DefaultSceneCurveVisualDegrees = 0.5f;
        SceneGraphConfig.DefaultPivotVisualDegrees      = 1.5f;
        SceneGraphConfig.DefaultAxisGizmoVisualDegrees  = 10.0f;
        SceneGraphConfig.InitialSceneTranslate          = -4 * Vector3f.AxisY;


        SceneOptions options = new SceneOptions();

        options.UseSystemMouseCursor = false;
        options.Use2DCockpit         = false;
        options.EnableTransforms     = true;
        options.EnableCockpit        = true;
        options.CockpitInitializer   = new PhotoToolCockpit();

        options.MouseCameraControls = new MayaCameraHotkeys();
        options.SpatialCameraRig    = VRCameraRig;

        // very verbose
        options.LogLevel = 2;

        context = new FContext();
        context.Start(options);

        // if you had other gizmos, you would register them here
        //context.TransformManager.RegisterGizmoType("snap_drag", new SnapDragGizmoBuilder());
        //controller.TransformManager.SetActiveGizmoType("snap_drag");

        // if you had other tools, you would register them here.
        context.ToolManager.RegisterToolType(DrawPrimitivesTool.Identifier, new DrawPrimitivesToolBuilder());
        context.ToolManager.RegisterToolType(DrawSurfaceCurveTool.Identifier, new DrawSurfaceCurveToolBuilder()
        {
            AttachCurveToSurface = true,
            DefaultSamplingRateS = 0.0025f, DefaultSurfaceOffsetS = 0.0025f,
            CurveMaterialF       = () => { var mat = context.Scene.DefaultCurveSOMaterial.Clone(); mat.RGBColor = Colorf.VideoRed; return(mat); }
        });
        context.ToolManager.SetActiveToolType(DrawSurfaceCurveTool.Identifier, ToolSide.Right);

        // Set up standard scene lighting if requested
        if (options.EnableDefaultLighting)
        {
            GameObject lighting = GameObject.Find("SceneLighting");
            if (lighting == null)
            {
                lighting = new GameObject("SceneLighting");
            }
            SceneLightingSetup setup = lighting.AddComponent <SceneLightingSetup>();
            setup.Context       = context;
            setup.LightDistance = 30.0f; // related to total scene scale...
        }


        Context.Scene.DisableSelectionMaterial = true;


        /*
         * Import elements of Unity scene that already exist into the FScene
         */

        // set up ground plane geometry (optional)
        GameObject groundPlane = GameObject.Find("GroundPlane");

        if (groundPlane != null && groundPlane.IsVisible())
        {
            context.Scene.AddWorldBoundsObject(groundPlane);
        }


        float    fSquareSize = 1.0f;
        Vector3f eyePos      = context.ActiveCamera.GetPosition();

        System.Random rand = new System.Random(31337);

        // [RMS] this path only works in Editor, is relative to top-level project directory
        string sPhotoFolder = "Data\\PhotoSets\\kitchen";

        string[] photos = Directory.GetFiles(sPhotoFolder);
        foreach (string filename in photos)
        {
            Texture2D tex = load_texture(filename);
            if (tex == null)
            {
                continue;
            }

            float fScale = fSquareSize / (float)tex.width;
            if (tex.height > tex.width)
            {
                fScale = fSquareSize / (float)tex.height;
            }

            float w = fScale * (float)tex.width;
            float h = fScale * (float)tex.height;

            TrivialRectGenerator rectgen = new TrivialRectGenerator()
            {
                Width = w, Height = h
            };
            rectgen.Generate();
            DMesh3 mesh = new DMesh3(MeshComponents.VertexUVs);
            rectgen.MakeMesh(mesh);

            SOMaterial material = new SOMaterial()
            {
                Name     = "photomaterial",
                Type     = SOMaterial.MaterialType.TextureMap,
                RGBColor = Colorf.White
            };
            material.MainTexture = tex;

            DMeshSO so = new DMeshSO();
            so.Create(mesh, material);
            context.Scene.AddSceneObject(so);

            float horz = rand.Next(-50, 50);
            float vert = rand.Next(-20, 25);
            int   mult = 1000;
            float dist = (float)(rand.Next(2 * mult, 3 * mult)) / (float)mult;

            Ray3f r = VRUtil.MakeRayFromSphereCenter(horz, vert);
            r.Origin += eyePos;
            float fRayT = 0.0f;
            RayIntersection.Sphere(r.Origin, r.Direction, eyePos, dist, out fRayT);
            Vector3f v = r.Origin + fRayT * r.Direction;
            Frame3f  f = new Frame3f(v, v.Normalized);

            Vector3f toEye = context.ActiveCamera.GetPosition() - f.Origin;
            toEye.Normalize();
            f.AlignAxis(1, toEye);
            f.ConstrainedAlignAxis(2, Vector3f.AxisY, f.Y);

            so.SetLocalFrame(f, CoordSpace.WorldCoords);
        }
    }
コード例 #15
0
        public override Capture BeginCapture(InputState input, CaptureSide eSide)
        {
            if (input.LeftCaptureActive || input.RightCaptureActive || popup != null)
            {
                DebugUtil.Warning("SceneRightClickBehavior.BeginCapture - we should not be here...");
                return(Capture.Ignore);
            }

            Ray3f useRay = new Ray3f(Vector3f.Zero, Vector3f.AxisY);

            if (input.IsForDevice(InputDevice.Mouse) || input.IsForDevice(InputDevice.Gamepad))
            {
                useRay = (input.bRightMousePressed) ? input.vMouseWorldRay : input.vGamepadWorldRay;
            }
            else if (input.IsForDevice(InputDevice.AnySpatialDevice))
            {
                useRay = (eSide == CaptureSide.Left) ? input.vLeftSpatialWorldRay : input.vRightSpatialWorldRay;
            }

            // raycast into scene to find hit object/position for menu. We try Pivots first,
            // because they are special and have priority (?? always ??). Then we cast into general scene.
            SORayHit  pivotHit;
            bool      bHitPivot = cockpit.Scene.FindSORayIntersection(useRay, out pivotHit, (x) => { return(x is PivotSO); });
            AnyRayHit rayHit;

            if (bHitPivot)
            {
                rayHit = new AnyRayHit(pivotHit);
            }
            else
            {
                if (cockpit.Scene.FindSceneRayIntersection(useRay, out rayHit) == false)
                {
                    return(Capture.Ignore);
                }
            }

            // find center of menu in space
            Vector3f vHUDCenter   = cockpit.RootGameObject.GetPosition();
            Vector3f menuLocation = rayHit.hitPos;

            if (Placement == PlacementMode.OnHUDSphere)
            {
                float fRayT;
                bool  bHit = RayIntersection.Sphere(useRay.Origin, useRay.Direction,
                                                    vHUDCenter, HUDRadius, out fRayT);
                Debug.Assert(bHit);
                menuLocation = useRay.Origin + fRayT * useRay.Direction;
            }

            // compute extents
            float fDiameter = VRUtil.GetVRRadiusForVisualAngle(
                menuLocation, cockpit.ActiveCamera.GetPosition(), VisualDiameter);

            if (rayHit.eType == HitType.SceneObjectHit && rayHit.hitSO is PivotSO)
            {
                popup = GeneratePivotRadialMenu(fDiameter, rayHit.hitSO as PivotSO);
            }
            else if (rayHit.eType == HitType.SceneObjectHit)
            {
                popup = GenerateSceneObjectRadialMenu(fDiameter, rayHit);
            }
            else
            {
                popup = GenerateDefaultRadialMenu(fDiameter, rayHit);
            }
            popup.Create();
            popup.Name = "popup_menu";

            if (Placement == PlacementMode.InScene)
            {
                HUDUtil.PlaceInScene(popup, vHUDCenter, menuLocation);
            }
            else
            {
                HUDUtil.PlaceInSphere(popup, HUDRadius, vHUDCenter, menuLocation);
            }

            // this is a bit of a hack...radial menu lives in-scene, if we attach it to
            //   cockpit then it will move with cockpit, which is wrong. So we want to
            //   stick it in the scene. But, at least for now, we still want it to be
            //   drawn in the cockpit layer, so we add to cockpit first, them remove and
            //   re-add to the scene
            cockpit.AddUIElement(popup, false);
            cockpit.RemoveUIElement(popup, false);
            cockpit.Scene.AddUIElement(popup, false);

            HUDUtil.AnimatedShow(popup, 0.2f);

            return(Capture.Begin(this, eSide));
        }
コード例 #16
0
 public float CalculateIndirectSpecularContribution(RayIntersection data, Scene scene)
 {
     return data.MaterialData.Specular;
 }
コード例 #17
0
 public float CalculateIndirectDiffuseContribution(RayIntersection data, Scene scene)
 {
     return data.MaterialData.Diffuse;
 }