Exemplo n.º 1
0
        public GameObjectRayHit RaycastMeshObject(Ray ray, GameObject gameObject)
        {
            EditorMesh editorMesh = EditorMeshDb.Get.GetEditorMesh(gameObject.GetMesh());

            if (editorMesh != null)
            {
                MeshRayHit meshRayHit = editorMesh.Raycast(ray, gameObject.transform.localToWorldMatrix);
                if (meshRayHit != null)
                {
                    return(new GameObjectRayHit(ray, gameObject, meshRayHit));
                }
            }
            else
            {
                // If no EditorMesh instance is available, we will cast a ray against
                // the object's MeshCollider as a last resort. This is actually useful
                // when dealing with static mesh objects. These objects' meshes have
                // their 'isReadable' flag set to false and can not be used to create
                // an EditorMesh instance. Thus a mesh collider is the next best choice.
                MeshCollider meshCollider = gameObject.GetComponent <MeshCollider>();
                if (meshCollider != null)
                {
                    RaycastHit rayHit;
                    if (meshCollider.Raycast(ray, out rayHit, float.MaxValue))
                    {
                        return(new GameObjectRayHit(ray, rayHit));
                    }
                }
            }

            return(null);
        }
Exemplo n.º 2
0
 public bool Contains(EditorMesh editorMesh)
 {
     if (editorMesh == null)
     {
         return(false);
     }
     return(_meshes.ContainsKey(editorMesh.UnityMesh));
 }
Exemplo n.º 3
0
        private EditorMesh CreateEditorMesh(Mesh unityMesh)
        {
            EditorMesh editorMesh = EditorMesh.Create(unityMesh);

            if (editorMesh != null)
            {
                _meshes.Add(unityMesh, editorMesh);
                return(editorMesh);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 4
0
 public MeshTree(EditorMesh mesh)
 {
     _mesh = mesh;
 }
Exemplo n.º 5
0
        public static List <Vector3> CollectHierarchyVerts(GameObject root, BoxFace collectFace, float collectBoxScale, float collectEps)
        {
            var meshObjects   = root.GetMeshObjectsInHierarchy();
            var spriteObjects = root.GetSpriteObjectsInHierarchy();

            if (meshObjects.Count == 0 && spriteObjects.Count == 0)
            {
                return(new List <Vector3>());
            }

            var boundsQConfig = new ObjectBounds.QueryConfig();

            boundsQConfig.ObjectTypes = GameObjectType.Mesh | GameObjectType.Sprite;

            OOBB hierarchyOOBB = ObjectBounds.CalcHierarchyWorldOOBB(root, boundsQConfig);

            if (!hierarchyOOBB.IsValid)
            {
                return(new List <Vector3>());
            }

            int     faceAxisIndex = BoxMath.GetFaceAxisIndex(collectFace);
            Vector3 faceCenter    = BoxMath.CalcBoxFaceCenter(hierarchyOOBB.Center, hierarchyOOBB.Size, hierarchyOOBB.Rotation, collectFace);
            Vector3 faceNormal    = BoxMath.CalcBoxFaceNormal(hierarchyOOBB.Center, hierarchyOOBB.Size, hierarchyOOBB.Rotation, collectFace);

            float   sizeEps         = collectEps * 2.0f;
            Vector3 collectAABBSize = hierarchyOOBB.Size;

            collectAABBSize[faceAxisIndex]            = (hierarchyOOBB.Size[faceAxisIndex] * collectBoxScale) + sizeEps;
            collectAABBSize[(faceAxisIndex + 1) % 3] += sizeEps;
            collectAABBSize[(faceAxisIndex + 2) % 3] += sizeEps;

            OOBB collectOOBB = new OOBB(faceCenter + faceNormal * (-collectAABBSize[faceAxisIndex] * 0.5f + collectEps), collectAABBSize);

            collectOOBB.Rotation = hierarchyOOBB.Rotation;

            var collectedVerts = new List <Vector3>(80);

            foreach (var meshObject in meshObjects)
            {
                Mesh       mesh       = meshObject.GetMesh();
                EditorMesh editorMesh = EditorMeshDb.Get.GetEditorMesh(mesh);
                if (editorMesh == null)
                {
                    continue;
                }

                var verts = editorMesh.OverlapVerts(collectOOBB, meshObject.transform);
                if (verts.Count != 0)
                {
                    collectedVerts.AddRange(verts);
                }
            }

            foreach (var spriteObject in spriteObjects)
            {
                var verts = CollectWorldSpriteVerts(spriteObject.GetSprite(), spriteObject.transform, collectOOBB);
                if (verts.Count != 0)
                {
                    collectedVerts.AddRange(verts);
                }
            }

            return(collectedVerts);
        }