Beispiel #1
0
 public void ResetFull()
 {
     Reset();
     GCommon.FillTexture(HeightMap, Color.clear);
     SetRegionDirty(GCommon.UnitRect);
     TerrainData.SetDirty(GTerrainData.DirtyFlags.GeometryTimeSliced);
 }
Beispiel #2
0
        internal void Internal_CreateNewSubDivisionMap(Texture altHeightMap)
        {
            if (subDivisionMap != null)
            {
                if (subDivisionMap.width != GCommon.SUB_DIV_MAP_RESOLUTION ||
                    subDivisionMap.height != GCommon.SUB_DIV_MAP_RESOLUTION)
                {
                    Object.DestroyImmediate(subDivisionMap);
                }
            }

            if (subDivisionMap == null)
            {
                subDivisionMap = new Texture2D(GCommon.SUB_DIV_MAP_RESOLUTION, GCommon.SUB_DIV_MAP_RESOLUTION, TextureFormat.ARGB32, false);
            }

            int           resolution = GCommon.SUB_DIV_MAP_RESOLUTION;
            RenderTexture rt         = new RenderTexture(resolution, resolution, 0, RenderTextureFormat.ARGB32);
            Material      mat        = GInternalMaterials.SubDivisionMapMaterial;

            Graphics.Blit(altHeightMap, rt, mat);
            GCommon.CopyFromRT(subDivisionMap, rt);
            rt.Release();
            Object.DestroyImmediate(rt);
        }
Beispiel #3
0
        public static void OnSceneViewGUI(SceneView sv)
        {
            Event e = Event.current;

            if (e == null)
            {
                return;
            }
            int controlId = EditorGUIUtility.GetControlID(FocusType.Passive, sv.position);

            if (e.type == EventType.DragUpdated)
            {
                Object[] draggedObjects = DragAndDrop.objectReferences;
                if (draggedObjects.Length == 1 &&
                    draggedObjects[0] is GTerrainData)
                {
                    DragAndDrop.AcceptDrag();
                    DragAndDrop.visualMode      = DragAndDropVisualMode.Generic;
                    DragAndDrop.activeControlID = controlId;
                    e.Use();
                }
            }
            else if (e.type == EventType.DragPerform)
            {
                Object[] draggedObjects = DragAndDrop.objectReferences;
                if (draggedObjects.Length == 1 &&
                    draggedObjects[0] is GTerrainData)
                {
                    GTerrainData data = draggedObjects[0] as GTerrainData;
                    GCommon.CreateTerrain(data);
                    e.Use();
                }
            }
        }
        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();
            }
        }
