コード例 #1
0
        void Update()
        {
            if (VRInput.TryGetDevices())
            {
                // Toggle selection
                if (enableToggleTool)
                {
                    VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.secondaryButton, () =>
                    {
                        ToolsManager.ToggleTool();
                    });
                }

                // if tool has switch, THIS is now disabled, we dont want updates.
                if (!gameObject.activeSelf)
                {
                    return;
                }

                // Custom tool update
                if (IsInGui)
                {
                    DoUpdateGui();
                }
                else // TODO: voir si il faut pas quand meme faire DoUpdate dans tous les cas.
                // le probleme de faire les deux vient quand ils reagissent au meme input (ex: Grip dans UI)
                {
                    DoUpdate(); // call children DoUpdate
                }
            }
        }
コード例 #2
0
        protected override void DoUpdate()
        {
            VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.triggerButton,
                                () =>
            {
                group = new CommandGroup("Gun");
            },
                                () =>
            {
                group.Submit();
                group = null;
            }
                                );

            bool triggered = VRInput.GetValue(VRInput.primaryController, CommonUsages.triggerButton);

            if (triggered && prefabs.Count > 0)
            {
                if (Time.time - prevTime > 1f / fireRate)
                {
                    int           prefabIndex = UnityEngine.Random.Range(0, prefabs.Count);
                    GameObject    spawned     = Instantiate(prefabs[prefabIndex]);
                    ThrowedObject throwed     = spawned.AddComponent <ThrowedObject>();
                    throwed.AddForce(transform.forward * power);
                    throwed.SetScale(objectScale);
                    new CommandAddGameObject(spawned).Submit();
                    Matrix4x4 matrix = SceneManager.RightHanded.worldToLocalMatrix * mouthpiece.localToWorldMatrix;
                    Maths.DecomposeMatrix(matrix, out Vector3 t, out _, out _);
                    Vector3 scale = Vector3.one;
                    SceneManager.SetObjectMatrix(spawned, Matrix4x4.TRS(t, Quaternion.identity, scale));
                    prevTime = Time.time;
                }
            }
        }
コード例 #3
0
        private void UpdateSelection()
        {
            // Get right controller buttons states
            bool primaryButtonState = VRInput.GetValue(VRInput.primaryController, CommonUsages.primaryButton);
            bool triggerState       = VRInput.GetValue(VRInput.primaryController, CommonUsages.triggerButton);

            GameObject hoveredObject = Selection.HoveredObject;

            VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.grip,
                                () =>
            {
                Selection.HoveredObject = hoveredObject;
            },
                                () => { Selection.AuxiliarySelection = null; });

            // Multi-selection using the trigger button
            if (triggerState && !GlobalState.Instance.selectionGripped && null != hoveredObject && !selector.Deforming)
            {
                if (!primaryButtonState)
                {
                    foreach (GameObject obj in collidedObjects)
                    {
                        selector.AddSiblingsToSelection(obj);
                    }
                }
            }
        }
コード例 #4
0
 protected override void DoUpdateGui()
 {
     VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.gripButton, () =>
     {
         if (UIObject)
         {
             CreateLight(UIObject.name);
             UIObject = null;
         }
         OnStartGrip();
     }, OnEndGrip);
 }
コード例 #5
0
ファイル: PlayerController.cs プロジェクト: ubisoft/vrtist
 private void HandleUndoRedo()
 {
     VRInput.ButtonEvent(VRInput.secondaryController, CommonUsages.primaryButton, () => { },
                         () =>
     {
         CommandManager.Undo();
     });
     VRInput.ButtonEvent(VRInput.secondaryController, CommonUsages.secondaryButton, () => { },
                         () =>
     {
         CommandManager.Redo();
     });
 }
コード例 #6
0
ファイル: PlayerController.cs プロジェクト: ubisoft/vrtist
        private void HandleResetScale()
        {
            VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.primary2DAxisClick,
                                () =>
            {
                ResetCameraClipPlanes();

                Transform cam = vrCamera.transform;
                Vector3 initCameraInWorldPosition = cam.position;
                transform.localScale = Vector3.one;

                transform.position += initCameraInWorldPosition - cam.position;
            });
        }
コード例 #7
0
 protected override void DoUpdateGui()
 {
     VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.triggerButton, () =>
     {
     }, () =>
     {
         // Manage click on an asset bank item
         if (selectedItem != -1)
         {
             if (AssetBankUtils.TryGetItem(selectedItem, out AssetBankItemData data))
             {
                 AddPrefab(data);
             }
         }
     });
 }
コード例 #8
0
ファイル: Colorize.cs プロジェクト: ubisoft/vrtist
        protected override void DoUpdate()
        {
            // Alt button
            VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.primaryButton, () =>
            {
                previousColorOp = colorOp;
                colorOp         = ColorOp.Pick;
                uiInitialized   = false;
            }, () =>
            {
                colorOp       = previousColorOp;
                uiInitialized = false;
            });

            // Update UI
            if (!uiInitialized)
            {
                uiInitialized = true;
                switch (colorOp)
                {
                case ColorOp.Colorize: OnSetColorize(); break;

                case ColorOp.UpdateSelection: OnSetUpdateSelection(); break;

                case ColorOp.Pick: OnSetPick(); break;
                }
                UpdatePreview();
            }

            // Clear selection: only when triggering on nothing with the ColorOp.UpdateSelection
            if (ColorOp.UpdateSelection == colorOp)
            {
                VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.triggerButton, () =>
                {
                    selectionHasChanged = false;
                }, () =>
                {
                    if (!selectionHasChanged && ColorOp.UpdateSelection == colorOp)
                    {
                        CommandRemoveFromSelection command = new CommandRemoveFromSelection(Selection.SelectedObjects.ToList());
                        command.Redo();
                        command.Submit();
                    }
                });
            }
        }
コード例 #9
0
ファイル: PlayerController.cs プロジェクト: ubisoft/vrtist
 private void HandleReset()
 {
     VRInput.ButtonEvent(VRInput.secondaryController, CommonUsages.primary2DAxisClick,
                         () =>
     {
         if (Selection.SelectedObjects.Count == 0)
         {
             ResetCameraClipPlanes();
             transform.localPosition = Vector3.zero;
             transform.localRotation = Quaternion.identity;
             transform.localScale    = Vector3.one;
         }
         else
         {
             FitToSelection();
         }
     });
 }
コード例 #10
0
ファイル: CameraTool.cs プロジェクト: ubisoft/vrtist
        protected override void DoUpdateGui()
        {
            VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.gripButton, () =>
            {
                if (UIObject)
                {
                    Matrix4x4 matrix        = cameraContainer.worldToLocalMatrix * mouthpiece.localToWorldMatrix * Matrix4x4.Scale(new Vector3(5f, 5f, 5f));
                    GameObject cameraPrefab = ResourceManager.GetPrefab(PrefabID.Camera);

                    GameObject instance = SceneManager.InstantiateUnityPrefab(cameraPrefab);
                    Vector3 position    = matrix.GetColumn(3);
                    Quaternion rotation = Quaternion.AngleAxis(180, Vector3.forward) * Quaternion.LookRotation(matrix.GetColumn(2), matrix.GetColumn(1));
                    Vector3 scale       = new Vector3(matrix.GetColumn(0).magnitude, matrix.GetColumn(1).magnitude, matrix.GetColumn(2).magnitude);

                    CommandGroup undoGroup = new CommandGroup("Instantiate Camera");
                    try
                    {
                        ClearSelection();
                        CommandAddGameObject command = new CommandAddGameObject(instance);
                        command.Submit();
                        GameObject newCamera = command.newObject;
                        AddToSelection(newCamera);
                        SceneManager.SetObjectTransform(instance, position, rotation, scale);
                        Selection.HoveredObject = newCamera;
                    }
                    finally
                    {
                        undoGroup.Submit();
                        UIObject = null;
                    }
                }
                OnStartGrip();
            },
                                () =>
            {
                OnEndGrip();
            });

            // called to update focal slider value
            UpdateUI();
        }
コード例 #11
0
ファイル: AssetBank.cs プロジェクト: AnthonyMir/vrtist
        protected override void DoUpdateGui()
        {
            bool gripped = false;

            VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.gripButton, async() =>
            {
                await OnGrabUIObject();

                // Since OnGrabUIObject may take some time, check we are still gripped
                gripped = VRInput.GetValue(VRInput.primaryController, CommonUsages.gripButton);
                if (gripped)
                {
                    OnStartGrip();
                }
            }, () =>
            {
                // Only end grip if we were effectively gripped
                if (gripped)
                {
                    OnEndGrip();
                }
            });
        }
コード例 #12
0
        protected override void DoUpdate()
        {
            // Base selection update
            base.DoUpdate();

            // Deform
            if (deformEnabled && activePlane != null)
            {
                VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.trigger, () =>
                {
                    InitDeformerMatrix();
                    InitTransforms();

                    planeControllerDelta = FilterControllerDirection() - activePlane.transform.position; // in absolute coordinates

                    Vector3 initDelta = activePlane.transform.position - activePlane.opposite.position;
                    initMagnitude     = initDelta.magnitude; // initial scale value

                    OnStartDeform();
                }, () =>
                {
                    OnEndDeform();
                });
            }

            if (deformEnabled && deforming)
            {
                Vector3 controllerPosition = FilterControllerDirection();
                controllerPosition -= planeControllerDelta;

                Vector3 delta     = controllerPosition - activePlane.opposite.position;
                float   magnitude = delta.magnitude;

                float scaleFactor = magnitude / initMagnitude;

                Vector3 scale = new Vector3(scaleFactor, scaleFactor, scaleFactor);

                int  selectionCount     = Selection.ActiveObjects.Count;
                bool foundLightOrCamera = false;
                if (selectionCount == 1)
                {
                    foundLightOrCamera = IsHierarchical(Selection.ActiveObjects);
                }

                bool scaleAll = selectionCount != 1 || foundLightOrCamera || uniformScale;
                if (!scaleAll)
                {
                    scale = new Vector3(
                        activePlane.direction.x == 0f ? 1f : scale.x,
                        activePlane.direction.y == 0f ? 1f : scale.y,
                        activePlane.direction.z == 0f ? 1f : scale.z
                        );
                }

                Matrix4x4 scaleMatrix          = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
                Matrix4x4 transformationMatrix = initOppositeMatrix * scaleMatrix;

                TransformSelection(transformationMatrix * initMouthPieceWorldToLocal);
            }

            // Bounds
            if (deformEnabled)
            {
                ComputeSelectionBounds();
                bool enable = Selection.ActiveObjects.Count != 0;
                if (Selection.ActiveObjects.Count == 1)
                {
                    foreach (GameObject gobject in Selection.ActiveObjects)
                    {
                        ParametersController controller = gobject.GetComponent <ParametersController>();
                        if (null != controller && !controller.IsDeformable())
                        {
                            enable = false;
                        }
                    }
                }
                boundingBox.SetActive(enable);
            }

            // Move grid with object(s), enable/disable it.
            UpdateGrid();
        }
コード例 #13
0
        private void UpdateToolPaint(Vector3 position, Quaternion _)
        {
            // ON TRIGGER
            VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.trigger, () =>
            {
                BeginPaint();

                paintPrevPosition = Vector3.zero;
                undoGroup         = new CommandGroup("Paint");
            },
                                () =>
            {
                try
                {
                    EndCurrentPaint();
                }
                finally
                {
                    if (null != undoGroup)
                    {
                        undoGroup.Submit();
                        undoGroup = null;
                    }
                }
            });

            float triggerValue = VRInput.GetValue(VRInput.primaryController, CommonUsages.trigger);

            // Change brush size
            if (navigation.CanUseControls(NavigationMode.UsedControls.RIGHT_JOYSTICK))
            {
                Vector2 val = VRInput.GetValue(VRInput.primaryController, CommonUsages.primary2DAxis);
                if (val != Vector2.zero)
                {
                    if (val.y > 0.3f)
                    {
                        brushSize += 0.001f;
                    }
                    if (val.y < -0.3f)
                    {
                        brushSize -= 0.001f;
                    }
                    brushSize             = Mathf.Clamp(brushSize, 0.001f, 0.5f);
                    mouthpiece.localScale = new Vector3(brushSize, brushSize, brushSize);
                }
            }

            paintLineRenderer.enabled = false;
            Vector3 penPosition = mouthpiece.position;

            if (paintOnSurface)
            {
                Vector3 direction = transform.forward; // (paintItem.position - centerEye.position).normalized;
                Vector3 startRay  = penPosition + mouthpiece.lossyScale.x * direction;
                Vector3 endRay    = startRay + 1000f * direction;
                paintLineRenderer.enabled       = true;
                paintLineRenderer.positionCount = 2;
                paintLineRenderer.SetPosition(0, startRay);
                paintLineRenderer.SetPosition(1, endRay);
                paintLineRenderer.startWidth = 0.005f / GlobalState.WorldScale;
                paintLineRenderer.endWidth   = paintLineRenderer.startWidth;
                bool hit = Physics.Raycast(startRay, direction, out RaycastHit hitInfo, Mathf.Infinity);
                if (!hit)
                {
                    return;
                }
                penPosition = hitInfo.point - 0.001f * direction;
                paintLineRenderer.SetPosition(1, penPosition);
            }
            else if (paintTool == PaintTools.Volume)
            {
                if (currentVolume)
                {
                    VolumeController controller = currentVolume.GetComponent <VolumeController>();
                    if (null != controller)
                    {
                        paintLineRenderer.enabled = true;

                        Vector3 C = controller.bounds.center;
                        Vector3 E = controller.bounds.extents;

                        Vector3 tlf = controller.transform.TransformPoint(C + new Vector3(-E.x, E.y, -E.z));
                        Vector3 trf = controller.transform.TransformPoint(C + new Vector3(E.x, E.y, -E.z));
                        Vector3 blf = controller.transform.TransformPoint(C + new Vector3(-E.x, -E.y, -E.z));
                        Vector3 brf = controller.transform.TransformPoint(C + new Vector3(E.x, -E.y, -E.z));
                        Vector3 tlb = controller.transform.TransformPoint(C + new Vector3(-E.x, E.y, E.z));
                        Vector3 trb = controller.transform.TransformPoint(C + new Vector3(E.x, E.y, E.z));
                        Vector3 blb = controller.transform.TransformPoint(C + new Vector3(-E.x, -E.y, E.z));
                        Vector3 brb = controller.transform.TransformPoint(C + new Vector3(E.x, -E.y, E.z));

                        paintLineRenderer.positionCount = 16;
                        paintLineRenderer.SetPositions(new Vector3[] {
                            blf, tlf, brf, trf, brb, trb, blb,
                            blf, brf, brb, blb,
                            tlb, tlf, trf, trb, tlb
                        });
                        paintLineRenderer.startWidth = 0.001f / GlobalState.WorldScale;
                        paintLineRenderer.endWidth   = 0.001f / GlobalState.WorldScale;
                    }
                }
            }

            // Draw
            float deadZone = VRInput.deadZoneIn;

            if (triggerValue >= deadZone &&
                (
                    (position != paintPrevPosition && currentPaint != null) || currentVolume != null)
                )
            {
                // Add a point (the current world position) to the line renderer

                float pressure = (triggerValue - deadZone) / (1f - deadZone);
                float value    = brushSize / GlobalState.WorldScale * pressure;

                switch (paintTool)
                {
                case PaintTools.Pencil: freeDraw.AddControlPoint(penPosition, 0.5f * value); break;

                case PaintTools.FlatPencil: freeDraw.AddFlatLineControlPoint(penPosition, -transform.forward, 0.5f * value); break;

                case PaintTools.ConvexHull: freeDraw.AddConvexHullPoint(penPosition); break;

                case PaintTools.Volume: volumeGenerator.AddPoint(penPosition, 2.0f * value * strength); break;
                }

                switch (paintTool)
                {
                case PaintTools.Pencil:
                case PaintTools.FlatPencil:
                case PaintTools.ConvexHull:
                {
                    // set mesh components
                    MeshFilter meshFilter = currentPaint.GetComponent <MeshFilter>();
                    Mesh       mesh       = meshFilter.mesh;
                    mesh.Clear();
                    mesh.vertices  = freeDraw.vertices;
                    mesh.normals   = freeDraw.normals;
                    mesh.triangles = freeDraw.triangles;
                    break;
                }

                case PaintTools.Volume:
                    if (null != currentVolume)
                    {
                        MeshFilter meshFilter = currentVolume.GetComponent <MeshFilter>();
                        Mesh       mesh       = meshFilter.mesh;
                        mesh.Clear();
                        mesh.vertices  = volumeGenerator.vertices;
                        mesh.triangles = volumeGenerator.triangles;
                        mesh.RecalculateNormals();

                        // Recompute collider
                        MeshCollider meshCollider = currentVolume.GetComponent <MeshCollider>();
                        meshCollider.sharedMesh = mesh;
                        // force update
                        meshCollider.enabled = false;
                        meshCollider.enabled = true;

                        VolumeController controller = currentVolume.GetComponent <VolumeController>();
                        controller.origin     = volumeGenerator.origin;
                        controller.bounds     = volumeGenerator.bounds; // TODO: dont duplicate data? use volumeparameters in volumegenerator?
                        controller.field      = volumeGenerator.field;
                        controller.resolution = volumeGenerator.resolution;
                        controller.stepSize   = volumeGenerator.stepSize;
                        //controller.UpdateBoundsRenderer();
                    }
                    break;
                }
            }

            paintPrevPosition = position;
        }
コード例 #14
0
        public override void Update()
        {
            // TODO: on garde le rotate 45 degres ou on le reserve au mode teleport (et on fait du continu vomitif pour le mode fly)?

            //
            // Joystick -- go forward/backward, and rotate 45 degrees.
            //

            Vector2 val = VRInput.GetValue(VRInput.secondaryController, CommonUsages.primary2DAxis);

            if (val != Vector2.zero)
            {
                float d = Vector3.Distance(world.transform.TransformPoint(Vector3.one), world.transform.TransformPoint(Vector3.zero));

                Vector3 velocity = Camera.main.transform.forward * val.y * d;
                rig.position += velocity * flySpeed;

                if (Mathf.Abs(val.x) > 0.95f && !rotating)
                {
                    rig.rotation *= Quaternion.Euler(0f, Mathf.Sign(val.x) * 45f, 0f);
                    rotating      = true;
                }
                if (Mathf.Abs(val.x) <= 0.95f && rotating)
                {
                    rotating = false;
                }
            }

            //
            // LEFT GRIP WORLD (on click)
            //

            VRInput.ButtonEvent(VRInput.secondaryController, CommonUsages.gripButton,
                                () =>
            {
                ResetInitControllerMatrices();
                ResetInitWorldMatrix();

                SetLeftControllerVisibility(ControllerVisibility.SHOW_NORMAL);
                isLeftGripped = true;
                GlobalState.IsGrippingWorld = true;
            },
                                () =>
            {
                SetLeftControllerVisibility(ControllerVisibility.SHOW_NORMAL);
                isLeftGripped = false;
                GlobalState.IsGrippingWorld = false;
            });

            // NOTE: we test isLeftGrip because we can be ungripped but still over the deadzone, strangely.
            if (isLeftGripped && VRInput.GetValue(VRInput.secondaryController, CommonUsages.grip) > deadZone)
            {
                float prevScale = scale;

                // Scale using left joystick.
                Vector2 joystickAxis = VRInput.GetValue(VRInput.secondaryController, CommonUsages.primary2DAxis);
                if (joystickAxis.y > deadZone)
                {
                    scale *= fixedScaleFactor;
                }
                if (joystickAxis.y < -deadZone)
                {
                    scale /= fixedScaleFactor;
                }

                // update left joystick
                Vector3    currentLeftControllerPosition_L;
                Quaternion currentLeftControllerRotation_L;
                VRInput.GetControllerTransform(VRInput.secondaryController, out currentLeftControllerPosition_L, out currentLeftControllerRotation_L);
                Matrix4x4 currentLeftControllerMatrix_L_Scaled = Matrix4x4.TRS(currentLeftControllerPosition_L, currentLeftControllerRotation_L, new Vector3(scale, scale, scale));

                Matrix4x4 currentLeftControllerMatrix_W_Delta = initPivotMatrix * currentLeftControllerMatrix_L_Scaled * initLeftControllerMatrix_WtoL;
                Matrix4x4 transformed = currentLeftControllerMatrix_W_Delta * initRigMatrix_W;

                transformed = transformed.inverse;

                rig.localPosition = new Vector3(transformed.GetColumn(3).x, transformed.GetColumn(3).y, transformed.GetColumn(3).z);
                rig.localRotation = transformed.rotation;
                float clampedScale = Mathf.Clamp(transformed.lossyScale.x, 1.0f / maxPlayerScale, minPlayerScale);
                rig.localScale = new Vector3(clampedScale, clampedScale, clampedScale);
                if (transformed.lossyScale.x != clampedScale)
                {
                    scale = prevScale;
                }

                GlobalState.WorldScale = 1f / rig.localScale.x;

                UpdateCameraClipPlanes();
            }
        }
コード例 #15
0
        public override void Update()
        {
            //
            // LEFT GRIP WORLD
            //

            VRInput.ButtonEvent(VRInput.secondaryController, CommonUsages.gripButton,
                                () =>
            {
                // left AFTER right => reset all
                // NOTE: isRightGripped && Selection.selection.Count > 0 means that the selectionTool will/has gripped objects,
                //       and is no longer able to be used for two-hands interaction.
                if (isRightGripped) // && Selection.selection.Count == 0)
                {
                    ResetInitControllerMatrices(ResetType.LEFT_AND_RIGHT);
                    ResetInitWorldMatrix();
                    ResetDistance(); // after reset world, use scale

                    SetLeftControllerVisibility(ControllerVisibility.SHOW_NORMAL);

                    lineUI.Show(true, StretchUI.LineMode.DOUBLE);
                    GlobalState.IsGrippingWorld = true;
                    ToolsManager.ActivateCurrentTool(false);
                }

                isLeftGripped = true;
            },
                                () =>
            {
                SetLeftControllerVisibility(ControllerVisibility.SHOW_NORMAL);

                lineUI.Show(false);
                ToolsManager.ActivateCurrentTool(true);
                GlobalState.IsGrippingWorld = false;

                isLeftGripped = false;
            });

            //
            // RIGHT GRIP WORLD
            //

            // NOTE: On ne peut predire dans quel ordre les Update vont s'executer. Le Selector/SelectorTrigger peuvent
            //       recuperer le LeftGrip avant nous, et commencer a grip un objet avant qu'on ait pu set la property
            //       GlobalState.IsGrippingWorld. Cela pose-t-il encore un probleme?
            VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.gripButton,
                                () =>
            {
                //if (Selection.selection.Count == 0)
                {
                    // right AFTER left and no selection, reset all
                    if (isLeftGripped)
                    {
                        ResetInitControllerMatrices(ResetType.LEFT_AND_RIGHT);
                        ResetInitWorldMatrix();
                        ResetDistance(); // NOTE: called after "reset world", because it uses the scale.

                        SetLeftControllerVisibility(ControllerVisibility.SHOW_NORMAL);
                        lineUI.Show(true, StretchUI.LineMode.DOUBLE);
                        GlobalState.IsGrippingWorld = true;
                        ToolsManager.ActivateCurrentTool(false);
                    }

                    // even if no left gripped, just flag the right as gripped for the next update
                    isRightGripped = true;
                }
            },
                                () =>
            {
                // si on relache le right et que le left est tjs grip, reset left
                if (isLeftGripped)
                {
                    ResetInitControllerMatrices(ResetType.LEFT_ONLY);
                    ResetInitWorldMatrix();

                    SetLeftControllerVisibility(ControllerVisibility.SHOW_NORMAL);
                }
                //lineUI.Show(true, StretchUI.LineMode.SINGLE);
                ToolsManager.ActivateCurrentTool(true);
                GlobalState.IsGrippingWorld = false;

                isRightGripped = false;
            });

            // NOTE: we test isLeftGrip because we can be ungripped but still over the deadzone, strangely.
            if (isLeftGripped && VRInput.GetValue(VRInput.secondaryController, CommonUsages.grip) > deadZone)
            {
                if (isRightGripped)
                {
                    float prevScale = scale;

                    VRInput.GetControllerTransform(VRInput.secondaryController, out Vector3 currentLeftControllerPosition_L, out Quaternion currentLeftControllerRotation_L);
                    VRInput.GetControllerTransform(VRInput.primaryController, out Vector3 currentRightControllerPosition_L, out Quaternion currentRightControllerRotation_L);

                    Matrix4x4 currentLeftControllerMatrix_L_Scaled = Matrix4x4.TRS(currentLeftControllerPosition_L, currentLeftControllerRotation_L, new Vector3(scale, scale, scale));
                    Matrix4x4 currentLeftControllerMatrix_W        = initPivotMatrix * currentLeftControllerMatrix_L_Scaled;
                    Vector3   currentLeftControllerPosition_W      = currentLeftControllerMatrix_W.MultiplyPoint(Vector3.zero);

                    // update right joystick
                    Matrix4x4 currentRightControllerMatrix_L_Scaled = Matrix4x4.TRS(currentRightControllerPosition_L, currentRightControllerRotation_L, new Vector3(scale, scale, scale));
                    Vector3   currentRightControllerPosition_W      = (initPivotMatrix * currentRightControllerMatrix_L_Scaled).MultiplyPoint(Vector3.zero);

                    // scale handling (before computing the "transformed" matrix with the new scale)
                    float newDistance = Vector3.Distance(currentLeftControllerPosition_W, currentRightControllerPosition_W);
                    float factor      = newDistance / prevDistance;

                    float oldScale = scale;
                    scale       *= factor;
                    prevDistance = newDistance;

                    Vector3    middlePosition_L    = (currentLeftControllerPosition_L + currentRightControllerPosition_L) * 0.5f;
                    Vector3    middleXVector       = (currentRightControllerPosition_L - currentLeftControllerPosition_L).normalized;
                    Vector3    middleForwardVector = -Vector3.Cross(middleXVector, pivot.up).normalized;
                    Quaternion middleRotation_L    = Quaternion.LookRotation(middleForwardVector, pivot.up);

                    Matrix4x4 middleMatrix_L_Scaled = Matrix4x4.TRS(middlePosition_L, middleRotation_L, Vector3.one) * Matrix4x4.Scale(Vector3.one * scale);
                    Matrix4x4 middleMatrix_W_Delta  = initPivotMatrix * middleMatrix_L_Scaled * initMiddleMatrix_WtoL;
                    Matrix4x4 transformed           = middleMatrix_W_Delta * initWorldMatrix_W;
                    transformed = transformed.inverse;

                    float s            = 1.0f;
                    float clampedScale = Mathf.Clamp(transformed.lossyScale.x, 1.0f / maxPlayerScale, minPlayerScale);
                    if (transformed.lossyScale.x == clampedScale)
                    {
                        // translate/rotate/scale using the new scale
                        rig.localPosition = new Vector3(transformed.GetColumn(3).x, transformed.GetColumn(3).y, transformed.GetColumn(3).z);
                        rig.localRotation = transformed.rotation;
                        rig.localScale    = new Vector3(clampedScale, clampedScale, clampedScale);

                        s = oldScale;
                    }

                    // Get head position
                    VRInput.GetControllerTransform(VRInput.head, out Vector3 HeadPosition, out Quaternion headRotation);
                    Matrix4x4 invHeadMatrix = Matrix4x4.TRS(HeadPosition, headRotation, Vector3.one).inverse;
                    // Project left & right controller into head matrix to determine which one is on the left
                    Vector3 leftControllerInHeadMatrix  = invHeadMatrix.MultiplyPoint(currentLeftControllerPosition_L);
                    Vector3 rightControllerInHeadMatrix = invHeadMatrix.MultiplyPoint(currentRightControllerPosition_L);
                    // reverse text if right and left hands are crossed
                    if (leftControllerInHeadMatrix.x > rightControllerInHeadMatrix.x)
                    {
                        middleXVector = -middleXVector;
                    }

                    // Rotation for the line text
                    Vector3    middleForward180              = Vector3.Cross(middleXVector, pivot.up).normalized;
                    Vector3    rolledUp                      = Vector3.Cross(-middleXVector, middleForward180).normalized;
                    Quaternion middleRotationWithRoll_L      = Quaternion.LookRotation(middleForward180, rolledUp);
                    Matrix4x4  middleMatrixWithRoll_L_Scaled = Matrix4x4.TRS(middlePosition_L, middleRotationWithRoll_L, new Vector3(s, s, s));
                    Quaternion middleRotationWithRoll_W      = (pivot.localToWorldMatrix * middleMatrixWithRoll_L_Scaled).rotation;

                    lineUI.UpdateLineUI(pivot.TransformPoint(currentLeftControllerPosition_L), pivot.TransformPoint(currentRightControllerPosition_L), middleRotationWithRoll_W, 1f / GlobalState.WorldScale);
                }
                GlobalState.WorldScale = 1f / rig.localScale.x;

                UpdateCameraClipPlanes();
            }
        }
コード例 #16
0
ファイル: NavigationMode_FPS.cs プロジェクト: ubisoft/vrtist
        // Update is called once per frame
        public override void Update()
        {
            float   speed         = fpsSpeedFactor * options.fpsSpeed;
            Vector4 joystickValue = GetJoysticksValue();

            Vector2 rightJoyValue = new Vector2(joystickValue.z, joystickValue.w);

            if (rightJoyValue != Vector2.zero)
            {
                float rSpeed = fpsRotationSpeedFactor * options.fpsRotationSpeed;
                float d      = Vector3.Distance(world.transform.TransformPoint(Vector3.one), world.transform.TransformPoint(Vector3.zero));
                // move up
                Vector3 up    = Vector3.up;
                Vector3 right = Vector3.Cross(up, cameraForward).normalized;

                // rotate
                Quaternion rotation = Quaternion.AngleAxis(rightJoyValue.x * rSpeed, up);
                rig.rotation = rotation * rig.rotation;

                // update forward
                Matrix4x4 m = new Matrix4x4();
                m.SetColumn(0, right);
                m.SetColumn(1, up);
                m.SetColumn(2, cameraForward);
                m.SetColumn(3, new Vector4(0, 0, 0, 1));
                Matrix4x4 rotated = Matrix4x4.Rotate(rotation) * m;
                cameraForward = rotated.GetColumn(2).normalized;
            }

            Vector2 leftJoyValue = new Vector2(joystickValue.x, joystickValue.y);

            if (leftJoyValue != Vector2.zero)
            {
                float d = Vector3.Distance(world.transform.TransformPoint(Vector3.one), world.transform.TransformPoint(Vector3.zero));

                // move forward
                Vector3 up              = Vector3.up;
                Vector3 right           = Vector3.Cross(up, cameraForward).normalized;
                Vector3 forward         = Vector3.Cross(right, up).normalized;
                Vector3 forwardVelocity = forward * leftJoyValue.y * d;

                // strafe
                Vector3 leftRightVelocity = right * leftJoyValue.x * d;

                //rig.position += forwardVelocity * speed + leftRightVelocity * speed;
                controller.Move(forwardVelocity * speed + leftRightVelocity * speed);
            }

            isGrounded = Physics.CheckSphere(rig.position - Vector3.up, groundDistance, 5);
            Ray        ray = new Ray(rig.position, -Vector3.up);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                Vector3 hitPoint = hit.point;
                isGrounded = Mathf.Abs(hitPoint.y - rig.position.y) < 0.1f;
            }

            VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.primaryButton,
                                () =>
            {
                if (isGrounded)
                {
                    velocity.y = Mathf.Sqrt(jumpHeight * 2f * options.fpsGravity);
                }
            });

            if (isGrounded && velocity.y < 0 || (rig.position.y < -10f))
            {
                velocity.y = 0f;
            }
            else
            {
                velocity.y -= options.fpsGravity * Time.deltaTime * Time.deltaTime;
            }
            controller.Move(velocity);
        }
