示例#1
0
        static Bounds GetBounds(PF.Vector3[] points, Vector3 right, Vector3 up, Vector3 forward, Vector3 origin, float minimumHeight)
        {
            if (points == null || points.Length == 0)
            {
                return(new Bounds());
            }
            float miny = points[0].y, maxy = points[0].y;

            for (int i = 0; i < points.Length; i++)
            {
                miny = Mathf.Min(miny, points[i].y);
                maxy = Mathf.Max(maxy, points[i].y);
            }
            var extraHeight = Mathf.Max(minimumHeight - (maxy - miny), 0) * 0.5f;

            miny -= extraHeight;
            maxy += extraHeight;

            Vector3 min = right * points[0].x + up * points[0].y + forward * points[0].z;
            Vector3 max = min;

            for (int i = 0; i < points.Length; i++)
            {
                var p  = right * points[i].x + forward * points[i].z;
                var p1 = p + up * miny;
                var p2 = p + up * maxy;
                min = Vector3.Min(min, p1);
                min = Vector3.Min(min, p2);
                max = Vector3.Max(max, p1);
                max = Vector3.Max(max, p2);
            }
            return(new Bounds((min + max) * 0.5F + origin, max - min));
        }
        protected override void OnPointCloudUpdated(PointCloudUpdatedEventArgs e)
        {
            base.OnPointCloudUpdated(e);

            if (!_indices.IsCreated)
            {
                InitializeIndices();
            }

            var vertices = new NativeArray <Vertex>(e.count, Allocator.Temp);

            for (var i = 0; i < vertices.Length; i++)
            {
                vertices[i] = new Vertex(e.pointCloud.points[e.startIndex + i], e.pointCloud.colors[e.startIndex + i]);

                _boundsMin = Vector3.Min(_boundsMin, vertices[i].position);
                _boundsMax = Vector3.Max(_boundsMax, vertices[i].position);
            }

            _mesh.SetVertexBufferParams(pointCloud.count, _vertexAttributeDescriptors);
            _mesh.SetVertexBufferData(vertices, 0, e.startIndex, e.count, 0);

            _mesh.SetIndexBufferParams(pointCloud.count, IndexFormat.UInt32);
            _mesh.SetIndexBufferData(_indices, e.startIndex, e.startIndex, e.count);
            _mesh.SetSubMesh(0, new SubMeshDescriptor(0, pointCloud.count, MeshTopology.Points));

            var bounds = new Bounds();

            bounds.SetMinMax(_boundsMin, _boundsMax);
            _mesh.bounds = bounds;
        }
示例#3
0
        public static Bounds InverseTransform(this GraphTransform self, Bounds bounds)
        {
            if (self.onlyTranslational)
            {
                return(new Bounds(bounds.center - self.translation.ToUnityV3(), bounds.size));
            }

            var corners = PF.ArrayPool <Vector3> .Claim(8);

            var extents = bounds.extents;

            corners[0] = self.InverseTransform(bounds.center + new Vector3(extents.x, extents.y, extents.z));
            corners[1] = self.InverseTransform(bounds.center + new Vector3(extents.x, extents.y, -extents.z));
            corners[2] = self.InverseTransform(bounds.center + new Vector3(extents.x, -extents.y, extents.z));
            corners[3] = self.InverseTransform(bounds.center + new Vector3(extents.x, -extents.y, -extents.z));
            corners[4] = self.InverseTransform(bounds.center + new Vector3(-extents.x, extents.y, extents.z));
            corners[5] = self.InverseTransform(bounds.center + new Vector3(-extents.x, extents.y, -extents.z));
            corners[6] = self.InverseTransform(bounds.center + new Vector3(-extents.x, -extents.y, extents.z));
            corners[7] = self.InverseTransform(bounds.center + new Vector3(-extents.x, -extents.y, -extents.z));

            var min = corners[0];
            var max = corners[0];

            for (int i = 1; i < 8; i++)
            {
                min = Vector3.Min(min, corners[i]);
                max = Vector3.Max(max, corners[i]);
            }
            PF.ArrayPool <Vector3> .Release(ref corners);

            return(new Bounds((min + max) * 0.5f, max - min));
        }
    void OnValidate()
    {
        boundaryDistance = Vector3.Max(boundaryDistance, Vector3.zero);
        float maxInner = Mathf.Min(
            Mathf.Min(boundaryDistance.x, boundaryDistance.y), boundaryDistance.z
            );

        innerDistance        = Mathf.Min(innerDistance, maxInner);
        innerFalloffDistance =
            Mathf.Max(Mathf.Min(innerFalloffDistance, maxInner), innerDistance);
        outerFalloffDistance = Mathf.Max(outerFalloffDistance, outerDistance);

        innerFalloffFactor = 1f / (innerFalloffDistance - innerDistance);
        outerFalloffFactor = 1f / (outerFalloffDistance - outerDistance);
    }
示例#5
0
        public override void OnInspectorGUI(NavGraph target)
        {
            var graph = target as RecastGraph;

            bool preEnabled = GUI.enabled;

            System.Int64 estWidth = Mathf.RoundToInt(Mathf.Ceil(graph.forcedBoundsSize.x / graph.cellSize));
            System.Int64 estDepth = Mathf.RoundToInt(Mathf.Ceil(graph.forcedBoundsSize.z / graph.cellSize));

            // Show a warning if the number of voxels is too large
            if (estWidth * estDepth >= 1024 * 1024 || estDepth >= 1024 * 1024 || estWidth >= 1024 * 1024)
            {
                GUIStyle helpBox = GUI.skin.FindStyle("HelpBox") ?? GUI.skin.FindStyle("Box");

                Color preColor = GUI.color;
                if (estWidth * estDepth >= 2048 * 2048 || estDepth >= 2048 * 2048 || estWidth >= 2048 * 2048)
                {
                    GUI.color = Color.red;
                }
                else
                {
                    GUI.color = Color.yellow;
                }

                GUILayout.Label("Warning : Might take some time to calculate", helpBox);
                GUI.color = preColor;
            }

            GUI.enabled = false;
            EditorGUILayout.LabelField(new GUIContent("Width (voxels)", "Based on the cell size and the bounding box"), new GUIContent(estWidth.ToString()));

            EditorGUILayout.LabelField(new GUIContent("Depth (voxels)", "Based on the cell size and the bounding box"), new GUIContent(estDepth.ToString()));
            GUI.enabled = preEnabled;

            graph.cellSize = EditorGUILayout.FloatField(new GUIContent("Cell Size", "Size of one voxel in world units"), graph.cellSize);
            if (graph.cellSize < 0.001F)
            {
                graph.cellSize = 0.001F;
            }

            graph.useTiles = (UseTiles)EditorGUILayout.EnumPopup("Use Tiles", graph.useTiles ? UseTiles.UseTiles : UseTiles.DontUseTiles) == UseTiles.UseTiles;

            if (graph.useTiles)
            {
                EditorGUI.indentLevel++;
                graph.editorTileSize = EditorGUILayout.IntField(new GUIContent("Tile Size", "Size in voxels of a single tile.\n" +
                                                                               "This is the width of the tile.\n" +
                                                                               "\n" +
                                                                               "A large tile size can be faster to initially scan (but beware of out of memory issues if you try with a too large tile size in a large world)\n" +
                                                                               "smaller tile sizes are (much) faster to update.\n" +
                                                                               "\n" +
                                                                               "Different tile sizes can affect the quality of paths. It is often good to split up huge open areas into several tiles for\n" +
                                                                               "better quality paths, but too small tiles can lead to effects looking like invisible obstacles."), graph.editorTileSize);
                EditorGUI.indentLevel--;
            }

            graph.minRegionSize = EditorGUILayout.FloatField(new GUIContent("Min Region Size", "Small regions will be removed. In square world units"), graph.minRegionSize);

            graph.walkableHeight = EditorGUILayout.FloatField(new GUIContent("Walkable Height", "Minimum distance to the roof for an area to be walkable"), graph.walkableHeight);
            graph.walkableHeight = Mathf.Max(graph.walkableHeight, 0);

            graph.walkableClimb = EditorGUILayout.FloatField(new GUIContent("Walkable Climb", "How high can the character climb"), graph.walkableClimb);

            // A walkableClimb higher than this can cause issues when generating the navmesh since then it can in some cases
            // Both be valid for a character to walk under an obstacle and climb up on top of it (and that cannot be handled with a navmesh without links)
            if (graph.walkableClimb >= graph.walkableHeight)
            {
                graph.walkableClimb = graph.walkableHeight;
                EditorGUILayout.HelpBox("Walkable climb should be less than walkable height. Clamping to " + graph.walkableHeight + ".", MessageType.Warning);
            }
            else if (graph.walkableClimb < 0)
            {
                graph.walkableClimb = 0;
            }

            graph.characterRadius = EditorGUILayout.FloatField(new GUIContent("Character Radius", "Radius of the character. It's good to add some margin.\nIn world units."), graph.characterRadius);
            graph.characterRadius = Mathf.Max(graph.characterRadius, 0);

            if (graph.characterRadius < graph.cellSize * 2)
            {
                EditorGUILayout.HelpBox("For best navmesh quality, it is recommended to keep the character radius at least 2 times as large as the cell size. Smaller cell sizes will give you higher quality navmeshes, but it will take more time to scan the graph.", MessageType.Warning);
            }

            graph.maxSlope      = EditorGUILayout.Slider(new GUIContent("Max Slope", "Approximate maximum slope"), graph.maxSlope, 0F, 90F);
            graph.maxEdgeLength = EditorGUILayout.FloatField(new GUIContent("Max Border Edge Length", "Maximum length of one border edge in the completed navmesh before it is split. A lower value can often yield better quality graphs, but don't use so low values so that you get a lot of thin triangles."), graph.maxEdgeLength);
            graph.maxEdgeLength = graph.maxEdgeLength < graph.cellSize ? graph.cellSize : graph.maxEdgeLength;

            graph.contourMaxError = EditorGUILayout.FloatField(new GUIContent("Max Edge Error", "Amount of simplification to apply to edges.\nIn world units."), graph.contourMaxError);

            graph.rasterizeTerrain = EditorGUILayout.Toggle(new GUIContent("Rasterize Terrain", "Should a rasterized terrain be included"), graph.rasterizeTerrain);
            if (graph.rasterizeTerrain)
            {
                EditorGUI.indentLevel++;
                graph.rasterizeTrees = EditorGUILayout.Toggle(new GUIContent("Rasterize Trees", "Rasterize tree colliders on terrains. " +
                                                                             "If the tree prefab has a collider, that collider will be rasterized. " +
                                                                             "Otherwise a simple box collider will be used and the script will " +
                                                                             "try to adjust it to the tree's scale, it might not do a very good job though so " +
                                                                             "an attached collider is preferable."), graph.rasterizeTrees);
                if (graph.rasterizeTrees)
                {
                    EditorGUI.indentLevel++;
                    graph.colliderRasterizeDetail = EditorGUILayout.FloatField(new GUIContent("Collider Detail", "Controls the detail of the generated collider meshes. " +
                                                                                              "Increasing does not necessarily yield better navmeshes, but lowering will speed up scan.\n" +
                                                                                              "Spheres and capsule colliders will be converted to meshes in order to be able to rasterize them, a higher value will increase the number of triangles in those meshes."), graph.colliderRasterizeDetail);
                    EditorGUI.indentLevel--;
                }

                graph.terrainSampleSize = EditorGUILayout.IntField(new GUIContent("Terrain Sample Size", "Size of terrain samples. A lower value is better, but slower"), graph.terrainSampleSize);
                graph.terrainSampleSize = graph.terrainSampleSize < 1 ? 1 : graph.terrainSampleSize;                //Clamp to at least 1
                EditorGUI.indentLevel--;
            }

            graph.rasterizeMeshes    = EditorGUILayout.Toggle(new GUIContent("Rasterize Meshes", "Should meshes be rasterized and used for building the navmesh"), graph.rasterizeMeshes);
            graph.rasterizeColliders = EditorGUILayout.Toggle(new GUIContent("Rasterize Colliders", "Should colliders be rasterized and used for building the navmesh"), graph.rasterizeColliders);
            if (graph.rasterizeColliders)
            {
                EditorGUI.indentLevel++;
                graph.colliderRasterizeDetail = EditorGUILayout.FloatField(new GUIContent("Collider Detail", "Controls the detail of the generated collider meshes. " +
                                                                                          "Increasing does not necessarily yield better navmeshes, but lowering will speed up scan.\n" +
                                                                                          "Spheres and capsule colliders will be converted to meshes in order to be able to rasterize them, a higher value will increase the number of triangles in those meshes."), graph.colliderRasterizeDetail);
                EditorGUI.indentLevel--;
            }

            if (graph.rasterizeMeshes && graph.rasterizeColliders)
            {
                EditorGUILayout.HelpBox("You are rasterizing both meshes and colliders, this might just be duplicating the work that is done if the colliders and meshes are similar in shape. You can use the RecastMeshObj component" +
                                        " to always include some specific objects regardless of what the above settings are set to.", MessageType.Info);
            }

            Separator();

            graph.forcedBoundsCenter = EditorGUILayout.Vector3Field("Center", graph.forcedBoundsCenter);
            graph.forcedBoundsSize   = EditorGUILayout.Vector3Field("Size", graph.forcedBoundsSize);
            // Make sure the bounding box is not infinitely thin along any axis
            graph.forcedBoundsSize = Vector3.Max(graph.forcedBoundsSize, Vector3.one * 0.001f);
            graph.rotation         = EditorGUILayout.Vector3Field("Rotation", graph.rotation);

            if (GUILayout.Button(new GUIContent("Snap bounds to scene", "Will snap the bounds of the graph to exactly contain all meshes that the bounds currently touches")))
            {
                graph.SnapForceBoundsToScene();
                GUI.changed = true;
            }

            Separator();

            EditorGUILayout.HelpBox("Objects contained in any of these masks will be rasterized", MessageType.None);
            graph.mask     = EditorGUILayoutx.LayerMaskField("Layer Mask", graph.mask);
            tagMaskFoldout = EditorGUILayoutx.UnityTagMaskList(new GUIContent("Tag Mask"), tagMaskFoldout, graph.tagMask);

            Separator();

            GUILayout.BeginHorizontal();
            GUILayout.Space(18);
            graph.showMeshSurface     = GUILayout.Toggle(graph.showMeshSurface, new GUIContent("Show surface", "Toggles gizmos for drawing the surface of the mesh"), EditorStyles.miniButtonLeft);
            graph.showMeshOutline     = GUILayout.Toggle(graph.showMeshOutline, new GUIContent("Show outline", "Toggles gizmos for drawing an outline of the nodes"), EditorStyles.miniButtonMid);
            graph.showNodeConnections = GUILayout.Toggle(graph.showNodeConnections, new GUIContent("Show connections", "Toggles gizmos for drawing node connections"), EditorStyles.miniButtonRight);
            GUILayout.EndHorizontal();


            Separator();
            GUILayout.Label(new GUIContent("Advanced"), EditorStyles.boldLabel);

            if (GUILayout.Button("Export to .obj file"))
            {
                ExportToFile(graph);
            }

            graph.relevantGraphSurfaceMode = (RecastGraph.RelevantGraphSurfaceMode)EditorGUILayout.EnumPopup(new GUIContent("Relevant Graph Surface Mode",
                                                                                                                            "Require every region to have a RelevantGraphSurface component inside it.\n" +
                                                                                                                            "A RelevantGraphSurface component placed in the scene specifies that\n" +
                                                                                                                            "the navmesh region it is inside should be included in the navmesh.\n\n" +
                                                                                                                            "If this is set to OnlyForCompletelyInsideTile\n" +
                                                                                                                            "a navmesh region is included in the navmesh if it\n" +
                                                                                                                            "has a RelevantGraphSurface inside it, or if it\n" +
                                                                                                                            "is adjacent to a tile border. This can leave some small regions\n" +
                                                                                                                            "which you didn't want to have included because they are adjacent\n" +
                                                                                                                            "to tile borders, but it removes the need to place a component\n" +
                                                                                                                            "in every single tile, which can be tedious (see below).\n\n" +
                                                                                                                            "If this is set to RequireForAll\n" +
                                                                                                                            "a navmesh region is included only if it has a RelevantGraphSurface\n" +
                                                                                                                            "inside it. Note that even though the navmesh\n" +
                                                                                                                            "looks continous between tiles, the tiles are computed individually\n" +
                                                                                                                            "and therefore you need a RelevantGraphSurface component for each\n" +
                                                                                                                            "region and for each tile."),
                                                                                                             graph.relevantGraphSurfaceMode);

            graph.nearestSearchOnlyXZ = EditorGUILayout.Toggle(new GUIContent("Nearest node queries in XZ space",
                                                                              "Recomended for single-layered environments.\nFaster but can be inacurate esp. in multilayered contexts."), graph.nearestSearchOnlyXZ);

            if (graph.nearestSearchOnlyXZ && (Mathf.Abs(graph.rotation.x) > 1 || Mathf.Abs(graph.rotation.z) > 1))
            {
                EditorGUILayout.HelpBox("Nearest node queries in XZ space is not recommended for rotated graphs since XZ space no longer corresponds to the ground plane", MessageType.Warning);
            }
        }
