Class: Mittsu::PlaneBufferGeometry

Inherits:
BufferGeometry show all
Defined in:
lib/mittsu/extras/geometries/plane_buffer_geometry.rb

Instance Attribute Summary

Attributes inherited from BufferGeometry

#attributes, #bounding_box, #bounding_sphere, #draw_calls, #id, #name, #type, #uuid

Instance Method Summary collapse

Methods inherited from BufferGeometry

#[], #[]=, #add_draw_call, #apply_matrix, #center, #clone, #compute_bounding_box, #compute_bounding_sphere, #compute_offsets, #compute_tangents, #compute_vertex_normals, #dispose, #from_geometry, #keys, #merge, #normalize_normals, #reorder_buffers, #to_json

Methods included from EventDispatcher

#add_event_listener, #dispatch_event, #has_event_listener, #remove_event_listener

Constructor Details

#initialize(width, height, width_segments = 1, height_segments = 1) ⇒ PlaneBufferGeometry

Returns a new instance of PlaneBufferGeometry.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mittsu/extras/geometries/plane_buffer_geometry.rb', line 3

def initialize(width, height, width_segments = 1, height_segments = 1)
  super()

  @type = 'PlaneBufferGeometry'

  @parameters = {
    width:           width,
    height:          height,
    width_segments:  width_segments,
    height_segments: height_segments
  }

  width_half = width / 2.0
  height_half = height / 2.0

  grid_x = width_segments || 1
  grid_y = height_segments || 1

  grid_x1 = grid_x + 1
  grid_y1 = grid_y + 1

  segment_width = width.to_f / grid_x.to_f
  segment_height = height.to_f / grid_y.to_f

  vertices = Array.new(grid_x1 * grid_y1 * 3) # Float32Array
  normals = Array.new(grid_x1 * grid_y1 * 3) #Float32Array
  uvs = Array.new(grid_x1 * grid_y1 * 2) # Float32Array

  offset = 0
  offset2 = 0

  for iy in 0...grid_y1 do
    y = iy.to_f * segment_height - height_half
    for ix in 0...grid_x1 do
      x = ix.to_f * segment_width - width_half

      vertices[offset] = x
      vertices[offset + 1] = -y

      normals[offset + 2] = 1.0

      uvs[offset2] = ix.to_f / grid_x.to_f
      uvs[offset2 + 1] = 1.0 - (iy.to_f / grid_y.to_f)

      offset += 3
      offset2 += 2
    end
  end

  offset = 0

  indices = Array.new(grid_x * grid_y * 6) # ( ( vertices.length / 3 ) > 65535 ? Uint32Array : Uint16Array )

  for iy in 0...grid_y do
    for ix in 0...grid_x do
      a = ix + grid_x1 * iy
      b = ix + grid_x1 * (iy + 1)
      c = (ix + 1) + grid_x1 * (iy + 1)
      d = (ix + 1) + grid_x1 * iy

      indices[offset    ] = a
      indices[offset + 1] = b
      indices[offset + 2] = d

      indices[offset + 3] = b
      indices[offset + 4] = c
      indices[offset + 5] = d

      offset += 6
    end
  end

  self[:index]    = BufferAttribute.new(indices, 1)
  self[:position] = BufferAttribute.new(vertices, 3)
  self[:normal]   = BufferAttribute.new(normals, 3)
  self[:uv]       = BufferAttribute.new(uvs, 2)
end