设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 1503|回复: 0
打印 上一主题 下一主题

[谜之脚本] 自动将图像转为RM地图

[复制链接]

只有笨蛋才会看到

梦石
1
星屑
21005
在线时间
9337 小时
注册时间
2012-6-19
帖子
7107

开拓者短篇九导演组冠军

跳转到指定楼层
1
发表于 2015-1-6 19:18:38 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
本帖最后由 喵呜喵5 于 2015-1-8 01:39 编辑

  1. #******************************************************************************
  2. #
  3. #    * PNG 保存
  4. #
  5. #  --------------------------------------------------------------------------
  6. #    バージョン :  1.0.0
  7. #    対      応 :  RPGツクールVX Ace : RGSS3
  8. #    制  作  者 :  CACAO
  9. #    配  布  元 :  [url]http://cacaosoft.web.fc2.com/[/url]
  10. #  --------------------------------------------------------------------------
  11. #   == 概    要 ==
  12. #
  13. #    : ピングファイルの出力機能を追加します。
  14. #
  15. #  --------------------------------------------------------------------------
  16. #   == 使用方法 ==
  17. #
  18. #    ★ Bitmap#save_png(filename, alpha = false)
  19. #     このビットマップをピングファイルで出力
  20. #     filename : 保存するファイル名
  21. #     alpha    : αチャンネルの有無
  22. #
  23. #
  24. #******************************************************************************


  25. #/////////////////////////////////////////////////////////////////////////////#
  26. #                                                                             #
  27. #                  このスクリプトに設定項目はありません。                     #
  28. #                                                                             #
  29. #/////////////////////////////////////////////////////////////////////////////#


  30. class Bitmap
  31.   #--------------------------------------------------------------------------
  32.   # ● ピング画像として保存
  33.   #     filename : ファイル名
  34.   #     alpha    : アルファチャンネルの有無
  35.   #--------------------------------------------------------------------------
  36.   def save_png(filename, alpha = false)
  37.     sgnt = "\x89PNG\r\n\x1a\n"
  38.     ihdr = _chunk('IHDR', [width,height,8,(alpha ? 6 : 2),0,0,0].pack('N2C5'))
  39.     data = []
  40.     height.times do |y|
  41.       data << 0
  42.       width.times do |x|
  43.         color = self.get_pixel(x, y)
  44.         data << color.red << color.green << color.blue
  45.         data << color.alpha if alpha
  46.       end
  47.     end
  48.     idat = _chunk('IDAT', Zlib::Deflate.deflate(data.pack('C*')))
  49.     iend = _chunk('IEND', "")
  50.     File.open(filename, 'wb') do |file|
  51.       file.write(sgnt)
  52.       file.write(ihdr)
  53.       file.write(idat)
  54.       file.write(iend)
  55.     end
  56.   end
  57. private
  58.   #--------------------------------------------------------------------------
  59.   # ● チャンクの作成
  60.   #     name : チャンク名
  61.   #     data : チャンクデータ
  62.   #--------------------------------------------------------------------------
  63.   def _chunk(name, data)
  64.     return [data.size, name, data, Zlib.crc32(name + data)].pack('NA4A*N')
  65.   end
  66. end
复制代码

  1. mapid = 3
  2. # 地图 ID

  3. filename = "pic"
  4. # 转换图片文件名

  5. power = 12
  6. # 初始压缩质量(值越大速度越快,生成的图像质量越差)

  7. speed = 1
  8. # 压缩粗糙程度(值越大速度越快,生成的图像质量越差)

  9. tilesetid = 6
  10. # 新地图使用的图块,为0新建图块,否则修改原图块

  11. tilename = ["tileB","tileC","tileD","tileE"]
  12. # 图块素材文件名

  13. def data_reset(map)
  14.   $tilemap = map.data
  15.   $color_list = [Color.new(255,255,255,0)]
  16. end
  17. def add_color(color)
  18.   index = $color_list.index(color)
  19.   return index if index
  20.   $color_list.push color
  21.   return $color_list.size - 1
  22. end
  23. def change_color(red,green,blue,power)  
  24.   return Color.new( (red.to_i / power) * power, ( green.to_i / power ) * power,
  25.     ( blue.to_i / power) * power, 255)
  26. end
  27. def get_bitmap_data(map,bitmap,need_change = false,power = 2,speed = 2)
  28.   data_reset(map)
  29.   error = false
  30.   catch :get_data_normal do
  31.     map.width.times do |x|
  32.       map.height.times do |y|
  33.         color = bitmap.get_pixel(x, y)
  34.         if need_change
  35.           color = change_color(color.red, color.green, color.blue, power)         
  36.         end
  37.         index = add_color(color)        
  38.         if index >= 1024
  39.           p "处理失败,对画质进行压缩后重新开始"
  40.           error = true
  41.           throw :get_data_normal
  42.         end
  43.         $tilemap[x,y,2] = index
  44.         $tilemap[x,y,0],$tilemap[x,y,1],$tilemap[x,y,3] = 2048,0,16        
  45.       end
  46.       p "正在处理 #{x} / #{map.width}"
  47.     end
  48.   end #catch
  49.   get_bitmap_data(map, bitmap, true, power + speed, speed ) if error
  50.   msgbox("转换结束,正在生成地图,当前压缩度为#{power}") unless error
  51. end
  52. def save_bitmap_to_file(name)
  53.   tile_bitmap1 = Bitmap.new(512,512)
  54.   tile_bitmap2 = Bitmap.new(512,512)
  55.   tile_bitmap3 = Bitmap.new(512,512)
  56.   tile_bitmap4 = Bitmap.new(512,512)
  57.   $color_list.each_with_index do |color,index|
  58.     tile_id = index / 256
  59.     index = index % 256
  60.     x = index % 8
  61.     y = index / 8
  62.     if index >= 128
  63.       x += 8; y -= 16
  64.     end
  65.     bitmap = [tile_bitmap1,tile_bitmap2,tile_bitmap3,tile_bitmap4]
  66.     bitmap[tile_id].fill_rect(x * 32, y * 32, 32, 32, color)
  67.   end
  68.   tile_bitmap1.save_png("Graphics/Tilesets/#{name[0]}.png", true)
  69.   tile_bitmap2.save_png("Graphics/Tilesets/#{name[1]}.png", true)
  70.   tile_bitmap3.save_png("Graphics/Tilesets/#{name[2]}.png", true)
  71.   tile_bitmap4.save_png("Graphics/Tilesets/#{name[3]}.png", true)
  72. end
  73. map = load_data(sprintf("Data/Map%03d.rvdata2", mapid))
  74. tileset = load_data("Data/Tilesets.rvdata2")
  75. bitmap = Bitmap.new(filename)
  76. get_bitmap_data(map, bitmap, false, power.to_i, speed.to_i)
  77. save_bitmap_to_file(tilename)
  78. if !tileset[tilesetid]
  79.   tilesetid = tileset.size
  80.   tileset[tilesetid] = tileset[1].clone
  81. end
  82. tileset[tilesetid].tileset_names[5, 4] = tilename
  83. map.data = $tilemap
  84. map.tileset_id = tilesetid
  85. save_data(map,sprintf("Data/Map%03d.rvdata2", mapid))
  86. save_data(tileset,"Data/Tilesets.rvdata2")
  87. exit
复制代码
拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-5-8 06:20

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表