private void CreateXRHandMesh(XRHandedness handedness, Material material)
        {
            Entity handEntity = new Entity()
                                .AddComponent(new Transform3D())
                                .AddComponent(new MaterialComponent()
            {
                Material = material
            })
                                .AddComponent(new XRDeviceMeshComponent()
            {
                XRModelName = handedness.ToString(),
            })
                                .AddComponent(new MeshRenderer())
                                .AddComponent(new TrackModelNode()
            {
                SelectionStrategy = TrackModelNode.SelectionNodeStrategy.RootNode,
            })
                                .AddComponent(new HoloHandsUpdater()
            {
                Handedness = handedness
            })
            ;

            this.Managers.EntityManager.Add(handEntity);
        }
        private Entity CreateCursor(XRHandedness handedness, Material pressedMaterial, Material releasedMaterial, Texture handRayTexture, SamplerState handRaySampler)
        {
            const float cursorPlaneSize = 0.01f;

            var mainCursor = new Entity($"{nameof(CursorTouch)}_{handedness}")
                             .AddComponent(new Transform3D())
                             .AddComponent(new CursorTouch()
            {
                PressedMaterial  = pressedMaterial,
                ReleasedMaterial = releasedMaterial,
            })
                             .AddComponent(new SphereCollider3D()
            {
                Radius = 0.005f
            })
                             .AddComponent(new StaticBody3D()
            {
                IsSensor            = true,
                CollisionCategories = this.CursorCollisionCategory,
                MaskBits            = this.CursorCollisionCategoryMask & ~this.CursorCollisionCategory,
            })
                             .AddChild(new Entity("visual")
                                       .AddComponent(new Transform3D())
                                       .AddComponent(new MaterialComponent())
                                       .AddComponent(new PlaneMesh()
            {
                TwoSides    = true,
                PlaneNormal = PlaneMesh.NormalAxis.ZNegative,
                Width       = cursorPlaneSize,
                Height      = cursorPlaneSize,
            })
                                       .AddComponent(new MeshRenderer())
                                       .AddComponent(new ProximityLight()));

            if (Tools.IsXRPlatformInputTrackingAvailable())
            {
                // HoloLens 2
                mainCursor.AddComponent(new TrackXRJoint()
                {
                    Handedness        = handedness,
                    SelectionStrategy = TrackXRDevice.SelectionDeviceStrategy.ByHandedness,
                    JointKind         = XRHandJointKind.IndexTip,
                    TrackingLostMode  = TrackXRDevice.XRTrackingLostMode.KeepLastPose,
                })
                .AddComponent(new HoloLensControlBehavior());
            }
            else
            {
                // Windows
                var key = handedness == XRHandedness.RightHand ? Keys.LeftShift : Keys.Space;
                mainCursor.AddComponent(new MouseControlBehavior()
                {
                    Key = key
                });
            }

            var ray = new Entity()
                      .AddComponent(new Transform3D())
                      .AddComponent(new LineMesh()
            {
                IsCameraAligned = true,
                LinePoints      = new List <LinePointInfo>()
                {
                    new LinePointInfo()
                    {
                        Position = Vector3.Zero, Thickness = 0.003f, Color = Color.White
                    },
                    new LinePointInfo()
                    {
                        Position = -Vector3.UnitZ, Thickness = 0.003f, Color = Color.White
                    },
                },
                DiffuseTexture = handRayTexture,
                DiffuseSampler = handRaySampler,
            })
                      .AddComponent(new LineMeshRenderer3D());

            var farCursor = new Entity($"{nameof(CursorRay)}_{handedness}")
                            .AddComponent(new Transform3D())
                            .AddComponent(new CursorRay()
            {
                PressedMaterial   = pressedMaterial,
                ReleasedMaterial  = releasedMaterial,
                TouchCursorEntity = mainCursor,
            })
                            .AddChild(ray)
                            .AddChild(new Entity("visual")
                                      .AddComponent(new Transform3D())
                                      .AddComponent(new MaterialComponent())
                                      .AddComponent(new PlaneMesh()
            {
                PlaneNormal = PlaneMesh.NormalAxis.ZNegative,
                Width       = cursorPlaneSize,
                Height      = cursorPlaneSize,
            })
                                      .AddComponent(new MeshRenderer())
                                      .AddComponent(new CameraDistanceScale()
            {
                UpdateOrder = 1
            })
                                      .AddComponent(new HoverLight()));

            var entityManager = this.Managers.EntityManager;

            entityManager.Add(mainCursor);
            entityManager.Add(farCursor);

            return(mainCursor);
        }
