Ejemplo n.º 1
0
        private void RotateObjectAroundAxis(Vector3 rotationAxis, float rotationAmountInDegrees)
        {
            OrientedBox hierarchyWorldOrientedBox = _gameObject.GetHierarchyWorldOrientedBox();

            if (hierarchyWorldOrientedBox.IsInvalid())
            {
                Box modelSpaceBox = new Box(Vector3.zero, Vector3.one * 0.001f);
                hierarchyWorldOrientedBox = new OrientedBox(modelSpaceBox, _gameObjectTransform);
            }
            if (_mouseRotationSettings.UseSnapping)
            {
                _accumulatedRotationInDegrees += rotationAmountInDegrees;
                float absAccumulatedRotationInDegrees = Mathf.Abs(_accumulatedRotationInDegrees);
                if (absAccumulatedRotationInDegrees >= _mouseRotationSettings.SnapStepInDegrees)
                {
                    float floatNumberOfSteps   = absAccumulatedRotationInDegrees / _mouseRotationSettings.SnapStepInDegrees;
                    int   integerNumberOfSteps = (int)floatNumberOfSteps;

                    rotationAmountInDegrees = integerNumberOfSteps * _mouseRotationSettings.SnapStepInDegrees * Mathf.Sign(_accumulatedRotationInDegrees);
                    _gameObject.RotateHierarchyBoxAroundPoint(rotationAmountInDegrees, rotationAxis, hierarchyWorldOrientedBox.Center);

                    _accumulatedRotationInDegrees = 0.0f;
                }
            }
            else
            {
                _accumulatedRotationInDegrees = 0.0f;
                _gameObject.RotateHierarchyBoxAroundPoint(rotationAmountInDegrees, rotationAxis, hierarchyWorldOrientedBox.Center);
            }
        }
Ejemplo n.º 2
0
        public static GameObject ReplaceGameObjectHierarchyPrefab(GameObject gameObject, GameObject newPrefab)
        {
            if (gameObject == null || PrefabUtility.GetPrefabType(newPrefab) != PrefabType.Prefab)
            {
                return(null);
            }

            // Store any needed object data
            OrientedBox originalWorldOrientedBox = gameObject.GetHierarchyWorldOrientedBox();

            if (originalWorldOrientedBox.IsInvalid())
            {
                return(null);
            }
            int  originalObjectLayer = gameObject.layer;
            bool isObjectStatic      = gameObject.isStatic;

            Transform  originalObjectTransform = gameObject.transform;
            Vector3    worldScale    = originalObjectTransform.lossyScale;
            Quaternion worldRotation = originalObjectTransform.rotation;

            // Create a new game object from the specified prefab
            GameObject newObject = PrefabUtility.InstantiatePrefab(newPrefab) as GameObject;

            if (newObject != null)
            {
                // Register the created object for Undo and set its transform data. Also store any significant
                // data that the original object had before it was destroyed.
                UndoEx.RegisterCreatedGameObject(newObject);
                Transform newObjectTransform = newObject.transform;
                newObjectTransform.localScale = worldScale;
                newObjectTransform.rotation   = worldRotation;
                newObjectTransform.parent     = originalObjectTransform.transform.parent;
                newObject.SetSelectedHierarchyWireframeHidden(ObjectPlacementSettings.Get().HideWireframeWhenPlacingObjects);
                newObject.layer    = originalObjectLayer;
                newObject.isStatic = isObjectStatic;

                // We will adjust the new object's position such that its center is the same as the
                // one the original object had. This produces better results especially when the new
                // object is a multi-level object hierarchy.
                OrientedBox newHierarchyWorldOrientedBox = newObject.GetHierarchyWorldOrientedBox();
                if (newHierarchyWorldOrientedBox.IsInvalid())
                {
                    GameObject.DestroyImmediate(newObject);
                    return(null);
                }
                newObjectTransform.position = ObjectPositionCalculator.CalculateObjectHierarchyPosition(newPrefab, originalWorldOrientedBox.Center, worldScale, worldRotation);

                // We will also inform the scene that a new object was created so that all the necessary steps can be performed
                Octave3DScene.Get().RegisterObjectHierarchy(newObject);

                // Destroy the old object
                UndoEx.DestroyObjectImmediate(gameObject);

                return(newObject);
            }

            return(null);
        }
        public bool ObjectMeshIntersectsAnyMesh(Octave3DMesh octave3DQueryMesh, TransformMatrix worldMatrix, HashSet <GameObject> ignoreObjects)
        {
            if (ignoreObjects == null)
            {
                ignoreObjects = new HashSet <GameObject>();
            }
            OrientedBox queryMeshOOBB = octave3DQueryMesh.GetOOBB(worldMatrix);

            if (queryMeshOOBB.IsInvalid())
            {
                return(false);
            }

            List <SphereTreeNode <GameObject> > allOverlappedNodes = _sphereTree.OverlapBox(queryMeshOOBB);

            if (allOverlappedNodes.Count == 0)
            {
                return(false);
            }

            var overlappedObjects = new List <GameObject>();

            foreach (SphereTreeNode <GameObject> node in allOverlappedNodes)
            {
                GameObject gameObject = node.Data;
                if (gameObject == null)
                {
                    continue;
                }
                if (!gameObject.activeSelf)
                {
                    continue;
                }
                if (ignoreObjects.Contains(gameObject))
                {
                    continue;
                }

                Mesh objectMesh = gameObject.GetMeshFromFilterOrSkinnedMeshRenderer();
                if (objectMesh != null)
                {
                    Octave3DMesh octave3DMesh = Octave3DMeshDatabase.Get().GetOctave3DMesh(objectMesh);
                    if (octave3DMesh != null)
                    {
                        if (octave3DQueryMesh.IntersectsMesh(worldMatrix, octave3DMesh, gameObject.transform.GetWorldMatrix()))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }