Пример #1
0
        private void AppendModifierVolumes(ref List <NavMeshBuildSource> sources)
        {
            List <NavMeshModifierVolume> list;

            if (this.m_CollectObjects == CollectObjects.Children)
            {
                list = new List <NavMeshModifierVolume>(base.GetComponentsInChildren <NavMeshModifierVolume>());
                list.RemoveAll((NavMeshModifierVolume x) => !x.isActiveAndEnabled);
            }
            else
            {
                list = NavMeshModifierVolume.activeModifiers;
            }
            foreach (NavMeshModifierVolume navMeshModifierVolume in list)
            {
                if ((this.m_LayerMask & 1 << navMeshModifierVolume.gameObject.layer) != 0 && navMeshModifierVolume.AffectsAgentType(this.m_AgentTypeID))
                {
                    Vector3            pos        = navMeshModifierVolume.transform.TransformPoint(navMeshModifierVolume.center);
                    Vector3            lossyScale = navMeshModifierVolume.transform.lossyScale;
                    Vector3            size       = new Vector3(navMeshModifierVolume.size.x * Mathf.Abs(lossyScale.x), navMeshModifierVolume.size.y * Mathf.Abs(lossyScale.y), navMeshModifierVolume.size.z * Mathf.Abs(lossyScale.z));
                    NavMeshBuildSource item       = default(NavMeshBuildSource);
                    item.shape     = NavMeshBuildSourceShape.ModifierBox;
                    item.transform = Matrix4x4.TRS(pos, navMeshModifierVolume.transform.rotation, Vector3.one);
                    item.size      = size;
                    item.area      = navMeshModifierVolume.area;
                    sources.Add(item);
                }
            }
        }
Пример #2
0
        private static void CollectSources(List <NavMeshBuildSource> sources, SpriteRenderer sprite, int area, NavMeshBuilder2dWrapper builder)
        {
            if (sprite == null)
            {
                return;
            }
            var src = new NavMeshBuildSource();

            src.shape = NavMeshBuildSourceShape.Mesh;
            src.area  = area;

            Mesh mesh;

            mesh = builder.GetMesh(sprite.sprite);
            if (mesh == null)
            {
#if UNITY_EDITOR
                if (!builder.hideEditorLogs)
                {
                    Debug.Log($"{sprite.name} mesh is null");
                }
#endif
                return;
            }
            src.transform    = Matrix4x4.TRS(Vector3.Scale(sprite.transform.position, builder.overrideVector), sprite.transform.rotation, sprite.transform.lossyScale);
            src.sourceObject = mesh;
            sources.Add(src);
        }
        private static void CollectTrees(Component owner, TerrainData terrain, Matrix4x4 transform, int layerMask, int area, List <NavMeshBuildSource> sources)
        {
            var size = terrain.size;

            foreach (var tree in terrain.treeInstances)
            {
                var proto = terrain.treePrototypes[tree.prototypeIndex].prefab;
                var pos   = Vector3.Scale(tree.position, size);
                var rot   = Quaternion.AngleAxis(Mathf.Rad2Deg * tree.rotation, Vector3.up);
                var scale = new Vector3(tree.widthScale, tree.heightScale, tree.widthScale);
                scale = Vector3.one;
                var treeLocal = Matrix4x4.TRS(pos, rot, scale);

                foreach (var collider in proto.GetComponentsInChildren <Collider>())
                {
                    // Take into account layer mask
                    if (((1 << collider.gameObject.layer) & layerMask) != 0)
                    {
                        Matrix4x4 colliderMatrix = proto.transform.worldToLocalMatrix * collider.transform.localToWorldMatrix;

                        NavMeshBuildSource src = new NavMeshBuildSource();
                        src.area         = area;
                        src.component    = owner;
                        src.sourceObject = null;
                        if (SetCollider(collider, transform * treeLocal * colliderMatrix, ref src))
                        {
                            sources.Add(src);
                        }
                    }
                }
            }
        }
