public void GenerateAndApplyMesh() { Mesh mesh = terrainGenerator.GenerateMesh(); meshFilter.mesh = mesh; float[,] heightmap = terrainGenerator.heightmapGenerator.GenerateNoisemap(); Texture2D heightmapTexture = Texture2DUtility.FromNoisemap(heightmap); Texture2D normalmapTexture = Normalmap.getNormalMap(heightmapTexture, 2); meshRenderer.sharedMaterial.EnableKeyword("_NORMALMAP"); meshRenderer.sharedMaterial.EnableKeyword("_PARALLAXMAP"); meshRenderer.sharedMaterial.SetTexture("_BumpMap", normalmapTexture); meshRenderer.sharedMaterial.SetTexture("_ParallaxMap", heightmapTexture); }
public static void GenerateNormalmap() { if (!(Selection.activeObject is Texture2D)) { Debug.LogError("selection is not texture"); } Texture2D src = (Texture2D)Selection.activeObject; EnableReading(src); Texture2D dst = Normalmap.Generate(src, 1.0f); string srcPath = AssetDatabase.GetAssetPath(src); File.Delete(srcPath); int end = srcPath.LastIndexOf("."); string dstPath = srcPath.Substring(0, end) + ".png"; File.WriteAllBytes(dstPath, dst.EncodeToPNG()); AssetDatabase.Refresh(); }