RMVXA-GL - 3D graphics in RPG Maker!

rey meustrus wrote:
Tileset! I've got an RMXP implementation for you right here. Well...it will probably take some modification, because the viewport code uses other systems I wrote and it is designed for GOSU rather than plain OpenGL. You probably don't want to split the images into individual textures like this, you want to load it as one texture (if you can) and draw with UVs. But you can do this I think. The only part that needs explaining is that `Gosu::Image.load_tiles` creates an array of textures, in this case all 32x32 pixel tiles, row-by-row (so the first row would be tiles 0-7, the next 8-15, etc).

Every time I come back to this I am again disappointed that I did not do the GL calls myself in this way. Oh well. This should help you get started though.

  1. # RMXP Tilemap (gosu)
  2. #
  3. # Readable/Writable Attributes :
  4. #  - viewport         : Viewport used for sprites
  5. #  - map_data         : 3D Table of Tile ID Data
  6. #  - flash_data       : 3D Table of Tile Color Data
  7. #  - priorities       : 3D Table of Tile Priorities
  8. #  - ox, oy           : Tilemap layer offsets
  9. #  - zoom             : Zoom value as fraction
  10. #  - tilesize         : Zoom value as size of individual tile (normal is 32.0)
  11. #  - visible          : Tilemap Visible Flag
  12. #  - is_a_plane       : Behaves like a Plane (loops around edges)
  13. #  - tileset          : Name of Bitmap
  14. #  - autotiles        : Array of Autotile Filenames
  15. class Tilemap
  16.   include Disposable
  17.   VP                = 2    # Number of tiles beyond viewport to refresh
  18.   FA                = 64   # Alpha value of the color of flash data
  19.   Autotile_Speed    = 20   # Number of frames between autotile animations
  20.   Autotiles = [
  21.     [416,432,512,528], [ 64,432,512,528], [416, 80,512,528], [ 64, 80,512,528],
  22.     [416,432,512,176], [ 64,432,512,176], [416, 80,512,176], [ 64, 80,512,176],
  23.     [416,432,160,528], [ 64,432,160,528], [416, 80,160,528], [ 64, 80,160,528],
  24.     [416,432,160,176], [ 64,432,160,176], [416, 80,160,176], [ 64, 80,160,176],
  25.     [384,400,480,496], [384, 80,480,496], [384,400,480,176], [384, 80,480,176],
  26.     [224,240,320,336], [224,240,320,176], [224,240,160,336], [224,240,160,176],
  27.     [448,464,544,560], [448,464,160,560], [ 64,464,544,560], [ 64,464,160,560],
  28.     [608,624,704,720], [ 64,624,704,720], [608, 80,704,720], [ 64, 80,704,720],
  29.     [384,464,480,560], [224,240,704,720], [192,208,288,304], [192,208,288,176],
  30.     [256,272,352,368], [256,272,160,368], [640,656,736,752], [ 64,656,736,752],
  31.     [576,592,672,688], [576, 80,672,688], [192,272,288,368], [192,208,672,688],
  32.     [576,656,672,752], [256,272,736,752], [192,272,672,752], [  0, 16, 96,112]
  33.   ]
  34.   DEFAULTS = {:tileset => nil, :autotiles => [], :map_data => nil,
  35.               :flash_data => nil, :priorities => nil, :visible => true,
  36.               :ox => 0, :oy => 0, :zoom => 1.0, :is_a_plane => true,
  37.               :manager => nil}
  38.  
  39.   # Public Instance Variables
  40.   attr_reader   :gosuwindow     # Instance of Gosu::Window it's drawing to
  41.   attr_reader   :tileset        # Tileset Image
  42.   attr_reader   :autotiles      # Array of Autotile Images
  43.   attr_accessor :map_data       # 3D Table of Tile Settings
  44.   attr_reader   :flash_data     # 3D Table of Sprite Flash Colors
  45.   attr_accessor :priorities     # 3D Table of Tileset Priorities
  46.   attr_accessor :visible        # Tilemap Visibility
  47.   attr_accessor :ox             # Bitmap Offsets
  48.   attr_accessor :oy             # Bitmap Offsets
  49.   attr_accessor :zoom           # Zoom Fraction
  50.   attr_accessor :is_a_plane     # Loops around edges
  51.  
  52.   # Object Initialization
  53.   def initialize(manager = nil, opts = {})
  54.     @gosuwindow = $GameWindow
  55.     DEFAULTS.merge!(opts).each do |sym, val|
  56.       instance_variable_set("@#{sym}", val)
  57.     end
  58.     @manager           = manager unless manager.nil?
  59.     @autotiles_cache   = @autotiles
  60.     self.flash_data    = @flash_data
  61.     @tiles             = []
  62.     @frame             = 0
  63.     (@manager.nil? ? @gosuwindow.draw_manager : @manager).manage(self)
  64.   end
  65.  
  66.   # Get "viewport"
  67.   def viewport
  68.     @manager
  69.   end
  70.  
  71.   # Set "viewport"
  72.   def viewport=(manager)
  73.     (@manager.nil? ? @gosuwindow.draw_manager : @manager).unmanage(self)
  74.     (manager.nil? ? @gosuwindow.draw_manager : manager).manage(self)
  75.     @manager = manager
  76.   end
  77.  
  78.   # Get Tile Size
  79.   def tilesize
  80.     (zoom * 32).to_i
  81.   end
  82.  
  83.   # Set Tile Size
  84.   def tilesize=(tilesize)
  85.     self.zoom = tilesize / 32.0
  86.   end
  87.  
  88.   # Set tileset graphic. Recaches the internal tileset.
  89.   def tileset=(tileset)
  90.     @tiles[384..-1] = Gosu::Image.load_tiles(@gosuwindow, tileset, 32, 32, true)
  91.     @tileset = tileset
  92.   end
  93.  
  94.   # Set list of autotiles. Recaches the internal tileset.
  95.   def autotiles=(autotiles)
  96.     autotiles.each_with_index do |autotile, i|
  97.       Autotiles.each_with_index do |tiles, j|
  98.         key = (i + 1) * 48 + j
  99.         @tiles[key] = []
  100.         # Draws Auto-Tile Rects
  101.         for f in 0..(autotile.width / 96)
  102.           bmp = Gosu::Image.new(@gosuwindow,
  103.                                 TexPlay::EmptyImageStub.new(32, 32), true)
  104.           tiles.each_with_index do |pos, k|
  105.             opts = {:crop => [x1 = pos%96+f*96, x2=pos / 96, x1 + 16, x2 + 16]}
  106.             bmp.splice(k % 2 * 16, k / 2 * 16, autotile, opts)
  107.           end
  108.           @tiles[key][f] = bmp
  109.         end
  110.       end
  111.     end
  112.     @autotiles = @autotiles_cache = autotiles
  113.   end
  114.  
  115.   # Set flash data. For speed we maintain a separate list of GOSU::Color
  116.   # object for actual display.
  117.   def flash_data=(flash_data)
  118.     @flash_data = flash_data
  119.     if flash_data.nil?
  120.       @flash_data_colors = nil
  121.     else
  122.       @flash_data_colors = Hash.new
  123.       for y in 0...flash_data.ysize
  124.         for x in 0...flash_data.xsize
  125.           hex = @flash_data[x, y]
  126.           @flash_data_colors[[x, y]] =
  127.             Gosu::Color.rgba((hex<<8)*16, ((hex<<4)%16)*16, (hex%16)*16, FA))
  128.         end
  129.       end
  130.     end
  131.   end
  132.  
  133.   # Dispose
  134.   def dispose
  135.     @visible = false
  136.     super
  137.   end
  138.  
  139.   # Update autotile animation
  140.   def update
  141.     @frame += 1
  142.   end
  143.  
  144.   # Per-frame draw function
  145.   def draw
  146.     return if @map_data.nil? or @priorities.nil? or !@visible
  147.    
  148.     self.autotiles = @autotiles if @autotiles != @autotiles_cache
  149.     ts = tilesize
  150.     frame = @frame / Autotile_Speed
  151.     xmax = (((@viewport.nil? ? @gosuwindow : @viewport.rect).width + @ox) / ts)
  152.     ymax = (((@viewport.nil? ? @gosuwindow : @viewport.rect).height + @oy) / ts)
  153.     ydts = @oy / ts
  154.     yrange = ydts - VP...ymax + VP
  155.     xrange = (@ox / ts) - VP...xmax + VP
  156.    
  157.     for y in yrange
  158.       ytop = y - ydts
  159.       if y < 0 or y >= @map_data.ysize
  160.         next unless @is_a_plane
  161.         y %= @map_data.ysize
  162.         y += @map_data.ysize if y < 0
  163.       end
  164.       draw_y = y * ts - @oy
  165.      
  166.       for x in xrange
  167.         if x < 0 or x >= @map_data.xsize
  168.           next unless @is_a_plane
  169.           x %= @map_data.xsize
  170.           x += @map_data.xsize if x < 0
  171.         end
  172.         draw_x = x * ts - @ox
  173.        
  174.         for layer in 0...@map_data.zsize
  175.           id = @map_data[x, y, layer]
  176.           next if id == 0
  177.           z = (ytop + z) * 32 + 64 unless (z = @priorities[id]) == 0
  178.           tile = @tiles[id]
  179.           tile = @tiles[id][frame % tile.size] if tile.is_a?(Array)
  180.           tile.draw(draw_x, draw_y, z, @zoom, @zoom)
  181.         end
  182.         unless @flash_data.nil?
  183.           c = @flash_data_colors[[x, y]]
  184.           @gosuwindow.draw_quad(draw_x, draw_y,    c, draw_x+ts, draw_y,    c,
  185.                                 draw_x, draw_y+ts, c, draw_x+ts, draw_y+ts, c)
  186.         end
  187.       end # for x
  188.     end # for y
  189.   end
  190. end
  191.  
Expand to see the code.


Woa!
Interesting. I'll try to play with it, thanks. One question though, your implementation works in RMVXAce as it is?
Absolutely not. It is RMXP only and would not work in any RGSS-based project without modification. I don't know enough about VX or VX Ace's Tilemap class to construct a newer version. Mainly because I haven't really used either maker much at all. I've been...lazy, I guess. If you count various time-consuming real life things getting in the way as lazy, which I do because I hate myself.

If you want an immediately RGSS-compatible option, you could also extract it from the Map Image Maker. Just create a new project, close it, replace its Data/Scripts.rxdata file with the MapMaker.rxdata file (renaming it to Scripts.rxdata), and open up the project and the script editor. In RMXP, of course. There are a number of other things in there in which you may be interested as well.
Sorry to bump this old topic but I noticed upon downloading the demo that it does not come with the "Shaders" folder or a "fragments.fsh" file mentioned in the first post (something that is kind of "hard" to edit when the file needed doesn't exist). I get the feeling that those two things were a part of the prior GLSL demo from the other thread but just like the link to that demo, the file and folder was "removed/deleted" before this demo went up. Also, because there's no documentation on how to "call" for the shaders (through events or otherwise), it kind of makes it difficult to work out how to utilize GLSL in my own project.

Is there some way to obtain these?
Espilonarge wrote:
Sorry to bump this old topic but I noticed upon downloading the demo that it does not come with the "Shaders" folder or a "fragments.fsh" file mentioned in the first post (something that is kind of "hard" to edit when the file needed doesn't exist). I get the feeling that those two things were a part of the prior GLSL demo from the other thread but just like the link to that demo, the file and folder was "removed/deleted" before this demo went up. Also, because there's no documentation on how to "call" for the shaders (through events or otherwise), it kind of makes it difficult to work out how to utilize GLSL in my own project.

Is there some way to obtain these?

This experiment is not production ready nor is it supported. Do not use it in your own project (if you are unable to figure out how to use this, then you definitely should not be using it!).

All 3D functionality experimented with here is much easier to implement in RPG Maker MV, if you wish to do 3D graphics in RPG Maker then consider switching to MV.
I was aiming more towards effects like Gaussian Blur and other filters, not the 3D aspect of it.

The reason why I was asking is because I've been trying to find something to odd-load some of the heavy-ended effects that a couple of scripts are doing that is driving CPU usage through the roof (Zeus81's Map Effects being a prime culprit when using Gaussian Blur and such). I also tried to get SweetFX to work with VXAce but because it doesn't use Direct3D or OpenGL, I had to find an alternative, hence why when I ran in to your previous project and this one while searching around, I wanted to at least find some way of implementing at least a basic Gaussian Blur effect that is less likely to cripple the CPU.

To give you an example of what I'm trying to do.

I currently have Zeus81's Map Effects to generate a Guassian Blur with an Opacity of 25% to give sprites and tiles a "soft" edge effect (a form of "fantasy" like bloom). The problem is that if I try to use the functions/effects from MGC's Mode 7 or Woratora's Multiple Layers scripts on any map with those effects enabled, it can cause the FPS to nosedive painfully as it puts too much strain doing a "call" to the CPU to make the snapshots. At the same time, there is a bug with Zeus81's Map Effects affecting Effectus causing events and shadows (engine-generated) to shift 2 pixels in the same direction you're walking which is also why I wanted to try and get your script functions working.

Image
My script functions here are not production ready and have compatibility and security problems. If it's post-processing shaders you're after, then it may be worth finding a scripter (or paying me) to write you a complete script and new processing DLL based on the technique I used here.

I am guessing that you're simply wanting a way to program GPU accelerated post-processing atop the Tilemap?

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=78477