Class: TT::UV_Plane

Inherits:
Object
  • Object
show all
Defined in:
TT_Lib2/uv_plane.rb

Overview

Helper class for planar UV mapping.

Map induvidual faces:

model = Sketchup.active_model
face = model.entities.find { |e| e.is_a?( Sketchup::Face ) }
uv_plane = TT::UV_Plane.new( ORIGIN, Z_AXIS.reverse, model.materials.current )
uv_plane.project( face, true )

Map faces in entity or array collection:

model = Sketchup.active_model
uv_plane = TT::UV_Plane.new( ORIGIN, Z_AXIS.reverse, model.materials.current )
uv_plane.project_to_entities( model.entities, true )

Since:

  • 2.5.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, normal, material = nil) ⇒ UV_Plane

Creates a new UV Plane.

Parameters:

  • origin (Geom::Point3d)
  • normal (Geom::Vector3d)
  • material (Sketchup::Material) (defaults to: nil)

Since:

  • 2.5.0



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'TT_Lib2/uv_plane.rb', line 37

def initialize(origin, normal, material = nil)
  @material = material
  if @material.nil? || @material.texture.nil?
    @width  = 10
    @height = 10
  else
    @width  = @material.texture.width
    @height = @material.texture.height
  end
  
  @origin = origin.clone
  @normal = normal.clone
  @plane = [@origin, @normal]
  
  @xaxis = @normal.axes.x
  
  update()
end

Instance Attribute Details

#heightObject

Since:

  • 2.5.0



26
27
28
# File 'TT_Lib2/uv_plane.rb', line 26

def height
  @height
end

#materialObject

Since:

  • 2.5.0



27
28
29
# File 'TT_Lib2/uv_plane.rb', line 27

def material
  @material
end

#normalObject

Since:

  • 2.5.0



26
27
28
# File 'TT_Lib2/uv_plane.rb', line 26

def normal
  @normal
end

#originObject

Since:

  • 2.5.0



26
27
28
# File 'TT_Lib2/uv_plane.rb', line 26

def origin
  @origin
end

#widthObject

Since:

  • 2.5.0



26
27
28
# File 'TT_Lib2/uv_plane.rb', line 26

def width
  @width
end

#xaxisObject

Since:

  • 2.5.0



26
27
28
# File 'TT_Lib2/uv_plane.rb', line 26

def xaxis
  @xaxis
end

Instance Method Details

#centerGeom::Point3d

Returns the center point of the first UV tile.

Returns:

  • (Geom::Point3d)

Since:

  • 2.5.0



83
84
85
86
87
88
89
# File 'TT_Lib2/uv_plane.rb', line 83

def center
  x = @xaxis
  y = yaxis()
  pt = @origin.offset( x, @width / 2.0 )
  pt.offset!( y, @height / 2.0 )
  pt
end

#center=(point) ⇒ Geom::Point3d

Centers the UV Plane around point.

Parameters:

  • point (Geom::Point3d)

Returns:

  • (Geom::Point3d)

Since:

  • 2.5.0



98
99
100
101
102
103
104
105
106
# File 'TT_Lib2/uv_plane.rb', line 98

def center=(point)
  x = @xaxis
  y = yaxis()
  pt = point.offset( x.reverse, @width / 2.0 )
  pt.offset!( y.reverse, @height / 2.0 )
  @origin = pt
  update()
  @origin.dup
end

#frame_segmentsArray<Geom::Point3d>

Returns an array of Point3d objects representing the segment boundary of one UV tile. Can be used for pick_helper.pick_segment.

Returns:

  • (Array<Geom::Point3d>)

Since:

  • 2.5.0



257
258
259
260
261
262
263
264
265
266
# File 'TT_Lib2/uv_plane.rb', line 257

def frame_segments
  y = yaxis()
  pts = []
  pts << @origin
  pts << pts.last.offset( @xaxis, @width )
  pts << pts.last.offset( y, @height )
  pts << pts.first.offset( y, @height )
  pts << pts.first
  pts
end

#get_UV(point3d) ⇒ Geom::Point3d

Calculates the UV coordinates for the given 3d point. The point should be on the UV plane.

Ref: mathforum.org/library/drmath/view/51727.html

Thanks to Chris Thomson for the SU solution. forums.sketchucation.com/viewtopic.php?f=180&t=28484&p=247275#p247271

Parameters:

  • point3d (Geom::Point3d)

Returns:

  • (Geom::Point3d)

Since:

  • 2.4.0



244
245
246
247
248
249
# File 'TT_Lib2/uv_plane.rb', line 244

def get_UV( point3d )
  point2d = point3d.transform(@to_local)
  u = point2d.x / @width
  v = point2d.y / @height
  return [u, v, 1.0]
end

#project(face, on_front = true, on_back = false) ⇒ Boolean

Projects a texture material from the UV plane onto the face. Material will applied even if it's not a textured material.

Parameters:

  • face (Sketchup::Face)
  • on_front (Boolean) (defaults to: true)
  • on_back (Boolean) (defaults to: false)

Returns:

  • (Boolean)

    true if material had a texture

Since:

  • 2.4.0



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'TT_Lib2/uv_plane.rb', line 172

def project(face, on_front=true, on_back=false)
  # Special case for untextured materials
  if @material.nil? || @material.materialType == 0
    face.material = @material if on_front
    face.back_material = @material if on_back
    return false
  end
  
  # The UV mapping co-ords to feed position_material.
  uv_points = []
  
  # Can't rely on the vertex positions, since a face might have only tree
  # and four sets are required to correctly map a distorted texture.
  # And the point can't be co-linear either.
  #
  # Instead, generate four points on the plane of the target face.
  # 3  4
  #
  # 1  2
  pts = []
  pts << face.vertices.first.position               # 1
  pts << pts.first.offset( face.normal.axes.x, 10 ) # 2
  pts << pts.first.offset( face.normal.axes.y, 10 ) # 3
  pts << pts.last.offset(  face.normal.axes.x, 10 ) # 4
  
  # Now project each of the point to the UV plane in order to obtain
  # the UV coordinates relative to the UV origin.
  pts.each { |p1|
    # Get UV coordinates.
    p2 = p1.project_to_plane(@plane)
    uv = get_UV( p2 )
    # Add the points to the pool.
    uv_points << p1
    uv_points << uv
  }
  face.position_material( @material, uv_points, on_front )
  face.position_material( @material, uv_points, !on_back )
  true
end

#project_to_entities(entities, on_front = true, on_back = false) ⇒ Nil

Projects a texture material from the UV plane onto the faces in the given collection of entities.

Parameters:

  • entities (Sketchup::Entities, Sketchup::Selection, Array<Sketchup::Entity>)
  • on_front (Boolean) (defaults to: true)
  • on_back (Boolean) (defaults to: false)

Returns:

  • (Nil)

Since:

  • 2.4.0



222
223
224
225
226
227
228
# File 'TT_Lib2/uv_plane.rb', line 222

def project_to_entities(entities, on_front=true, on_back=false)
  for e in entities
    next unless e.is_a?(Sketchup::Face)
    project( e, on_front )
  end
  nil
end

#to_aArray<Geom::Point3d,Geom::Vector3d>

Returns an array representing the UV plane.

Returns:

  • (Array<Geom::Point3d,Geom::Vector3d>)

Since:

  • 2.4.0



61
62
63
# File 'TT_Lib2/uv_plane.rb', line 61

def to_a
  return [origin, normal]
end