void FillVertexColor()
 {
     if (curMesh)
     {
         Vector3[] verts  = curMesh.vertices;
         Color[]   colors = new Color[0];
         if (curMesh.colors.Length > 0)
         {
             colors = curMesh.colors;
         }
         else
         {
             colors = new Color[verts.Length];
         }
         for (int i = 0; i < verts.Length; i++)
         {
             if (curColorChannel == (int)PaintType.All)
             {
                 colors[i] = brushColor;
             }
             else
             {
                 colors[i] = SVTXPainterUtils.VTXOneChannelLerp(colors[i], brushIntensity, 1, (PaintType)curColorChannel);
             }
             //Debug.Log("Blend");
         }
         curMesh.colors = colors;
     }
     else
     {
         Debug.LogWarning("Nothing to fill!");
     }
 }
 void PaintVertexColor()
 {
     if (m_target && m_active)
     {
         curMesh = SVTXPainterUtils.GetMesh(m_active);
         if (curMesh)
         {
             if (isRecord)
             {
                 m_target.PushUndo();
                 isRecord = false;
             }
             Vector3[] verts  = bakedSkinnedMesh ? bakedSkinnedMesh.vertices : curMesh.vertices;
             Color[]   colors = new Color[0];
             if (curMesh.colors.Length > 0)
             {
                 colors = curMesh.colors;
             }
             else
             {
                 colors = new Color[verts.Length];
             }
             for (int i = 0; i < verts.Length; i++)
             {
                 Vector3 vertPos = m_target.transform.TransformPoint(verts[i]);
                 float   mag     = (vertPos - curHit.point).magnitude;
                 if (mag > brushSize)
                 {
                     continue;
                 }
                 float falloff = SVTXPainterUtils.LinearFalloff(mag, brushSize);
                 falloff = Mathf.Pow(falloff, Mathf.Clamp01(1 - brushFalloff / brushSize)) * brushOpacity;
                 if (curColorChannel == (int)PaintType.All)
                 {
                     colors[i] = SVTXPainterUtils.VTXColorLerp(colors[i], brushColor, falloff);
                 }
                 else
                 {
                     colors[i] = SVTXPainterUtils.VTXOneChannelLerp(colors[i], brushIntensity, falloff, (PaintType)curColorChannel);
                 }
                 //Debug.Log("Blend");
             }
             curMesh.colors = colors;
         }
         else
         {
             OnSelectionChange();
             Debug.LogWarning("Nothing to paint!");
         }
     }
     else
     {
         OnSelectionChange();
         Debug.LogWarning("Nothing to paint!");
     }
 }
