Beispiel #1
0
        private void RenderGeometryHeightMap(GStylizedTerrain terrain, RenderTexture targetRt)
        {
            GGeneralParams param = GTextureToolParams.Instance.General;
            RenderTexture  rt    = terrain.GetHeightMap(param.Resolution);

            GCommon.CopyToRT(rt, targetRt);
        }
        private void DrawGeneralParamsGUI()
        {
            string id    = "texture-editor-general";
            string label = "General Parameters";

            GEditorCommon.Foldout(label, true, id, () =>
            {
                GGeneralParams param = GTextureToolParams.Instance.General;

                param.Mode       = (GTextureGenerationMode)EditorGUILayout.EnumPopup("Mode", param.Mode);
                param.Resolution = EditorGUILayout.IntPopup("Resolution", param.Resolution, textureResolutionLabels, textureResolutionValues);
                param.Extension  = (GImageFileExtension)EditorGUILayout.EnumPopup("Extension", param.Extension);
                param.UseHighPrecisionTexture = EditorGUILayout.Toggle("High Precision", param.UseHighPrecisionTexture);
                string dir = param.Directory;
                GEditorCommon.BrowseFolderMiniButton("Directory", ref dir);
                param.Directory = dir;

                GTextureToolParams.Instance.General = param;
            });
        }
        private void SaveAsset()
        {
            RecordAnalytics();
            RenderPreviewTexture();

            GGeneralParams generalParams = GTextureToolParams.Instance.General;

            GUtilities.EnsureDirectoryExists(generalParams.Directory);

            string ext =
                generalParams.Extension == GImageFileExtension.PNG ? "png" :
                generalParams.Extension == GImageFileExtension.JPG ? "jpg" :
                generalParams.Extension == GImageFileExtension.EXR ? "exr" :
                generalParams.Extension == GImageFileExtension.TGA ? "tga" : "file";
            string fileName = string.Format("{0}_{1}.{2}", GCommon.GetTimeTick(), generalParams.Mode.ToString(), ext);
            string filePath = Path.Combine(generalParams.Directory, fileName);

            TextureFormat format = generalParams.UseHighPrecisionTexture ? TextureFormat.RGBAFloat : TextureFormat.RGBA32;
            Texture2D     tex    = new Texture2D(generalParams.Resolution, generalParams.Resolution, format, false);

            tex.wrapMode = TextureWrapMode.Clamp;
            GCommon.CopyFromRT(tex, PreviewRt);

            byte[] data =
                generalParams.Extension == GImageFileExtension.PNG ? tex.EncodeToPNG() :
                generalParams.Extension == GImageFileExtension.JPG ? tex.EncodeToPNG() :
                generalParams.Extension == GImageFileExtension.EXR ? tex.EncodeToPNG() :
                generalParams.Extension == GImageFileExtension.TGA ? tex.EncodeToPNG() : new byte[0];
            File.WriteAllBytes(filePath, data);

            GUtilities.DestroyObject(tex);
            AssetDatabase.Refresh();

            Object o = AssetDatabase.LoadAssetAtPath <Texture2D>(filePath);

            if (o != null)
            {
                EditorGUIUtility.PingObject(o);
                Selection.activeObject = o;
            }
        }
        private void OnBeginCameraRender(Camera cam)
        {
            GLivePreviewParams livePreviewParam = GTextureToolParams.Instance.LivePreview;

            if (!livePreviewParam.Enable || livePreviewParam.Terrain == null)
            {
                return;
            }

            GGeneralParams generalParam = GTextureToolParams.Instance.General;

            Material mat = GInternalMaterials.UnlitTextureMaterial;

            livePreviewMaterialProperties.Clear();

            if (livePreviewParam.Mode == GLivePreviewMode.Mask)
            {
                mat = GInternalMaterials.MaskVisualizerMaterial;
                livePreviewMaterialProperties.SetTexture("_MainTex", PreviewRt);
                livePreviewMaterialProperties.SetColor("_Color", livePreviewParam.Color);
            }
            else if (livePreviewParam.Mode == GLivePreviewMode.ColorMap)
            {
                mat = GInternalMaterials.UnlitTextureMaterial;
                livePreviewMaterialProperties.SetTexture("_MainTex", PreviewRt);
            }
            else if (livePreviewParam.Mode == GLivePreviewMode.Geometry)
            {
                mat = GInternalMaterials.GeometryLivePreviewMaterial;
                GStylizedTerrain t           = livePreviewParam.Terrain;
                Vector3          terrainSize = new Vector3(
                    t.TerrainData.Geometry.Width,
                    t.TerrainData.Geometry.Height,
                    t.TerrainData.Geometry.Length);
                livePreviewMaterialProperties.SetTexture("_OldHeightMap", t.TerrainData.Geometry.HeightMap);
                livePreviewMaterialProperties.SetTexture("_NewHeightMap", PreviewRt);
                livePreviewMaterialProperties.SetTexture("_MainTex", PreviewRt);
                livePreviewMaterialProperties.SetFloat("_Height", t.TerrainData.Geometry.Height);
                livePreviewMaterialProperties.SetVector("_BoundMin", t.transform.position);
                livePreviewMaterialProperties.SetVector("_BoundMax", t.transform.TransformPoint(terrainSize));
            }

            GTerrainChunk[] chunks = livePreviewParam.Terrain.GetChunks();
            for (int i = 0; i < chunks.Length; ++i)
            {
                Mesh m = chunks[i].MeshFilterComponent.sharedMesh;
                if (m == null)
                {
                    continue;
                }
                Graphics.DrawMesh(
                    m,
                    chunks[i].transform.localToWorldMatrix,
                    mat,
                    chunks[i].gameObject.layer,
                    cam,
                    0,
                    livePreviewMaterialProperties,
                    false,
                    false);
            }
        }