Jump to content

How to assign a texture to a 3D object (Solved)


Recommended Posts

This is a question that probably has a simple answer but that, for some reason I cannot manage to get right.

So far I have this code but it does not seem to produce the expected results (the 3D poly-line does not show any texture at all):

 

vs.BeginPoly3D()
vs.Add3DPt((-5, 0, -5))
vs.Add3DPt(( 5, 0, -5))
vs.Add3DPt(( 5, 0,  5))
vs.Add3DPt((-5, 0,  5))
vs.Add3DPt((-5, 0, -5))
vs.EndPoly3D()
image = vs.LNewObj()
textureRef = vs.Name2Index("Test texture")

vs.SetObjExpandTexture(image, 0)
vs.SetTextureRef(image, textureRef, 1)
vs.SetDefaultTexMap(image)

vs.SetTexMapBool(image, 3, 3, True)		# repeat horizontally
vs.SetTexMapBool(image, 3, 4, True)		# repeat vertically
vs.SetTexMapInt(image, 3, 1, 0)			# plane projection
vs.SetTexMapReal(image, 3, 1, 0)		# offset x
vs.SetTexMapReal(image, 3, 2, 0)		# offset y
vs.SetTexMapReal(image, 3, 3, 10)		# scale
vs.SetTexMapReal(image, 3, 4, 0)		# rotation

Why this doesn't work?

Any hint will be appreciated.

 

--Carles

Edited by Carles Olle
Link to comment

To assign a texture to the 3D object use the vs.SetTextureRefN(obj, textureRef, texPartID, texLayerID), so your script would look like:

 

vs.BeginPoly3D()
vs.Add3DPt((-5, 0, -5))
vs.Add3DPt(( 5, 0, -5))
vs.Add3DPt(( 5, 0,  5))
vs.Add3DPt((-5, 0,  5))
vs.Add3DPt((-5, 0, -5))
vs.EndPoly3D()
image = vs.LNewObj()

textureRef = vs.Name2Index("Test texture")

texPartID = 0
texLayerID = 0
vs.SetTextureRefN(image, textureRef, texPartID, texLayerID)

 In the above the texPartID is the primary and the texLayerID is about the base object (0) or decals(1 and further).

  • Like 1
Link to comment
  • 5 years later...

Hi 
Searched the Forum but did not found another thread about the textur mapping:

1. I have attach a textur on an extrude. The Extruse can have 4 or more vertex points of a 2D Poly
2. Starting point is alsways the same and also the direction is known.

Main question is, how to prevent texture mapping from aligning to the longes vertex of the face? I use as example this,  to rotate, which works.
Also Flipping is working
 

vs.SetTexMapRealN(rec, 3, 0, 4, angle)


I tried this but this does not change the mapping

vs.SetTexMapBoolN(rec, 3, 0, 7, False)
vs.SetTexMapBoolN(rec, 3, 0, 5, False)


What i do now is, i get the bound in x and y (view on the face) and then rotate by 90 degrees if the y is bigger than y so texture would stay horizontal aligned to the world coordinates , which works for rectangular shapes. I now have to deside if i analyse the shape (if shape is not rectangular) for the longest edge or if there would be an existing function for that.

image.png.45eb3cbe8935605493b3590218660072.png


My testing code looks like this:

                        vs.SetDefaultTexMapN(rec, 3, 0)
                        vs.SetTextureRefN(rec, texRef, 0, 0) # flip horizontal
                        #vs.SetObjectVariableInt(rec, 540,1)
                        vs.SetTexMapBoolN(rec, 3, 0, 7, False)
                        vs.SetTexMapBoolN(rec, 3, 0, 5, False)
                        #texture part, overall is 3. Selector: init:1, flip:2, repH:3, repV:4, long edge:5, worldZ:6, auto align:7
                        
                        angle = 0
                        '''
                        if 'links' in c:
                            if breite < h:
                                angle = 90 * (pi / 180)

                            else:
                                if len(final_contour) < 5: #Rechteck
                                    angle = 180 * (pi / 180)
                                else:
                                    angle = 180 * (pi / 180)
         '''

                        if 'rechts' in c:
                            vs.SetTexMapBoolN(rec, 3, 0, 2, True)#flip vertical axes  
                            '''texture is aligning not to the bounds it aligns to single edges of object'''
                            if breite < h:
                                angle = 90 * (pi / 180)

                            else:
                                
                                if len(final_contour) < 5: #Rechteck
                                    #vs.AlrtDialog(tex_name)
                                    angle = 180 * (pi / 180)

                                else:
                                    #vs.AlrtDialog(tex_name)
                                    angle = 0


                        vs.SetTexMapRealN(rec, 3, 0, 4, angle)
                        #vs.SetTexMapBoolN (rec,3,0,3,True)
                        

      
                        #vs.SetTexMapBoolN(rec, 3, 0, 2, False)#flip vertical axes
                        #vs.SetTexMapBoolN(rec, 3, 0, 2, True)#flip horiz axes
                        res = vs.SetEntityMatrix(rec, (ps[0], ps[1], 0), 90,0,b)
                        vs.SetClass(rec, c)


Short movie which shows, that the texture changes alignment if another edge is getting the longest edge.
 

 

 

 

Link to comment

was not able to wait and solved it with a funktion:

 

def get_longest_direction(final_contour):
	# Define the contour points


	# Calculate the centroid (center) of the contour
	centroid_x = sum(point[0] for point in final_contour) / len(final_contour)
	centroid_y = sum(point[1] for point in final_contour) / len(final_contour)

	# Initialize variables to keep track of the longest edge and its direction
	max_distance = 0
	direction = ''
	longest_edge_points = ()

	# Iterate over the points to calculate x and y distances between consecutive points
	for i in range(len(final_contour)):
		# Get the current point and the next point in the list (wrapping around at the end)
		x1, y1 = final_contour[i]
		x2, y2 = final_contour[(i + 1) % len(final_contour)]

		# Calculate the x and y distances
		x_distance = abs(x2 - x1)
		y_distance = abs(y2 - y1)

		# Check if x distance is the longest so far
		if x_distance > max_distance:
			max_distance = x_distance
			direction = 'x'
			longest_edge_points = ((x1, y1), (x2, y2))

		# Check if y distance is the longest so far
		if y_distance > max_distance:
			max_distance = y_distance
			direction = 'y'
			longest_edge_points = ((x1, y1), (x2, y2))

	# Determine the position of the longest edge relative to the centroid
	(x1, y1), (x2, y2) = longest_edge_points

	# Check the relative position of the longest edge
	if direction == 'x':
		if centroid_y > y1:  # Edge is below the centroid
			position = 'below'
		else:                # Edge is above the centroid
			position = 'above'
	elif direction == 'y':
		if centroid_x > x1:  # Edge is to the left of the centroid
			position = 'left'
		else:                # Edge is to the right of the centroid
			position = 'right'


	return position


longest_direction = get_longest_direction(final_contour)



'''and for the mapping this code'''

vs.SetDefaultTexMapN(rec, 3, 0)
vs.SetTextureRefN(rec, texRef, 0, 0) # flip horizontal
#vs.SetObjectVariableInt(rec, 540,1)
vs.SetTexMapBoolN(rec, 3, 0, 7, False)
vs.SetTexMapBoolN(rec, 3, 0, 5, False)
#texture part, overall is 3. Selector: init:1, flip:2, repH:3, repV:4, long edge:5, worldZ:6, auto align:7

angle = 0

if 'links' in c:
    if longest_direction == 'left':
        angle = -90 * (pi / 180)
        
    if longest_direction == 'right':
        angle = 90 * (pi / 180)
        
    elif longest_direction == 'above':
        angle = 180 * (pi / 180)



if 'rechts' in c:
    vs.SetTexMapBoolN(rec, 3, 0, 2, True) 

    if longest_direction == 'left':
        angle = -90 * (pi / 180)
        
    if longest_direction == 'right':
        angle = 90 * (pi / 180)
        
    elif longest_direction == 'above':
        angle = 180 * (pi / 180)




vs.SetTexMapRealN(rec, 3, 0, 4, angle)

 

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...