Search Unity

Errors while Generating Terrain in batchmode/headless

Discussion in 'Scripting' started by ZackNewworldcoders, Nov 12, 2020.

  1. ZackNewworldcoders

    ZackNewworldcoders

    Joined:
    Jul 19, 2016
    Posts:
    20
    hi there.

    Im using MapMagic2 to generate some terrains. Everything works fine in a normal stand alone.

    However when launching the application with the -batchmode arugument, to run the application in dedicated server mode, im given errors while generating the terrain

    Specifically on
    data.CopyActiveRenderTextureToHeightmap(rect, rect.min, TerrainHeightmapSyncControl.None);

    this throws a "ActiveRenderTexture is null" most likley because its running without graphics and such so this happens.... i also get some other errors that i havent tracked down yet but... maybe some one might know where to start, so ive included them:

    ArgumentException: Kernel 'MergedInstancedIndirectBuffers' not found


    I feel i am missing somthing, like there is another method of doing this in a non-graphics enviroment,


    I also notice that unity isnt printing the logs out to the AppData/LocalLow/<Project>/ Directory area that a stand alone normally does. Why not? and can i turn it on?

    Thanks


    on second look im noticing in the MapMagic Code he is using Graphics.Blit() to do somthing and then sets renTex as the Render Texture... i assume this is returning null because its using the Graphics namespace/class? and its running in headless? not sure.. heres the snippit of code.... any help is appreciated


    Code (CSharp):
    1. if (terrain==null || terrain.Equals(null) || terrain.terrainData==null) yield break; //chunk removed during apply
    2.                 TerrainData data = terrain.terrainData;
    3.  
    4.                 Profiler.BeginSample("PrepareTexApply");
    5.                 RectInt texRect = new RectInt(0,0,res,res);
    6.  
    7.                 if (tempTex == null ||  tempTex.width != res)
    8.                     tempTex = new Texture2D(res, res, TextureFormat.RFloat, mipChain:false, linear:true);
    9.                 Profiler.BeginSample("Load Tex");
    10.                 tempTex.LoadRawTextureData(texBytes);
    11.                 tempTex.Apply(updateMipmaps:false);
    12.                 Profiler.EndSample();
    13.  
    14.                 if (renTex == null ||  renTex.width != res)
    15.                 #if UNITY_2019_2_OR_NEWER
    16.                     renTex = new RenderTexture(res,res,32, RenderTextureFormat.RFloat, mipCount:0);
    17.                 #else
    18.                     renTex = new RenderTexture(res,res,32, RenderTextureFormat.RFloat);
    19.                 #endif
    20.                    
    21.                 Profiler.BeginSample("Blit Tex");
    22.                 Graphics.Blit(tempTex, renTex);
    23.                 Profiler.EndSample();
    24.  
    25.                 //no resize algorithm
    26.                 Profiler.BeginSample("Change Res");
    27.                 Vector3 terrainSize = data.size;
    28.                 //data.heightmapResolution = res;
    29.                 data.size = new Vector3(terrainSize.x, height, terrainSize.z);
    30.                 ApplySplitData.FastHeightmapResize(terrain, res, new Vector3(terrainSize.x, height, terrainSize.z));
    31.                 terrain.groupingID = res;
    32.                 Profiler.EndSample();
    33.  
    34.                 //splitting in parts
    35.                 int numSplits = (int)(res/splitSize);
    36.                 if (numSplits*splitSize < res) numSplits++;
    37.                 Profiler.EndSample();
    38.  
    39.                 for (int sx = 0; sx<numSplits; sx++)
    40.                     for (int sz = 0; sz<numSplits; sz++)
    41.                     {
    42.                         Profiler.BeginSample("Apply Heightmap");
    43.  
    44.                         Profiler.BeginSample("Set active rendertex");
    45.                         RenderTexture bacRenTex = RenderTexture.active;
    46.                         RenderTexture.active = renTex;
    47.                         Profiler.EndSample();
    48.  
    49.                         RectInt rect = new RectInt(sx*splitSize, sz*splitSize, splitSize, splitSize);
    50.                         rect.xMax = Mathf.Min(rect.xMax, res);
    51.                         rect.yMax = Mathf.Min(rect.yMax, res);
    52.  
    53.                        
    54.  
    55.                        
    56.                             Profiler.BeginSample("Copy to heightmap");
    57.                             data.CopyActiveRenderTextureToHeightmap(rect, rect.min, TerrainHeightmapSyncControl.None);
    58.                             Profiler.EndSample();
    59.                        
     
    Last edited: Nov 12, 2020