Class: VoxelammingGem::BuildBox
- Inherits:
-
Object
- Object
- VoxelammingGem::BuildBox
- Defined in:
- lib/voxelamming_gem.rb
Overview
Main process
Constant Summary collapse
- @@texture_names =
["grass", "stone", "dirt", "planks", "bricks"]
Instance Method Summary collapse
- #animate(x, y, z, pitch: 0, yaw: 0, roll: 0, scale: 1, interval: 10) ⇒ Object
- #animate_global(x, y, z, pitch: 0, yaw: 0, roll: 0, scale: 1, interval: 10) ⇒ Object
- #change_material(is_metallic: false, roughness: 0.5) ⇒ Object
- #change_shape(shape) ⇒ Object
- #clear_data ⇒ Object
- #create_box(x, y, z, r: 1, g: 1, b: 1, alpha: 1, texture: '') ⇒ Object
- #draw_line(x1, y1, z1, x2, y2, z2, r: 1, g: 1, b: 1, alpha: 1) ⇒ Object
- #frame_in ⇒ Object
- #frame_out ⇒ Object
-
#initialize(room_name) ⇒ BuildBox
constructor
A new instance of BuildBox.
- #pop_matrix ⇒ Object
- #push_matrix ⇒ Object
- #remove_box(x, y, z) ⇒ Object
- #round_colors(num_list) ⇒ Object
- #round_numbers(num_list) ⇒ Object
- #send_data ⇒ Object
- #set_box_size(box_size) ⇒ Object
- #set_build_interval(interval) ⇒ Object
- #set_command(command) ⇒ Object
- #set_frame_fps(fps = 2) ⇒ Object
- #set_frame_repeats(repeats = 10) ⇒ Object
- #set_light(x, y, z, r: 1, g: 1, b: 1, alpha: 1, intensity: 1000, interval: 1, light_type: 'point') ⇒ Object
- #translate(x, y, z, pitch: 0, yaw: 0, roll: 0) ⇒ Object
- #write_sentence(sentence, x, y, z, r: 1, g: 1, b: 1, alpha: 1) ⇒ Object
Constructor Details
#initialize(room_name) ⇒ BuildBox
Returns a new instance of BuildBox.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/voxelamming_gem.rb', line 23 def initialize(room_name) @room_name = room_name @is_allowed_matrix = 0 @saved_matrices = [] @translation = [0, 0, 0, 0, 0, 0] @matrix_translation = [0, 0, 0, 0, 0, 0] @frame_translations = [] @global_animation = [0, 0, 0, 0, 0, 0, 1, 0] @animation = [0, 0, 0, 0, 0, 0, 1, 0] @boxes = [] @frames = [] @sentence = [] @lights = [] @commands = [] @size = 1 @shape = 'box' @is_metallic = 0 @roughness = 0.5 @is_allowed_float = 0 @build_interval = 0.01 @is_framing = false @frame_id = 0 end |
Instance Method Details
#animate(x, y, z, pitch: 0, yaw: 0, roll: 0, scale: 1, interval: 10) ⇒ Object
181 182 183 184 |
# File 'lib/voxelamming_gem.rb', line 181 def animate(x, y, z, pitch: 0, yaw: 0, roll: 0, scale: 1, interval: 10) x, y, z = round_numbers([x, y, z]) @animation = [x, y, z, pitch, yaw, roll, scale, interval] end |
#animate_global(x, y, z, pitch: 0, yaw: 0, roll: 0, scale: 1, interval: 10) ⇒ Object
176 177 178 179 |
# File 'lib/voxelamming_gem.rb', line 176 def animate_global(x, y, z, pitch: 0, yaw: 0, roll: 0, scale: 1, interval: 10) x, y, z = round_numbers([x, y, z]) @global_animation = [x, y, z, pitch, yaw, roll, scale, interval] end |
#change_material(is_metallic: false, roughness: 0.5) ⇒ Object
283 284 285 286 |
# File 'lib/voxelamming_gem.rb', line 283 def change_material(is_metallic: false, roughness: 0.5) @is_metallic = ? 1 : 0 @roughness = roughness end |
#change_shape(shape) ⇒ Object
279 280 281 |
# File 'lib/voxelamming_gem.rb', line 279 def change_shape(shape) @shape = shape end |
#clear_data ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/voxelamming_gem.rb', line 47 def clear_data @translation = [0, 0, 0, 0, 0, 0] @is_allowed_matrix = 0 @saved_matrices = [] @translation = [0, 0, 0, 0, 0, 0] @matrix_translation = [0, 0, 0, 0, 0, 0] @frame_translations = [] @global_animation = [0, 0, 0, 0, 0, 0, 1, 0] @animation = [0, 0, 0, 0, 0, 0, 1, 0] @boxes = [] @frames = [] @sentence = [] @lights = [] @commands = [] @size = 1 @shape = 'box' @is_metallic = 0 @roughness = 0.5 @is_allowed_float = 0 @build_interval = 0.01 @is_framing = false @frame_id = 0 end |
#create_box(x, y, z, r: 1, g: 1, b: 1, alpha: 1, texture: '') ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/voxelamming_gem.rb', line 131 def create_box(x, y, z, r: 1, g: 1, b: 1, alpha: 1, texture: '') if @is_allowed_matrix > 0 # 移動用のマトリックスにより位置を計算する matrix = @matrix_translation base_position = matrix[0..2] if matrix.length == 6 base_rotation_matrix = get_rotation_matrix(matrix[3], matrix[4], matrix[5]) else base_rotation_matrix = [matrix[3..5], matrix[6..8], matrix[9..11]] end # Compute the new position after translation add_x, add_y, add_z = transform_point_by_rotation_matrix([x, y, z], transpose_3x3(base_rotation_matrix)) x, y, z = add_vectors(base_position, [add_x, add_y, add_z]) end x, y, z = round_numbers([x, y, z]) r, g, b, alpha = round_colors([r, g, b, alpha]) # 重ねておくことを防止 remove_box(x, y, z) if !@@texture_names.include?(texture) texture_id = -1 else texture_id = @@texture_names.index(texture) end if @is_framing @frames.append([x, y, z, r, g, b, alpha, texture_id, @frame_id]) else @boxes.append([x, y, z, r, g, b, alpha, texture_id]) end end |
#draw_line(x1, y1, z1, x2, y2, z2, r: 1, g: 1, b: 1, alpha: 1) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/voxelamming_gem.rb', line 225 def draw_line(x1, y1, z1, x2, y2, z2, r: 1, g: 1, b: 1, alpha: 1) x1, y1, z1, x2, y2, z2 = [x1, y1, z1, x2, y2, z2].map(&:floor) diff_x = x2 - x1 diff_y = y2 - y1 diff_z = z2 - z1 max_length = [diff_x.abs, diff_y.abs, diff_z.abs].max return false if diff_x == 0 && diff_y == 0 && diff_z == 0 if diff_x.abs == max_length if x2 > x1 (x1..x2).each do |x| y = y1 + (x - x1) * diff_y.to_f / diff_x z = z1 + (x - x1) * diff_z.to_f / diff_x create_box(x, y, z, r: r, g: g, b: b, alpha: alpha) end else x1.downto(x2 + 1) do |x| y = y1 + (x - x1) * diff_y.to_f / diff_x z = z1 + (x - x1) * diff_z.to_f / diff_x create_box(x, y, z, r: r, g: g, b: b, alpha: alpha) end end elsif diff_y.abs == max_length if y2 > y1 (y1..y2).each do |y| x = x1 + (y - y1) * diff_x.to_f / diff_y z = z1 + (y - y1) * diff_z.to_f / diff_y create_box(x, y, z, r: r, g: g, b: b, alpha: alpha) end else y1.downto(y2 + 1) do |y| x = x1 + (y - y1) * diff_x.to_f / diff_y z = z1 + (y - y1) * diff_z.to_f / diff_y create_box(x, y, z, r: r, g: g, b: b, alpha: alpha) end end elsif diff_z.abs == max_length if z2 > z1 (z1..z2).each do |z| x = x1 + (z - z1) * diff_x.to_f / diff_z y = y1 + (z - z1) * diff_y.to_f / diff_z create_box(x, y, z, r: r, g: g, b: b, alpha: alpha) end else z1.downto(z2 + 1) do |z| x = x1 + (z - z1) * diff_x.to_f / diff_z y = y1 + (z - z1) * diff_y.to_f / diff_z create_box(x, y, z, r: r, g: g, b: b, alpha: alpha) end end end end |
#frame_in ⇒ Object
79 80 81 |
# File 'lib/voxelamming_gem.rb', line 79 def frame_in @is_framing = true end |
#frame_out ⇒ Object
83 84 85 86 |
# File 'lib/voxelamming_gem.rb', line 83 def frame_out @is_framing = false @frame_id += 1 end |
#pop_matrix ⇒ Object
93 94 95 96 |
# File 'lib/voxelamming_gem.rb', line 93 def pop_matrix @is_allowed_matrix -= 1 @matrix_translation = @saved_matrices.pop end |
#push_matrix ⇒ Object
88 89 90 91 |
# File 'lib/voxelamming_gem.rb', line 88 def push_matrix @is_allowed_matrix += 1 @saved_matrices.push(@matrix_translation) end |
#remove_box(x, y, z) ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/voxelamming_gem.rb', line 166 def remove_box(x, y, z) x, y, z = round_numbers([x, y, z]) if @is_framing @frames.reject! { |frame| frame[0] == x && frame[1] == y && frame[2] == z && frame[8] == @frame_id } else @boxes.reject! { |box| box[0] == x && box[1] == y && box[2] == z } end end |
#round_colors(num_list) ⇒ Object
348 349 350 |
# File 'lib/voxelamming_gem.rb', line 348 def round_colors(num_list) num_list.map { |val| val.round(2) } end |
#round_numbers(num_list) ⇒ Object
340 341 342 343 344 345 346 |
# File 'lib/voxelamming_gem.rb', line 340 def round_numbers(num_list) if @is_allowed_float == 1 num_list.map { |val| val.round(2) } else num_list.map { |val| val.round(1).floor } end end |
#send_data ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/voxelamming_gem.rb', line 288 def send_data puts 'send_data' now = DateTime.now data_to_send = { "translation": @translation, "frameTranslations": @frame_translations, "globalAnimation": @global_animation, "animation": @animation, "boxes": @boxes, "frames": @frames, "sentence": @sentence, "lights": @lights, "commands": @commands, "size": @size, "shape": @shape, "interval": @build_interval, "isMetallic": @is_metallic, "roughness": @roughness, "isAllowedFloat": @is_allowed_float, "date": now.to_s }.to_json EM.run do ws = Faye::WebSocket::Client.new('wss://websocket.voxelamming.com') ws.on :open do |_event| p [:open] puts 'WebSocket connection open' ws.send(@room_name) puts "Joined room: #{@room_name}" ws.send(data_to_send) puts data_to_send puts 'Sent data to server' EM.add_timer(1) do ws.close EM.stop end end ws.on :error do |event| puts "WebSocket error: #{event.}" EM.stop end ws.on :close do |_event| puts 'WebSocket connection closed' EM.stop end end end |
#set_box_size(box_size) ⇒ Object
186 187 188 |
# File 'lib/voxelamming_gem.rb', line 186 def set_box_size(box_size) @size = box_size end |
#set_build_interval(interval) ⇒ Object
190 191 192 |
# File 'lib/voxelamming_gem.rb', line 190 def set_build_interval(interval) @build_interval = interval end |
#set_command(command) ⇒ Object
217 218 219 220 221 222 223 |
# File 'lib/voxelamming_gem.rb', line 217 def set_command(command) @commands << command if command == 'float' @is_allowed_float = 1 end end |
#set_frame_fps(fps = 2) ⇒ Object
71 72 73 |
# File 'lib/voxelamming_gem.rb', line 71 def set_frame_fps(fps = 2) @commands << "fps #{fps}" end |
#set_frame_repeats(repeats = 10) ⇒ Object
75 76 77 |
# File 'lib/voxelamming_gem.rb', line 75 def set_frame_repeats(repeats = 10) @commands << "repeats #{repeats}" end |
#set_light(x, y, z, r: 1, g: 1, b: 1, alpha: 1, intensity: 1000, interval: 1, light_type: 'point') ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/voxelamming_gem.rb', line 201 def set_light(x, y, z, r: 1, g: 1, b: 1, alpha: 1, intensity: 1000, interval: 1, light_type: 'point') x, y, z = round_numbers([x, y, z]) r, g, b, alpha = round_colors([r, g, b, alpha]) if light_type == 'point' light_type = 1 elsif light_type == 'spot' light_type = 2 elsif light_type == 'directional' light_type = 3 else light_type = 1 end @lights << [x, y, z, r, g, b, alpha, intensity, interval, light_type] end |
#translate(x, y, z, pitch: 0, yaw: 0, roll: 0) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/voxelamming_gem.rb', line 98 def translate(x, y, z, pitch: 0, yaw: 0, roll: 0) if @is_allowed_matrix > 0 # 移動用のマトリックスを計算する matrix = @saved_matrices.last puts "before matrix: #{matrix}" base_position = matrix[0..2] if matrix.length == 6 base_rotation_matrix = get_rotation_matrix(matrix[3], matrix[4], matrix[5]) else base_rotation_matrix = [matrix[3..5], matrix[6..8], matrix[9..11]] end # Compute the new position after translation add_x, add_y, add_z = transform_point_by_rotation_matrix([x, y, z], transpose_3x3(base_rotation_matrix)) x, y, z = add_vectors(base_position, [add_x, add_y, add_z]) x, y, z = round_numbers([x, y, z]) # Compute the rotation after translation translate_rotation_matrix = get_rotation_matrix(-pitch, -yaw, -roll) rotate_matrix = matrix_multiply(translate_rotation_matrix, base_rotation_matrix) @matrix_translation = [x, y, z, *rotate_matrix[0], *rotate_matrix[1], *rotate_matrix[2]] else x, y, z = round_numbers([x, y, z]) if @is_framing @frame_translations.append([x, y, z, pitch, yaw, roll, @frame_id]) else @translation = [x, y, z, pitch, yaw, roll] end end end |
#write_sentence(sentence, x, y, z, r: 1, g: 1, b: 1, alpha: 1) ⇒ Object
194 195 196 197 198 199 |
# File 'lib/voxelamming_gem.rb', line 194 def write_sentence(sentence, x, y, z, r: 1, g: 1, b: 1, alpha: 1) x, y, z = round_numbers([x, y, z]).map(&:to_s) r, g, b, alpha = round_colors([r, g, b, alpha]) r, g, b, alpha = [r, g, b, alpha].map(&:floor).map(&:to_s) @sentence = [sentence, x, y, z, r, g, b, alpha] end |