Beispiel #5
0
 private void RestoreHeightMap(Texture2D heightMap)
 {
     if (heightMapBackup != null && heightMapBackup.IsCreated())
     {
         GCommon.CopyFromRT(heightMap, heightMapBackup);
     }
 }
        private void ResizeSplatControlMaps()
        {
            for (int i = 0; i < SplatControlMapCount; ++i)
            {
                Texture2D t = GetSplatControl(i);
                if (t == null)
                {
                    return;
                }
                Texture2D     tmp = GCommon.CreateTexture(SplatControlResolution, Color.clear);
                RenderTexture rt  = new RenderTexture(SplatControlResolution, SplatControlResolution, 32, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
                GCommon.CopyToRT(t, rt);
                GCommon.CopyFromRT(tmp, rt);
                rt.Release();
                Object.DestroyImmediate(rt);

                tmp.name       = t.name;
                tmp.filterMode = t.filterMode;
                tmp.wrapMode   = t.wrapMode;
                Object.DestroyImmediate(t, true);
                SplatControls[i] = tmp;
                GCommon.TryAddObjectToAsset(tmp, TerrainData);
            }

            UpdateMaterials();
        }
Beispiel #7
0
        private void ReFormatHeightMap()
        {
            if (heightMap == null)
            {
                return;
            }
            if (heightmapVersion < HEIGHT_MAP_VERSION_ENCODE_RG)
            {
                Texture2D     tmp = new Texture2D(HeightMapResolution, HeightMapResolution, HeightMapFormat, false);
                RenderTexture rt  = new RenderTexture(HeightMapResolution, HeightMapResolution, 32, HeightMapRTFormat);
                Material      mat = GInternalMaterials.HeightmapConverterEncodeRGMaterial;
                mat.SetTexture("_MainTex", heightMap);
                GCommon.DrawQuad(rt, GCommon.FullRectUvPoints, mat, 0);
                GCommon.CopyFromRT(tmp, rt);
                rt.Release();
                Object.DestroyImmediate(rt);

                tmp.name       = heightMap.name;
                tmp.filterMode = heightMap.filterMode;
                tmp.wrapMode   = heightMap.wrapMode;
                Object.DestroyImmediate(heightMap, true);
                heightMap = tmp;
                GCommon.TryAddObjectToAsset(heightMap, TerrainData);

                heightmapVersion = HEIGHT_MAP_VERSION_ENCODE_RG;
                Debug.Log("Polaris auto upgrade: Converted Height Map from RGBAFloat to RGBA32.");
            }
        }
 public void ResetFull()
 {
     Reset();
     GCommon.FillTexture(AlbedoMap, Color.clear);
     GCommon.FillTexture(MetallicMap, Color.clear);
     UpdateLookupTextures();
     TerrainData.SetDirty(GTerrainData.DirtyFlags.Shading);
     UpdateMaterials();
 }
Beispiel #9
0
        internal void Internal_UpdateMeshLOD0()
        {
            Mesh meshLod0 = GetMesh(0);

            UpdateMesh(meshLod0, subDivTree);
            MeshFilterComponent.sharedMesh   = meshLod0;
            MeshColliderComponent.sharedMesh = meshLod0;
            GCommon.SetDirty(meshLod0);
        }
        public static void DrawGeometryLivePreview(GStylizedTerrain t, Camera cam, Texture newHeightMap, Rect dirtyRect)
        {
            if (t.transform.rotation != Quaternion.identity ||
                t.transform.lossyScale != Vector3.one)
            {
                return;
            }

            Mesh previewMesh = GGriffinSettings.Instance.GetLivePreviewMesh(t.TerrainData.Geometry.MeshResolution);

            Vector3 terrainSize = new Vector3(
                t.TerrainData.Geometry.Width,
                t.TerrainData.Geometry.Height,
                t.TerrainData.Geometry.Length);

            previewPropertyBlock.Clear();
            previewPropertyBlock.SetTexture("_OldHeightMap", t.TerrainData.Geometry.HeightMap);
            previewPropertyBlock.SetTexture("_NewHeightMap", newHeightMap);
            previewPropertyBlock.SetTexture("_MainTex", newHeightMap);
            previewPropertyBlock.SetFloat("_Height", t.TerrainData.Geometry.Height);
            previewPropertyBlock.SetVector("_BoundMin", t.transform.position);
            previewPropertyBlock.SetVector("_BoundMax", t.transform.TransformPoint(terrainSize));
            Material mat = GInternalMaterials.GeometryLivePreviewMaterial;

            mat.renderQueue = 4000;

            int gridSize = t.TerrainData.Geometry.ChunkGridSize;

            for (int x = 0; x < gridSize; ++x)
            {
                for (int z = 0; z < gridSize; ++z)
                {
                    Rect uvRect = GCommon.GetUvRange(gridSize, x, z);
                    if (!uvRect.Overlaps(dirtyRect))
                    {
                        continue;
                    }
                    Vector3 localPos = new Vector3(
                        terrainSize.x * uvRect.x,
                        0f,
                        terrainSize.z * uvRect.y);
                    Vector3    worldPos = t.transform.TransformPoint(localPos);
                    Quaternion rotation = Quaternion.identity;
                    Vector3    scale    = new Vector3(terrainSize.x * uvRect.width, 1, terrainSize.z * uvRect.height);

                    Graphics.DrawMesh(
                        previewMesh,
                        Matrix4x4.TRS(worldPos, rotation, scale),
                        mat,
                        LayerMask.NameToLayer("Default"),
                        cam,
                        0,
                        previewPropertyBlock);
                }
            }
        }
Beispiel #11
0
        public Vector4 GetDecodedHeightMapSample(Vector2 uv)
        {
            Vector4 c             = HeightMap.GetPixelBilinear(uv.x, uv.y);
            Vector2 encodedHeight = new Vector2(c.x, c.y);
            float   decodedHeight = GCommon.DecodeTerrainHeight(encodedHeight);

            c.x = decodedHeight;
            c.y = decodedHeight;
            return(c);
        }
 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);
     }
 }
