Esempio n. 1
0
        public void InitializeHeightsAndNormals()
        {
            // Create a procedural height field.
            int numberOfSamples = 1025;

            float[] heights = new float[numberOfSamples * numberOfSamples];
            var     creator = new ProceduralTerrainCreator(RandomHelper.Random.Next(), 256, _noiseMu);

            creator.CreateTerrain(
                7777, 9999, _noiseWidth * _terrainTile.CellSize, _noiseWidth * _terrainTile.CellSize,
                0, _noiseHeight, 8, heights, numberOfSamples, numberOfSamples);

            float tileSize = (numberOfSamples - 1) * _terrainTile.CellSize;

            _terrainTile.OriginX = -tileSize / 2;
            _terrainTile.OriginZ = -tileSize / 2;

            Debug.Assert(Numeric.IsZero(_terrainTile.OriginX % _terrainTile.CellSize), "The tile origin must be an integer multiple of the cell size.");
            Debug.Assert(Numeric.IsZero(_terrainTile.OriginZ % _terrainTile.CellSize), "The tile origin must be an integer multiple of the cell size.");

            //TerrainHelper.SmoothTexture(heights, numberOfSamples, numberOfSamples, 1e10f);

            // Rebuild height texture.
            Texture2D heightTexture = _terrainTile.HeightTexture;

            TerrainHelper.CreateHeightTexture(
                _graphicsService.GraphicsDevice,
                heights,
                numberOfSamples,
                numberOfSamples,
                false,
                ref heightTexture);

            // Rebuild normal texture.
            Texture2D normalTexture = _terrainTile.NormalTexture;

            TerrainHelper.CreateNormalTexture(
                _graphicsService.GraphicsDevice,
                heights,
                numberOfSamples,
                numberOfSamples,
                _terrainTile.CellSize,
                false,
                ref normalTexture);

            _terrainTile.HeightTexture = heightTexture;
            _terrainTile.NormalTexture = normalTexture;

            // Set the height field data for collision detection.
            var heightField = (HeightField)_rigidBody.Shape;

            heightField.OriginX = _terrainTile.OriginX;
            heightField.OriginZ = _terrainTile.OriginZ;
            heightField.WidthX  = tileSize;
            heightField.WidthZ  = tileSize;
            heightField.SetSamples(heights, heightTexture.Width, heightTexture.Height);
            heightField.Invalidate();

            TerrainNode.Terrain.Invalidate();
        }
Esempio n. 2
0
    public void InitializeHeightsAndNormals()
    {
      // Create a procedural height field.
      int numberOfSamples = 1025;
      float[] heights = new float[numberOfSamples * numberOfSamples];
      var creator = new ProceduralTerrainCreator(RandomHelper.Random.Next(), 256, _noiseMu);
      creator.CreateTerrain(
        7777, 9999, _noiseWidth * _terrainTile.CellSize, _noiseWidth * _terrainTile.CellSize,
        0, _noiseHeight, 8, heights, numberOfSamples, numberOfSamples);

      float tileSize = (numberOfSamples - 1) * _terrainTile.CellSize;

      _terrainTile.OriginX = -tileSize / 2;
      _terrainTile.OriginZ = -tileSize / 2;

      Debug.Assert(Numeric.IsZero(_terrainTile.OriginX % _terrainTile.CellSize), "The tile origin must be an integer multiple of the cell size.");
      Debug.Assert(Numeric.IsZero(_terrainTile.OriginZ % _terrainTile.CellSize), "The tile origin must be an integer multiple of the cell size.");

      //TerrainHelper.SmoothTexture(heights, numberOfSamples, numberOfSamples, 1e10f);

      // Rebuild height texture.
      Texture2D heightTexture = _terrainTile.HeightTexture;
      TerrainHelper.CreateHeightTexture(
        _graphicsService.GraphicsDevice,
        heights,
        numberOfSamples,
        numberOfSamples,
        false,
        ref heightTexture);

      // Rebuild normal texture.
      Texture2D normalTexture = _terrainTile.NormalTexture;
      TerrainHelper.CreateNormalTexture(
        _graphicsService.GraphicsDevice,
        heights,
        numberOfSamples,
        numberOfSamples,
        _terrainTile.CellSize,
        false,
        ref normalTexture);

      _terrainTile.HeightTexture = heightTexture;
      _terrainTile.NormalTexture = normalTexture;

      // Set the height field data for collision detection.
      var heightField = (HeightField)_rigidBody.Shape;
      heightField.OriginX = _terrainTile.OriginX;
      heightField.OriginZ = _terrainTile.OriginZ;
      heightField.WidthX = tileSize;
      heightField.WidthZ = tileSize;
      heightField.SetSamples(heights, heightTexture.Width, heightTexture.Height);
      heightField.Invalidate();

      TerrainNode.Terrain.Invalidate();
    }