public sealed override void ProcessSlice(Terrain sourceTerrain, TerrainSlice terrainSlice) { #if UNITY_EDITOR string applicationSavePath, unitySavePath; string finalFolderName = sliceConfiguration.OutputNameGenerator.GenerateName(sliceConfiguration.SliceOutputGroupIdentifier, terrainSlice.Row, terrainSlice.Column); CreateFolder(subFolder + "/" + finalFolderName, WhatToDoIfFolderAlreadyExists.OverwriteExistingFolder, out applicationSavePath, out unitySavePath); SliceMaterial(terrainSlice, sourceTerrain.materialTemplate, applicationSavePath, unitySavePath); AssetDatabase.SaveAssets(); #endif }
/// <summary> /// Slice the material /// </summary> /// <param name="slicedTerrain">The sliced terrain which we're generating sliced /// materials/textures for</param> /// <param name="materialToSlice">The material to slice</param> /// <param name="saveDirectory">The system specific save path for the sliced files</param> /// <param name="unitySavePath">The Unity relative save path for the sliced files</param> void SliceMaterial(TerrainSlice slicedTerrain, Material materialToSlice, string applicationSavePath, string unitySavePath) { string fullAssetPathOfSourceMaterial = AssetDatabase.GetAssetPath(materialToSlice); string fullAssetPathOfNewMaterial = unitySavePath + sliceConfiguration.OutputNameGenerator.GenerateName(materialToSlice.name, slicedTerrain.Row, slicedTerrain.Column) + ".mat"; AssetDatabase.CopyAsset(fullAssetPathOfSourceMaterial, fullAssetPathOfNewMaterial); RefreshAssetDatabase(); Material slicedMaterial = AssetDatabase.LoadAssetAtPath <Material>(fullAssetPathOfNewMaterial); SliceProperties(slicedTerrain, slicedMaterial, applicationSavePath, unitySavePath); slicedTerrain.SliceTerrain.materialTemplate = slicedMaterial; }
void SliceProperties(TerrainSlice terrainSlice, Material materialSlice, string applicationSavePath, string unityPathToSaveTextures) { List <string> texturePropertiesToSliceNormally = new List <string>(), texturePropertiesToSliceAbnormally = new List <string>(); for (int i = 0; i < ShaderUtil.GetPropertyCount(materialSlice.shader); i++) { ShaderUtil.ShaderPropertyType propertyType = ShaderUtil.GetPropertyType(materialSlice.shader, i); if (propertyType == ShaderUtil.ShaderPropertyType.TexEnv) { string texturePropertyName = ShaderUtil.GetPropertyName(materialSlice.shader, i); if (texturePropertyName.StartsWith("_Splat")) { texturePropertiesToSliceAbnormally.Add(texturePropertyName); } else if (!texturePropertyName.StartsWith("_CombinedNormal")) { texturePropertiesToSliceNormally.Add(texturePropertyName); } } } SliceTextureScaleAndOffset_NormalMethod(terrainSlice, materialSlice, texturePropertiesToSliceNormally); SliceTextureScaleAndOffset_AbnormalMethod(terrainSlice, materialSlice, texturePropertiesToSliceAbnormally); }
protected void initialize(GroundMap groundMap, WeightsMap weights, ColorSurface normals) { GroundMap = groundMap; Debug.Assert((groundMap.Width%TerrainPlane.SquareSize) == 0 && (groundMap.Height%TerrainPlane.SquareSize) == 0); _position = World.TranslationVector; Textures[0] = Textures[0] ?? VContent.Load<Texture2D>("terraintextures/sand"); Textures[1] = Textures[1] ?? VContent.Load<Texture2D>("terraintextures/sahara"); Textures[2] = Textures[2] ?? VContent.Load<Texture2D>("terraintextures/grass"); Textures[3] = Textures[3] ?? VContent.Load<Texture2D>("terraintextures/rock"); Textures[4] = Textures[4] ?? VContent.Load<Texture2D>("terraintextures/snow"); Textures[5] = Textures[5] ?? VContent.Load<Texture2D>("terraintextures/stones"); Textures[6] = Textures[6] ?? VContent.Load<Texture2D>("terraintextures/dirtground"); Textures[7] = Textures[7] ?? VContent.Load<Texture2D>("terraintextures/path"); Textures[8] = Textures[8] ?? VContent.Load<Texture2D>("terraintextures/wheatfield"); HeightsMap = groundMap.CreateHeightsTexture(Effect.GraphicsDevice); WeightsMap = weights.CreateTexture2D(Effect.GraphicsDevice); NormalsMap = normals.CreateTexture2D(Effect.GraphicsDevice); var slicesW = groundMap.Width/Side; var slicesH = groundMap.Height/Side; //TODO - this is wrong - I guess... var sliceFracX = 1f/slicesW; var sliceFracY = 1f/slicesH; _slices = new TerrainSlice[slicesW*slicesH]; var gurka = Vector3.TransformNormal(new Vector3(HalfSide, 0, HalfSide), World); var radius = Math.Max(gurka.X, gurka.Z) * (float)Math.Sqrt(2); var i = 0; for (var y = 0; y < slicesH; y++) for (var x = 0; x < slicesW; x++) { var world = Matrix.Translation(Side*x, 0, Side*y)*World; _slices[i++] = new TerrainSlice { TexOffsetAndScale = new Vector4(x*sliceFracX, y*sliceFracY, sliceFracX, sliceFracY), World = world, BoundingSphere = new BoundingSphere(world.TranslationVector + gurka, radius) }; } BoundingSphere = new BoundingSphere( _position + new Vector3(groundMap.Width, 0, groundMap.Height)/2, (float) Math.Sqrt(groundMap.Width*groundMap.Width + groundMap.Height*groundMap.Height)/2); GroundExtentX = slicesW; GroundExtentZ = slicesH; }