Beispiel #13
0
        private static void InitPolygonProcessorCollection()
        {
            List <Type> loadedTypes = GCommon.GetAllLoadedTypes();

            for (int i = 0; i < loadedTypes.Count; ++i)
            {
                Type t = loadedTypes[i];
                if (t.GetInterface(PolygonProcessorInterfaceName) != null)
                {
                    PolygonProcessorTypeCollection.Add(t);
                }
            }
        }
 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);
     }
 }
        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);
        }
 public void SetMesh(string key, Mesh mesh)
 {
     if (GeneratedMeshes.ContainsKey(key))
     {
         Mesh oldMesh = GeneratedMeshes[key];
         if (oldMesh != null)
         {
             GUtilities.DestroyObject(oldMesh);
         }
         GeneratedMeshes.Remove(key);
     }
     GCommon.TryAddObjectToAsset(mesh, this);
     GeneratedMeshes.Add(key, mesh);
     GCommon.SetDirty(this);
 }
        public void ConvertSplatsToAlbedo()
        {
            if (Splats == null)
            {
                return;
            }
            RenderTexture albedoRt = new RenderTexture(AlbedoMapResolution, AlbedoMapResolution, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
            Material      mat      = GInternalMaterials.SplatsToAlbedoMaterial;

            for (int i = 0; i < SplatControlMapCount; ++i)
            {
                Texture2D controlMap = GetSplatControl(i);
                mat.SetTexture("_Control0", controlMap);
                for (int channel = 0; channel < 4; ++channel)
                {
                    int prototypeIndex = i * 4 + channel;
                    if (prototypeIndex < Splats.Prototypes.Count)
                    {
                        GSplatPrototype p = Splats.Prototypes[prototypeIndex];
                        mat.SetTexture("_Splat" + channel, p.Texture);
                        Vector2 terrainSize  = new Vector2(TerrainData.Geometry.Width, TerrainData.Geometry.Length);
                        Vector2 textureScale = new Vector2(
                            p.TileSize.x != 0 ? terrainSize.x / p.TileSize.x : 0,
                            p.TileSize.y != 0 ? terrainSize.y / p.TileSize.y : 0);
                        Vector2 textureOffset = new Vector2(
                            p.TileOffset.x != 0 ? terrainSize.x / p.TileOffset.x : 0,
                            p.TileOffset.y != 0 ? terrainSize.y / p.TileOffset.y : 0);
                        mat.SetTextureScale("_Splat" + channel, textureScale);
                        mat.SetTextureOffset("_Splat" + channel, textureOffset);
                    }
                    else
                    {
                        mat.SetTexture("_Splat" + channel, null);
                        mat.SetTextureScale("_Splat" + channel, Vector2.zero);
                        mat.SetTextureOffset("_Splat" + channel, Vector2.zero);
                    }
                }

                GCommon.DrawQuad(albedoRt, GCommon.FullRectUvPoints, mat, 0);
            }

            GCommon.CopyFromRT(AlbedoMap, albedoRt);
            albedoRt.Release();
            GUtilities.DestroyObject(albedoRt);
        }
Beispiel #18
0
        public static void GetPixels2DNonAlloc(
            Texture2D src,
            int startX, int startZ,
            Color[,] sample,
            Color defaultColor)
        {
            int sampleLength = sample.GetLength(0);
            int sampleWidth  = sample.GetLength(1);

            Color[] colors = GCommon.GetPixels(src, startX, startZ, sampleWidth, sampleLength, defaultColor);
            for (int z = 0; z < sampleLength; ++z)
            {
                for (int x = 0; x < sampleWidth; ++x)
                {
                    sample[z, x] = colors[GUtilities.To1DIndex(x, z, sampleWidth)];
                }
            }
        }
Beispiel #19
0
        public static Color[,] GetPixels2D(
            Texture2D src,
            int startX, int startZ,
            int sampleWidth, int sampleLength,
            Color defaultColor)
        {
            Color[,] sample = new Color[sampleLength, sampleWidth];

            Color[] colors = GCommon.GetPixels(src, startX, startZ, sampleWidth, sampleLength, defaultColor);
            for (int z = 0; z < sampleLength; ++z)
            {
                for (int x = 0; x < sampleWidth; ++x)
                {
                    sample[z, x] = colors[GUtilities.To1DIndex(x, z, sampleWidth)];
                }
            }

            return(sample);
        }
Beispiel #20
0
        public static float[,] GetPixels2D(
            Texture2D src,
            int startX, int startZ,
            int sampleWidth, int sampleLength,
            Color defaultColor,
            System.Func <Color, float> converter)
        {
            float[,] sample = new float[sampleLength, sampleWidth];

            Color[] colors = GCommon.GetPixels(src, startX, startZ, sampleWidth, sampleLength, defaultColor);
            for (int z = 0; z < sampleLength; ++z)
            {
                for (int x = 0; x < sampleWidth; ++x)
                {
                    sample[z, x] = converter(colors[GUtilities.To1DIndex(x, z, sampleWidth)]);
                }
            }

            return(sample);
        }
Beispiel #21
0
        private void ResampleMaskMap()
        {
            if (maskMap == null)
            {
                return;
            }
            Texture2D     tmp = new Texture2D(MaskMapResolution, MaskMapResolution, TextureFormat.RGBA32, false);
            RenderTexture rt  = new RenderTexture(MaskMapResolution, MaskMapResolution, 32, RenderTextureFormat.ARGB32);

            GCommon.CopyToRT(maskMap, rt);
            GCommon.CopyFromRT(tmp, rt);
            rt.Release();
            Object.DestroyImmediate(rt);

            tmp.name       = maskMap.name;
            tmp.filterMode = maskMap.filterMode;
            tmp.wrapMode   = maskMap.wrapMode;
            Object.DestroyImmediate(maskMap, true);
            maskMap = tmp;
            GCommon.TryAddObjectToAsset(maskMap, TerrainData);
        }
 public Texture2D GetSplatControl(int index)
 {
     if (index < 0 || index >= SplatControlMapCount)
     {
         throw new System.ArgumentException("Index must be >=0 and <=SplatControlMapCount");
     }
     else
     {
         Texture2D t = SplatControls[index];
         if (t == null)
         {
             Color fillColor = (index == 0 && SplatControlMapCount == 1) ? new Color(1, 0, 0, 0) : new Color(0, 0, 0, 0);
             t                    = GCommon.CreateTexture(SplatControlResolution, fillColor);
             t.filterMode         = FilterMode.Bilinear;
             t.wrapMode           = TextureWrapMode.Clamp;
             t.name               = SPLAT_CONTROL_MAP_NAME + " " + index;
             SplatControls[index] = t;
         }
         GCommon.TryAddObjectToAsset(t, TerrainData);
         return(t);
     }
 }
        private void ResizeMetallicMap()
        {
            if (metallicMap == null)
            {
                return;
            }
            Texture2D     tmp = GCommon.CreateTexture(MetallicMapResolution, Color.black);
            RenderTexture rt  = new RenderTexture(MetallicMapResolution, MetallicMapResolution, 32, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);

            GCommon.CopyToRT(metallicMap, rt);
            GCommon.CopyFromRT(tmp, rt);
            rt.Release();
            Object.DestroyImmediate(rt);

            tmp.name       = metallicMap.name;
            tmp.filterMode = metallicMap.filterMode;
            tmp.wrapMode   = metallicMap.wrapMode;
            Object.DestroyImmediate(metallicMap, true);
            metallicMap = tmp;
            GCommon.TryAddObjectToAsset(metallicMap, TerrainData);
            UpdateMaterials();
        }
Beispiel #24
0
        private static void OnHierarchyGUI(int instanceID, Rect r)
        {
            Event e = Event.current;

            if (e == null)
            {
                return;
            }
            int controlId = EditorGUIUtility.GetControlID(FocusType.Passive);

            if (e.type == EventType.DragUpdated)
            {
                Object[] draggedObjects = DragAndDrop.objectReferences;
                if (draggedObjects.Length == 1 &&
                    draggedObjects[0] is GTerrainData)
                {
                    DragAndDrop.AcceptDrag();
                    DragAndDrop.visualMode      = DragAndDropVisualMode.Generic;
                    DragAndDrop.activeControlID = controlId;
                    e.Use();
                }
            }
            else if (e.type == EventType.DragPerform)
            {
                Object[] draggedObjects = DragAndDrop.objectReferences;
                if (draggedObjects.Length == 1 &&
                    draggedObjects[0] is GTerrainData)
                {
                    GTerrainData data = draggedObjects[0] as GTerrainData;
                    GCommon.CreateTerrain(data);
                    data.Geometry.ClearDirtyRegions();
                    data.Foliage.ClearTreeDirtyRegions();
                    data.Foliage.ClearGrassDirtyRegions();
                    e.Use();
                }
            }
        }
        private void DrawOnTerrain(GStylizedTerrain t)
        {
            Vector3[] worldCorners = GCommon.GetBrushQuadCorners(transform.position, Radius, 0);
            Vector2[] uvCorners    = new Vector2[worldCorners.Length];
            for (int i = 0; i < uvCorners.Length; ++i)
            {
                uvCorners[i] = t.WorldPointToUV(worldCorners[i]);
            }

            Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners);

            if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1)))
            {
                return;
            }

            RenderTexture rt  = t.GetGrassVectorFieldRenderTexture();
            Material      mat = GInternalMaterials.InteractiveGrassVectorFieldMaterial;

            mat.SetFloat("_Opacity", t.TerrainData.Foliage.BendSensitive);
            int pass = 0;

            GCommon.DrawQuad(rt, uvCorners, mat, pass);
        }
Beispiel #26
0
        private void ResampleHeightMap()
        {
            if (heightMap == null)
            {
                return;
            }
            Texture2D     tmp = new Texture2D(HeightMapResolution, HeightMapResolution, HeightMapFormat, false);
            RenderTexture rt  = new RenderTexture(HeightMapResolution, HeightMapResolution, 32, HeightMapRTFormat);

            GCommon.CopyToRT(heightMap, rt);
            GCommon.CopyFromRT(tmp, rt);
            rt.Release();
            Object.DestroyImmediate(rt);

            tmp.name       = heightMap.name;
            tmp.filterMode = heightMap.filterMode;
            tmp.wrapMode   = heightMap.wrapMode;
            Object.DestroyImmediate(heightMap, true);
            heightMap = tmp;
            GCommon.TryAddObjectToAsset(heightMap, TerrainData);

            Internal_CreateNewSubDivisionMap();
            SetRegionDirty(GCommon.UnitRect);
        }
Beispiel #27
0
 public void Reset()
 {
     id = GCommon.GetUniqueID();
 }
        public static void DrawSubdivLivePreview(GStylizedTerrain t, Camera cam, Texture newHeightMap, Rect dirtyRect, Texture mask, Matrix4x4 worldPointToMaskMatrix)
        {
            if (t.transform.rotation != Quaternion.identity ||
                t.transform.lossyScale != Vector3.one)
            {
                return;
            }

            t.TerrainData.Geometry.Internal_CreateNewSubDivisionMap(newHeightMap);

            GPolygonDistributionMode polygonMode = t.TerrainData.Geometry.PolygonDistribution;
            int baseResolution = polygonMode == GPolygonDistributionMode.Dynamic ?
                                 t.TerrainData.Geometry.MeshBaseResolution :
                                 t.TerrainData.Geometry.MeshResolution;
            int resolution = t.TerrainData.Geometry.MeshResolution;
            int maxLevel   = baseResolution + Mathf.Min(Mathf.FloorToInt(1f / GCommon.SUB_DIV_STEP), resolution - baseResolution);

            Vector3 terrainSize = new Vector3(
                t.TerrainData.Geometry.Width,
                t.TerrainData.Geometry.Height,
                t.TerrainData.Geometry.Length);

            previewPropertyBlock.Clear();
            previewPropertyBlock.SetColor("_Color", new Color(0, 0, 0, 0.6f));
            previewPropertyBlock.SetTexture("_HeightMap", newHeightMap);
            previewPropertyBlock.SetVector("_Dimension", terrainSize);
            previewPropertyBlock.SetVector("_BoundMin", t.transform.position);
            previewPropertyBlock.SetVector("_BoundMax", t.transform.TransformPoint(terrainSize));
            previewPropertyBlock.SetTexture("_SubdivMap", t.TerrainData.Geometry.Internal_SubDivisionMap);
            previewPropertyBlock.SetTexture("_Mask", mask);
            previewPropertyBlock.SetMatrix("_WorldPointToMask", worldPointToMaskMatrix);

            Material mat = GInternalMaterials.SubdivLivePreviewMaterial;

            mat.renderQueue = 4000;
            Mesh previewMesh = null;

            int gridSize = t.TerrainData.Geometry.ChunkGridSize;

            for (int x = 0; x < gridSize; ++x)
            {
                for (int z = 0; z < gridSize; ++z)
                {
                    Rect uvRect = GCommon.GetUvRange(gridSize, x, z);
                    if (!uvRect.Overlaps(dirtyRect))
                    {
                        continue;
                    }
                    Vector3 localPos = new Vector3(
                        terrainSize.x * uvRect.x,
                        0f,
                        terrainSize.z * uvRect.y);
                    Vector3    worldPos = t.transform.TransformPoint(localPos);
                    Quaternion rotation = Quaternion.identity;
                    Vector3    scale    = new Vector3(terrainSize.x * uvRect.width, 1, terrainSize.z * uvRect.height);

                    for (int i = baseResolution; i <= maxLevel; ++i)
                    {
                        previewMesh = GGriffinSettings.Instance.GetLivePreviewWireframeMesh(i);
                        previewPropertyBlock.SetVector("_SubdivRange", new Vector4(
                                                           (i - baseResolution) * GCommon.SUB_DIV_STEP,
                                                           i != maxLevel ? (i - baseResolution + 1) * GCommon.SUB_DIV_STEP : 1f,
                                                           0, 0));

                        Graphics.DrawMesh(
                            previewMesh,
                            Matrix4x4.TRS(worldPos, rotation, scale),
                            mat,
                            LayerMask.NameToLayer("Default"),
                            cam,
                            0,
                            previewPropertyBlock);
                    }
                }
            }
        }
Beispiel #29
0
        private void ConvertToPrefabPrototypeGroup()
        {
            GPrefabPrototypeGroup group = ScriptableObject.CreateInstance <GPrefabPrototypeGroup>();

            for (int i = 0; i < instance.Prototypes.Count; ++i)
            {
                if (instance.Prototypes[i].Shape != GGrassShape.DetailObject)
                {
                    continue;
                }
                GameObject prefab = instance.Prototypes[i].Prefab;
                if (prefab != null)
                {
                    group.Prototypes.Add(prefab);
                }
            }

            string path      = AssetDatabase.GetAssetPath(instance);
            string directory = Path.GetDirectoryName(path);
            string filePath  = Path.Combine(directory, string.Format("{0}_{1}_{2}.asset", instance.name, "Prefabs", GCommon.GetUniqueID()));

            AssetDatabase.CreateAsset(group, filePath);

            Selection.activeObject = group;
        }
        public void UpdateLookupTextures()
        {
            if (colorByHeightMap != null)
            {
                Object.DestroyImmediate(colorByHeightMap, true);
            }
            if (colorByNormalMap != null)
            {
                Object.DestroyImmediate(colorByNormalMap, true);
            }
            if (colorBlendMap != null)
            {
                Object.DestroyImmediate(colorBlendMap, true);
            }
            int width  = 256;
            int height = 8;

            colorByHeightMap            = new Texture2D(width, height, TextureFormat.ARGB32, false, true);
            colorByHeightMap.filterMode = FilterMode.Bilinear;
            colorByHeightMap.wrapMode   = TextureWrapMode.Clamp;
            colorByHeightMap.name       = COLOR_BY_HEIGHT_MAP_NAME;

            colorByNormalMap            = new Texture2D(width, height, TextureFormat.ARGB32, false, true);
            colorByNormalMap.filterMode = FilterMode.Bilinear;
            colorByNormalMap.wrapMode   = TextureWrapMode.Clamp;
            colorByNormalMap.name       = COLOR_BY_NORMAL_MAP_NAME;

            colorBlendMap            = new Texture2D(width, height, TextureFormat.ARGB32, false, true);
            colorBlendMap.filterMode = FilterMode.Bilinear;
            colorBlendMap.wrapMode   = TextureWrapMode.Clamp;
            colorBlendMap.name       = COLOR_BLEND_MAP_NAME;

            Color[] cbhColors = new Color[width * height];
            Color[] cbnColors = new Color[width * height];
            Color[] cbColors  = new Color[width * height];

            for (int x = 0; x < width; ++x)
            {
                float f   = Mathf.InverseLerp(0, width - 1, x);
                Color cbh = ColorByHeight.Evaluate(f);
                Color cbn = ColorByNormal.Evaluate(f);
                Color cb  = Color.white * ColorBlendCurve.Evaluate(f);
                for (int y = 0; y < height; ++y)
                {
                    int index = GUtilities.To1DIndex(x, y, width);
                    cbhColors[index] = cbh;
                    cbnColors[index] = cbn;
                    cbColors[index]  = cb;
                }
            }

            colorByHeightMap.SetPixels(cbhColors);
            colorByHeightMap.Apply();
            GCommon.TryAddObjectToAsset(colorByHeightMap, TerrainData);

            colorByNormalMap.SetPixels(cbnColors);
            colorByNormalMap.Apply();
            GCommon.TryAddObjectToAsset(colorByNormalMap, TerrainData);

            colorBlendMap.SetPixels(cbColors);
            colorBlendMap.Apply();
            GCommon.TryAddObjectToAsset(colorBlendMap, TerrainData);
        }