- Home /
Question by
TJFal · Jan 24, 2018 at 02:21 PM ·
scripting problemterraindataterrain-editor
Why isn't the DetailPrototype being added through a script waving?
I have created an EditorWindow script to create a terrain object in my scene. The script uses a block of information that I am storing in another game object. The game object with the information script on it gives inputs for DetailPrototypes that I want to add to the terrain. This all works fine, except that any prototypes I add to this terrain in this way do not sway when being used with the "Grass" render mode. However, if I then manually create a prototype in the normal inspector interface, then the prototype does indeed wave.
Is there something I am not "turning on" in the DetailPrototype object when I adding it in the script?
Here is the function where the terrain is created (look for the "Initialize Details" portion):
void CreateTerrain()
{
if (GOInitializer == null) return;
if (GOInitializer.GetComponent<TerrainConstructorSettings>() == null) return;
if (Settings == null) InitializeSettings();
// Get rid of old version of the Terrain --------------------------------------------
WorldName = Settings.WorldName;
string TerrainName = WorldName + "_Tile_" + Position.X.ToString() + "_" + Position.Z.ToString();
if (GameObject.Find(TerrainName) != null)
{
GameObject oldgo = GameObject.Find(TerrainName);
DestroyImmediate(oldgo);
}
// Initialize the Terrain Data ------------------------------------------------------
TDATA = new TerrainData();
TDATA.heightmapResolution = Settings.HeightmapResolution;
TDATA.alphamapResolution = Settings.AlphamapResolution;
TDATA.SetDetailResolution(Settings.AlphamapResolution, 8);
AssetDatabase.CreateAsset(TDATA, "Assets/Resources/" + WorldName + "/TerrainData/" + TerrainName + ".asset");
TDATA.SetHeights(0, 0, GenerateHeightmap());
// Initialize the textures ------------------------------------------------------------
SplatPrototype[] sprot = new SplatPrototype[Settings.terrain_textures.Length];
for (int i = 0; i < Settings.terrain_textures.Length; i++)
{
TextInfo TI = Settings.terrain_textures[i];
sprot[i] = new SplatPrototype();
sprot[i].texture = TI.tex0;
sprot[i].normalMap = TI.texn;
sprot[i].tileSize = new Vector2(TI.scale, TI.scale);
}
TDATA.splatPrototypes = sprot;
// Initialize the details ------------------------------------------------------------
DetailPrototype[] DPROTS = new DetailPrototype[Settings.terrain_Details.Length];
for(int i = 0; i < DPROTS.Length; i++)
{
DPROTS[i] = new DetailPrototype();
DPROTS[i].renderMode = Settings.terrain_Details[i].mode;
if (DPROTS[i].renderMode == DetailRenderMode.GrassBillboard)
{
DPROTS[i].prototypeTexture = Settings.terrain_Details[i].grass_tex;
}
else
{
DPROTS[i].prototype = Settings.terrain_Details[i].prefab;
}
if (DPROTS[i].prototype != null) DPROTS[i].usePrototypeMesh = true;
DPROTS[i].bendFactor = 1f;// Settings.terrain_Details[i].bendFactor;
DPROTS[i].dryColor = Settings.terrain_Details[i].dry_color;
DPROTS[i].healthyColor = Settings.terrain_Details[i].healthy_color;
if (Settings.terrain_Details[i].minWidth != 0) DPROTS[i].minWidth = Settings.terrain_Details[i].minWidth;
if (Settings.terrain_Details[i].maxWidth != 0) DPROTS[i].maxWidth = Settings.terrain_Details[i].maxWidth;
if (Settings.terrain_Details[i].minHeight != 0) DPROTS[i].minHeight = Settings.terrain_Details[i].minHeight;
if (Settings.terrain_Details[i].maxHeight != 0) DPROTS[i].maxHeight = Settings.terrain_Details[i].maxHeight;
}
TDATA.wavingGrassAmount = 0.5f;
TDATA.wavingGrassSpeed = 0.5f;
TDATA.wavingGrassStrength = 0.5f;
TDATA.detailPrototypes = DPROTS;
TDATA.RefreshPrototypes();
// Add TreePrototypes ------------------------------------------------------------
TreePrototype treeproto = new TreePrototype();
treeproto.prefab = Settings.Tree1;
TDATA.treePrototypes = new TreePrototype[]
{
treeproto,
};
// Create the terrain object and set Terrain Parameters ---------------------------------------------------------
TDATA.size = new Vector3(Settings.Length, Settings.Height, Settings.Length);
var newTerrainGameObject = Terrain.CreateTerrainGameObject(TDATA);
newTerrainGameObject.transform.position = new Vector3(Position.X * Settings.Length, 0, Position.Z * Settings.Length);
newTerrainGameObject.name = TerrainName;
T = newTerrainGameObject.GetComponent<Terrain>();
T.terrainData.RefreshPrototypes();
T.basemapDistance = 1000f;
T.treeDistance = 1000f;
T.heightmapPixelError = Settings.PixelError;
T.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
T.detailObjectDistance = 200f;
CompleteTerrainShader cTS = newTerrainGameObject.AddComponent(typeof(CompleteTerrainShader)) as CompleteTerrainShader;
cTS.AutoBakeNormalMap = false;
cTS.AutoBakeColorMap = false;
cTS.Profile = Settings.cTS_Profile;
T.Flush();
// Save the game object as a prefab to the resource folder
PrefabUtility.CreatePrefab("Assets/Resources/" + WorldName + "/" + TerrainName + ".prefab",newTerrainGameObject);
}
Comment
Your answer