|
| 1 | +#!/usr/bin/env jruby |
| 2 | + |
| 3 | +require 'propane' |
| 4 | + |
| 5 | +class BloomDemo < Propane::App |
| 6 | + |
| 7 | + load_library :pixel_flow |
| 8 | + java_import 'com.thomasdiewald.pixelflow.java.DwPixelFlow' |
| 9 | + java_import 'com.thomasdiewald.pixelflow.java.imageprocessing.filter.DwFilter' |
| 10 | + # |
| 11 | + # PixelFlow | Copyright (C) 2016 Thomas Diewald - http://thomasdiewald.com |
| 12 | + # |
| 13 | + # A Processing/Java library for high performance GPU-Computing (GLSL). |
| 14 | + # MIT License: https://opensource.org/licenses/MIT |
| 15 | + # |
| 16 | + # |
| 17 | + # Bloom Shader applied as post-processing effect on a simple 2D scene. |
| 18 | + # |
| 19 | + VIEWPORT_W = 1280 |
| 20 | + VIEWPORT_H = 720 |
| 21 | + VIEWPORT_X = 230 |
| 22 | + VIEWPORT_Y = 0 |
| 23 | + |
| 24 | + attr_reader :context, :filter, :pg_render, :pg_luminance, :pg_bloom, :font12 |
| 25 | + attr_reader :display_mode |
| 26 | + |
| 27 | + def settings |
| 28 | + size(VIEWPORT_W, VIEWPORT_H, P2D) |
| 29 | + smooth(0) |
| 30 | + end |
| 31 | + |
| 32 | + def setup |
| 33 | + surface.setLocation(VIEWPORT_X, VIEWPORT_Y) |
| 34 | + @display_mode = '1' |
| 35 | + @context = DwPixelFlow.new(self) |
| 36 | + context.print |
| 37 | + context.printGL |
| 38 | + @filter = DwFilter.new(context) |
| 39 | + @pg_render = create_graphics(width, height, P2D) |
| 40 | + pg_render.smooth(8) |
| 41 | + @pg_luminance = create_graphics(width, height, P2D) |
| 42 | + pg_luminance.smooth(8) |
| 43 | + @pg_bloom = create_graphics(width, height, P2D) |
| 44 | + pg_bloom.smooth(8) |
| 45 | + @font12 = create_font("SourceCodePro-Regular", 12) |
| 46 | + background(0) |
| 47 | + # frame_rate(60) |
| 48 | + frame_rate(1000) |
| 49 | + end |
| 50 | + |
| 51 | + def draw |
| 52 | + # just draw something into pg_src_A |
| 53 | + pg_render.begin_draw |
| 54 | + pg_render.blend_mode(BLEND) # default |
| 55 | + pg_render.rect_mode(CENTER) |
| 56 | + pg_render.background(0) |
| 57 | + num_x = 20 |
| 58 | + num_y = (num_x / (width / height)).ceil |
| 59 | + size_x = (width - 1) / num_x.to_f |
| 60 | + size_y = (height - 1) / num_y.to_f |
| 61 | + scale_xy = 0.4 |
| 62 | + grid(num_y, num_x) do |y, x| # using the JRubyArt convenience grid method |
| 63 | + px = x * size_x + size_x * 0.5 |
| 64 | + py = y * size_y + size_y * 0.5 |
| 65 | + norm_x = x / (num_x - 1).to_f |
| 66 | + norm_y = y / (num_y - 1).to_f |
| 67 | + rgb_r = 255 - 255 * norm_x * norm_y |
| 68 | + rgb_g = norm_x * 255 |
| 69 | + rgb_b = norm_y * 255 |
| 70 | + col = color(rgb_r, rgb_g, rgb_b) |
| 71 | + pg_render.push_matrix |
| 72 | + pg_render.translate(px, py) |
| 73 | + pg_render.stroke_weight(2) |
| 74 | + pg_render.stroke(col) |
| 75 | + if( ((x^y)&1) == 0) |
| 76 | + pg_render.fill(col) |
| 77 | + else |
| 78 | + pg_render.no_fill |
| 79 | + end |
| 80 | + pg_render.rect(0, 0, size_x * scale_xy, size_y * scale_xy, 0) |
| 81 | + pg_render.pop_matrix |
| 82 | + end |
| 83 | + pg_render.stroke(255) |
| 84 | + pg_render.fill(0, 240) |
| 85 | + pg_render.stroke_weight(2) |
| 86 | + pg_render.ellipse(mouse_x, mouse_y, height / 4, height / 4) |
| 87 | + pg_render.end_draw |
| 88 | + unless display_mode == '0' |
| 89 | + # luminance pass |
| 90 | + filter.luminance_threshold.param.threshold = 0.0 # when 0, all colors are used |
| 91 | + filter.luminance_threshold.param.exponent = 5 |
| 92 | + filter.luminance_threshold.apply(pg_render, pg_luminance) |
| 93 | + |
| 94 | + # bloom pass |
| 95 | + # if the original image is used as source, the previous luminance pass |
| 96 | + # can just be skipped |
| 97 | + filter.bloom.param.mult = map1d(mouse_x, 0..width, 0..10) |
| 98 | + filter.bloom.param.radius = map1d(mouse_y, 0..height, 0..1) |
| 99 | + filter.bloom.apply(pg_luminance, pg_bloom, pg_render) |
| 100 | + end |
| 101 | + case(display_mode) |
| 102 | + when '2' |
| 103 | + filter.copy.apply(pg_bloom, pg_render) |
| 104 | + when '3' |
| 105 | + filter.copy.apply(pg_luminance, pg_render) |
| 106 | + when '4' |
| 107 | + filter.copy.apply(filter.bloom.gaussianpyramid.tex_blur[0], pg_render) |
| 108 | + when '5' |
| 109 | + filter.copy.apply(filter.bloom.gaussianpyramid.tex_blur[1], pg_render) |
| 110 | + when '6' |
| 111 | + filter.copy.apply(filter.bloom.gaussianpyramid.tex_blur[2], pg_render) |
| 112 | + when '7' |
| 113 | + filter.copy.apply(filter.bloom.gaussianpyramid.tex_blur[3], pg_render) |
| 114 | + when '8' |
| 115 | + filter.copy.apply(filter.bloom.gaussianpyramid.tex_blur[4], pg_render) |
| 116 | + end |
| 117 | + # display result |
| 118 | + blend_mode(REPLACE) |
| 119 | + background(0) |
| 120 | + image(pg_render, 0, 0) |
| 121 | + |
| 122 | + # mouse position hints |
| 123 | + s = 3 |
| 124 | + blend_mode(BLEND) |
| 125 | + stroke_cap(SQUARE) |
| 126 | + stroke_weight(s * 2) |
| 127 | + stroke(0, 50) |
| 128 | + line(0, s, width, s) |
| 129 | + line(s, 0, s, height) |
| 130 | + stroke(0, 160) |
| 131 | + line(0, s, mouse_x, s) |
| 132 | + line(s, 0, s, mouse_y) |
| 133 | + # info |
| 134 | + format_string = 'Bloom Demo [size %d/%d] [mode %d] [frame %d] [fps: (%6.2f)]' |
| 135 | + fps = format(format_string, pg_render.width, pg_render.height, display_mode, frame_count, frame_rate) |
| 136 | + surface.set_title(fps) |
| 137 | + # ke_y hints |
| 138 | + text_font(font12) |
| 139 | + tx = 15 |
| 140 | + ty = 15 |
| 141 | + gap = 15 |
| 142 | + stroke(255) |
| 143 | + text(fps, tx, ty += gap) |
| 144 | + text("'0' bloom OFF" , tx, ty += gap) |
| 145 | + text("'1' Bloom ON" , tx, ty += gap) |
| 146 | + text("'2' bloom only", tx, ty += gap) |
| 147 | + text("'3' Luminance" , tx, ty += gap) |
| 148 | + text("'4' Blur[0]" , tx, ty += gap) |
| 149 | + text("'5' Blur[1]" , tx, ty += gap) |
| 150 | + text("'6' Blur[2]" , tx, ty += gap) |
| 151 | + text("'7' Blur[3]" , tx, ty += gap) |
| 152 | + text("'8' Blur[4]" , tx, ty += gap) |
| 153 | + text("mouseX: bloom multiplier" , tx, ty += gap) |
| 154 | + text("mouseY: bloom radius" , tx, ty += gap) |
| 155 | + end |
| 156 | + |
| 157 | + def key_released |
| 158 | + case key |
| 159 | + when '0'..'8' |
| 160 | + @display_mode = key |
| 161 | + end |
| 162 | + end |
| 163 | +end |
| 164 | + |
| 165 | +BloomDemo.new |
0 commit comments