Esempio n. 1
0
        public static HBSettings GetSettings(bool attachToCameraInEditor)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                return(new HBSettings(BendingMode._HB_OFF));
            }

            float horizonXOffset;
            float horizonYOffset;
            float horizonZOffset;

            if (attachToCameraInEditor)
            {
                horizonXOffset = hb.HorizonXOffset;
                horizonYOffset = hb.HorizonYOffset;
                horizonZOffset = hb.HorizonZOffset;
            }
            else
            {
                horizonXOffset = hb.HorizonXOffset + hb.transform.position.x;
                horizonYOffset = hb.HorizonYOffset + hb.transform.position.y;
                horizonZOffset = hb.HorizonZOffset + hb.transform.position.z;
            }

            return(new HBSettings(
                       hb.BendingMode,
                       hb.Curvature * HB.CURVATURE_FACTOR,
                       hb.Flatten,
                       horizonXOffset,
                       horizonYOffset,
                       horizonZOffset,
                       attachToCameraInEditor));
        }
Esempio n. 2
0
        private void Update()
        {
            if (HB.Instance == null)
            {
                return;
            }

            if (HB.Instance.FixLightsPositionCamera == null)
            {
                return;
            }

            Transform cameraTransform = HB.Instance.FixLightsPositionCamera.transform;

            if (cameraTransform.position != m_prevCameraPosition || transform.position != m_prevPosition)
            {
                Vector3 offset = HB.GetOffset(transform.position, cameraTransform);
                if (Mathf.Infinity != offset.magnitude)
                {
                    m_transform.localPosition = m_transform.InverseTransformVector(-offset);
                }

                m_prevCameraPosition = cameraTransform.position;
                m_prevPosition       = transform.position;
            }
        }
Esempio n. 3
0
        private void OnEnable()
        {
            if (Instance != this && Instance != null)
            {
                Debug.LogError("Another instance of HB exists in scene");
                return;
            }
            else
            {
                Instance = this;
            }

            CreateHBCameras();



#if UNITY_EDITOR
            AttachToCamera = Application.isPlaying;
            if (AttachToCamera)
            {
                ApplyAll(Curvature, Flatten, HorizonXOffset, HorizonYOffset, HorizonZOffset);
            }
            else
            {
                ApplyAll(Curvature, Flatten, HorizonXOffset, HorizonYOffset, HorizonZOffset, transform);
            }
#else
            ApplyAll(Curvature, Flatten, HorizonXOffset, HorizonYOffset, HorizonZOffset);
#endif
        }
Esempio n. 4
0
        public static Vector3 GetOffset(Vector3 atPosition, Transform cameraTransform)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                return(new Vector3(0.0f, 0.0f, 0.0f));
            }

            Vector3    offset;
            HBSettings settings = GetSettings();

#if UNITY_EDITOR
            if (settings.AttachToCameraInEditor)
            {
                offset = HBUtils.GetOffset(atPosition, settings, cameraTransform);
            }
            else
            {
                offset = HBUtils.GetOffset(atPosition, settings);
            }
#else
            offset = HBUtils.GetOffset(atPosition, settings, cameraTransform);
#endif

            return(offset);
        }
Esempio n. 5
0
        public static void ChangeHorizonOffset(float deltaX, float deltaY, float deltaZ, Transform transform = null)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                return;
            }
            ApplyHorizonOffset(hb.HorizonXOffset + deltaX, hb.HorizonYOffset + deltaY, hb.HorizonZOffset + deltaZ, transform);
        }
Esempio n. 6
0
        public static void Rollback()
        {
            HB hb = GameObject.FindObjectOfType <HB>();

            if (hb != null)
            {
                hb.Internal_Rollback(true);
                GameObject.DestroyImmediate(hb.gameObject);
            }
        }
Esempio n. 7
0
        public static void ChangeFlatten(float delta)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                return;
            }
            ApplyFlatten(hb.Flatten + delta);
        }
Esempio n. 8
0
        public static void ChangeCurvature(float delta)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                return;
            }

            ApplyCurvature(hb.Curvature + delta);
        }
