private void OnGUISetShader()
        {
            DrawMaterialSettings();

            if (GUILayout.Button("Set Shader"))
            {
                RecordSetShaderAnalytics();
                RecordShadingAnalytics();
                Material mat = GGriffinSettings.Instance.WizardSettings.GetClonedMaterial();
                if (BulkSetShader)
                {
                    GCommon.ForEachTerrain(
                        BulkSetShaderGroupId,
                        (t) =>
                    {
                        if (t.TerrainData == null)
                        {
                            return;
                        }
                        t.TerrainData.Shading.MaterialToRender.shader = mat.shader;
                        t.TerrainData.Shading.UpdateMaterials();
                        t.TerrainData.SetDirty(GTerrainData.DirtyFlags.Shading);
                    });
                }
                else
                {
                    terrainData.Shading.MaterialToRender.shader = mat.shader;
                    terrainData.Shading.UpdateMaterials();
                }
                GUtilities.DestroyObject(mat);
                Close();
            }
        }
Exemple #2
0
 public void RemoveMetallicMap()
 {
     if (metallicMap != null)
     {
         GUtilities.DestroyObject(metallicMap);
     }
 }
Exemple #3
0
        public static void DestroyIf(GStylizedTerrain terrain, System.Predicate <GameObject> condition)
        {
            List <Transform> parents = new List <Transform>();

            foreach (Transform t in terrain.transform)
            {
                if (t.name.StartsWith("~Root"))
                {
                    parents.Add(t);
                }
            }

            for (int i = 0; i < parents.Count; ++i)
            {
                GameObject[] children = GUtilities.GetChildrenGameObjects(parents[i]);
                for (int j = 0; j < children.Length; ++j)
                {
                    if (condition.Invoke(children[j]))
                    {
#if UNITY_EDITOR
                        Undo.DestroyObjectImmediate(children[j]);
#else
                        GUtilities.DestroyGameobject(children[j]);
#endif
                    }
                }
            }
        }
        public static Color GetColorBilinear(Color[] textureData, int width, int height, Vector2 uv)
        {
            Color   color      = Color.clear;
            Vector2 pixelCoord = new Vector2(
                Mathf.Lerp(0, width - 1, uv.x),
                Mathf.Lerp(0, height - 1, uv.y));
            //apply a bilinear filter
            int xFloor = Mathf.FloorToInt(pixelCoord.x);
            int xCeil  = Mathf.CeilToInt(pixelCoord.x);
            int yFloor = Mathf.FloorToInt(pixelCoord.y);
            int yCeil  = Mathf.CeilToInt(pixelCoord.y);

            Color f00 = textureData[GUtilities.To1DIndex(xFloor, yFloor, width)];
            Color f01 = textureData[GUtilities.To1DIndex(xFloor, yCeil, width)];
            Color f10 = textureData[GUtilities.To1DIndex(xCeil, yFloor, width)];
            Color f11 = textureData[GUtilities.To1DIndex(xCeil, yCeil, width)];

            Vector2 unitCoord = new Vector2(
                pixelCoord.x - xFloor,
                pixelCoord.y - yFloor);

            color =
                f00 * (1 - unitCoord.x) * (1 - unitCoord.y) +
                f01 * (1 - unitCoord.x) * unitCoord.y +
                f10 * unitCoord.x * (1 - unitCoord.y) +
                f11 * unitCoord.x * unitCoord.y;

            return(color);
        }
        public static float GetValueBilinear(float[] data, int width, int height, Vector2 uv)
        {
            float   value      = 0;
            Vector2 pixelCoord = new Vector2(
                Mathf.Lerp(0, width - 1, uv.x),
                Mathf.Lerp(0, height - 1, uv.y));
            //apply a bilinear filter
            int xFloor = Mathf.FloorToInt(pixelCoord.x);
            int xCeil  = Mathf.CeilToInt(pixelCoord.x);
            int yFloor = Mathf.FloorToInt(pixelCoord.y);
            int yCeil  = Mathf.CeilToInt(pixelCoord.y);

            float f00 = data[GUtilities.To1DIndex(xFloor, yFloor, width)];
            float f01 = data[GUtilities.To1DIndex(xFloor, yCeil, width)];
            float f10 = data[GUtilities.To1DIndex(xCeil, yFloor, width)];
            float f11 = data[GUtilities.To1DIndex(xCeil, yCeil, width)];

            Vector2 unitCoord = new Vector2(
                pixelCoord.x - xFloor,
                pixelCoord.y - yFloor);

            value =
                f00 * (1 - unitCoord.x) * (1 - unitCoord.y) +
                f01 * (1 - unitCoord.x) * unitCoord.y +
                f10 * unitCoord.x * (1 - unitCoord.y) +
                f11 * unitCoord.x * unitCoord.y;

            return(value);
        }
