Esempio n. 1
0
        private void RenderPreview()
        {
            GBillboardCreatorArgs args = ConstructArgs();

            GBillboardCreator.PrepareRenderTexture(ref previewRt, args);
            GBillboardCreator.RenderPreview(previewRt, args);
        }
Esempio n. 2
0
        private void HandleAddVertex(Rect previewRect)
        {
            if (vertices.Count >= MAX_VERTICES)
            {
                return;
            }
            if (Event.current == null)
            {
                return;
            }
            Event e = Event.current;

            if (e.type == EventType.MouseDown && e.shift)
            {
                if (!previewRect.Contains(e.mousePosition))
                {
                    return;
                }
                Vector2 uv = Rect.PointToNormalized(previewRect, e.mousePosition);
                vertices.Add(GUtilities.FlipY(uv));
                selectedVertexIndex = vertices.Count - 1;
                tris = GBillboardCreator.Triangulate(vertices.ToArray());
                e.Use();
            }
        }
Esempio n. 3
0
 private void OnEnable()
 {
     LoadPrefs();
     wantsMouseMove = true;
     tris           = GBillboardCreator.Triangulate(vertices.ToArray());
     RenderPreview();
     RefreshMaterialPropsSuggestions();
 }
Esempio n. 4
0
        private void HandleEditingVertices(Rect previewRect, int vertIndex)
        {
            if (vertIndex >= vertices.Count)
            {
                return;
            }
            if (Event.current == null)
            {
                return;
            }
            Event e = Event.current;

            Vector2 uv    = vertices[vertIndex];
            Vector2 point = Rect.NormalizedToPoint(previewRect, GUtilities.FlipY(uv));

            if (e.type == EventType.MouseDown)
            {
                selectedVertexIndex = -1;
                float d = Vector2.Distance(e.mousePosition, point);
                if (d <= vertexClickDistance)
                {
                    if (e.button == 0 && !e.control)
                    {
                        selectedVertexIndex = vertIndex;
                        e.Use();
                    }
                    else if (e.button == 0 && e.control)
                    {
                        vertices.RemoveAt(vertIndex);
                        tris = GBillboardCreator.Triangulate(vertices.ToArray());
                        e.Use();
                    }
                }
            }
            else if (e.type == EventType.MouseDrag)
            {
                if (selectedVertexIndex == vertIndex)
                {
                    point = e.mousePosition;
                    uv    = Rect.PointToNormalized(previewRect, point);
                    uv.Set(Mathf.Clamp01(uv.x), Mathf.Clamp01(uv.y));
                    vertices[vertIndex] = GUtilities.FlipY(uv);
                    tris = GBillboardCreator.Triangulate(vertices.ToArray());
                    e.Use();
                }
            }

            Color handleColor = selectedVertexIndex == vertIndex ? Color.yellow : Color.green;
            float handleSize  = 6;
            Rect  handleRect  = new Rect();

            handleRect.size   = Vector2.one * handleSize;
            handleRect.center = point;
            EditorGUI.DrawRect(handleRect, handleColor);
        }
Esempio n. 5
0
        private void DrawUtilities()
        {
            string label = "Utilites";
            string id    = "billboardeditor-utilities";

            GEditorCommon.Foldout(label, false, id, () =>
            {
                Rect fitButtonRect = EditorGUILayout.GetControlRect();
                if (GUI.Button(fitButtonRect, "Fit Camera"))
                {
                    GBillboardCreatorArgs args = GBillboardCreator.FitCameraToTarget(ConstructArgs());
                    CopyArgs(args);
                }
            });
        }
Esempio n. 6
0
        private void DrawTargetSettings()
        {
            string label = "Target";
            string id    = "billboardeditor-target";

            GEditorCommon.Foldout(label, false, id, () =>
            {
                EditorGUI.BeginChangeCheck();
                target = EditorGUILayout.ObjectField("Prefab", target, typeof(GameObject), true) as GameObject;
                if (EditorGUI.EndChangeCheck())
                {
                    GBillboardCreatorArgs args = GBillboardCreator.FitCameraToTarget(ConstructArgs());
                    CopyArgs(args);
                    RefreshMaterialPropsSuggestions();
                }
            });
        }
Esempio n. 7
0
 private void CopyArgs(GBillboardCreatorArgs args)
 {
     mode            = args.Mode;
     target          = args.Target;
     row             = args.Row;
     column          = args.Column;
     cellSize        = args.CellSize;
     cameraOffset    = args.CameraOffset;
     cameraSize      = args.CameraSize;
     atlasMaterial   = args.AtlasMaterial;
     normalMaterial  = args.NormalMaterial;
     srcColorProps   = args.SrcColorProps;
     desColorProps   = args.DesColorProps;
     srcTextureProps = args.SrcTextureProps;
     desTextureProps = args.DesTextureProps;
     cellIndex       = args.CellIndex;
     vertices        = new List <Vector2>(args.Vertices);
     width           = args.Width;
     height          = args.Height;
     bottom          = args.Bottom;
     tris            = GBillboardCreator.Triangulate(vertices.ToArray());
 }
Esempio n. 8
0
        private void SaveAssets()
        {
            string folder = EditorUtility.OpenFolderPanel("Select Folder", "Assets/", "");

            if (string.IsNullOrEmpty(folder))
            {
                return;
            }
            try
            {
                GAnalytics.Record(GAnalytics.BILLBOARD_SAVE);
                EditorUtility.DisplayProgressBar("Saving", "Saving assets...", 1f);
                GBillboardCreatorArgs args      = ConstructArgs();
                BillboardAsset        billboard = GBillboardCreator.CreateBillboardAsset(args);
                Texture2D             atlas     = GBillboardCreator.RenderAtlas(args);
                Texture2D             normal    = GBillboardCreator.RenderNormal(args);

                string         billboardPath  = Path.Combine(FileUtil.GetProjectRelativePath(folder), billboard.name + ".asset");
                BillboardAsset billboardAsset = AssetDatabase.LoadAssetAtPath <BillboardAsset>(billboardPath);
                if (billboardAsset == null)
                {
                    AssetDatabase.CreateAsset(billboard, billboardPath);
                    billboardAsset = billboard;
                }
                else
                {
                    billboardAsset.SetVertices(billboard.GetVertices());
                    billboardAsset.SetIndices(billboard.GetIndices());
                    billboardAsset.SetImageTexCoords(billboard.GetImageTexCoords());
                    billboardAsset.width  = billboard.width;
                    billboardAsset.height = billboard.height;
                    billboardAsset.bottom = billboard.bottom;
                    billboardAsset.name   = billboard.name;
                }

                string atlasPath = Path.Combine(FileUtil.GetProjectRelativePath(folder), atlas.name + ".png");
                byte[] atlasData = atlas.EncodeToPNG();
                File.WriteAllBytes(atlasPath, atlasData);
                GUtilities.DestroyObject(atlas);

                string normalPath = Path.Combine(FileUtil.GetProjectRelativePath(folder), normal.name + ".png");
                byte[] normalData = normal.EncodeToPNG();
                File.WriteAllBytes(normalPath, normalData);
                GUtilities.DestroyObject(normal);

                AssetDatabase.Refresh();

                TextureImporter atlasImporter = AssetImporter.GetAtPath(atlasPath) as TextureImporter;
                atlasImporter.wrapMode            = TextureWrapMode.Clamp;
                atlasImporter.alphaIsTransparency = true;
                atlasImporter.SaveAndReimport();
                atlas = AssetDatabase.LoadAssetAtPath <Texture2D>(atlasPath);

                TextureImporter normalImporter = AssetImporter.GetAtPath(normalPath) as TextureImporter;
                normalImporter.textureType = TextureImporterType.NormalMap;
                normalImporter.wrapMode    = TextureWrapMode.Clamp;
                normalImporter.SaveAndReimport();
                normal = AssetDatabase.LoadAssetAtPath <Texture2D>(normalPath);


                Material mat = null;
                if (GCommon.CurrentRenderPipeline == GRenderPipelineType.Universal)
                {
                    mat = Object.Instantiate(GGriffinSettings.Instance.TerrainDataDefault.Foliage.TreeBillboardMaterialURP);
                }
                else if (GCommon.CurrentRenderPipeline == GRenderPipelineType.Lightweight)
                {
                    mat = Object.Instantiate(GGriffinSettings.Instance.TerrainDataDefault.Foliage.TreeBillboardMaterialLWRP);
                }
                else
                {
                    mat = Object.Instantiate(GGriffinSettings.Instance.TerrainDataDefault.Foliage.TreeBillboardMaterial);
                }
                if (mat != null)
                {
                    if (mat.HasProperty("_MainTex"))
                    {
                        mat.SetTexture("_MainTex", atlas);
                    }
                    if (mat.HasProperty("_BumpMap"))
                    {
                        mat.SetTexture("_BumpMap", normal);
                    }
                    mat.name = args.Target.name + "_BillboardMaterial";
                    string matPath = Path.Combine(FileUtil.GetProjectRelativePath(folder), mat.name + ".mat");
                    AssetDatabase.CreateAsset(mat, matPath);
                    billboardAsset.material = mat;
                }
                AssetDatabase.Refresh();
            }
            catch (System.Exception e)
            {
                Debug.LogError(e.ToString());
            }

            EditorUtility.ClearProgressBar();
        }