Ejemplo n.º 1
0
 public static void MirrorZ(Vertex[] vertices, ProceduralObject obj)
 {
     for (int i = 0; i < vertices.Length; i++)
     {
         vertices[i].Position.z = -vertices[i].Position.z;
     }
     obj.flipFaces = !obj.flipFaces;
     VertexUtils.flipFaces(obj);
     if (obj.normalsRecalcMode == NormalsRecalculation.None && !obj.IsPloppableAsphalt())
     {
         obj.normalsRecalcMode = NormalsRecalculation.Default;
     }
     obj.RecalculateNormals();
 }
        private void draw(int id)
        {
            GUI.DragWindow(new Rect(0, 0, 348, 26));
            if (GUIUtils.CloseHelpButtons(winRect, "Advanced_Edition_tools"))
            {
                showWindow = false;
            }

            // SHOW ALWAYS/DAY/NIGHT
            if (GUI.Button(new Rect(5, 28, 192.5f, 22), LocalizationManager.instance.current.visibilityString(m_object.m_visibility)))
            {
                ProceduralObjectsLogic.PlaySound();
                if (m_object.m_visibility == ProceduralObjectVisibility.Always)
                {
                    m_object.m_visibility = ProceduralObjectVisibility.DayOnly;
                }
                else if (m_object.m_visibility == ProceduralObjectVisibility.DayOnly)
                {
                    m_object.m_visibility = ProceduralObjectVisibility.NightOnly;
                }
                else if (m_object.m_visibility == ProceduralObjectVisibility.NightOnly)
                {
                    m_object.m_visibility = ProceduralObjectVisibility.Always;
                }
            }
            // FLIP FACES
            if (GUI.Button(new Rect(202.5f, 28, 192.5f, 22), string.Format(LocalizationManager.instance.current["flipFaces"], m_object.flipFaces.GetHashCode())))
            {
                ProceduralObjectsLogic.PlaySound();
                m_object.flipFaces = !m_object.flipFaces;
                VertexUtils.flipFaces(m_object);
            }

            // CAST SHADOWS
            if (GUI.Button(new Rect(5, 52, 192.5f, 22), string.Format(LocalizationManager.instance.current["castShadows"], (!m_object.disableCastShadows).GetHashCode())))
            {
                ProceduralObjectsLogic.PlaySound();
                m_object.disableCastShadows = !m_object.disableCastShadows;
            }
            // RESET 3D MODEL
            if (GUI.Button(new Rect(202.5f, 52, 192.5f, 22), LocalizationManager.instance.current["resetModel"]))
            {
                ProceduralObjectsLogic.PlaySound();
                GUIUtils.ShowModal(LocalizationManager.instance.current["resetModel"],
                                   LocalizationManager.instance.current["resetModel_confirm"],
                                   (bool ok) =>
                {
                    if (ok)
                    {
                        m_object.ResetOriginalMesh();
                    }
                });
            }

            // NORMALS RECALCULATION
            if (GUI.Button(new Rect(5, 76, 390, 22), LocalizationManager.instance.current.normalsRecalcString(m_object.normalsRecalcMode)))
            {
                ProceduralObjectsLogic.PlaySound();
                m_object.ChangeNormalsRecalc();
            }

            GUI.Label(new Rect(5, 103, 390, 27), "<b><size=15>" + LocalizationManager.instance.current["edition_history"] + "</size></b>");

            // undo
            GUI.BeginGroup(new Rect(5, 131, 135, 60));
            if (m_object.historyEditionBuffer.CanUndo)
            {
                if (GUI.Button(new Rect(0, 0, 135, 60), string.Empty))
                {
                    undo.Invoke();
                }
                else
                {
                    GUI.Label(new Rect(3, 3, 98, 64), "<size=13><b>" + LocalizationManager.instance.current["undo"] + "</b></size>");
                    GUI.Label(new Rect(3, 20, 98, 55), "<size=10>" + LocalizationManager.instance.current["action_type"] + " : "
                              + m_object.historyEditionBuffer.stepsDone[m_object.historyEditionBuffer.stepsDone.Count - 1].GetLocalizedStepString() + "</size>");
                }
            }
            else
            {
                GUI.Box(new Rect(0, 0, 135, 60), string.Empty);
                GUI.color = Color.gray;
                GUI.Label(new Rect(10, 10, 95, 40), "<i><size=13>" + LocalizationManager.instance.current["cant_undo"] + "</size></i>");
            }
            GUI.Label(new Rect(110, 12, 30, 30), "<size=28>↺</size>");
            GUI.color = Color.white;
            GUI.EndGroup();

            // redo
            GUI.BeginGroup(new Rect(145, 131, 135, 60));
            if (m_object.historyEditionBuffer.CanRedo)
            {
                if (GUI.Button(new Rect(0, 0, 135, 60), string.Empty))
                {
                    redo.Invoke();
                }
                else
                {
                    GUI.Label(new Rect(30, 3, 102, 64), "<size=13><b>" + LocalizationManager.instance.current["redo"] + "</b></size>");
                    GUI.Label(new Rect(30, 20, 102, 55), "<size=10>" + LocalizationManager.instance.current["action_type"] + " : "
                              + m_object.historyEditionBuffer.stepsUndone[m_object.historyEditionBuffer.stepsUndone.Count - 1].GetLocalizedStepString() + "</size>");
                }
            }
            else
            {
                GUI.Box(new Rect(0, 0, 135, 60), string.Empty);
                GUI.color = Color.gray;
                GUI.Label(new Rect(37, 10, 95, 40), "<i><size=13>" + LocalizationManager.instance.current["cant_redo"] + "</size></i>");
            }
            GUI.Label(new Rect(7, 12, 30, 30), "<size=28>↻</size>");
            GUI.color = Color.white;
            GUI.EndGroup();

            // erase history buffer
            var erase = new Rect(285, 131, 110, 60);

            if (GUI.Button(erase, string.Empty))
            {
                m_object.historyEditionBuffer.stepsDone.Clear();
                m_object.historyEditionBuffer.stepsUndone.Clear();
            }
            GUI.Label(erase, LocalizationManager.instance.current["erase_history"]);

            // mirror
            GUI.Label(new Rect(5, 195, 145, 27), "<b><size=15>" + LocalizationManager.instance.current["mirror_mesh"] + "</size></b>");
            GUI.Label(new Rect(150, 195, 270, 27), "<b><size=15>" + LocalizationManager.instance.current["stretch_mesh"] + "</size></b>");

            if (m_object.isPloppableAsphalt)
            {
                GUI.color = Color.gray;
                GUI.Box(new Rect(5, 222, 385, 26), "<i>" + LocalizationManager.instance.current["no_mirror_no_stretch"] + "</i>");
                GUI.color = Color.white;
            }
            else
            {
                GUI.color = Color.red;
                if (GUI.Button(new Rect(5, 222, 35, 25), "<b>X</b>"))
                {
                    m_object.historyEditionBuffer.InitializeNewStep(EditingStep.StepType.mirrorX, m_vertices);
                    VertexUtils.MirrorX(m_vertices, m_object);
                    m_object.historyEditionBuffer.ConfirmNewStep(m_vertices);
                    apply.Invoke();
                }
                GUI.color = Color.green;
                if (GUI.Button(new Rect(45, 222, 35, 26), "<b>Y</b>"))
                {
                    m_object.historyEditionBuffer.InitializeNewStep(EditingStep.StepType.mirrorY, m_vertices);
                    VertexUtils.MirrorY(m_vertices, m_object);
                    m_object.historyEditionBuffer.ConfirmNewStep(m_vertices);
                    apply.Invoke();
                }
                GUI.color = Color.blue;
                if (GUI.Button(new Rect(85, 222, 35, 26), "<b>Z</b>"))
                {
                    m_object.historyEditionBuffer.InitializeNewStep(EditingStep.StepType.mirrorZ, m_vertices);
                    VertexUtils.MirrorZ(m_vertices, m_object);
                    m_object.historyEditionBuffer.ConfirmNewStep(m_vertices);
                    apply.Invoke();
                }
                GUI.color = Color.white;

                // stretch
                GUI.Label(new Rect(150, 218, 125, 20), "x" + ((float)stretchFactor / 10f).ToString());
                stretchFactor = Mathf.FloorToInt(GUI.HorizontalSlider(new Rect(150, 238, 125, 20), stretchFactor, 1f, 30f));

                GUI.color = Color.red;
                if (GUI.Button(new Rect(280, 222, 35, 25), "<b>X</b>"))
                {
                    m_object.historyEditionBuffer.InitializeNewStep(EditingStep.StepType.stretchX, (float)stretchFactor / 10f);
                    VertexUtils.StretchX(m_vertices, (float)stretchFactor / 10f);
                    m_object.historyEditionBuffer.ConfirmNewStep(m_vertices);
                    apply.Invoke();
                }
                GUI.color = Color.green;
                if (GUI.Button(new Rect(320, 222, 35, 26), "<b>Y</b>"))
                {
                    m_object.historyEditionBuffer.InitializeNewStep(EditingStep.StepType.stretchY, (float)stretchFactor / 10f);
                    VertexUtils.StretchY(m_vertices, (float)stretchFactor / 10f);
                    m_object.historyEditionBuffer.ConfirmNewStep(m_vertices);
                    apply.Invoke();
                }
                GUI.color = Color.blue;
                if (GUI.Button(new Rect(360, 222, 35, 26), "<b>Z</b>"))
                {
                    m_object.historyEditionBuffer.InitializeNewStep(EditingStep.StepType.stretchZ, (float)stretchFactor / 10f);
                    VertexUtils.StretchZ(m_vertices, (float)stretchFactor / 10f);
                    m_object.historyEditionBuffer.ConfirmNewStep(m_vertices);
                    apply.Invoke();
                }
                GUI.color = Color.white;
            }



            // texture UV
            GUI.Label(new Rect(5, 252, 390, 27), "<b><size=15>" + LocalizationManager.instance.current["texture_tiling"] + "</size></b>");
            if (m_object.RequiresUVRecalculation)
            {
                if (GUI.Button(new Rect(5, 276, 235, 40), LocalizationManager.instance.current["tex_uv_mode"] + " : " + LocalizationManager.instance.current[(m_object.disableRecalculation ? "uv_stretch" : "uv_repeat")]))
                {
                    if (m_object.disableRecalculation)
                    {
                        m_object.disableRecalculation = false;
                        m_object.m_mesh.uv            = Vertex.RecalculateUVMap(m_object, m_vertices);
                    }
                    else
                    {
                        m_object.disableRecalculation = true;
                        m_object.m_mesh.uv            = Vertex.DefaultUVMap(m_object);
                    }
                }
                GUI.Label(new Rect(245, 275, 150, 22), string.Format(LocalizationManager.instance.current["tiling_factor"], m_object.tilingFactor));
                m_object.tilingFactor = (int)Mathf.FloorToInt(GUI.HorizontalSlider(new Rect(245, 274, 150, 22), (float)m_object.tilingFactor, 1, 20));
            }
            else
            {
                GUI.color = Color.gray;
                GUI.Box(new Rect(5, 275, 390, 42), "<i>" + LocalizationManager.instance.current["no_tex_tiling"] + "</i>");
                GUI.color = Color.white;
            }
        }
 public ProceduralObject(CacheProceduralObject sourceCacheObj, int id, Vector3 position, LayerManager layerManager)
 {
     if (sourceCacheObj.baseInfoType == "PROP")
     {
         PropInfo sourceProp = sourceCacheObj._baseProp;
         if (sourceCacheObj._baseProp == null)
         {
             sourceProp = Resources.FindObjectsOfTypeAll <PropInfo>().FirstOrDefault(info => info.name == sourceCacheObj.basePrefabName);
         }
         this._baseProp          = sourceProp;
         this.id                 = id;
         this.basePrefabName     = sourceCacheObj.basePrefabName;
         this.baseInfoType       = "PROP";
         this.isPloppableAsphalt = sourceProp.IsPloppableAsphalt();
         m_material              = GameObject.Instantiate(sourceProp.m_material);
         if (sourceCacheObj.meshStatus == 2)
         {
             m_mesh = sourceProp.m_mesh.InstantiateMesh();
             m_mesh.SetVertices(new List <Vector3>(sourceCacheObj.allVertices));
             vertices   = Vertex.CreateVertexList(this);
             meshStatus = 2;
         }
         else
         {
             meshStatus = 1;
             m_mesh     = sourceProp.m_mesh;
             vertices   = Vertex.CreateVertexList(sourceProp);
         }
         if (sourceProp.m_mesh.name == "ploppableasphalt-prop" || sourceProp.m_mesh.name == "ploppableasphalt-decal")
         {
             m_color = m_material.ApplyPloppableColor();
         }
     }
     else if (sourceCacheObj.baseInfoType == "BUILDING")// building
     {
         BuildingInfo sourceBuilding = sourceCacheObj._baseBuilding;
         if (sourceCacheObj._baseBuilding == null)
         {
             sourceBuilding = Resources.FindObjectsOfTypeAll <BuildingInfo>().FirstOrDefault(info => info.name == sourceCacheObj.basePrefabName);
         }
         this._baseBuilding      = sourceBuilding;
         this.id                 = id;
         this.basePrefabName     = sourceCacheObj.basePrefabName;
         this.baseInfoType       = "BUILDING";
         this.isPloppableAsphalt = false;
         m_material              = GameObject.Instantiate(sourceBuilding.m_material);
         m_mesh     = sourceBuilding.m_mesh.InstantiateMesh();
         meshStatus = 2;
         m_mesh.SetVertices(new List <Vector3>(sourceCacheObj.allVertices));
         vertices        = Vertex.CreateVertexList(this);
         m_mesh.colors   = new Color[] { };
         m_mesh.colors32 = new Color32[] { };
     }
     if (sourceCacheObj.customTexture != null)
     {
         m_material.mainTexture = sourceCacheObj.customTexture as Texture;
         customTexture          = sourceCacheObj.customTexture;
     }
     m_visibility         = sourceCacheObj.visibility;
     historyEditionBuffer = new HistoryBuffer(this);
     if (sourceCacheObj.textParam != null)
     {
         m_textParameters = TextParameters.Clone(sourceCacheObj.textParam, false);
         // m_textParameters.SetFonts();
         var originalTex = new Texture2D(m_material.mainTexture.width, m_material.mainTexture.height, TextureFormat.RGBA32, false);
         originalTex.SetPixels(((Texture2D)m_material.mainTexture).GetPixels());
         originalTex.Apply();
         m_material.mainTexture = m_textParameters.ApplyParameters(originalTex);
     }
     else
     {
         m_textParameters = null;
     }
     if (sourceCacheObj.layer != null)
     {
         if (layerManager.m_layers.Contains(sourceCacheObj.layer))
         {
             layer = sourceCacheObj.layer;
         }
     }
     m_color                 = sourceCacheObj.m_color;
     m_material.color        = m_color;
     this.flipFaces          = sourceCacheObj.flipFaces;
     this.disableCastShadows = sourceCacheObj.disableCastShadows;
     if (this.flipFaces)
     {
         VertexUtils.flipFaces(this);
     }
     renderDistance   = sourceCacheObj.renderDistance;
     renderDistLocked = sourceCacheObj.renderDistLocked;
     MaterialOptions.FixDecalRenderDist(this);
     m_position           = position;
     m_rotation           = sourceCacheObj.m_rotation;
     disableRecalculation = sourceCacheObj.disableRecalculation;
     normalsRecalcMode    = sourceCacheObj.normalsRecalculation;
     this.tilingFactor    = sourceCacheObj.tilingFactor;
     if (sourceCacheObj.modules != null)
     {
         m_modules = new List <POModule>();
         foreach (var m in sourceCacheObj.modules)
         {
             POModule clone;
             try { clone = m.Clone(); }
             catch (Exception e)
             {
                 Debug.LogError("[ProceduralObjects] Error inside module Clone() method!\n" + e);
                 continue;
             }
             ModuleManager.instance.modules.Add(clone);
             if (clone.enabled)
             {
                 ModuleManager.instance.enabledModules.Add(clone);
             }
             clone.parentObject = this;
             try { clone.OnModuleCreated(ProceduralObjectsLogic.instance); }
             catch (Exception e) { Debug.LogError("[ProceduralObjects] Error inside module OnModuleCreated() method!\n" + e); }
             m_modules.Add(clone);
         }
     }
 }
        public ProceduralObject(ProceduralObjectContainer container, LayerManager layerManager, PropInfo[] props, BuildingInfo[] buildings)
        {
            if (container.objectType == "PROP")
            {
                PropInfo sourceProp = props.FirstOrDefault(info => info.name == container.basePrefabName);
                this._baseProp          = sourceProp;
                this.id                 = container.id;
                this.basePrefabName     = container.basePrefabName;
                this.baseInfoType       = "PROP";
                this.isPloppableAsphalt = sourceProp.IsPloppableAsphalt();
                m_position              = container.position.ToVector3();
                m_rotation              = container.rotation.ToQuaternion();
                m_material              = GameObject.Instantiate(sourceProp.m_material); // overkil ??
                if (container.meshStatus == 0 && container.vertices != null)
                {
                    // CHECK FOR MESH REPETITION
                    if (ProceduralUtils.CheckMeshEquivalence(container.vertices, sourceProp.m_mesh.vertices))
                    {
                        meshStatus = 1;
                        m_mesh     = sourceProp.m_mesh;
                        vertices   = Vertex.CreateVertexList(sourceProp);
                    }
                    else
                    {
                        meshStatus = 2;
                        m_mesh     = sourceProp.m_mesh.InstantiateMesh();
                        var vert = SerializableVector3.ToStandardVector3Array(container.vertices);
                        if (container.scale != 0)
                        {
                            for (int i = 0; i < vert.Count(); i++)
                            {
                                vert[i] = new Vector3(vert[i].x * container.scale, vert[i].y * container.scale, vert[i].z * container.scale);
                            }
                        }
                        m_mesh.SetVertices(new List <Vector3>(vert));
                        vertices = Vertex.CreateVertexList(this);
                    }
                }
                else if (container.meshStatus == 1)
                {
                    meshStatus = 1;
                    m_mesh     = sourceProp.m_mesh;
                    vertices   = Vertex.CreateVertexList(sourceProp);
                }
                else // meshstatus2
                {
                    meshStatus = 2;
                    m_mesh     = sourceProp.m_mesh.InstantiateMesh();
                    if (container.serializedMeshData != null)
                    {
                        container.serializedMeshData.ApplyDataToObject(this);
                    }
                    else if (container.vertices != null)
                    {
                        var vert = SerializableVector3.ToStandardVector3Array(container.vertices);
                        if (container.scale != 0)
                        {
                            for (int i = 0; i < vert.Count(); i++)
                            {
                                vert[i] = new Vector3(vert[i].x * container.scale, vert[i].y * container.scale, vert[i].z * container.scale);
                            }
                        }
                        m_mesh.SetVertices(new List <Vector3>(vert));
                    }
                    else
                    {
                        throw new Exception("[ProceduralObjects] Loading failure : Missing mesh data !");
                    }
                    vertices = Vertex.CreateVertexList(this);
                }
                if (sourceProp.m_mesh.name == "ploppableasphalt-prop" || sourceProp.m_mesh.name == "ploppableasphalt-decal")
                {
                    m_color = m_material.ApplyPloppableColor();
                }
                if (container.hasCustomTexture && TextureManager.instance != null)
                {
                    var customTex = TextureManager.instance.FindTexture(container.customTextureName);
                    m_material.mainTexture = customTex as Texture;
                    customTexture          = customTex;
                }
            }
            else if (container.objectType == "BUILDING")// building
            {
                BuildingInfo sourceProp = buildings.FirstOrDefault(info => info.name == container.basePrefabName);
                this._baseBuilding      = sourceProp;
                this.id                 = container.id;
                this.basePrefabName     = container.basePrefabName;
                this.baseInfoType       = "BUILDING";
                this.isPloppableAsphalt = false;
                m_position              = container.position.ToVector3();
                m_rotation              = container.rotation.ToQuaternion();
                meshStatus              = 2;
                m_material              = GameObject.Instantiate(sourceProp.m_material); // overkill ??
                m_mesh = sourceProp.m_mesh.InstantiateMesh();
                if (container.serializedMeshData != null)
                {
                    container.serializedMeshData.ApplyDataToObject(this);
                }
                else if (container.vertices != null)
                {
                    var vert = SerializableVector3.ToStandardVector3Array(container.vertices);
                    if (container.scale != 0)
                    {
                        for (int i = 0; i < vert.Count(); i++)
                        {
                            vert[i] = new Vector3(vert[i].x * container.scale, vert[i].y * container.scale, vert[i].z * container.scale);
                        }
                    }
                    m_mesh.SetVertices(new List <Vector3>(vert));
                }
                else
                {
                    throw new Exception("[ProceduralObjects] Loading failure : Missing mesh data !");
                }
                vertices        = Vertex.CreateVertexList(this);
                m_mesh.colors   = new Color[] { };
                m_mesh.colors32 = new Color32[] { };

                if (container.hasCustomTexture && TextureManager.instance != null)
                {
                    var customTex = TextureManager.instance.FindTexture(container.customTextureName);
                    m_material.mainTexture = customTex as Texture;
                    customTexture          = customTex;
                }
            }
            m_visibility   = container.visibility;
            renderDistance = container.renderDistance;
            MaterialOptions.FixDecalRenderDist(this);
            renderDistLocked = container.renderDistLocked;
            if (container.textParam != null)
            {
                m_textParameters = TextParameters.Clone(container.textParam, true);
                for (int i = 0; i < m_textParameters.Count(); i++)
                {
                    if (m_textParameters[i].m_fontColor == null)
                    {
                        if (m_textParameters[i].serializableColor != null)
                        {
                            m_textParameters[i].m_fontColor = m_textParameters[i].serializableColor.ToColor();
                        }
                        else
                        {
                            m_textParameters[i].m_fontColor = Color.white;
                        }
                    }
                }
                //  m_textParameters.SetFonts();
                var originalTex = new Texture2D(m_material.mainTexture.width, m_material.mainTexture.height, TextureFormat.RGBA32, false);
                originalTex.SetPixels(((Texture2D)m_material.mainTexture).GetPixels());
                originalTex.Apply();
                m_material.mainTexture = m_textParameters.ApplyParameters(originalTex) as Texture;
            }
            else
            {
                m_textParameters = null;
            }
            if (container.belongsToGroup)
            {
                if (container.groupRootId == -1)
                {
                    isRootOfGroup    = true;
                    _groupRootIdData = -1;
                }
                else
                {
                    _groupRootIdData = container.groupRootId;
                    isRootOfGroup    = false;
                }
            }
            else
            {
                _groupRootIdData = -2;
                group            = null;
                isRootOfGroup    = false;
            }

            disableRecalculation    = container.disableRecalculation;
            this.normalsRecalcMode  = container.normalsRecalculation;
            this.flipFaces          = container.flipFaces;
            this.disableCastShadows = container.disableCastShadows;
            if (this.flipFaces)
            {
                VertexUtils.flipFaces(this);
            }
            historyEditionBuffer = new HistoryBuffer(this);

            if (container.color == null)
            {
                if (!(m_mesh.name == "ploppableasphalt-prop" || m_mesh.name == "ploppableasphalt-decal"))
                {
                    m_color = Color.white;
                }
            }
            else
            {
                m_color          = container.color;
                m_material.color = m_color;
            }

            if (container.layerId != 0)
            {
                if (layerManager.m_layers.Any(l => l.m_id == container.layerId))
                {
                    layer = layerManager.m_layers.Single(l => l.m_id == container.layerId);
                }
                else
                {
                    Debug.LogError("[ProceduralObjects] Layer of an object not found !");
                }
            }
            else
            {
                layer = null;
            }
            if (container.tilingFactor == 0)
            {
                this.tilingFactor = 8;
            }
            else
            {
                this.tilingFactor = container.tilingFactor;
            }

            m_modules = ModuleManager.LoadModulesFromData(container.modulesData, true, this);
        }