Ejemplo 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);
        }
Ejemplo 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);
        }
Ejemplo n.º 3
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);
                }
            }
        }