示例#6
0
 public Bounds Encapsulate(Bounds bounds)
 {
     return(Bounds.FromMinMax(Vector3.Min(this.min, bounds.min), Vector3.Max(this.max, bounds.max)));
 }
示例#7
0
 public Bounds Encapsulate(Vector3 point)
 {
     return(Bounds.FromMinMax(Vector3.Min(this.min, point), Vector3.Max(this.max, point)));
 }
示例#8
0
 public void Encapsulate(Vector3 point)
 {
     this.SetMinMax(Vector3.Min(this.min, point), Vector3.Max(this.max, point));
 }
示例#9
0
文件: Bounds.cs 项目: mrcece/ETCore
 // Grows the Bounds to include the /point/.
 public void Encapsulate(Vector3 point)
 {
     SetMinMax(Vector3.Min(min, point), Vector3.Max(max, point));
 }
示例#10
0
        private void SelectAvatar(int index)
        {
            DestroyInstances();
            if (Asset.avatars.Length == 0)
            {
                return;
            }

            index = (index + Asset.avatars.Length) % Asset.avatars.Length;
            selectedAvatarIndex = index;

            var asset = Asset;

            if (selectedAvatarIndex >= asset.avatars.Length)
            {
                selectedAvatarIndex = 0;
            }
            if (selectedAvatarIndex < asset.avatars.Length)
            {
                avatarRes = avatars[selectedAvatarIndex];
            }

            if (avatarRes != null)
            {
                if (avatarRes.skeleton)
                {
                    skeleton = Instantiate(avatarRes.skeleton);
                    //skeleton.transform.forward = -Vector3.forward;
                    skeleton.transform.eulerAngles = new Vector3(0, 90, 0);
                    skeleton.transform.parent      = root;
                    bool    first = true;
                    Vector3 min = new Vector3(), max = new Vector3();
                    foreach (var smr in skeleton.GetComponentsInChildren <SkinnedMeshRenderer>())
                    {
                        if (first)
                        {
                            min = smr.bounds.min;
                            max = smr.bounds.max;
                        }
                        else
                        {
                            min = Vector3.Min(min, smr.bounds.min);
                            max = Vector3.Max(max, smr.bounds.max);
                        }
                    }
                    size      = (max - min).magnitude;
                    distance  = size * 1.3f;
                    height    = (max - min).y;
                    skinned   = skeleton.AddComponent <SkinnedCombine>();
                    animation = skeleton.GetComponent <Animation>();
                    animator  = skeleton.GetComponent <Animator>();

                    if (animator)
                    {
                        if (avatarRes.config.animator)
                        {
                            animator.runtimeAnimatorController = avatarRes.config.animator;
                        }
                    }

                    var smrs = skeleton.GetComponentsInChildren <SkinnedMeshRenderer>();

                    if (avatarRes.awatarParts.Length > 0)
                    {
                        foreach (var smr in smrs)
                        {
                            //if (smr.gameObject!=skeleton&& avatarRes.awatarParts.Where(o => string.Equals(o.partName, smr.name, StringComparison.InvariantCultureIgnoreCase)).Count() == 0)
                            //    continue;
                            smr.materials = new Material[0];
                        }
                        skinned.Clear();
                        for (int i = 0; i < avatarRes.awatarParts.Length; i++)
                        {
                            var part      = avatarRes.awatarParts[i];
                            int partIndex = avatarRes.selectedIndexs[i];
                            if (!(0 <= partIndex && partIndex < avatarRes.selectedIndexs.Length))
                            {
                                continue;
                            }
                            if (partIndex >= part.parts.Length)
                            {
                                continue;
                            }
                            skinned.AddPart(part.partName, part.parts[partIndex], combine);
                        }

                        skinned.GennerateSkin();
                    }
                }
            }
            SelectAnimation(avatarRes.selectedAnimationIndex);
        }