private TerrainPatchComponent CreatePatch( TerrainEffect terrainEffect, EffectPipelineStateHandle pipelineStateHandle, HeightMap heightMap, BlendTileData blendTileData, Int32Rect patchBounds, GraphicsDevice graphicsDevice, ResourceUploadBatch uploadBatch, TerrainPatchIndexBufferCache indexBufferCache) { var indexBuffer = indexBufferCache.GetIndexBuffer( patchBounds.Width, patchBounds.Height, uploadBatch, out var indices); var vertexBuffer = AddDisposable(CreateVertexBuffer( graphicsDevice, uploadBatch, heightMap, patchBounds, indices, out var boundingBox, out var triangles)); return(new TerrainPatchComponent( terrainEffect, pipelineStateHandle, patchBounds, vertexBuffer, indexBuffer, triangles, boundingBox)); }
private void CreatePatches( GraphicsDevice graphicsDevice, ResourceUploadBatch uploadBatch, Entity terrainEntity, HeightMap heightMap, BlendTileData blendTileData, TerrainEffect terrainEffect, EffectPipelineStateHandle pipelineStateHandle, TerrainPatchIndexBufferCache indexBufferCache) { const int numTilesPerPatch = TerrainComponent.PatchSize - 1; var numPatchesX = heightMap.Width / numTilesPerPatch; if (heightMap.Width % numTilesPerPatch != 0) { numPatchesX += 1; } var numPatchesY = heightMap.Height / numTilesPerPatch; if (heightMap.Height % numTilesPerPatch != 0) { numPatchesY += 1; } for (var y = 0; y < numPatchesY; y++) { for (var x = 0; x < numPatchesX; x++) { var patchX = x * numTilesPerPatch; var patchY = y * numTilesPerPatch; var patchBounds = new Int32Rect { X = patchX, Y = patchY, Width = Math.Min(TerrainComponent.PatchSize, heightMap.Width - patchX), Height = Math.Min(TerrainComponent.PatchSize, heightMap.Height - patchY) }; terrainEntity.Components.Add(CreatePatch( terrainEffect, pipelineStateHandle, heightMap, blendTileData, patchBounds, graphicsDevice, uploadBatch, indexBufferCache)); } } }
public void Load(ContentManager Content, string heightMapFileName, float blockScale, float heightScale) { if (!isInitialized) Initialize(); // Load heightMap file Texture2D heightMapTexture = Content.Load<Texture2D>(heightMapFileName); int heightMapSize = heightMapTexture.Width*heightMapTexture.Height; heightMap = new Color[heightMapSize]; heightMapTexture.GetData<Color>(heightMap); this.vertexCountX = heightMapTexture.Width; this.vertexCountZ = heightMapTexture.Height; this.blockScale = blockScale; this.heightScale = heightScale; // Generate terrain mesh GenerateTerrainMesh(); transformation = new Transformation(); // Load effect effect = new TerrainEffect(Game.Content.Load<Effect>(TerrainEffect.EFFECT_FILENAME)); terrainMaterial = new TerrainMaterial(); // Load vertex declaration once this.vertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionNormalTangentBinormalTexture.VertexElements); }
/// <summary> /// Constructor. /// </summary> public TileTerrainEffect(TerrainEffect terrainEffect, bool isAnchor) { this.TerrainEffect = terrainEffect; this.IsAnchor = isAnchor; }