Class: Mittsu::TorusGeometry

Inherits:
Geometry show all
Defined in:
lib/mittsu/extras/geometries/torus_geometry.rb

Instance Attribute Summary

Attributes inherited from Geometry

#bounding_box, #bounding_sphere, #colors, #dynamic, #face_vertex_uvs, #faces, #has_tangents, #id, #line_distances, #morph_colors, #morph_normals, #morph_targets, #name, #skin_indices, #skin_weights, #type, #uuid, #vertices

Instance Method Summary collapse

Methods inherited from Geometry

#apply_matrix, #center, #clone, #compute_bounding_box, #compute_bounding_sphere, #compute_face_normals, #compute_line_distances, #compute_morph_normals, #compute_tangents, #compute_vertex_normals, #dispose, #from_buffer_geometry, #merge, #merge_mesh, #merge_vertices, #to_json

Methods included from EventDispatcher

#add_event_listener, #dispatch_event, #has_event_listener, #remove_event_listener

Constructor Details

#initialize(radius = 100.0, tube = 40.0, radial_segments = 8, tubular_segments = 6, arc = (::Math::PI * 2.0)) ⇒ TorusGeometry

Returns a new instance of TorusGeometry.



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
# File 'lib/mittsu/extras/geometries/torus_geometry.rb', line 6

def initialize(radius = 100.0, tube = 40.0, radial_segments = 8, tubular_segments = 6, arc = (::Math::PI * 2.0))
  super()

  @type = 'TorusGeometry'

  @parameters = {
    radius:           radius,
    tube:             tube,
    radial_segments:  radial_segments,
    tubular_segments: tubular_segments,
    arc:              arc
  }

  center = Vector3.new
  uvs = []
  normals = []

  for j in 0..radial_segments do
    for i in 0..tubular_segments do
      u = i.to_f / tubular_segments * arc
      v = j.to_f / radial_segments * ::Math::PI * 2.0

      center.x = radius * ::Math.cos(u)
      center.y = radius * ::Math.sin(u)

      vertex = Vector3.new
      vertex.x = (radius + tube * ::Math.cos(v)) * ::Math.cos(u)
      vertex.y = (radius + tube * ::Math.cos(v)) * ::Math.sin(u)
      vertex.z = tube * ::Math.sin(v)

      @vertices << vertex

      uvs << Vector2.new(i.to_f / tubular_segments, j.to_f / radial_segments)
      normals << vertex.clone.sub(center).normalize
    end
  end

  for j in 1..radial_segments do
    for i in 1..tubular_segments do
      a = (tubular_segments + 1) * j + i - 1
      b = (tubular_segments + 1) * (j - 1) + i - 1
      c = (tubular_segments + 1) * (j - 1) + i
      d = (tubular_segments + 1) * j + i

      face = Face3.new(a, b, d, [normals[a].clone, normals[b].clone, normals[d].clone])
      @faces << face
      @face_vertex_uvs[0] << [uvs[a].clone, uvs[b].clone, uvs[d].clone]

      face = Face3.new(b, c, d, [normals[b].clone, normals[c].clone, normals[d].clone])
      @faces << face
      @face_vertex_uvs[0] << [uvs[b].clone, uvs[c].clone, uvs[d].clone]
    end
  end

  compute_face_normals
end