Hi all,
I'm trying to Ruby-ize the excellent tutorial found here:
http://www.go-2-school.com/media/view/1
This tutorial shows how to manually replace the B&W terrain images that are pulled from google earth with color ones.
I was hoping to eliminate the manual material "line-up" process with an automatic one.
I have it working with the 2D plane...but can't for the life of me figureout how to do it with the 3D terrain...and am hoping someone more talented than I can kick me in the right direction.
So, my steps for the 2D piece of the puzzle are in the attached code..to see it in action, fire up Google Earth, zoom into the required terrain. Switch to sketchup and "Get Current View" to import the google terrain. Go back into Google Earth and SAVE IMAGE (for the higher res color image). Edit the script to the image file you just saved and let it rip. You will see the 2D terrain is perfect. The 3d terrain needs resizing - but I can't figure out how to do it (and alter the aspect ratio!). HELP!
` # Grab a handle to the currently active model (aka the one the user is looking at in SketchUp.)
model = Sketchup.active_model
def flattenUVQ(uvq)
uvq.x = uvq.x / uvq.z
uvq.y = uvq.y / uvq.z
uvq.z = 1.0
return uvq
end
Grab other handles to commonly used collections inside the model.
entities = model.entities
layers = model.layers
materials = model.materials
component_definitions = model.definitions
selection = model.selection
Now that we have our handles, we can start pulling objects and making
method calls that are useful.
ge_entity = entities[0]
ge_snapshot = entities[0]
ge_terrain = entities[0]
UI.messagebox("First thing in your model is a " + first_entity.typename)
number_objects = entities.length
puts "\nnumber entities:" + number_objects.to_s
puts "=================\n"
for k in 1..number_objects
if entities[k-1].typename.to_s == "Group"
ge_entity = entities[k-1]
if ge_entity.name == "Google Earth Snapshot"
ge_snapshot = ge_entity
elsif ge_entity.name == "Google Earth Terrain"
ge_terrain = ge_entity
end
end
end
bounds = ge_snapshot.local_bounds #get the bounds of the image
puts "processing google earth snapshot..."
puts "---bounds [height:" + bounds.height.to_s + "] width:" + bounds.width.to_s
puts "---sub-entities:" + ge_snapshot.entities.length.to_s + " found. looking for face."
ge_snapshot.locked=false #unlock the entity
ge_terrain.locked = false
for h in 1..ge_snapshot.entities.length
face_entity = ge_snapshot.entities[h-1]
if face_entity.typename.to_s == "Face" # we've found our face
break
end
end
mat = face_entity.material # grab the material
texture = mat.texture #Assign the texture
puts "---face found. processing face."
puts "------[material:" + mat.display_name + "]\n------[texture:" + texture.filename + "]"
puts " height: " + texture.height.to_s
puts " width: " + texture.width.to_s
puts " image_height: " + texture.image_height.to_s
puts " image_width: " + texture.image_width.to_s
tw = Sketchup.create_texture_writer
uv_helper = face_entity.get_UVHelper true, false, tw
samples = [] #Create a set of samples
samples << face_entity.vertices[0].position # 0,0 | Origin
samples << samples[0].offset(face_entity.normal.axes.x) # 1,0 | Offset Origin in X
samples << samples[0].offset(face_entity.normal.axes.y) # 0,1 | Offset Origin in Y
samples << samples[1].offset(face_entity.normal.axes.y) # 1,1 | Offset X in Y
Arrays containing 3D and UV points.
xyz = []
uv = []
samples.each { |position|
# XYZ 3D coordinates
xyz << position
# UV 2D coordinates
uvq = uv_helper.get_front_UVQ(position)
uv << self.flattenUVQ(uvq)
}
print "\nxyz: " + xyz.to_s
print "\nuv : " + uv.to_s
print "\n"
Position texture.
pts = []
(0..3).each { |i|
pts << xyz[i]
pts << uv[i]
}
puts "------Setting size to " + bounds.width.to_s
mat.texture = "G:\Test Image 01.JPG" # Grab our hi-res image
mat.texture.size = bounds.width # Resize the thing
face_entity.position_material mat, pts, true # Apply the coordinates
puts "------Reapplying co-ordinates"
puts "=================\nend of run"`