Пример #4
0
        private static void CollectSources(List<NavMeshBuildSource> sources, NavMeshModifier modifier, int area, NavMeshBuilder2dWrapper builder)
        {
            var collider = modifier.GetComponent<Collider2D>();
            if (collider == null)
            {
                return;
            }

            if (collider.usedByComposite)
            {
                collider = collider.GetComponent<CompositeCollider2D>();
            }

            var src = new NavMeshBuildSource();
            src.shape = NavMeshBuildSourceShape.Mesh;
            src.area = area;

            Mesh mesh;
            mesh = builder.GetMesh(collider);
            if (mesh == null)
            {
                Debug.Log($"{collider.name} mesh is null");
                return;
            }
            if (collider.attachedRigidbody)
            {
                src.transform = Matrix4x4.TRS(Vector3.Scale(collider.transform.position, builder.overrideVector), collider.transform.rotation, Vector3.one);
            }
            else
            {
                src.transform = Matrix4x4.identity;
            }
            src.sourceObject = mesh;
            sources.Add(src);
        }
 static public int constructor(IntPtr l)
 {
     try {
                     #if DEBUG
         var    method     = System.Reflection.MethodBase.GetCurrentMethod();
         string methodName = GetMethodName(method);
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.BeginSample(methodName);
                     #else
         Profiler.BeginSample(methodName);
                     #endif
                     #endif
         UnityEngine.AI.NavMeshBuildSource o;
         o = new UnityEngine.AI.NavMeshBuildSource();
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
             #if DEBUG
     finally {
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.EndSample();
                     #else
         Profiler.EndSample();
                     #endif
     }
             #endif
 }
Пример #6
0
        private void AppendModifierVolumes(ref List <NavMeshBuildSource> sources)
        {
            List <NavMeshModifierVolume> meshModifierVolumeList;

            if (this.m_CollectObjects == CollectObjects.Children)
            {
                meshModifierVolumeList = new List <NavMeshModifierVolume>((IEnumerable <NavMeshModifierVolume>)((Component)this).GetComponentsInChildren <NavMeshModifierVolume>());
                meshModifierVolumeList.RemoveAll((Predicate <NavMeshModifierVolume>)(x => !((Behaviour)x).get_isActiveAndEnabled()));
            }
            else
            {
                meshModifierVolumeList = NavMeshModifierVolume.activeModifiers;
            }
            foreach (NavMeshModifierVolume meshModifierVolume in meshModifierVolumeList)
            {
                if ((LayerMask.op_Implicit(this.m_LayerMask) & 1 << ((Component)meshModifierVolume).get_gameObject().get_layer()) != 0 && meshModifierVolume.AffectsAgentType(this.m_AgentTypeID))
                {
                    Vector3 vector3_1  = ((Component)meshModifierVolume).get_transform().TransformPoint(meshModifierVolume.center);
                    Vector3 lossyScale = ((Component)meshModifierVolume).get_transform().get_lossyScale();
                    Vector3 vector3_2;
                    ((Vector3) ref vector3_2).\u002Ector((float)meshModifierVolume.size.x * Mathf.Abs((float)lossyScale.x), (float)meshModifierVolume.size.y * Mathf.Abs((float)lossyScale.y), (float)meshModifierVolume.size.z * Mathf.Abs((float)lossyScale.z));
                    NavMeshBuildSource navMeshBuildSource = (NavMeshBuildSource)null;
                    ((NavMeshBuildSource) ref navMeshBuildSource).set_shape((NavMeshBuildSourceShape)5);
                    ((NavMeshBuildSource) ref navMeshBuildSource).set_transform(Matrix4x4.TRS(vector3_1, ((Component)meshModifierVolume).get_transform().get_rotation(), Vector3.get_one()));
                    ((NavMeshBuildSource) ref navMeshBuildSource).set_size(vector3_2);
                    ((NavMeshBuildSource) ref navMeshBuildSource).set_area(meshModifierVolume.area);
                    sources.Add(navMeshBuildSource);
                }
            }
        }
Пример #7
0
        static private void CollectTileSources(List <NavMeshBuildSource> sources, Tilemap tilemap, int area, NavMeshBuilder2dWrapper builder)
        {
            var bound = tilemap.cellBounds;

            var vec3int = new Vector3Int(0, 0, 0);

            var        size       = new Vector3(tilemap.layoutGrid.cellSize.x, tilemap.layoutGrid.cellSize.y, tilemap.layoutGrid.cellSize.y);
            Mesh       sharedMesh = null;
            Quaternion rot        = default;

            var src = new NavMeshBuildSource();

            src.shape = NavMeshBuildSourceShape.Mesh;
            src.area  = area;

            Mesh mesh;

            if (builder.useMeshPrefab != null)
            {
                sharedMesh = builder.useMeshPrefab.GetComponent <MeshFilter>().sharedMesh;
                size       = builder.useMeshPrefab.transform.localScale;
                rot        = builder.useMeshPrefab.transform.rotation;
            }
            for (int i = bound.xMin; i < bound.xMax; i++)
            {
                for (int j = bound.yMin; j < bound.yMax; j++)
                {
                    vec3int.x = i;
                    vec3int.y = j;
                    if (!tilemap.HasTile(vec3int))
                    {
                        continue;
                    }

                    if (!builder.overrideByGrid && tilemap.GetColliderType(vec3int) == Tile.ColliderType.Sprite)
                    {
                        mesh             = builder.GetMesh(tilemap.GetSprite(vec3int));
                        src.transform    = Matrix4x4.Translate(Vector3.Scale(tilemap.GetCellCenterWorld(vec3int), builder.overrideVector)) * tilemap.GetTransformMatrix(vec3int);
                        src.sourceObject = mesh;
                        sources.Add(src);
                    }
                    else if (builder.useMeshPrefab != null || (builder.overrideByGrid && builder.useMeshPrefab != null))
                    {
                        src.transform    = Matrix4x4.TRS(Vector3.Scale(tilemap.GetCellCenterWorld(vec3int), builder.overrideVector), rot, size);
                        src.sourceObject = sharedMesh;
                        sources.Add(src);
                    }
                    else //default to box
                    {
                        var boxsrc = new NavMeshBuildSource();
                        boxsrc.transform = Matrix4x4.Translate(Vector3.Scale(tilemap.GetCellCenterWorld(vec3int), builder.overrideVector));
                        boxsrc.shape     = NavMeshBuildSourceShape.Box;
                        boxsrc.size      = size;
                        boxsrc.area      = area;
                        sources.Add(boxsrc);
                    }
                }
            }
        }
Пример #8
0
        void AppendModifierVolumes(ref List <NavMeshBuildSource> sources)

        {
            // Modifiers

            List <NavMeshModifierVolume> modifiers;

            if (m_CollectObjects == CollectObjects.Children)

            {
                modifiers = new List <NavMeshModifierVolume>(GetComponentsInChildren <NavMeshModifierVolume>());

                modifiers.RemoveAll(x => !x.isActiveAndEnabled);
            }

            else

            {
                modifiers = NavMeshModifierVolume.activeModifiers;
            }



            foreach (var m in modifiers)

            {
                if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
                {
                    continue;
                }

                if (!m.AffectsAgentType(m_AgentTypeID))
                {
                    continue;
                }

                var mcenter = m.transform.TransformPoint(m.center);

                var scale = m.transform.lossyScale;

                var msize = new Vector3(m.size.x * Mathf.Abs(scale.x), m.size.y * Mathf.Abs(scale.y), m.size.z * Mathf.Abs(scale.z));



                var src = new NavMeshBuildSource();

                src.shape = NavMeshBuildSourceShape.ModifierBox;

                src.transform = Matrix4x4.TRS(mcenter, m.transform.rotation, Vector3.one);

                src.size = msize;

                src.area = m.area;

                sources.Add(src);
            }
        }
Пример #9
0
 static private NavMeshBuildSource BoxBoundSource(Bounds localBounds)
 {
     var src = new NavMeshBuildSource();
     src.transform = Matrix4x4.Translate(localBounds.center);
     src.shape = NavMeshBuildSourceShape.Box;
     src.size = localBounds.size;
     src.area = 0;
     return src;
 }
Пример #10
0
        public NavMeshBuildSource BoxSource(int size = 10)
        {
            var src = new NavMeshBuildSource();

            src.transform = transform.localToWorldMatrix;
            src.shape     = NavMeshBuildSourceShape.Box;
            src.size      = new Vector3(size, 0.1f, size);
            return(src);
        }
        public NavMeshBuildSource BoxSource(float sizex, float sizey)
        {
            var src = new NavMeshBuildSource();

            src.transform = transform.localToWorldMatrix;
            src.shape     = NavMeshBuildSourceShape.Box;
            src.size      = new Vector3(sizex, 0.1f, sizey);
            src.area      = 0;
            return(src);
        }
        private static bool SetCollider(Collider collider, Matrix4x4 transform, ref NavMeshBuildSource source)
        {
            BoxCollider     box;
            SphereCollider  sphere;
            CapsuleCollider capsule;
            MeshCollider    mesh;

            if (collider.TryCast(out sphere))
            {
                // Size is supposed to be radius, but it's actually diameter
                source.shape     = NavMeshBuildSourceShape.Sphere;
                source.size      = sphere.radius * 2 * Vector3.one;
                source.transform = transform * Matrix4x4.Translate(sphere.center);
                return(true);
            }
            else if (collider.TryCast(out capsule))
            {
                // Capsule is expected to be in y-axis
                var rot = Matrix4x4.identity;
                if (capsule.direction == 0)
                {
                    rot = Matrix4x4.Rotate(Quaternion.AngleAxis(90, Vector3.forward));
                }
                else if (capsule.direction == 2)
                {
                    rot = Matrix4x4.Rotate(Quaternion.AngleAxis(90, Vector3.right));
                }

                // Size is supposed to be (radius, height, radius), but it's actually diameter instead of radius
                source.shape     = NavMeshBuildSourceShape.Capsule;
                source.size      = new Vector3(capsule.radius * 2, capsule.height, capsule.radius * 2);
                source.transform = transform * Matrix4x4.Translate(capsule.center) * rot;
                return(true);
            }
            else if (collider.TryCast(out box))
            {
                source.shape     = NavMeshBuildSourceShape.Box;
                source.size      = box.size;
                source.transform = transform * Matrix4x4.Translate(box.center);
                return(true);
            }
            else if (collider.TryCast(out mesh))
            {
                source.shape        = NavMeshBuildSourceShape.Mesh;
                source.size         = Vector3.one;
                source.transform    = transform;
                source.sourceObject = mesh.sharedMesh;
                return(true);
            }
            else
            {
                Debug.LogWarning("NavMeshSource tree collider not supported: " + collider.GetType().Name);
                return(false);
            }
        }
 static public int constructor(IntPtr l)
 {
     try {
         UnityEngine.AI.NavMeshBuildSource o;
         o = new UnityEngine.AI.NavMeshBuildSource();
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #14
0
        private Bounds CalculateWorldBounds(List <NavMeshBuildSource> sources)
        {
            Matrix4x4 matrix4x4 = Matrix4x4.TRS(((Component)this).get_transform().get_position(), ((Component)this).get_transform().get_rotation(), Vector3.get_one());

            matrix4x4 = ((Matrix4x4) ref matrix4x4).get_inverse();
            Bounds bounds = (Bounds)null;

            using (List <NavMeshBuildSource> .Enumerator enumerator = sources.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    NavMeshBuildSource current = enumerator.Current;
                    switch ((int)((NavMeshBuildSource) ref current).get_shape())
                    {
        private void CollectTileSources(List <NavMeshBuildSource> sources, Tilemap tilemap, int area)
        {
            var        bound   = tilemap.cellBounds;
            var        vec3int = new Vector3Int(0, 0, 0);
            var        size    = new Vector3(tilemap.layoutGrid.cellSize.x, tilemap.layoutGrid.cellSize.y, tilemap.layoutGrid.cellSize.y);
            Mesh       mesh    = null;
            Quaternion rot     = default;

            if (m_UseMeshPrefab != null)
            {
                mesh = m_UseMeshPrefab.GetComponent <MeshFilter>().sharedMesh;
                size = m_UseMeshPrefab.transform.localScale;
                rot  = m_UseMeshPrefab.transform.rotation;
            }
            for (int i = bound.xMin; i < bound.xMax; i++)
            {
                for (int j = bound.yMin; j < bound.yMax; j++)
                {
                    vec3int.x = i;
                    vec3int.y = j;
                    if (!tilemap.HasTile(vec3int))
                    {
                        continue;
                    }
                    if (mesh != null)
                    {
                        var src = new NavMeshBuildSource();
                        src.transform    = Matrix4x4.TRS(tilemap.GetCellCenterWorld(vec3int), rot, size);
                        src.shape        = NavMeshBuildSourceShape.Mesh;
                        src.sourceObject = m_UseMeshPrefab.GetComponent <MeshFilter>().sharedMesh;
                        src.area         = area;
                        sources.Add(src);
                    }
                    else
                    {
                        var src = new NavMeshBuildSource();
                        src.transform = Matrix4x4.Translate(tilemap.GetCellCenterWorld(vec3int));
                        src.shape     = NavMeshBuildSourceShape.Box;
                        src.size      = size;
                        src.area      = area;
                        sources.Add(src);
                    }
                }
            }
        }
        private void CollectGridSources(List <NavMeshBuildSource> sources, Tilemap tilemap, int area)
        {
            var bound   = tilemap.cellBounds;
            var vec3int = new Vector3Int(0, 0, 0);
            var size    = new Vector3(tilemap.layoutGrid.cellSize.x, .1f, tilemap.layoutGrid.cellSize.y);

            for (int i = bound.xMin; i < bound.xMax; i++)
            {
                for (int j = bound.yMin; j < bound.yMax; j++)
                {
                    vec3int.x = i;
                    vec3int.y = j;
                    if (tilemap.HasTile(vec3int))
                    {
                        var src = new NavMeshBuildSource();
                        src.transform = Matrix4x4.Translate(tilemap.GetCellCenterWorld(vec3int));
                        src.shape     = NavMeshBuildSourceShape.Box;
                        src.size      = size;
                        src.area      = area;
                        sources.Add(src);
                    }
                }
            }
        }
Пример #17
0
        void CollectChildMeshes(List <NavMeshBuildSource> sources)
        {
#if INCLUDE_PHYSICS_MODULE
            if (m_UseGeometry == NavMeshCollectGeometry.PhysicsColliders)
            {
                GetComponentsInChildren(k_MeshColliders);
                GetComponentsInChildren(k_SphereColliders);
                GetComponentsInChildren(k_BoxColliders);
                GetComponentsInChildren(k_CapsuleColliders);


                foreach (var currentCollider in k_MeshColliders)
                {
                    if ((currentCollider.gameObject.layer | m_LayerMask.value) == 0)
                    {
                        continue;
                    }

                    var buildSource = new NavMeshBuildSource
                    {
                        shape        = NavMeshBuildSourceShape.Mesh,
                        area         = m_DefaultArea,
                        component    = currentCollider,
                        sourceObject = currentCollider.sharedMesh,
                        size         = Vector3.zero,
                        transform    = currentCollider.transform.localToWorldMatrix
                    };
                    sources.Add(buildSource);
                }
                foreach (var currentCollider in k_SphereColliders)
                {
                    if ((currentCollider.gameObject.layer | m_LayerMask.value) == 0)
                    {
                        continue;
                    }

                    var buildSource = new NavMeshBuildSource
                    {
                        shape     = NavMeshBuildSourceShape.Sphere,
                        area      = m_DefaultArea,
                        component = currentCollider,
                        size      = Vector3.one * currentCollider.radius * 2.0f,
                        transform = currentCollider.transform.localToWorldMatrix * Matrix4x4.Translate(currentCollider.center)
                    };
                    sources.Add(buildSource);
                }
                foreach (var currentCollider in k_BoxColliders)
                {
                    if ((currentCollider.gameObject.layer | m_LayerMask.value) == 0)
                    {
                        continue;
                    }

                    var buildSource = new NavMeshBuildSource
                    {
                        shape     = NavMeshBuildSourceShape.Box,
                        area      = m_DefaultArea,
                        component = currentCollider,
                        size      = currentCollider.size,
                        transform = currentCollider.transform.localToWorldMatrix * Matrix4x4.Translate(currentCollider.center)
                    };
                    sources.Add(buildSource);
                }
                foreach (var currentCollider in k_CapsuleColliders)
                {
                    if ((currentCollider.gameObject.layer | m_LayerMask.value) == 0)
                    {
                        continue;
                    }

                    var radius      = currentCollider.radius * 2.0f;
                    var buildSource = new NavMeshBuildSource
                    {
                        shape     = NavMeshBuildSourceShape.Capsule,
                        area      = m_DefaultArea,
                        component = currentCollider,
                        size      = new Vector3(radius, currentCollider.height, radius),
                        transform = currentCollider.transform.localToWorldMatrix * Matrix4x4.Translate(currentCollider.center)
                    };
                    sources.Add(buildSource);
                }
                k_MeshColliders.Clear();
                k_SphereColliders.Clear();
                k_BoxColliders.Clear();
                k_CapsuleColliders.Clear();
            }
            else // NavMeshCollectGeometry.RenderMeshes
#endif
            {
                GetComponentsInChildren(k_MeshFilters);

                foreach (var currentFilter in k_MeshFilters)
                {
                    if ((currentFilter.gameObject.layer | m_LayerMask.value) == 0)
                    {
                        continue;
                    }

                    var buildSource = new NavMeshBuildSource
                    {
                        shape        = NavMeshBuildSourceShape.Mesh,
                        area         = m_DefaultArea,
                        component    = currentFilter,
                        sourceObject = currentFilter.sharedMesh,
                        size         = Vector3.zero,
                        transform    = currentFilter.transform.localToWorldMatrix
                    };
                    sources.Add(buildSource);
                }

                k_MeshFilters.Clear();
            }
        }
Пример #18
0
        void AddObstacles(List <NavMeshBuildSource> sources)
        {
            // ---------Terrain trees-- - ------

            Terrain     terrain     = Terrain.activeTerrain;
            TerrainData terrainData = terrain.terrainData;
            Vector3     size        = Terrain.activeTerrain.terrainData.size;
            Vector3     terrainPos  = terrain.GetPosition();
            //Debug.Log("terrainPos = " + terrainPos);
            int treesArea = NavMesh.GetAreaFromName("Not Walkable");

            if (treesArea < 0)
            {
                Debug.LogError("Unrecognized area name! The default area will be used instead.");
                treesArea = 0;
            }

            TreePrototype[] treePrototypes = terrainData.treePrototypes;
            TreeInstance[]  treeInstances  = terrainData.treeInstances;
            for (int i = 0; i < treeInstances.Length; i++)
            {
                //treeInstances[i] is the current ACTUAL tree we're going over.
                //the tree prototype is the "template" used by this tree.
                TreePrototype   prototype = treePrototypes[treeInstances[i].prototypeIndex];
                GameObject      prefab    = prototype.prefab;
                NavMeshObstacle obstacle  = prefab.GetComponent <NavMeshObstacle>();
                if (obstacle == null)
                {
                    continue;
                }

                //Debug.Log("treeInstances[" + i + "] info:\n" + treeInstances[i].position + " " + treeInstances[i].rotation + " " + treeInstances[i].widthScale + " " + treeInstances[i].heightScale);
                Vector3 worldTreePos = terrainPos + Vector3.Scale(treeInstances[i].position, size)
                                       + obstacle.center;
                Quaternion worldTreeRot   = Quaternion.Euler(0, treeInstances[i].rotation * Mathf.Rad2Deg, 0);
                Vector3    worldTreeScale = new Vector3(treeInstances[i].widthScale, treeInstances[i].heightScale, treeInstances[i].widthScale);
                //Debug.Log("CREATED MATRIX FOR TRS:\nworldTreePos = " + worldTreePos + "\nworldTreeRot = " + worldTreeRot + "\nworldTreeScale = " + worldTreeScale);

                NavMeshBuildSource src = new NavMeshBuildSource();
                src.transform = Matrix4x4.TRS(worldTreePos, worldTreeRot, worldTreeScale);

                switch (obstacle.shape)
                {
                case NavMeshObstacleShape.Capsule:
                    src.shape = NavMeshBuildSourceShape.Capsule;

                    //Unity 2019.2.0f1: BUG!! navMeshObstacle.height returns exactly HALF of the actual height of the obstacle.
                    //Use the size property instead.
                    src.size = obstacle.size;
                    break;

                case NavMeshObstacleShape.Box:
                    src.shape = NavMeshBuildSourceShape.Box;
                    src.size  = obstacle.size;
                    break;

                default:
                    Debug.LogError("Unsupported type of " + typeof(NavMeshObstacleShape).Name
                                   + " for the building of the " + typeof(NavMeshSurface).Name + "! (" + obstacle.shape + ")");
                    break;
                }
                src.size = Vector3.Scale(src.size, prefab.transform.localScale);
                //Debug.Log("src.size = " + src.size);
                src.area = treesArea;
                sources.Add(src);
            }

            // --- --- --- End of Terrain trees --- --- ---
        }
Пример #19
0
        List <NavMeshBuildSource> CollectSources()
        {
            var sources = new List <NavMeshBuildSource>();
            var markups = new List <NavMeshBuildMarkup>();

            List <NavMeshModifier> modifiers;

            if (m_CollectObjects == CollectObjects.Children)
            {
                modifiers = new List <NavMeshModifier>(GetComponentsInChildren <NavMeshModifier>());
                modifiers.RemoveAll(x => !x.isActiveAndEnabled);
            }
            else
            {
                modifiers = NavMeshModifier.activeModifiers;
            }

            foreach (var m in modifiers)
            {
                if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
                {
                    continue;
                }
                if (!m.AffectsAgentType(m_AgentTypeID))
                {
                    continue;
                }
                var markup = new NavMeshBuildMarkup();
                markup.root            = m.transform;
                markup.overrideArea    = m.overrideArea;
                markup.area            = m.area;
                markup.ignoreFromBuild = m.ignoreFromBuild;
                markups.Add(markup);
            }

#if UNITY_EDITOR
            if (!EditorApplication.isPlaying)
            {
                if (m_CollectObjects == CollectObjects.All)
                {
                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
                else if (m_CollectObjects == CollectObjects.Children)
                {
                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
                else if (m_CollectObjects == CollectObjects.Volume)
                {
                    Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                    var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));

                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
            }
            else
#endif
            {
                if (m_CollectObjects == CollectObjects.All)
                {
                    NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
                else if (m_CollectObjects == CollectObjects.Children)
                {
                    NavMeshBuilder.CollectSources(transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
                else if (m_CollectObjects == CollectObjects.Volume)
                {
                    Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                    var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
                    NavMeshBuilder.CollectSources(worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
            }

            // --- --- --- Terrain trees --- --- ---

            Terrain[] terrains = GameObject.FindObjectsOfType <Terrain>();

            if (terrains.Length > 0)
            {
                foreach (Terrain item in terrains)
                {
                    Terrain     terrain     = item.GetComponent <Terrain>();
                    TerrainData terrainData = terrain.terrainData;
                    Vector3     size        = Terrain.activeTerrain.terrainData.size;
                    Vector3     terrainPos  = terrain.GetPosition();
                    if (m_Debug)
                    {
                        Debug.Log("terrainPos = " + terrainPos);
                    }
                    int treesArea = NavMesh.GetAreaFromName("Not Walkable");
                    if (treesArea < 0)
                    {
                        Debug.LogError("Unrecognized area name! The default area will be used instead.");
                        treesArea = 0;
                    }

                    TreePrototype[] treePrototypes = terrainData.treePrototypes;
                    TreeInstance[]  treeInstances  = terrainData.treeInstances;
                    if (m_Debug)
                    {
                        Debug.Log("trees found = " + treeInstances.Length);
                    }
                    for (int i = 0; i < treeInstances.Length; i++)
                    {
                        //treeInstances[i] is the current ACTUAL tree we're going over.
                        //the tree prototype is the "template" used by this tree.
                        TreePrototype prototype = treePrototypes[treeInstances[i].prototypeIndex];
                        GameObject    prefab    = prototype.prefab;

                        NavMeshObstacle obstacle     = prefab.GetComponent <NavMeshObstacle>();
                        Collider        collider     = prefab.GetComponentInChildren <Collider>();
                        bool            obstacleBool = obstacle != null && (m_SelectionType == SelectionType.All || m_SelectionType == SelectionType.Obstacles);
                        bool            colliderBool = collider != null && (m_SelectionType == SelectionType.All || m_SelectionType == SelectionType.Colliders);
                        if (obstacleBool || colliderBool)
                        {
                            Vector3 center = Vector3.zero;
                            if (obstacle != null)
                            {
                                center = obstacle.center;
                            }
                            else if (collider != null)
                            {
                                center = collider.bounds.center;
                            }

                            if (m_Debug)
                            {
                                Debug.Log("treeInstances[" + i + "] info:\n" + treeInstances[i].position + " " + treeInstances[i].rotation + " " + treeInstances[i].widthScale + " " + treeInstances[i].heightScale);
                            }
                            Vector3 worldTreePos = terrainPos + Vector3.Scale(treeInstances[i].position, size) + center;

                            Quaternion worldTreeRot   = Quaternion.Euler(0, treeInstances[i].rotation * Mathf.Rad2Deg, 0);
                            Vector3    worldTreeScale = new Vector3(treeInstances[i].widthScale, treeInstances[i].heightScale, treeInstances[i].widthScale);
                            if (m_Debug)
                            {
                                Debug.Log("CREATED MATRIX FOR TRS:\nworldTreePos = " + worldTreePos + "\nworldTreeRot = " + worldTreeRot + "\nworldTreeScale = " + worldTreeScale);
                            }

                            NavMeshBuildSource src = new NavMeshBuildSource();
                            src.transform = Matrix4x4.TRS(worldTreePos, worldTreeRot, worldTreeScale);

                            if (obstacleBool)
                            {
                                switch (obstacle.shape)
                                {
                                case NavMeshObstacleShape.Capsule:
                                    src.shape = NavMeshBuildSourceShape.Capsule;

                                    //Unity 2019.2.0f1: BUG!! navMeshObstacle.height returns exactly HALF of the actual height of the obstacle.
                                    //Use the size property instead.
                                    src.size = obstacle.size;
                                    break;

                                case NavMeshObstacleShape.Box:
                                    src.shape = NavMeshBuildSourceShape.Box;
                                    src.size  = obstacle.size;
                                    break;

                                default:
                                    Debug.LogError("Unsupported type of " + typeof(NavMeshObstacleShape).Name
                                                   + " for the building of the " + typeof(NavMeshSurface).Name + "! (" + obstacle.shape + ")");
                                    break;
                                }
                            }
                            else if (colliderBool)
                            {
                                if (collider is CapsuleCollider)
                                {
                                    src.shape = NavMeshBuildSourceShape.Capsule;

                                    //Unity 2019.2.0f1: BUG!! navMeshObstacle.height returns exactly HALF of the actual height of the obstacle.
                                    //Use the size property instead.
                                    src.size = ((CapsuleCollider)collider).radius * Vector3.one * m_ColliderTolerance;
                                }
                                else if (collider is BoxCollider)
                                {
                                    src.shape = NavMeshBuildSourceShape.Box;
                                    src.size  = collider.bounds.size * m_ColliderTolerance;
                                }
                                else
                                {
                                    Debug.LogError("Unsupported type of " + typeof(NavMeshObstacleShape).Name
                                                   + " for the building of the " + typeof(NavMeshSurface).Name + "! (" + obstacle.shape + ")");
                                }
                            }

                            // Scale size
                            src.size = Vector3.Scale(src.size, prefab.transform.localScale);
                            if (m_Debug)
                            {
                                Debug.Log("src.size = " + src.size);
                            }
                            src.area = treesArea;
                            sources.Add(src);
                        }
                    }
                }
            }

            // --- --- --- End of Terrain trees --- --- ---

            if (m_IgnoreNavMeshAgent)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshAgent>() != null));
            }

            if (m_IgnoreNavMeshObstacle)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshObstacle>() != null));
            }

            AppendModifierVolumes(ref sources);

            return(sources);
        }
Пример #20
0
        List <NavMeshBuildSource> CollectSources()
        {
            var sources = new List <NavMeshBuildSource>();
            var markups = new List <NavMeshBuildMarkup>();

            List <NavMeshModifier> modifiers;

            if (m_CollectObjects == CollectObjects2d.Children)
            {
                modifiers = new List <NavMeshModifier>(GetComponentsInChildren <NavMeshModifier>());
                modifiers.RemoveAll(x => !x.isActiveAndEnabled);
            }
            else
            {
                modifiers = NavMeshModifier.activeModifiers;
            }

            foreach (var m in modifiers)
            {
                if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
                {
                    continue;
                }
                if (!m.AffectsAgentType(m_AgentTypeID))
                {
                    continue;
                }
                var markup = new NavMeshBuildMarkup();
                markup.root            = m.transform;
                markup.overrideArea    = m.overrideArea;
                markup.area            = m.area;
                markup.ignoreFromBuild = m.ignoreFromBuild;
                markups.Add(markup);
            }

            if (m_CollectObjects == CollectObjects2d.All)
            {
                NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
            }
            else if (m_CollectObjects == CollectObjects2d.Children)
            {
                NavMeshBuilder.CollectSources(transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
            }
            else if (m_CollectObjects == CollectObjects2d.Volume)
            {
                Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
                NavMeshBuilder.CollectSources(worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
            }
            if (m_CollectObjects == CollectObjects2d.Grid)
            {
                NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                var grid    = FindObjectOfType <Grid>();
                var colider = grid.GetComponentInChildren <TilemapCollider2D>();
                var tilemap = colider.GetComponent <Tilemap>();
                var bound   = tilemap.cellBounds;
                var vec3int = new Vector3Int(0, 0, 0);
                var size    = new Vector3(tilemap.layoutGrid.cellSize.x, .1f, tilemap.layoutGrid.cellSize.y);
                for (int i = bound.xMin; i < bound.xMax; i++)
                {
                    for (int j = bound.yMin; j < bound.yMax; j++)
                    {
                        vec3int.x = i;
                        vec3int.y = j;
                        if (!tilemap.HasTile(vec3int))
                        {
                            var src = new NavMeshBuildSource();
                            src.transform = Matrix4x4.Translate(tilemap.GetCellCenterWorld(vec3int));
                            Debug.Log(src.transform);
                            src.shape = NavMeshBuildSourceShape.Box;
                            src.size  = size;
                            sources.Add(src);
                        }
                    }
                }
                //var composite = grid.GetComponentInChildren<CompositeCollider2D>();
                //var count = composite.pathCount;
                ////TODO: 100
                //var points = new Vector2[100];
                //for (int i = 0; i < count; i++)
                //{
                //    var len = composite.GetPath(i, points);
                //    for (int j = 0; j < len; j++)
                //    {
                //        Debug.Log("Box:" + i + "-" + j + " = [" + points[j].x + ";" + points[j].y + "]");
                //    }
                //}
            }

            //sources.Add(BoxSource(40));

            if (m_IgnoreNavMeshAgent)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshAgent>() != null));
            }

            if (m_IgnoreNavMeshObstacle)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshObstacle>() != null));
            }

            AppendModifierVolumes(ref sources);

            return(sources);
        }