Esempio n. 9
0
        public static void ApplyFlatten(float flatten)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                return;
            }
            hb.Flatten = flatten;
            HBUtils.HBFlatten(flatten);
        }
Esempio n. 10
0
        public static void ApplyCurvature(float curvature)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                return;
            }
            hb.Curvature = curvature;
            HBUtils.HBCurvature(curvature * CURVATURE_FACTOR);
        }
Esempio n. 11
0
        public static void FixSkinned(SkinnedMeshRenderer skinned, float fixBoundsRadius)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                return;
            }

            Vector3 hbOffset = HBUtils.GetOffset(GetSettings(), fixBoundsRadius);

            HBUtils.FixSkinned(skinned, skinned.localBounds, skinned.transform, hbOffset);
        }
Esempio n. 12
0
        public static void FixMesh(MeshFilter meshFilter, float fixBoundsRadius)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                return;
            }

            Vector3 hbOffset = HBUtils.GetOffset(GetSettings(), fixBoundsRadius);

            meshFilter.sharedMesh = HBUtils.FixMesh(meshFilter.sharedMesh, meshFilter.transform, hbOffset);
        }
Esempio n. 13
0
        public static void ScreenPointToRays(Camera camera, out Ray[] rays, out float[] maxDistances)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                Ray ray = camera.ScreenPointToRay(Input.mousePosition);
                rays         = new[] { ray };
                maxDistances = new[] { Mathf.Infinity };
                return;
            }

            HBUtils.ScreenPointToRays(camera, hb.RaycastStride, hb.FixBoundsRadius, GetSettings(), out rays, out maxDistances);
        }
Esempio n. 14
0
        public bool IsGlobalSettingOverriden()
        {
            HB hb = HB.Instance;

            return(hb == null ||
                   FixBoundsRadius != hb.FixBoundsRadius ||
                   Curvature != hb.Curvature ||
                   Flatten != hb.Flatten ||
                   HorizonXOffset != hb.HorizonXOffset ||
                   HorizonYOffset != hb.HorizonYOffset ||
                   HorizonZOffset != hb.HorizonZOffset ||
                   BendingMode != hb.BendingMode ||
                   OverrideBounds);
        }
Esempio n. 15
0
        public static void CameraToRays(Transform camerTransform, out Ray[] rays, out float[] maxDistances)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                Ray ray = new Ray(camerTransform.position, camerTransform.forward);
                rays         = new[] { ray };
                maxDistances = new[] { Mathf.Infinity };
                return;
            }

            HBUtils.CameraToRays(camerTransform, hb.RaycastStride, hb.FixBoundsRadius, GetSettings(), out rays, out maxDistances);
        }
Esempio n. 16
0
        public static void ApplyHorizonOffset(float horizonX, float horizonY, float horizonZ, Transform transform = null)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                return;
            }

            hb.HorizonXOffset = horizonX;
            hb.HorizonYOffset = horizonY;
            hb.HorizonZOffset = horizonZ;
            HBUtils.HBHorizonOffset(horizonX, horizonY, horizonZ, transform);
        }
Esempio n. 17
0
        public static void FixLineRenderer(LineRenderer lineRenderer, Vector3 start, Vector3 end)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                lineRenderer.SetPositions(new[] { start, end });
                return;
            }

            Vector3[] positions = SubdivideLine(start, end);

            lineRenderer.numPositions = positions.Length;
            lineRenderer.SetPositions(positions);
        }
Esempio n. 18
0
        public static HBSettings GetSettings()
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                return(new HBSettings(BendingMode._HB_OFF));
            }

#if UNITY_EDITOR
            bool attachToCameraInEditor = hb.AttachToCamera;
#else
            bool attachToCameraInEditor = true;
#endif
            return(GetSettings(attachToCameraInEditor));
        }
Esempio n. 19
0
        private static Vector3[] SubdivideLine(Vector3 start, Vector3 end)
        {
            HB    hb     = HB.Instance;
            float length = (end - start).magnitude;
            int   count  = Mathf.Max(0, Mathf.CeilToInt((length - hb.RaycastStride) / hb.RaycastStride));

            Vector3[] positions = new Vector3[count + 2];
            Vector3   dir       = (end - start).normalized * hb.RaycastStride;

            for (int i = 0; i <= count; ++i)
            {
                positions[i] = start;
                start       += dir;
            }

            positions[count + 1] = end;
            return(positions);
        }
