示例#1
0
    /// <summary>
    /// Resizes the texture. (If original texture has mipmaps, all mipmap levels are automatically
    /// recreated.)
    /// </summary>
    /// <param name="width">The new width.</param>
    /// <param name="height">The new height.</param>
    /// <param name="depth">The new depth. Must be 1 for 2D textures and cube map textures.</param>
    /// <param name="filter">The filter to use for resizing.</param>
    /// <param name="alphaTransparency">
    /// <see langword="true"/> if the image contains uses non-premultiplied alpha; otherwise,
    /// <see langword="false"/> if the image uses premultiplied alpha or has no alpha.
    /// </param>
    /// <param name="wrapMode">
    /// The texture address mode that will be used for sampling the at runtime.
    /// </param>
    /// <returns>The resized texture.</returns>
    public Texture Resize(int width, int height, int depth, ResizeFilter filter, bool alphaTransparency, TextureAddressMode wrapMode)
    {
      var description = Description;
      description.Width = width;
      description.Height = height;
      description.Depth = depth;

      var resizedTexture = new Texture(description);

      // Resize mipmap level 0.
      for (int arrayIndex = 0; arrayIndex < description.ArraySize; arrayIndex++)
        TextureHelper.Resize(this, 0, arrayIndex, resizedTexture, 0, arrayIndex, filter, alphaTransparency, wrapMode);

      // Regenerate mipmap levels, if necessary.
      if (description.MipLevels > 1)
        resizedTexture.GenerateMipmaps(filter, alphaTransparency, wrapMode);

      return resizedTexture;
    }
示例#2
0
    /// <summary>
    /// (Re-)Generates all mipmap levels.
    /// </summary>
    /// <param name="filter">The filter to use for resizing.</param>
    /// <param name="alphaTransparency">
    /// <see langword="true"/> if the image contains uses non-premultiplied alpha; otherwise,
    /// <see langword="false"/> if the image uses premultiplied alpha or has no alpha.
    /// </param>
    /// <param name="wrapMode">
    /// The texture address mode that will be used for sampling the at runtime.
    /// </param>
    public void GenerateMipmaps(ResizeFilter filter, bool alphaTransparency, TextureAddressMode wrapMode)
    {
      var oldDescription = Description;
      var newDescription = Description;

      // Determine number of mipmap levels.
      if (oldDescription.Dimension == TextureDimension.Texture3D)
        newDescription.MipLevels = TextureHelper.CalculateMipLevels(oldDescription.Width, oldDescription.Height, oldDescription.Depth);
      else
        newDescription.MipLevels = TextureHelper.CalculateMipLevels(oldDescription.Width, oldDescription.Height);

      if (oldDescription.MipLevels != newDescription.MipLevels)
      {
        // Update Description and Images.
        var oldImages = Images;
        Description = newDescription;
#if DEBUG
        ValidateTexture(newDescription);
#endif
        // Recreate image collection. (Mipmap level 0 is copied from existing image collection.)
        Images = CreateImageCollection(newDescription, true);
        for (int arrayIndex = 0; arrayIndex < newDescription.ArraySize; arrayIndex++)
        {
          for (int zIndex = 0; zIndex < newDescription.Depth; zIndex++)
          {
            int oldIndex = oldDescription.GetImageIndex(0, arrayIndex, zIndex);
            int newIndex = newDescription.GetImageIndex(0, arrayIndex, zIndex);
            Images[newIndex] = oldImages[oldIndex];
          }
        }
      }

      // Downsample mipmap levels.
      for (int arrayIndex = 0; arrayIndex < newDescription.ArraySize; arrayIndex++)
        for (int mipIndex = 0; mipIndex < newDescription.MipLevels - 1; mipIndex++)
          TextureHelper.Resize(this, mipIndex, arrayIndex, this, mipIndex + 1, arrayIndex, filter, alphaTransparency, wrapMode);
    }