void SaveInstancePrefab() { GameObject prefab = new GameObject(); prefab.name = mapName + "_Instance"; TerrainInstance terrainInstance = prefab.AddComponent <TerrainInstance>(); string meshName = mapName + "_Chunk_Mesh.asset"; Mesh mesh = AssetDatabase.LoadAssetAtPath(assetsPath + "/" + meshName, typeof(Mesh)) as Mesh; terrainInstance.InitData(mesh, chunkCountX, chunkCountZ); string path = assetsPath + "/" + mapName + "_Total_HeightNormalMap.png"; Texture2D tex = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D; terrainInstance.SetMatData(tex, data.size.y); //Material mat = terrainInstance.Mat; //MaterialPropertyBlock prop = terrainInstance.Prop; //UpdateProp(prop); //UpdateMat(mat); UpdateTRS(terrainInstance); string prefabName = mapName + "_Instance.prefab"; PrefabUtility.SaveAsPrefabAssetAndConnect(prefab, assetsPath + "/" + prefabName, InteractionMode.AutomatedAction); //GameObject.DestroyImmediate(prefab); //prefab = null; }
/// <summary> /// Creates a new TerrainInstance /// </summary> /// <param name="node">The node the TerrainInstance represents, and from where it derives its position</param> /// <param name="terrainType">the type of the terrainInstance</param> /// <returns>a terrainInstance instance</returns> public static TerrainInstance Create(Node node, TerrainType terrainType) { if (node.terrain != null) { throw new System.Exception("TerrainInstance.Create() was called on a node with a non-null terrainInstance. Use TerrainInstance.Replace() instead."); } //Debug.Log( terrainType.prefabs.block ); GameObject go = GameObject.Instantiate( terrainType.prefabs.block, node.position, Quaternion.identity) as GameObject; TerrainInstance terrainInstance = go.AddComponent <TerrainInstance>(); terrainInstance.node = node; node.terrain = terrainInstance; terrainInstance.type = terrainType; terrainInstance.transform.parent = node.tile.transform; //nobody cares //go.name = terrainInstance.type.prefabs.block.name; return(terrainInstance); }
/// <summary> /// Creates a new Propertyinstance. /// </summary> /// <param name="terrainInstance">the terrain instance this property will be attached to</param> /// <param name="propertyType"></param> /// <returns></returns> public static PropertyInstance Create(TerrainInstance terrainInstance, PropertyType propertyType) { if (terrainInstance.property != null) { throw new System.Exception("PropertyInstance.Create() was called on a terrainInstance with a non-null property. Use PropertyInstance.Replace() instead."); } //Debug.Log( terrainType.prefabs.block ); GameObject go = GameObject.Instantiate( propertyType.prefab, terrainInstance.node.position, Quaternion.identity) as GameObject; PropertyInstance propertyInstance = go.AddComponent <PropertyInstance>(); propertyInstance.terrainInstance = terrainInstance; terrainInstance.property = propertyInstance; propertyInstance.type = propertyType; propertyInstance.transform.parent = terrainInstance.transform; //nobody cares //go.name = propertyType.prefab.name; return(propertyInstance); }
public void Register(TerrainInstance terrainInstance) { if (!terrainInstanceList.Contains(terrainInstance)) { terrainInstanceList.Add(terrainInstance); } }
public void Remove(TerrainInstance terrainInstance) { if (!terrainInstanceList.Contains(terrainInstance)) { terrainInstanceList.Remove(terrainInstance); } }
public static void Replace(PropertyInstance propertyInstance, PropertyType newType) { TerrainInstance t = propertyInstance.terrainInstance; Destroy(t.property.gameObject); t.property = null; Create(t, newType); }
//void UpdateProp(MaterialPropertyBlock prop) //{ // Vector4[] startEndUVs = new Vector4[chunkCountZ * chunkCountX]; // int index = 0; // for (int j = 0; j < chunkCountZ; j++) // { // for(int i = 0; i < chunkCountX; i++) // { // index = j * chunkCountX + i; // startEndUVs[index] = (new Vector4((float)i / chunkCountX, (float)j / chunkCountZ, (float)(i + 1) / chunkCountX, (float)(j + 1) / chunkCountZ)); // } // } // prop.SetVectorArray("_StartEndUV", startEndUVs); //} //void UpdateMat(Material mat) //{ // mat.SetTexture("_HeightNormalTex", heightNormalTex); // mat.SetFloat("_MaxHeight", data.size.y); //} void UpdateTRS(TerrainInstance terrainInstance) { Matrix4x4 matr; int index = 0; for (int j = 0; j < chunkCountZ; j++) { for (int i = 0; i < chunkCountX; i++) { index = j * chunkCountX + i; matr = new Matrix4x4(); matr.SetTRS(new Vector3(i * chunkWidth, 0, j * chunkLength), terrainInstance.transform.rotation, Vector3.one); terrainInstance.AddTRS(matr, index); } } }
/// <summary> /// Replaces a terrainInstance with a new type of terrain. /// </summary> /// <param name="block">the terrainInstance to replace.</param> /// <param name="newType">the type of terrain of the new terrainInstance.</param> public static void Replace(TerrainInstance block, TerrainType newType) { Node n = block.node; Destroy(n.terrain.gameObject); n.terrain = null; Create(n, newType); //SPEEDUP: Could be optimized if know previous block modified. List <Node> nearNodes = Board.instance.getNodesAdjacent(n.position); List <Node> fartherNodes; foreach (Node nearNode in nearNodes) { fartherNodes = Board.instance.getNodesAdjacent(nearNode.position); foreach (Node fartherNode in fartherNodes) { fartherNode.terrain.autoWall(); } nearNode.terrain.autoWall(); } }
void TerrainEditorUpdate() { if (!Input.GetMouseButton(0)) { return; } if (cursorInfo.transform != null && UICamera.hoveredObject == null) { tile = cursorInfo.transform.GetComponent <Tile>(); if (tile != null) { if (tile.node.terrain == null) { TerrainInstance.Create(tile.node, chosenTerrainType); //this should never be necessary. } else if (tile.node.terrain.type != chosenTerrainType) { TerrainInstance.Replace(tile.node.terrain, chosenTerrainType); } } } }
/// <summary> /// Creates a new Propertyinstance. /// </summary> /// <param name="terrainInstance">the terrain instance this property will be attached to</param> /// <param name="propertyType"></param> /// <returns></returns> public static PropertyInstance Create(TerrainInstance terrainInstance, PropertyType propertyType) { if (terrainInstance.property != null) { throw new System.Exception( "PropertyInstance.Create() was called on a terrainInstance with a non-null property. Use PropertyInstance.Replace() instead." ); } //Debug.Log( terrainType.prefabs.block ); GameObject go = GameObject.Instantiate( propertyType.prefab, terrainInstance.node.position, Quaternion.identity ) as GameObject; PropertyInstance propertyInstance = go.AddComponent<PropertyInstance>(); propertyInstance.terrainInstance = terrainInstance; terrainInstance.property = propertyInstance; propertyInstance.type = propertyType; propertyInstance.transform.parent = terrainInstance.transform; //nobody cares //go.name = propertyType.prefab.name; return propertyInstance; }
//Visibility Data public Node(TerrainType terrainType, Vector3 position) { this.position = position; Tile.Create(this); TerrainInstance.Create(this, terrainType); }
/// <summary> /// Replaces a terrainInstance with a new type of terrain. /// </summary> /// <param name="block">the terrainInstance to replace.</param> /// <param name="newType">the type of terrain of the new terrainInstance.</param> public static void Replace(TerrainInstance block , TerrainType newType) { Node n = block.node; Destroy( n.terrain.gameObject ); n.terrain = null; Create( n , newType ); //SPEEDUP: Could be optimized if know previous block modified. List<Node> nearNodes = Board.instance.getNodesAdjacent( n.position ); List<Node> fartherNodes; foreach ( Node nearNode in nearNodes ) { fartherNodes = Board.instance.getNodesAdjacent( nearNode.position ); foreach ( Node fartherNode in fartherNodes ) { fartherNode.terrain.autoWall(); } nearNode.terrain.autoWall(); } }