Esempio n. 20
0
        void Update()
        {
#if UNITY_EDITOR
            if (Input.GetMouseButtonDown(0))
            {
                HB.DebugScreenPointsToRay(Camera.main);

                Ray[]   rays;
                float[] maxDistances;
                HB.ScreenPointToRays(Camera.main, out rays, out maxDistances);

                RaycastHit hitInfo;
                if (HB.Raycast(rays, out hitInfo, maxDistances))
                {
                    Debug.Log(hitInfo.transform.gameObject.name);
                }
            }
#endif
        }
Esempio n. 21
0
        public static void ApplyAll(float curvature, float flatten, float horizonX, float horizonY, float horizonZ, Transform transform = null)
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                return;
            }

            hb.Curvature      = curvature;
            hb.Flatten        = flatten;
            hb.HorizonXOffset = horizonX;
            hb.HorizonYOffset = horizonY;
            hb.HorizonZOffset = horizonZ;

            HBSettings settings = GetSettings();

            HBUtils.HorizonBend(settings, transform);
        }
Esempio n. 22
0
        private void OnPreCull()
        {
            HB hb = HB.Instance;

            if (hb == null)
            {
                Debug.LogWarning("HB is null");
            }
            Vector4 position;

#if UNITY_EDITOR
            if (hb.AttachToCamera)
            {
                position = m_camera.transform.position;
            }
            else
            {
                if (hb != null)
                {
                    position = hb.transform.position;
                }
                else
                {
                    position = Vector4.zero;
                }
            }
#else
            position = m_camera.transform.position;
#endif

            position.w = 1.0f;
            Shader.SetGlobalVector("_HBWorldSpaceCameraPos", position);
            if (m_camera.orthographic)
            {
                m_currentOrthographicSize  = m_camera.orthographicSize;
                m_camera.orthographicSize += hb.FixOrthographicSize;
            }
            else
            {
                m_currentFieldOfView  = m_camera.fieldOfView;
                m_camera.fieldOfView += hb.FixFieldOfView;
            }
        }
Esempio n. 23
0
        private void OnDrawGizmos()
        {
            if (UnityEditor.Selection.activeObject != gameObject)
            {
                return;
            }

            HB hb = HB.Instance;

            Gizmos.matrix = m_camera.transform.localToWorldMatrix;
            Gizmos.color  = Color.green;
            if (m_camera.orthographic)
            {
                Gizmos.DrawWireCube(new Vector3(0.0f, 0.0f, m_camera.nearClipPlane + (m_camera.farClipPlane - m_camera.nearClipPlane) / 2.0f),
                                    new Vector3(m_camera.aspect * (m_camera.orthographicSize + hb.FixOrthographicSize) * 2.0f, (m_camera.orthographicSize + hb.FixOrthographicSize) * 2.0f, m_camera.farClipPlane - m_camera.nearClipPlane));
            }
            else
            {
                Gizmos.DrawFrustum(Vector3.zero, m_camera.fieldOfView + hb.FixFieldOfView, m_camera.farClipPlane, m_camera.nearClipPlane, m_camera.aspect);
            }
        }
Esempio n. 24
0
        public static void FixLineRenderer(LineRenderer lineRenderer, UnityEngine.AI.NavMeshPath path)
        {
            HB hb = HB.Instance;

            if (hb == null || path.corners.Length == 0)
            {
                lineRenderer.numPositions = path.corners.Length;

                // Go through all the corners and set the line's points to the corners' positions.
                for (int i = 0; i < path.corners.Length; i++)
                {
                    lineRenderer.SetPosition(i, path.corners[i]);
                }
                return;
            }

            List <Vector3> points = new List <Vector3>();

            for (int i = 0; i < path.corners.Length - 1; i++)
            {
                Vector3   start      = path.corners[i];
                Vector3   end        = path.corners[i + 1];
                Vector3[] subdivided = SubdivideLine(start, end);
                for (int j = 0; j < subdivided.Length - 1; ++j)
                {
                    points.Add(subdivided[j]);
                }
            }

            points.Add(path.corners[path.corners.Length - 1]);

            lineRenderer.numPositions = points.Count;
            for (int i = 0; i < points.Count; i++)
            {
                lineRenderer.SetPosition(i, points[i]);
            }
        }
Esempio n. 25
0
        private void Configure()
        {
            string path = Application.dataPath + "/Battlehub/HorizonBending/Shaders/_Templates";

            if (!Directory.Exists(path))
            {
                Debug.LogError("Unable to configure. Required " + path + " folder is missing. Try reimport");
                return;
            }

            string      properties;
            string      feature;
            string      hbst;
            string      hbstm;
            BendingMode bendingMode = BendingMode._HB_XZ_YUP;

            if (m_profile == 0)
            {
                if (m_worldUpAxis == 0)   //X
                {
                    if (m_bendAxis == 0)
                    {
                        feature     = "_HB_YZ_XUP 1";
                        bendingMode = BendingMode._HB_YZ_XUP;
                    }
                    else if (m_bendAxis == 1)
                    {
                        feature     = "_HB_Y_XUP 1";
                        bendingMode = BendingMode._HB_Y_XUP;
                    }
                    else
                    {
                        feature     = "_HB_Z_XUP 1";
                        bendingMode = BendingMode._HB_Z_XUP;
                    }
                }
                else if (m_worldUpAxis == 1) //Y
                {
                    if (m_bendAxis == 0)
                    {
                        feature     = "_HB_XZ_YUP 1";
                        bendingMode = BendingMode._HB_XZ_YUP;
                    }
                    else if (m_bendAxis == 1)
                    {
                        feature     = "_HB_X_YUP 1";
                        bendingMode = BendingMode._HB_X_YUP;
                    }
                    else
                    {
                        feature     = "_HB_Z_YUP 1";
                        bendingMode = BendingMode._HB_Z_YUP;
                    }
                }
                else //Z
                {
                    if (m_bendAxis == 0)
                    {
                        feature     = "_HB_XY_ZUP 1";
                        bendingMode = BendingMode._HB_XY_ZUP;
                    }
                    else if (m_bendAxis == 1)
                    {
                        feature     = "_HB_X_ZUP 1";
                        bendingMode = BendingMode._HB_X_ZUP;
                    }
                    else
                    {
                        feature     = "_HB_Y_ZUP 1";
                        bendingMode = BendingMode._HB_Y_ZUP;
                    }
                }

                properties = string.Empty;
                hbst       = "#define " + feature;
                hbstm      = "#define " + feature;
            }
            else
            {
                feature    = "#pragma multi_compile _HB_YZ_XUP _HB_Y_XUP _HB_Z_XUP _HB_XZ_YUP _HB_X_YUP _HB_Z_YUP _HB_XY_ZUP _HB_X_ZUP _HB_Y_ZUP";
                hbst       = feature;
                hbstm      = feature;
                properties = "XZ_YUp, X_YUp, Z_YUp, YZ_XUp, Y_XUp, Z_XUp, XY_ZUp, X_ZUp, Y_ZUp";
            }

            properties = string.Format("[KeywordEnum({0})] _HB(\"Bending Mode\", Float) = 0", properties);
            bool importShaders = bendingMode != Internal_BendingModePrefs;

            if (!importShaders)
            {
                importShaders = !ShadersExist(path, "hbst") || !ShadersExist(path, "hbstm");
            }
            if (importShaders)
            {
                CreateShaders(m_profile, path, properties, hbst, "hbst");
                CreateShaders(m_profile, path, properties, hbstm, "hbstm");
            }
            HB hb = GameObject.FindObjectOfType <HB>();

            if (hb != null)
            {
                hb.Internal_Rollback(true);
                GameObject.DestroyImmediate(hb.gameObject);
            }

            GameObject hbGO = StorageHelper.InstantiatePrefab("HB.prefab");

            hb                        = hbGO.GetComponent <HB>();
            hb.BendingMode            = bendingMode;
            Internal_BendingModePrefs = bendingMode;
            if (m_profile == 0)
            {
                Internal_AllowChangeBendingMode = false;
            }
            else
            {
                Internal_AllowChangeBendingMode = true;
            }
            StorageHelper.CreatePrefab(hbGO.name + ".prefab", hbGO);
            DestroyImmediate(hbGO);
            if (importShaders)
            {
                AssetDatabase.ImportAsset("Assets/Battlehub/HorizonBending/Shaders/", ImportAssetOptions.ImportRecursive);
            }

            EditorPrefs.SetBool("HBConfigured", true);
        }
Esempio n. 26
0
        public static void Apply()
        {
            HB hb = GameObject.FindObjectOfType <HB>();

            bool        copySettings           = false;
            float       curvature              = 0.0f;
            float       flatten                = 0.0f;
            float       horizonXOffset         = 0.0f;
            float       horizonYOffset         = 0.0f;
            float       horizonZOffset         = 0.0f;
            BendingMode bendingMode            = BendingMode._HB_OFF;
            float       raycastStride          = 0.0f;
            float       fixBoundsRadius        = 0.0f;
            float       fixFieldOfView         = 0.0f;
            float       fixOrthographicSize    = 0.0f;
            bool        attachToCameraInEditor = false;
            bool        lockMaterials          = false;

            Material[] materials               = new Material[0];
            Shader[]   integratedShaders       = new Shader[0];
            Camera     fixLightPositionsCamera = null;

            GameObject[] excludeGameObjects = new GameObject[0];
            if (hb != null)
            {
                copySettings            = true;
                curvature               = hb.Curvature;
                flatten                 = hb.Flatten;
                horizonXOffset          = hb.HorizonXOffset;
                horizonYOffset          = hb.HorizonYOffset;
                horizonZOffset          = hb.HorizonZOffset;
                bendingMode             = hb.BendingMode;
                raycastStride           = hb.RaycastStride;
                fixBoundsRadius         = hb.FixBoundsRadius;
                fixFieldOfView          = hb.FixFieldOfView;
                fixOrthographicSize     = hb.FixOrthographicSize;
                fixLightPositionsCamera = hb.FixLightsPositionCamera;
                attachToCameraInEditor  = hb.AttachToCamera;
                lockMaterials           = hb.LockMaterials;
                materials               = hb.Materials;
                integratedShaders       = hb.CustomShaders;
                excludeGameObjects      = hb.ExcludeGameObjects;

                hb.Internal_Rollback(false);
                GameObject.DestroyImmediate(hb.gameObject);
            }

            GameObject hbGO = StorageHelper.InstantiatePrefab("HB.prefab");

            PrefabUtility.DisconnectPrefabInstance(hbGO);
            hb = hbGO.GetComponent <HB>();
            if (copySettings)
            {
                hb.Curvature               = curvature;
                hb.Flatten                 = flatten;
                hb.HorizonXOffset          = horizonXOffset;
                hb.HorizonYOffset          = horizonYOffset;
                hb.HorizonZOffset          = horizonZOffset;
                hb.BendingMode             = bendingMode;
                hb.RaycastStride           = raycastStride;
                hb.FixBoundsRadius         = fixBoundsRadius;
                hb.FixFieldOfView          = fixFieldOfView;
                hb.FixOrthographicSize     = fixOrthographicSize;
                hb.FixLightsPositionCamera = fixLightPositionsCamera;
                hb.AttachToCamera          = attachToCameraInEditor;
                hb.LockMaterials           = lockMaterials;
                hb.Materials               = materials;
                hb.CustomShaders           = integratedShaders;
                hb.ExcludeGameObjects      = excludeGameObjects;
            }
            else
            {
                hb.FixFieldOfView      = 0.0f;
                hb.FixOrthographicSize = 0.0f;
            }

            hb.Internal_Apply();
            Selection.activeObject = hb.gameObject;
        }
Esempio n. 27
0
 private void OnDisable()
 {
     DestroyHBCameras();
     Instance = null;
 }