Ejemplo n.º 1
0
        ///////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// HandleObjectsMode
        /// # To insert objects in scene using mouse
        /// </summary>
        ///////////////////////////////////////////////////////////////////////////////////////////////////////
        void HandleObjectsMode()
        {
            if ((GetEditorTimeDiff() > 0.1f) && EditorBasicFunctions.GetMouseButtonDown(0) && EditorBasicFunctions.GetInsertModeKeyPressed() && !GetDoingSomethingSpecial())
            {
                previousEditorTime = EditorApplication.timeSinceStartup;

                Ray        ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                RaycastHit hit;

                if (Physics.Raycast(ray, out hit))
                {
                    if (lastPrefabHitPoint.ToString() == hit.point.ToString())
                    {
                        //Debug.Log ("NOTE: same point duplicate -> lastPrefabHitPoint: " + lastPrefabHitPoint);
                    }
                    else
                    {
                        //Debug.Log ("lastPrefabHitPoint A: " + lastPrefabHitPoint);

                        lastPrefabHitPoint = hit.point;

                        //Debug.Log ("New Object");
                        //Debug.Log ("Hit position: " + hit.point);
                        //Debug.Log ("Collider Name: " + hit.collider.name);

                        GameObject actualObject = Instantiate(genericObject);

                        switch (pivotMode)
                        {
                        case cPivotMode.useOriginalPivot:
                        {
                            actualObject.transform.position = hit.point + extraNormalOffset * hit.normal;
                        }
                        break;

                        case cPivotMode.autoCalculate:
                        {
                            MeshFilter actualMeshFilter = BasicFunctions.GetMeshFilterInChilds(actualObject.transform);

                            if (actualMeshFilter == null)
                            {
                                actualObject.transform.position = hit.point + extraNormalOffset * hit.normal;
                            }
                            else
                            {
                                float pivotOffset = extraNormalOffset + 0.5f * (Mathf.Abs(actualMeshFilter.sharedMesh.bounds.max.y) + Mathf.Abs(actualMeshFilter.sharedMesh.bounds.min.y));

                                //Debug.Log ("pivotOffset: " + pivotOffset);
                                //Debug.Log ("max: " + actualMeshFilter.sharedMesh.bounds.max);
                                //Debug.Log ("min: " + actualMeshFilter.sharedMesh.bounds.min);
                                //Debug.Log ("Center: " + actualMeshFilter.sharedMesh.bounds.center);

                                actualObject.transform.position = hit.point + pivotOffset * hit.normal;
                            }
                        }
                        break;
                        }

                        actualObject.transform.localScale = Random.Range(objectScaleRange.x, objectScaleRange.y) * actualObject.transform.localScale;
                        actualObject.transform.rotation   = Quaternion.FromToRotation(Vector3.up, hit.normal);
                        actualObject.transform.Rotate(genericObject.transform.rotation.eulerAngles);
                        actualObject.transform.Rotate(Random.Range(objectRotationRangeX.x, objectRotationRangeX.y), Random.Range(objectRotationRangeY.x, objectRotationRangeY.y), Random.Range(objectRotationRangeZ.x, objectRotationRangeZ.y));

                        if (attachObjectToCollisionObject)
                        {
                            actualObject.transform.parent = hit.collider.transform;
                        }
                        else
                        {
                            GameObject objectsContainer = BasicFunctions.CreateContainerIfNotExists(BasicDefines.OBJECT_CONTAINER_NAME);
                            actualObject.transform.parent = objectsContainer.transform;
                        }

                        actualObject.AddComponent <GenericObject> ();

                        actualObject.name = actualObject.GetComponent <GenericObject> ().Generate(BasicDefines.OBJECT_BASE_NAME, GetSeedForInstancies(), true, genericObject.name);

                        actualObjectToForceSelect = actualObject.gameObject;
                    }
                }
            }
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// HandleProjectedDecalsMode
        /// # To insert projected decals in scene using mouse
        /// </summary>
        ///////////////////////////////////////////////////////////////////////////////////////////////////////
        void HandleProjectedDecalsMode()
        {
            if ((GetEditorTimeDiff() > 0.1f) && EditorBasicFunctions.GetMouseButtonDown(0) && EditorBasicFunctions.GetInsertModeKeyPressed() && !GetDoingSomethingSpecial())
            {
                previousEditorTime = EditorApplication.timeSinceStartup;

                //Debug.Log ("Event.current.mousePosition: " + Event.current.mousePosition);

                Ray        ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                RaycastHit hit;

                if (Physics.Raycast(ray, out hit))
                {
                    if (lastProjectedDecalsHitPoint.ToString() == hit.point.ToString())
                    {
                        //Debug.Log ("NOTE: same point duplicate -> lastProjectedDecalsHitPoint: " + lastProjectedDecalsHitPoint);
                    }
                    else
                    {
                        //Debug.Log ("lastProjectedDecalsHitPoint A: " + lastProjectedDecalsHitPoint);

                        lastProjectedDecalsHitPoint = hit.point;

                        //Debug.Log ("New Decal");
                        //Debug.Log ("Hit position: " + hit.point);
                        //Debug.Log ("Collider Name: " + hit.collider.name);

                        GenericProjectorDecal actualProjectedDecal = Instantiate(projectedDecalPrefab.gameObject).GetComponent <GenericProjectorDecal> () as GenericProjectorDecal;
                        actualProjectedDecal.SetOldParameters(projectedDecalPrefab.transform.localScale, projectedDecalPrefab.GetComponent <Projector> ().orthographicSize, projectedDecalPrefab.GetComponent <Projector> ().aspectRatio);

                        //Debug.Log (actualProjectedDecal.material.mainTexture.name);

                        actualProjectedDecal.transform.position = hit.point + 0.3f * hit.normal;

                        Vector3 finalScale = Random.Range(actualProjectedDecal.scaleRange.x, actualProjectedDecal.scaleRange.y) * actualProjectedDecal.transform.localScale;

                        float textureAspectRatio = (float)actualProjectedDecal.material.mainTexture.width / (float)actualProjectedDecal.material.mainTexture.height;

                        finalScale.x = textureAspectRatio * finalScale.x;

                        actualProjectedDecal.transform.localScale = finalScale;

                        actualProjectedDecal.transform.LookAt(hit.point);

                        actualProjectedDecal.transform.Rotate(new Vector3(0, 0, Random.Range(actualProjectedDecal.rotationRange.x, actualProjectedDecal.rotationRange.y)));

                        actualProjectedDecal.name = actualProjectedDecal.Generate(BasicDefines.PROJECTED_DECAL_BASE_NAME, GetSeedForInstancies(), true, actualProjectedDecal.material.name);

                        //Debug.Log ("actualProjectedDecal.attachToCollisionObject: " + actualProjectedDecal.attachToCollisionObject);

                        if (actualProjectedDecal.attachToCollisionObject)
                        {
                            //Debug.Log ("Parent name: " + hit.collider.name);
                            actualProjectedDecal.transform.parent = hit.collider.transform;
                        }
                        else
                        {
                            GameObject decalsContainer = BasicFunctions.CreateContainerIfNotExists(BasicDefines.PROJECTED_DECAL_CONTAINER_NAME);
                            actualProjectedDecal.transform.parent = decalsContainer.transform;
                        }

                        actualProjectedDecal.UpdateShape();

                        actualObjectToForceSelect = actualProjectedDecal.gameObject;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        ///////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// HandleAllElementsEdition
        /// # To edit objects in scene using Unity controls
        /// </summary>
        ///////////////////////////////////////////////////////////////////////////////////////////////////////
        void HandleExtraOptions()
        {
            if (splineDecalModeEnabled && (systemMode == cSystemMode.edition) && (GetEditorTimeDiff() > 0.1f) && EditorBasicFunctions.GetMouseButtonDown(0) && EditorBasicFunctions.GetInsertModeKeyPressed() && !GetDoingSomethingSpecial())
            {
                previousEditorTime = EditorApplication.timeSinceStartup;

                AddAWayPointWithMouse();
            }
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// HandleMeshDecalsMode
        /// # To insert mesh decals in scene using mouse
        /// </summary>
        ///////////////////////////////////////////////////////////////////////////////////////////////////////
        void HandleMeshDecalsMode()
        {
            if ((GetEditorTimeDiff() > 0.1f) && EditorBasicFunctions.GetMouseButtonDown(0) && EditorBasicFunctions.GetInsertModeKeyPressed() && !GetDoingSomethingSpecial())
            {
                previousEditorTime = EditorApplication.timeSinceStartup;

                //Debug.Log ("Event.current.mousePosition: " + Event.current.mousePosition);

                Ray        ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                RaycastHit hit;

                //Debug.Log ("-----------------------------------------------------------");

                if (Physics.Raycast(ray, out hit))
                {
                    if (lastMeshDecalsHitPoint.ToString() == hit.point.ToString())
                    {
                        //Debug.Log ("NOTE: same point duplicate -> lastMeshDecalsHitPoint: " + lastMeshDecalsHitPoint);
                    }
                    else
                    {
                        //Debug.Log ("lastMeshDecalsHitPoint A: " + lastMeshDecalsHitPoint);

                        lastMeshDecalsHitPoint = hit.point;

                        //Debug.Log ("hit.point: " + hit.point);
                        //Debug.Log ("lastMeshDecalsHitPoint B: " + lastMeshDecalsHitPoint);

                        //Debug.Log ("New Decal");
                        //Debug.Log ("Hit position: " + hit.point);
                        //Debug.Log ("Collider Name: " + hit.collider.name);

                        bool setPlanarDecal = EditorBasicFunctions.planarMeshDecals;

                        if (hit.collider.GetComponent <Terrain> ())
                        {
                            //Debug.Log ("It's a terrain, set the decal as planar");
                            setPlanarDecal = true;
                        }

                        meshDecalPrefab.planarDecal    = setPlanarDecal;
                        meshDecalPrefab.comeFromEditor = setPlanarDecal;

                        GenericMeshDecal actualDecal = Instantiate(meshDecalPrefab.gameObject).GetComponent <GenericMeshDecal> ();
                        actualDecal.transform.position = hit.point;

                        meshDecalPrefab.comeFromEditor = false;

                        Vector3 finalScale = Random.Range(actualDecal.scaleRange.x, actualDecal.scaleRange.y) * actualDecal.transform.localScale;

                        float textureAspectRatio = (float)actualDecal.material.mainTexture.width / (float)actualDecal.material.mainTexture.height;

                        if (actualDecal.GetPlanar())
                        {
                            finalScale.x = 0.5f * finalScale.x;
                            finalScale.y = textureAspectRatio * finalScale.x;
                        }
                        else
                        {
                            finalScale.x = textureAspectRatio * finalScale.x;
                        }

                        //finalScale.y = finalScale.x;
                        //finalScale.z = finalScale.x;


                        actualDecal.transform.localScale = finalScale;

                        actualDecal.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);

                        actualDecal.name = actualDecal.Generate(BasicDefines.MESH_DECAL_BASE_NAME, GetSeedForInstancies(), true, actualDecal.material.name);

                        actualDecal.UpdateDecallShape(true, false);

                        //Debug.Log ("actualDecal.attachToCollisionObject: " + actualDecal.attachToCollisionObject);

                        if (actualDecal.attachToCollisionObject)
                        {
                            //Debug.Log ("Parent name: " + hit.collider.name);
                            actualDecal.transform.parent = hit.collider.transform;
                        }
                        else
                        {
                            GameObject decalsContainer = BasicFunctions.CreateContainerIfNotExists(BasicDefines.MESH_DECAL_CONTAINER_NAME);
                            actualDecal.transform.parent = decalsContainer.transform;
                        }

                        if (actualDecal.addCollider)
                        {
                            actualDecal.gameObject.AddComponent <MeshCollider> ();
                            //actualDecal.gameObject.GetComponent<MeshCollider> ().convex = true;
                        }

                        actualObjectToForceSelect = actualDecal.gameObject;
                    }
                }
            }
        }