Exemple #6
0
        public static IEnumerable <Rect> CompareHeightMap(int gridSize, Color32[] oldValues, Color32[] newValues)
        {
            if (oldValues.LongLength != newValues.LongLength)
            {
                return(new Rect[1] {
                    new Rect(0, 0, 1, 1)
                });
            }
            Rect[] rects = new Rect[gridSize * gridSize];
            for (int x = 0; x < gridSize; ++x)
            {
                for (int z = 0; z < gridSize; ++z)
                {
                    rects[GUtilities.To1DIndex(x, z, gridSize)] = GetUvRange(gridSize, x, z);
                }
            }

            HashSet <Rect> dirtyRects = new HashSet <Rect>();

            int index      = 0;
            int resolution = Mathf.RoundToInt(Mathf.Sqrt(newValues.LongLength));

            for (int rectIndex = 0; rectIndex < rects.Length; ++rectIndex)
            {
                Rect r      = rects[rectIndex];
                int  startX = (int)Mathf.Lerp(0, resolution - 1, r.min.x);
                int  startY = (int)Mathf.Lerp(0, resolution - 1, r.min.y);
                int  endX   = (int)Mathf.Lerp(0, resolution - 1, r.max.x);
                int  endY   = (int)Mathf.Lerp(0, resolution - 1, r.max.y);
                for (int x = startX; x <= endX; ++x)
                {
                    for (int y = startY; y <= endY; ++y)
                    {
                        index = GUtilities.To1DIndex(x, y, resolution);
                        if (oldValues[index].Equals(newValues[index]))
                        {
                            continue;
                        }
                        dirtyRects.Add(r);

                        Rect hRect = new Rect();
                        hRect.size   = new Vector2(r.width * 1.2f, r.height);
                        hRect.center = r.center;
                        dirtyRects.Add(hRect);

                        Rect vRect = new Rect();
                        vRect.size   = new Vector2(r.width, r.height * 1.2f);
                        vRect.center = r.center;
                        dirtyRects.Add(vRect);
                        break;
                    }
                    if (dirtyRects.Contains(r))
                    {
                        break;
                    }
                }
            }

            return(dirtyRects);
        }
Exemple #7
0
 public void CleanUp()
 {
     if (geometry != null && geometry.subDivisionMap != null)
     {
         GUtilities.DestroyObject(geometry.subDivisionMap);
     }
 }
Exemple #8
0
        public GCombineInfo(Mesh m)
        {
            if (m != null)
            {
                vertices  = m.vertices;
                uvs       = m.uv;
                colors    = m.colors32;
                triangles = m.triangles;
                transform = new Matrix4x4();

                if (uvs.Length != vertices.Length)
                {
                    uvs = new Vector2[vertices.Length];
                    GUtilities.Fill(uvs, Vector2.zero);
                }
                if (colors.Length != vertices.Length)
                {
                    colors = new Color32[vertices.Length];
                    GUtilities.Fill(colors, Color.clear);
                }
            }
            else
            {
                vertices  = null;
                uvs       = null;
                colors    = null;
                triangles = null;
                transform = new Matrix4x4();
            }
        }
Exemple #9
0
 public void RemoveMaskMap()
 {
     if (maskMap != null)
     {
         GUtilities.DestroyObject(maskMap);
     }
 }
Exemple #10
0
        internal NativeArray <bool> GetVertexMarkerFromMeshUV(int lod)
        {
            int dimension = GGeometryJobUtilities.VERTEX_MARKER_DIMENSION;
            NativeArray <bool> markers = new NativeArray <bool>(dimension * dimension, Allocator.TempJob);

            Mesh m = GetMesh(lod);

            Vector2[] uvs = m.uv;
            int       x   = 0;
            int       y   = 0;
            Vector2   uv  = Vector2.zero;

            for (int i = 0; i < uvs.Length; ++i)
            {
                uv = uvs[i];
                x  = (int)(uv.x * (dimension - 1));
                y  = (int)(uv.y * (dimension - 1));
                markers[GGeometryJobUtilities.To1DIndex(ref x, ref y, ref dimension)] = true;
            }

            GUtilities.EnsureArrayLength(ref vertexMarker_Cache, markers.Length);
            markers.CopyTo(vertexMarker_Cache);

            return(markers);
        }
Exemple #11
0
 public void RemoveHeightMap()
 {
     if (heightMap != null)
     {
         GUtilities.DestroyObject(heightMap);
     }
 }
Exemple #12
0
 public void RemoveAlbedoMap()
 {
     if (albedoMap != null)
     {
         GUtilities.DestroyObject(albedoMap);
     }
 }
Exemple #13
0
        internal void InitMeshDataNativeContainers(GAlbedoToVertexColorMode albedoToVertexColor)
        {
            int vertexCount = generationMetadata[0] * 3;

            vertexNativeArray = new NativeArray <Vector3>(vertexCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            GUtilities.EnsureArrayLength(ref vertexArray, vertexCount);

            uvsNativeArray = new NativeArray <Vector2>(vertexCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            GUtilities.EnsureArrayLength(ref uvsArray, vertexCount);

            trianglesNativeArray = new NativeArray <int>(vertexCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            GUtilities.EnsureArrayLength(ref trianglesArray, vertexCount);

            normalsNativeArray = new NativeArray <Vector3>(vertexCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            GUtilities.EnsureArrayLength(ref normalsArray, vertexCount);

            if (albedoToVertexColor == GAlbedoToVertexColorMode.None)
            {
                vertexColorsNativeArray = new NativeArray <Color32>(1, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            }
            else
            {
                vertexColorsNativeArray = new NativeArray <Color32>(vertexCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                GUtilities.EnsureArrayLength(ref vertexColorsArray, vertexCount);
            }
        }
Exemple #14
0
        public static Color[] GetPixels(Texture2D src, int startX, int startY, int width, int height, Color defaultColor)
        {
            int     endX    = startX + width - 1;
            int     endY    = startY + height - 1;
            Vector2 startUV = new Vector2(
                GUtilities.InverseLerpUnclamped(0, src.width - 1, startX),
                GUtilities.InverseLerpUnclamped(0, src.height - 1, startY));
            Vector2 endUV = new Vector2(
                GUtilities.InverseLerpUnclamped(0, src.width - 1, endX),
                GUtilities.InverseLerpUnclamped(0, src.height - 1, endY));
            Material mat = GInternalMaterials.CopyTextureMaterial;

            mat.SetTexture("_MainTex", src);
            mat.SetVector("_StartUV", startUV);
            mat.SetVector("_EndUV", endUV);
            mat.SetColor("_DefaultColor", defaultColor);
            mat.SetPass(0);
            RenderTexture rt = new RenderTexture(width, height, 32);

            RenderTexture.active = rt;
            Graphics.Blit(src, mat);

            Texture2D tex = new Texture2D(width, height);

            tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            Color[] colors = tex.GetPixels();

            RenderTexture.active = null;
            rt.Release();
            Object.DestroyImmediate(rt);
            Object.DestroyImmediate(tex);

            return(colors);
        }
Exemple #15
0
        public static RenderTexture CopyToRT(Texture src, int startX, int startY, int width, int height, Color defaultColor)
        {
            int     endX    = startX + width - 1;
            int     endY    = startY + height - 1;
            Vector2 startUV = new Vector2(
                GUtilities.InverseLerpUnclamped(0, src.width - 1, startX),
                GUtilities.InverseLerpUnclamped(0, src.height - 1, startY));
            Vector2 endUV = new Vector2(
                GUtilities.InverseLerpUnclamped(0, src.width - 1, endX),
                GUtilities.InverseLerpUnclamped(0, src.height - 1, endY));
            Material mat = GInternalMaterials.CopyTextureMaterial;

            mat.SetTexture("_MainTex", src);
            mat.SetVector("_StartUV", startUV);
            mat.SetVector("_EndUV", endUV);
            mat.SetColor("_DefaultColor", defaultColor);
            mat.SetPass(0);
            RenderTexture rt = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);

            RenderTexture.active = rt;
            Graphics.Blit(src, mat);
            RenderTexture.active = null;

            return(rt);
        }
Exemple #16
0
 public static void FillTexture(Texture2D t, Color c)
 {
     Color[] colors = new Color[t.width * t.height];
     GUtilities.Fill(colors, c);
     t.SetPixels(colors);
     t.Apply();
 }
Exemple #17
0
 internal void InvokeGrassChange(Vector2 gridIndex)
 {
     if (GrassPatchChanged != null)
     {
         int index = GUtilities.To1DIndex((int)gridIndex.x, (int)gridIndex.y, Foliage.PatchGridSize);
         GrassPatchChanged.Invoke(index);
     }
 }
Exemple #18
0
 internal void CacheVertexMarker()
 {
     if (vertexMarkerNativeArray.IsCreated)
     {
         GUtilities.EnsureArrayLength(ref vertexMarker_Cache, vertexMarkerNativeArray.Length);
         vertexMarkerNativeArray.CopyTo(vertexMarker_Cache);
     }
 }
Exemple #19
0
        public static void FillTexture(RenderTexture rt, Color c)
        {
            Texture2D tex = new Texture2D(1, 1, TextureFormat.ARGB32, false);

            tex.SetPixel(0, 0, c);
            tex.Apply();
            CopyToRT(tex, rt);
            GUtilities.DestroyObject(tex);
        }
Exemple #20
0
        public static void CopyTexture(Texture2D src, Texture2D des)
        {
            RenderTexture rt = new RenderTexture(des.width, des.height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);

            CopyToRT(src, rt);
            CopyFromRT(des, rt);
            rt.Release();
            GUtilities.DestroyObject(rt);
        }
Exemple #21
0
        public static Texture2D CreateTexture(int resolution, Color fill, TextureFormat format = TextureFormat.ARGB32, bool linear = true)
        {
            Texture2D t = new Texture2D(resolution, resolution, format, false, linear);

            Color[] colors = new Color[resolution * resolution];
            GUtilities.Fill(colors, fill);
            t.SetPixels(colors);
            t.Apply();
            return(t);
        }
 public static void ResetArray <T>(ref T[] array, int count, T defaultValue)
 {
     if (array != null && array.Length == count)
     {
         GUtilities.Fill(array, defaultValue);
     }
     else
     {
         array = new T[count];
     }
 }
        private CapsuleCollider GetCollider(int index)
        {
            if (Colliders[index] == null)
            {
                GameObject g = new GameObject("Collider");
                GUtilities.ResetTransform(g.transform, transform);

                CapsuleCollider col = g.AddComponent <CapsuleCollider>();
                Colliders[index] = col;
            }
            return(Colliders[index]);
        }
        public void UpdateBillboardMesh(BillboardAsset billboard)
        {
            if (BillboardMeshes.ContainsKey(billboard) && BillboardMeshes[billboard] != null)
            {
                GUtilities.DestroyObject(BillboardMeshes[billboard]);
                BillboardMeshes.Remove(billboard);
            }

            Mesh m = CreateBillboardMesh(billboard);

            BillboardMeshes.Add(billboard, m);
        }
Exemple #25
0
 private void OnDisable()
 {
     if (Terrain != null)
     {
         Terrain.PreProcessHeightMap  -= OnPreProcessHeightMap;
         Terrain.PostProcessHeightMap -= OnPostProcessHeightMap;
     }
     if (heightMapBackup != null)
     {
         GUtilities.DestroyObject(heightMapBackup);
     }
 }
 public void DeleteMesh(Vector3Int key)
 {
     if (Meshes.ContainsKey(key))
     {
         Mesh m = Meshes[key];
         if (m != null)
         {
             GUtilities.DestroyObject(m);
         }
         Meshes.Remove(key);
         GCommon.SetDirty(this);
     }
 }
 public void DeleteMesh(string key)
 {
     if (GeneratedMeshes.ContainsKey(key))
     {
         Mesh m = GeneratedMeshes[key];
         if (m != null)
         {
             GUtilities.DestroyObject(m);
         }
         GeneratedMeshes.Remove(key);
         GCommon.SetDirty(this);
     }
 }
Exemple #28
0
 public void RemoveSplatControlMaps()
 {
     if (splatControls != null)
     {
         for (int i = 0; i < splatControls.Length; ++i)
         {
             if (splatControls[i] != null)
             {
                 GUtilities.DestroyObject(splatControls[i]);
             }
         }
     }
 }
        private void Export()
        {
            RenderTexture rt = new RenderTexture(resolution, resolution, 24, RenderTextureFormat.ARGB32);

            GCommon.FillTexture(rt, Color.clear);

            Vector2[] uv        = targetMesh.uv;
            int[]     tris      = targetMesh.triangles;
            int       trisCount = tris.Length / 3;

            CreateLineMaterial();
            // Apply the line material
            lineMaterial.SetPass(0);

            RenderTexture.active = rt;
            GL.PushMatrix();
            GL.LoadOrtho();
            // Draw lines
            GL.Begin(GL.LINES);
            for (int i = 0; i < trisCount; ++i)
            {
                GL.Color(Color.black);
                Vector2 p0 = uv[tris[i * 3 + 0]];
                Vector2 p1 = uv[tris[i * 3 + 1]];
                Vector2 p2 = uv[tris[i * 3 + 2]];

                GL.Vertex3(p0.x, p0.y, 0);
                GL.Vertex3(p1.x, p1.y, 0);

                GL.Vertex3(p1.x, p1.y, 0);
                GL.Vertex3(p2.x, p2.y, 0);

                GL.Vertex3(p2.x, p2.y, 0);
                GL.Vertex3(p0.x, p0.y, 0);
            }
            GL.End();
            GL.PopMatrix();

            Texture2D tex = new Texture2D(resolution, resolution, TextureFormat.ARGB32, false);

            tex.ReadPixels(new Rect(0, 0, resolution, resolution), 0, 0);
            tex.Apply();

            byte[] data = tex.EncodeToPNG();
            File.WriteAllBytes(string.Format("Assets/{0}_UV_{1}.png", targetMesh.name, resolution), data);
            GUtilities.DestroyObject(tex);

            RenderTexture.active = null;
            rt.Release();
            GUtilities.DestroyObject(rt);
        }
Exemple #30
0
 internal void CleanUpNonSerializedMeshes()
 {
     if (nonSerializedMeshes != null)
     {
         for (int i = 0; i < nonSerializedMeshes.Length; ++i)
         {
             Mesh m = nonSerializedMeshes[i];
             if (m != null)
             {
                 GUtilities.DestroyObject(m);
             }
         }
     }
 }