Created
October 29, 2021 19:07
-
-
Save DanRathbun/4c2247da235c276663fffc7d251f5725 to your computer and use it in GitHub Desktop.
Projected Textures: An example using the new SketchUp v2021.1 functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copy face materials (front and back) from a source face to a target face. | |
# For v2021.1 and higher, any texture positioning and projection is copied. | |
# @param face1 [Sketchup::Face] the source face. | |
# @param face2 [Sketchup::Face] the target face. | |
# @return [Boolean] whether texture positioning was enabled during copy. | |
def copy_face_materials(face1, face2) | |
# Test if the API is at or higher than 2021.1: | |
positioning_enabled = face1.respond_to?(:texture_projected?) | |
# | |
frontside = true | |
backside = false | |
reference = face1.vertices.first.position | |
# | |
for side in [frontside, backside] | |
meth =( side ? :material : :back_material ) | |
matl = face1.send(meth) | |
# Copy this side's material (might set to nil): | |
face2.send("#{meth.to_s}=", matl) | |
next unless positioning_enabled | |
# If this side's material has a texture and is positioned: | |
if matl.texture && face1.texture_positioned?(side) | |
mapping = face.uv_tile_at(reference, side) | |
if face1.texture_projected?(side) | |
face2.position_material( | |
matl, mapping, side, | |
face1.get_texture_projection(side) | |
) | |
else | |
face2.position_material(matl, mapping, side) | |
end | |
end | |
end # for side | |
# | |
return positioning_enabled | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment