Esempio n. 1
0
        public override bool HandleStart(Ray ray, Plane drawPlane)
        {
            m_Plane = drawPlane;

            if (!VRMath.GetPointOnPlane(ray, m_Plane, out m_StartPoint))
            {
                return(false);
            }

            m_EndPoint   = Snapping.Snap(m_StartPoint, m_SnapIncrement, VECTOR3_ONE);
            m_StartPoint = m_EndPoint;

            m_Mesh = pb_ShapeGenerator.CubeGenerator(VECTOR3_ONE);

            m_Mesh.gameObject.GetComponent <MeshRenderer>().sharedMaterial = pb_Constant.DefaultMaterial;

            foreach (pb_Face face in m_Mesh.faces)
            {
                face.uv.useWorldSpace = true;
            }

            m_GameObject = m_Mesh.gameObject;

            // we'll place vertex positions in world space while drawing
            m_GameObject.transform.position = Vector3.zero;

            m_Size.x = MIN_SIZE;
            m_Size.y = MIN_SIZE;
            m_Size.z = MIN_SIZE;

            UpdateShape();

            return(true);
        }
Esempio n. 2
0
        public override bool HandleDrag(Ray ray, ref Vector3 planeIntersection)
        {
            if (state == State.Base)
            {
                VRMath.GetPointOnPlane(ray, m_Plane, out planeIntersection);
                m_EndPoint = planeIntersection;
                m_EndPoint = Snapping.Snap(m_EndPoint, m_SnapIncrement, VECTOR3_ONE);
            }
            else
            {
                planeIntersection = VRMath.CalculateNearestPointRayRay(ray.origin, ray.direction, m_BaseEndPoint, m_Plane.normal);
                Vector3 dir = planeIntersection - m_BaseEndPoint;
                dir.Normalize();
                float m        = Vector3.Dot(m_Plane.normal, dir) / 1f;
                float distance = Vector3.Distance(planeIntersection, m_BaseEndPoint) * m;
                distance   = Snapping.Snap(distance, m_SnapIncrement);
                m_EndPoint = m_BaseEndPoint + (m_Plane.normal * distance);
            }

            UpdateShape();

            // @todo sanity check ray input to prevent gigantic shapes
            return(true);
        }
        public override void pb_OnSceneGUI(EditorWindow win)
        {
            if (Event.current.type != EventType.MouseMove)
            {
                return;
            }

            if (m_State == CreateState.Finish && m_Object != null)
            {
                Vector3 collision;

                m_VertexSnap.valid = Snapping.FindNearestVertex2(new Ray(rayOrigin.position, rayOrigin.forward), m_ProBuilderObjectsInScene, out collision, out m_VertexSnap.point);

                if (m_VertexSnap.valid)
                {
                    m_VertexSnap.valid = Vector3.Distance(collision, m_VertexSnap.point) < VertexSnap.MAX_VERTEX_SNAP_DISTANCE;
                    // m_VertexSnap.valid = HandleUtility.DistancePointLine(m_VertexSnap.point, rayOrigin.position, rayOrigin.position + rayOrigin.forward * 100f) < VertexSnap.MAX_VERTEX_SNAP_DISTANCE;
                }
            }
            else
            {
                m_VertexSnap.valid = false;
            }
        }
        private void HandleFinish(Standard input, Action <InputControl> consumeControl)
        {
            // Ready for next object to be created
            if (input.action.wasJustReleased)
            {
                m_AudioModule.Play(m_Trigger, true);
                m_GuideModule.SetVisible(false);
                m_HighlightModule.SetFaceHighlight(m_Object, null);

                m_Dragging = false;
                m_State    = CreateState.Start;
                m_Object.ToMesh();
                m_Object.Refresh();
            }
            else
            {
                if (m_IsDirectSelect)
                {
                    m_DraggedPoint = m_DragOrigin + (Vector3.Project(rayOrigin.position - m_DragOrigin, m_DragDirection));
                }
                else if (m_VertexSnap.valid)
                {
                    m_DraggedPoint = m_DragOrigin + (Vector3.Project(m_VertexSnap.point - m_DragOrigin, m_DragDirection));
                }
                else
                {
                    m_DraggedPoint = VRMath.CalculateNearestPointRayRay(m_DragOrigin, m_DragDirection, rayOrigin.position, rayOrigin.forward);
                }

                if (!m_Dragging)
                {
                    m_Offset   = m_IsDirectSelect ? m_DraggedPoint - m_DragOrigin : Vector3.zero;
                    m_Dragging = true;
                }

                m_DraggedPoint -= m_Offset;

                Vector3 localDragOrigin   = m_Object.transform.InverseTransformPoint(m_DragOrigin);
                Vector3 localDraggedPoint = m_Object.transform.InverseTransformPoint(m_DraggedPoint);
                Vector3 vertexTranslation = localDraggedPoint - localDragOrigin;

                if (vertexTranslation.magnitude > MAX_TRANSLATE_DISTANCE)
                {
                    vertexTranslation = vertexTranslation.normalized * MAX_TRANSLATE_DISTANCE;
                }

                vertexTranslation = Snapping.Snap(vertexTranslation, m_SnapIncrement, VECTOR3_ONE);

                if (vertexTranslation != m_PreviousVertexTranslation)
                {
                    m_PreviousVertexTranslation = vertexTranslation;
                    m_AudioModule.Play(m_Drag);
                }

                foreach (int ind in m_SelectedIndices)
                {
                    m_SettingPositions[ind] = m_Positions[ind] + vertexTranslation;
                }

                m_Object.SetVertices(m_SettingPositions);
                m_Object.msh.vertices = m_SettingPositions;
                m_Object.RefreshUV();
                m_Object.msh.RecalculateBounds();
                m_HighlightModule.UpdateVertices(m_Object);
            }

            setHighlight(m_Object.gameObject, false);
            consumeControl(input.action);
        }
Esempio n. 5
0
        void HandleStartPoint(Standard standardInput, ConsumeControlDelegate consumeControl)
        {
            // HandleStartPoint can be called before Start()?
            if (m_GridModule == null)
            {
                return;
            }

            GameObject first = this.GetFirstGameObject(rayOrigin);
            pb_Object  pb    = first != null?first.GetComponent <pb_Object>() : null;

            Vector3 rayCollisionPoint = Vector3.zero;

            Ray ray = new Ray(rayOrigin.position, rayForwardSmoothed.Add(rayOrigin.forward));

            if (pb != null && pb_HandleUtility.FaceRaycast(ray, pb, out m_RaycastHit))
            {
                if (m_HoveredObject == null)
                {
                    m_HoveredObject = pb;
                }

                rayCollisionPoint = pb.transform.TransformPoint(m_RaycastHit.point);
                rayCollisionPoint = Snapping.Snap(rayCollisionPoint, m_SnapIncrement, Vector3.one);
                m_Plane.SetNormalAndPosition(pb.transform.TransformDirection(m_RaycastHit.normal).normalized, rayCollisionPoint);
            }
            else
            {
                if (m_HoveredObject != null)
                {
                    m_HoveredObject = null;
                    m_Plane.SetNormalAndPosition(Vector3.up, Vector3.zero);
                }
            }

            if (m_HoveredObject != null || VRMath.GetPointOnPlane(ray, m_Plane, out rayCollisionPoint))
            {
                m_ProxyRay.SetLength(Vector3.Distance(rayCollisionPoint, ray.origin));

                m_GridModule.SetVisible(true);
                m_GridModule.transform.position      = Snapping.Snap(rayCollisionPoint, m_SnapIncrement, Vector3.one);
                m_GridModule.transform.localRotation = Quaternion.LookRotation(m_Plane.normal);

                // Calculate the snap increment based on distance from viewer.  Split into 4 segments.
                float distance = Mathf.Clamp(Vector3.Distance(m_GridModule.transform.position, cameraRig.position), 0f, 10f) / 10f;
                m_SnapIncrement = Snapping.DEFAULT_INCREMENT * System.Math.Max(1, Mathf.Pow(2, (int)(distance * 4)));

                m_GridModule.SetSnapIncrement(m_SnapIncrement);
                AShapeCreator.SetSnapIncrement(m_SnapIncrement);

                m_GuideModule.transform.position      = m_GridModule.transform.position;
                m_GuideModule.transform.localRotation = m_GridModule.transform.localRotation;
            }
            else
            {
                m_GridModule.SetVisible(false);
            }

            if (standardInput != null && standardInput.action.wasJustPressed)
            {
                // @todo
                switch (m_Shape)
                {
                default:
                    m_CurrentShape = new CreateCube();
                    break;
                }

                // If shape initialization failed no gameobject will have been created,
                // so don't worry about cleaning up.
                if (m_CurrentShape.HandleStart(ray, m_Plane))
                {
                    m_AudioModule.Play(m_TriggerReleased, true);
                    m_CurrentShape.onShapeChanged = (v) =>
                    {
                        m_GuideModule.transform.position = v;
                        m_AudioModule.Play(m_DragAudio);
                    };
                    m_CurrentShape.gameObject.GetComponent <MeshRenderer>().sharedMaterial = m_HighlightMaterial;
                    m_State = ShapeCreationState.EndPoint;
                    this.AddToSpatialHash(m_CurrentShape.gameObject);
                    consumeControl(standardInput.action);
                }
            }
        }