Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Closed TopicStart new topic
> RGSS Shader System, Pixel-level transformation
JoaoB
post May 8 2011, 12:25 PM
Post #1


Level 1
Group Icon

Group: Member
Posts: 10
Type: Scripter
RM Skill: Intermediate




RGSS Shader System

Version 1.0
Author JoaoB ("João B" on Brazilian forums)
Release Date May 9th 2010

Introduction
"A shader is a RGSS class with special methods used to apply pixel-level effects in bitmaps."
Sorry, but there's no way to hardware-accelerate this script (well, RGSS itself isn't hardware-accelerated). This script is slow, specially in RGSS2, but there are some uses.

Features
  • Easy shader creation using the def [tt]Shader::apply(x, y)[/tt]
  • Customizable output width and height
  • One can use another input bitmap
  • One can define and use shader parameters


Script
Shader Script
Shader Script
CODE
#===============================================================================
# RGSS Shaders System (main)
#-------------------------------------------------------------------------------
# Author: JoaoB
#===============================================================================

#===============================================================================
# class Shader
#===============================================================================
class Shader

def self.apply(x, y)
  return @@bitmap.get_pixel(x, y)
end

def self.bitmap=(bitmap)
  @@bitmap = bitmap
end

def self.bitmap2=(bitmap2)
  @@bitmap2 = bitmap
end

def self.need_two_bitmaps
  return false
end

def self.size
  if need_two_bitmaps
    width = [@@bitmap.width, @@bitmap2.width].max
    height = [@@bitmap.height, @@bitmap2.height].max
  else
    width = @@bitmap.width
    height = @@bitmap.height
  end
  return [width, height]
end

def self.parameters=(params)
  @@parameters = params
end
end

#===============================================================================
# Bitmap mods
#===============================================================================
class Bitmap
def apply_shader(shader, other_bitmap = nil, parameters = {})
  shader.parameters = parameters
  return if shader.need_two_bitmaps and not other_bitmap.is_a?(Bitmap)
  need_two_bitmaps = shader.need_two_bitmaps

  backup_bitmap = Bitmap.new(width, height)
  shader.bitmap = self
  shader.bitmap2 = other_bitmap if need_two_bitmaps

  width, height = shader.size

  y = 0
  while y < height
    x = 0
    while x < width
      pix = shader.apply(x, y)
      backup_bitmap.set_pixel(x, y, pix)
      x += 1
    end
    y += 1
  end

  return backup_bitmap
end

def apply_shader!(shader, other_bitmap = nil)
  backup = copy
  self.clear
  self.blt(
    0, 0,
    backup.apply_shader(shader, other_bitmap),
    Rect.new(0, 0, self.width, self.height)
  )
end

def copy
  copy_bitmap = Bitmap.new(self.width, self.height)
  copy_bitmap.blt(0, 0, self, Rect.new(0, 0, self.width, self.height))
  return copy_bitmap
end
end
#===============================================================================
# End of script
#===============================================================================


Custom Shader
Custom Shader
CODE
#===============================================================================
# RGSS Shaders System (shader definition)
#-------------------------------------------------------------------------------
# Author: JoaoB
# WARNING: Place this script AFTER "Shader System"
# Script where the shaders will be defined
#===============================================================================

class RedChannelShader < Shader

  def self.apply(x, y)
    curr_color = @@bitmap.get_pixel(x, y)
    return Color.new(curr_color.red, 0, 0, curr_color.alpha)
  end

end

class BlackWhiteShader < Shader

  def self.apply(x, y)
    color = @@bitmap.get_pixel(x, y)
    bw = color.red*0.3 + color.green*0.59 + color.blue*0.11
    return Color.new(bw, bw, bw, color.alpha)
  end

end

#===============================================================================
# End of script
#===============================================================================


Documentation
Attached File  shaders.html ( 5.75K ) Number of downloads: 50


Customization
Well, the customization possibilities are almost infinite. I said "almost" because it depends on the bitmap size, shader complexity and other things. Attached is an HTML file with instruction.

Compatibility
It's compatible with any other script you post. In addiction, there's the def [tt]Bitmap::copy[/tt], which returns a copy of the bitmap, not the bitmap itself (yes, there are differences).

Screenshot
There are various effects you can achieve with this script; no number of screenshots can show all of them.
Screenshot


This is a screenshot of one of the shaders that come with the script.


The same shader, on RMVX.


Installation
To install the script, copy the scripts Shader System and Custom Shader in that order.
To create and apply the shaders, see the attached HTML file.

FAQ
Q: Does it work on RMVX?
A:
Yes, it does, but it's much slower.

Terms and Conditions
Well, you can use it freely in your game, but please, give me credit if you use it and let me know if you post it in another forum.

Credits
The system was made by me, but the inspiration came from Adobe Pixel Bender.


__________________________
A skilled scripter presenting itself to the biggest Maker community in the world.

Go to the top of the page
 
+Quote Post
   

Closed TopicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 19th May 2013 - 10:46 PM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker