主要内容

sample

R2026b

Sample texture at UV coordinates

Since R2026b

Description

Add-On Required: This feature requires the 3D Asset Processing Library for MATLAB add-on.

colors = sample(texObj,uv) samples the texture object texObj at the specified UV coordinates uv and returns the interpolated RGBA color values colors. The object function applies the offset, scale, and rotation transform, wraps coordinates according to the Wrap mode, then samples using nearest-neighbor or bilinear interpolation based on the Interpolation property, as specified in the input texture object texObj.

example

Examples

collapse all

Read a texture image and display.

img = imread("brick.png");
imshow(img)

Create a texture object with repeat wrap mode, then sample it.

tex = asset3d.Texture(Image=img,Wrap="repeat",Scale=[2 2],Offset=[0.1 0.1]);
colors = sample(tex,[0.5 0.5; 1.5 1.5])
colors = 2×4 uint8 matrix

    162    67    52    255
    162    67    52    255

Create a textured material object.

texturedMat = asset3d.Material(Name="textured",BaseColorTexture=tex);

Create a pyramid mesh with textured material on it. Duplicate vertices so that each face gets independent UV coordinates.

faceIdx = [1 2 5; 2 3 5; 3 4 5; 4 1 5; 1 3 2; 1 4 3];
p = [0 0 0; 1 0 0; 1 1 0; 0 1 0; 0.5 0.5 1];
pyramidVerts = p(faceIdx',:);
pyramidFaces = reshape(1:18,3,[])';
uv = repmat([0 0; 1 0; 0.5 1],6,1);
texturedPyramid = asset3d.Mesh(pyramidVerts,pyramidFaces,UV=uv,Material=texturedMat);

Display the textured pyramid mesh.

show(texturedPyramid,Grid=true)

Displays a pyramid mesh with textured material

Input Arguments

collapse all

Source texture, specified as a Texture object.

UV coordinates at which to sample the texture, specified as an N-by-2 matrix. Each row is a [u v] coordinate pair, where N is the number of sample points.

Data Types: double

Output Arguments

collapse all

RGBA color values at each UV coordinate, returned as an N-by-4 matrix. Each row contains [R G B A] values in the range [0, 255].

The size of colors is N-by-4 regardless of the number of channels in the source image. Grayscale images are expanded to RGBA by replicating the single channel, and RGB images get alpha set to 255. When the Image property is empty, the method returns [255 255 255 255] for all coordinates.

Data Types: uint8

Version History

Introduced in R2026b

See Also

|