Пример #3
0
        private void OnSelectionChange()
        {
            m_target = null;
            m_active = null;
            curMesh  = null;
            if (Selection.activeGameObject != null)
            {
                m_target = Selection.activeGameObject.GetComponent <SVTXObject>();
                curMesh  = SVTXPainterUtils.GetMesh(Selection.activeGameObject);

                var activeGameObject = Selection.activeGameObject;
                if (curMesh != null)
                {
                    m_active = activeGameObject;
                }
            }
            allowSelect = (m_target == null);

            Repaint();
        }
        private void OnGUI()
        {
            //Header
            GUILayout.BeginHorizontal();
            GUILayout.Box("Simple Vertex Painter", titleStyle, GUILayout.Height(60), GUILayout.ExpandWidth(true));
            GUILayout.EndHorizontal();

            //Body
            GUILayout.BeginVertical(GUI.skin.box);

            if (m_target != null)
            {
                if (!m_target.isActiveAndEnabled)
                {
                    EditorGUILayout.LabelField("(Enable " + m_target.name + " to show Simple Vertex Painter)");
                }
                else
                {
                    GUILayout.BeginHorizontal();
                    if (GUILayout.Button("Backup Color Map"))
                    {
                        curMesh     = SVTXPainterUtils.GetMesh(m_active);
                        save_colors = curMesh.colors;
                    }
                    if (GUILayout.Button("Restore Color Map"))
                    {
                        curMesh        = SVTXPainterUtils.GetMesh(m_active);
                        curMesh.colors = save_colors;
                    }
                    GUILayout.EndHorizontal();
                    //bool lastAP = allowPainting;
                    allowPainting = GUILayout.Toggle(allowPainting, "Paint Mode");

                    if (allowPainting)
                    {
                        if (!shaderChanged)
                        {
                            save_materials = Selection.activeGameObject.GetComponent <Renderer>().sharedMaterials;
                            Material   svtxMaterial  = new Material(Shader.Find("SVTX/Vertex Color"));
                            Material[] svtxMaterials = new Material[save_materials.Length];
                            for (int i = 0; i < svtxMaterials.Length; i++)
                            {
                                svtxMaterials[i] = svtxMaterial;
                            }
                            Selection.activeGameObject.GetComponent <Renderer>().sharedMaterials = svtxMaterials;
                            shaderChanged = true;
                        }
                        //Selection.activeGameObject = null;
                        Tools.current = Tool.None;
                    }
                    else
                    {
                        if (shaderChanged)
                        {
                            Selection.activeGameObject.GetComponent <Renderer>().sharedMaterials = save_materials;
                            shaderChanged = false;
                        }
                    }


                    GUILayout.BeginHorizontal();
                    GUILayout.Label("Paint Type:", GUILayout.Width(90));
                    string[] channelName = { "All", "R", "G", "B", "A" };
                    int[]    channelIds  = { 0, 1, 2, 3, 4 };
                    curColorChannel = EditorGUILayout.IntPopup(curColorChannel, channelName, channelIds, GUILayout.Width(50));
                    GUILayout.EndHorizontal();
                    GUILayout.BeginHorizontal();
                    if (curColorChannel == (int)PaintType.All)
                    {
                        brushColor = EditorGUILayout.ColorField("Brush Color:", brushColor);
                    }
                    else
                    {
                        brushIntensity = EditorGUILayout.Slider("Intensity:", brushIntensity, 0, 1);
                    }
                    if (GUILayout.Button("Fill"))
                    {
                        FillVertexColor();
                    }
                    GUILayout.EndHorizontal();
                    brushSize    = EditorGUILayout.Slider("Brush Size:", brushSize, MinBrushSize, MaxBrushSize);
                    brushOpacity = EditorGUILayout.Slider("Brush Opacity:", brushOpacity, 0, 1);
                    brushFalloff = EditorGUILayout.Slider("Brush Falloff:", brushFalloff, MinBrushSize, brushSize);

                    if (GUILayout.Button("Export .asset file") && curMesh != null)
                    {
                        string path = EditorUtility.SaveFilePanel("Export .asset file", "Assets", SVTXPainterUtils.SanitizeForFileName(curMesh.name), "asset");
                        if (path.Length > 0)
                        {
                            var dataPath = Application.dataPath;
                            if (!path.StartsWith(dataPath))
                            {
                                Debug.LogError("Invalid path: Path must be under " + dataPath);
                            }
                            else
                            {
                                path = path.Replace(dataPath, "Assets");
                                AssetDatabase.CreateAsset(Instantiate(curMesh), path);
                                Debug.Log("Asset exported: " + path);
                            }
                        }
                    }



                    //Footer
                    GUILayout.Label("Key Z:Turn on or off\nRight mouse button:Paint\nRight mouse button+Shift:Opacity\nRight mouse button+Ctrl:Size\nRight mouse button+Shift+Ctrl:Falloff\n", EditorStyles.helpBox);
                    Repaint();
                }
            }
            else if (m_active != null)
            {
                if (GUILayout.Button("Add SVTX Object to " + m_active.name))
                {
                    m_active.AddComponent <SVTXObject>();
                    OnSelectionChange();
                }
            }
            else
            {
                EditorGUILayout.LabelField("Please select a mesh or skinnedmesh.");
            }
            GUILayout.EndVertical();
        }