ExportMeshWithEdgeFall.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace AmazingAssets.TerrainToMesh.Example
  5. {
  6. [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
  7. public class ExportMeshWithEdgeFall : MonoBehaviour
  8. {
  9. public TerrainData terrainData;
  10. public int vertexCountHorizontal = 100;
  11. public int vertexCountVertical = 100;
  12. public EdgeFall edgeFall = new EdgeFall(0, true);
  13. public Texture2D edgeFallTexture;
  14. void Start()
  15. {
  16. if (terrainData == null)
  17. return;
  18. //1. Export mesh with edge fall/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. Mesh terrainMesh = terrainData.TerrainToMesh().ExportMesh(vertexCountHorizontal, vertexCountVertical, TerrainToMesh.Normal.CalculateFromMesh, edgeFall);
  20. GetComponent<MeshFilter>().sharedMesh = terrainMesh;
  21. //2. Create materials////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  22. string shaderName = Utilities.GetUnityDefaultShader(); //Default shader based on used render pipeline
  23. string mainTexturePropName = Utilities.GetMaterailPropMainTex(); //_MainTex property name inside shader.
  24. Material meshMaterial = new Material(Shader.Find(shaderName)); //Material for main mesh
  25. Material edgeFallMaterial = new Material(Shader.Find(shaderName)); //Material for edge fall (saved in sub-mesh)
  26. edgeFallMaterial.SetTexture(mainTexturePropName, edgeFallTexture);
  27. GetComponent<Renderer>().sharedMaterials = new Material[] { meshMaterial, edgeFallMaterial };
  28. }
  29. }
  30. }