コード例 #17
0
        public override void Update()
        {
            if (ray == null)
            {
                return;
            }

            //
            // RAY - collision with scene objects.
            //
            if (!isLocked)
            {
                RaycastHit hit;
                Vector3    worldStart     = paletteController.TransformPoint(0.01f, 0.0f, 0.05f);
                Vector3    worldEnd       = paletteController.TransformPoint(0, 0, 3);
                Vector3    worldDirection = worldEnd - worldStart;
                Ray        r          = new Ray(worldStart, worldDirection);
                int        layersMask = LayerMask.GetMask(new string[] { "Default", "Selection", "Hover" });
                if (Physics.Raycast(r, out hit, 100.0f, layersMask))
                {
                    target         = hit.collider.transform;
                    targetPosition = hit.collider.bounds.center;
                    minDistance    = hit.collider.bounds.extents.magnitude;
                    ray.SetStartPosition(worldStart);
                    ray.SetEndPosition(hit.point);
                    ray.SetActiveColor();
                    if (target)
                    {
                        Selection.HoveredObject = target.gameObject;
                    }
                }
                else
                {
                    if (target)
                    {
                        Selection.HoveredObject = null;
                    }
                    target         = null;
                    targetPosition = Vector3.zero;
                    minDistance    = 0.0f;
                    ray.SetStartPosition(worldStart);
                    ray.SetEndPosition(worldEnd);
                    ray.SetDefaultColor();
                }
            }
            else
            {
                Vector3 up       = Vector3.up; //rig.up;
                Vector3 forward  = Vector3.Normalize(camera.position - targetPosition);
                Vector3 right    = Vector3.Cross(up, forward);
                float   distance = Vector3.Distance(camera.position, targetPosition);

                //
                // Left Joystick -- left/right = rotate left/right.
                //                  up/down = rotate up/down.
                Vector2 val = VRInput.GetValue(VRInput.secondaryController, CommonUsages.primary2DAxis);
                if (val != Vector2.zero)
                {
                    // Horizontal rotation
                    if (Mathf.Abs(val.x) > deadZone)
                    {
                        float value           = Mathf.Sign(val.x) * (Mathf.Abs(val.x) - deadZone) / (1.0f - deadZone); // remap
                        float rotate_amount_h = value * options.orbitRotationalSpeed;                                  //rotationalSpeed;
                        rig.RotateAround(targetPosition, up, -rotate_amount_h);
                    }

                    // Vertical rotation
                    if (Mathf.Abs(val.y) > deadZone)
                    {
                        float value                = Mathf.Sign(val.y) * (Mathf.Abs(val.y) - deadZone) / (1.0f - deadZone); // remap
                        float dot                  = Vector3.Dot(up, forward);
                        bool  in_safe_zone         = (Mathf.Abs(dot) < 0.8f);
                        bool  above_but_going_down = (dot > 0.8f) && (value < 0.0f);
                        bool  below_but_going_up   = (dot < -0.8f) && (value > 0.0f);
                        if (!limitVertical || in_safe_zone || above_but_going_down || below_but_going_up) // only within limits
                        {
                            float rotate_amount_v = value * options.orbitRotationalSpeed;                 //rotationalSpeed;
                            rig.RotateAround(targetPosition, right, -rotate_amount_v);
                        }
                    }
                }

                //
                // Right Joystick -- left/right = move closer/farther
                //                   up/down = scale world
                val = VRInput.GetValue(VRInput.primaryController, CommonUsages.primary2DAxis);
                if (val != Vector2.zero)
                {
                    float remainingDistance = distance - minDistance;
                    bool  in_safe_zone      = (remainingDistance > 0.0f);

                    // Move the world closer/farther
                    if (Mathf.Abs(val.x) > deadZone)
                    {
                        float value = Mathf.Sign(val.x) * (Mathf.Abs(val.x) - deadZone) / (1.0f - deadZone); // remap
                        bool  too_close_but_going_back = (remainingDistance <= 0.0f) && (value < 0.0f);
                        if (in_safe_zone || too_close_but_going_back)
                        {
                            Vector3 offset = forward * value * (minMoveDistance + options.orbitMoveSpeed * Mathf.Abs(remainingDistance)); //moveSpeed
                            rig.position -= offset;
                        }
                    }

                    /*
                     * // Scale the world
                     * if (Mathf.Abs(val.y) > deadZone)
                     * {
                     *  float value = Mathf.Sign(val.y) * (Mathf.Abs(val.y) - deadZone) / (1.0f - deadZone); // remap
                     *  bool too_close_but_scaling_down = (remainingDistance <= 0.0f) && (value < 0.0f);
                     *  if (in_safe_zone || too_close_but_scaling_down)
                     *  {
                     *      float scale = 1.0f + (value * options.orbitScaleSpeed); // scaleSpeed
                     *
                     *      Vector3 scalePivot = targetPosition;
                     *      Vector3 pivot_to_world = world.position - scalePivot;
                     *      pivot_to_world.Scale(new Vector3(scale, scale, scale));
                     *      world.position = scalePivot + pivot_to_world;
                     *      targetPosition = world.position - pivot_to_world;
                     *      minDistance *= scale;
                     *
                     *      float finalScale = scale * world.localScale.x;
                     *      float clampedScale = Mathf.Clamp(finalScale, 1.0f / maxPlayerScale, minPlayerScale);
                     *
                     *      // should touch rig, not world
                     *      world.localScale = new Vector3(clampedScale, clampedScale, clampedScale);
                     *
                     *      GlobalState.WorldScale = world.localScale.x;
                     *
                     *      UpdateCameraClipPlanes();
                     *  }
                     * }
                     */
                }

                // Position the ray AFTER the rotation of the camera, to avoid a one frame shift.
                ray.SetStartPosition(paletteController.TransformPoint(0.01f, 0.0f, 0.05f));
                ray.SetEndPosition(targetPosition);
            }


            //
            // LEFT GRIP (click) - lock on targetted object/point.
            //

            VRInput.ButtonEvent(VRInput.secondaryController, CommonUsages.gripButton,
                                () =>
            {
                if (target != null)
                {
                    isLocked = true;
                    ray.gameObject.SetActive(false); // hide ray on grip
                }

                GlobalState.IsGrippingWorld = true;
            },
                                () =>
            {
                isLocked = false;
                ray.gameObject.SetActive(true);
                GlobalState.IsGrippingWorld = false;
            });
        }