Esempio n. 3
0
        private static Entity CreateCursor(Scene scene, Material releasedMaterial, Material pressedMaterial, XRHandedness handedness, Texture handRayTexture, SamplerState handRaySampler)
        {
            Entity cursor = new Entity("Cursor_" + handedness)
                            .AddComponent(new Transform3D())
                            .AddComponent(new MaterialComponent())
                            .AddComponent(new PlaneMesh()
            {
                TwoSides = true, Normal = Vector3.Forward, Width = 0.01f, Height = 0.01f
            })
                            .AddComponent(new MeshRenderer())
                            .AddComponent(new SphereCollider3D())
                            .AddComponent(new StaticBody3D()
            {
                CollisionCategories = CollisionCategory3D.Cat2, IsSensor = true, MaskBits = CollisionCategory3D.Cat1
            })
                            .AddComponent(new Cursor()
            {
                PressedMaterial = pressedMaterial, ReleasedMaterial = releasedMaterial, UpdateOrder = 0.3f
            })
                            .AddComponent(new ProximityLight())
            ;

            TrackXRJoint trackXRJoint = null;
            var          xrPlatform   = Application.Current.Container.Resolve <XRPlatform>();

            if (xrPlatform != null)
            {
                trackXRJoint = new TrackXRJoint()
                {
                    Handedness        = handedness,
                    SelectionStrategy = TrackXRDevice.SelectionDeviceStrategy.ByHandedness,
                    JointKind         = XRHandJointKind.IndexTip,
                    TrackingLostMode  = TrackXRDevice.XRTrackingLostMode.KeepLastPose,
                };

                // HoloLens 2
                cursor.AddComponent(trackXRJoint)
                .AddComponent(new HoloLensControlBehavior())
                ;
            }
            else
            {
                // Windows
                cursor.AddComponent(new MouseControlBehavior()
                {
                    key = handedness == XRHandedness.RightHand ? Keys.LeftShift : Keys.Space
                });
            }

            scene.Managers.EntityManager.Add(cursor);

            var lineComp = new LineBezierMesh()
            {
                IsCameraAligned = true,
                LinePoints      = new List <BezierPointInfo>()
                {
                    new BezierPointInfo()
                    {
                        Position = Vector3.Zero, Thickness = 0.003f, Color = Color.White
                    },
                    new BezierPointInfo()
                    {
                        Position = -Vector3.UnitZ, Thickness = 0.003f, Color = Color.White
                    },
                },
                Resolution     = 10,
                DiffuseTexture = handRayTexture,
                DiffuseSampler = handRaySampler,
                TextureTiling  = new Vector2(10.0f, 1.0f),
            };

            Entity bezier = new Entity()
                            .AddComponent(new Transform3D())
                            .AddComponent(lineComp)
                            .AddComponent(new LineMeshRenderer3D())
            ;

            scene.Managers.EntityManager.Add(bezier);

            Entity cursorDist = new Entity("CursorDist_" + handedness)
                                .AddComponent(new Transform3D())
                                .AddComponent(new MaterialComponent())
                                .AddComponent(new PlaneMesh()
            {
                Normal = Vector3.Forward, Width = 0.01f, Height = 0.01f
            })
                                .AddComponent(new MeshRenderer())
                                .AddComponent(new SphereCollider3D())
                                .AddComponent(new StaticBody3D()
            {
                CollisionCategories = CollisionCategory3D.Cat2, IsSensor = true, MaskBits = CollisionCategory3D.Cat1
            })
                                .AddComponent(new Cursor()
            {
                PressedMaterial = pressedMaterial, ReleasedMaterial = releasedMaterial, UpdateOrder = 0.3f
            })
                                .AddComponent(new CursorRay()
            {
                MainCursor = cursor.FindComponent <Cursor>(), LineMesh = lineComp, Joint = trackXRJoint, collisionMask = CollisionCategory3D.Cat1
            })
                                .AddComponent(new Billboard())
                                .AddComponent(new HoverLight())
            ;

            scene.Managers.EntityManager.Add(cursorDist);

            return(cursor);
        }