Search Unity

Textures on dynamically generated Terrain only visible in Editor, not in exported App

Discussion in 'Scripting' started by _Sven_, Nov 21, 2017.

  1. _Sven_

    _Sven_

    Joined:
    Nov 17, 2014
    Posts:
    3
    Hey there!
    I have a problem and, since I am not a really experienced Unity3D user, have no idea to solve this.
    So in my application there is a GUI which creates a dynamically generated environment after clicking a button.
    When executing in editor, the result looks like this:

    The code used to do this looks like this:

    Code (CSharp):
    1. terrainData = new TerrainData();
    2. terrainData.heightmapResolution = 256;
    3. terrainData.alphamapResolution = 256;
    4.  
    5. var heightmap = new float[terrainData.heightmapResolution, terrainData.heightmapResolution];
    6.  
    7. //... left out code: build up heightmap ....
    8.  
    9. terrainData.SetHeights(0, 0, heightmap);
    10.  
    11. var flatSplat = new SplatPrototype();
    12. var steepSplat = new SplatPrototype();
    13.  
    14. flatSplat.texture = FlatTexture;
    15. steepSplat.texture = SteepTexture;
    16.  
    17. terrainData.splatPrototypes = new SplatPrototype[]
    18. {
    19.     flatSplat,
    20.     steepSplat
    21. };
    22.  
    23. terrainData.RefreshPrototypes();
    24.  
    25. var splatMap = new float[terrainData.alphamapResolution, terrainData.alphamapResolution, 2];
    26.  
    27. //... left out code: build up splat map ...
    28.  
    29. terrainData.SetAlphamaps(0, 0, splatMap);
    30.  
    31. terrainData.size = new Vector3(length, height, width);
    32. var newTerrainGameObject = Terrain.CreateTerrainGameObject(terrainData);
    33. newTerrainGameObject.transform.position = new Vector3(x, 0, y);
    34.  
    35. Terrain = newTerrainGameObject.GetComponent<Terrain>();
    36. Terrain.heightmapPixelError = 8;
    37. Terrain.materialType = UnityEngine.Terrain.MaterialType.Custom;
    38. Terrain.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
    The settings for e.g. the ground texture are as following when analyzed in play mode within unity:


    When exporting the application to an standalone executable, everything works perfectly, all the objects are textured but there are some problems:
    - The grass texture of the terrain is not shown correctly
    - The ground textures of the terrain is not shown correctly
    The files are located in the resources folder

    Here is an image how it looks like



    Do you have an idea what I am doing wrong?

    Thank you very much in advance!
     
  2. _Sven_

    _Sven_

    Joined:
    Nov 17, 2014
    Posts:
    3
    Problem solved:
    I made a debug build and found out that there were some standard shaders for the Nature missing since I build the terrain dynamically. Adding these three shaders (Nature/Terrain/*) into the Graphics settings of the project solved the problem: