简体   繁体   English

在Unity中平滑扩展纹理游戏对象

[英]Extending textured gameObject smoothly in Unity

I'm creating simple game and I need to create extending cylinder. 我正在创建简单的游戏,我需要创建扩展圆柱体。 I know how to normally do it - by changing objects pivot point and scaling it up, but now I have texture on it and I don't want it to stretch. 我知道通常如何做-通过更改对象的轴心点并按比例放大它,但是现在我有纹理了,我不希望它拉伸。

I fought about adding small segments to the end of the cylinder when it has to grow, but this wont work smooth and might affect performance. 我曾努力在圆柱体的末端增加一些小段,但这种方法无法顺利进行并可能影响性能。 Do you know any solution to this? 您知道对此有什么解决方案吗?

This the rope texture that I'm using right now(but it might change in the future): 我现在正在使用的绳子纹理(但是将来可能会改变):

Before scaling 缩放之前

在此处输入图片说明

After scaling 缩放后

在此处输入图片说明

I don't want it to stretch 我不要它伸展

This is not easy to accomplish but it is possible. 这并非易事,但有可能。

You have two ways to do this. 您有两种方法可以做到这一点。

1 .One is to procedurally generate the rope mesh and assign material real-time. 1。一种方法是程序性地生成绳网并实时分配材料。 This is complicated for beginners. 对于初学者来说这很复杂。 Can't help on this one. 对此无能为力。

2 .Another solution that doesn't require mesh procedurally mesh generation. 2,另一种不需要网格程序的解决方案。 Change the tiles while the Object is changing size. 在对象更改大小时更改图块。

For this to work, your texture must be tileable. 为此,您的纹理必须是可平铺的。 You can't just use any random texture online. 您不能只在线使用任何随机纹理。 Also, you need a normal map to actually make it look like a rope. 另外,您需要一个法线贴图才能使其看起来像绳子。 Here is a tutorial for making a rope texture with normal map in Maya. 这是在Maya中使用法线贴图制作绳索纹理的教程 There are other parts of the video you have to watch too. 您还必须观看视频的其他部分。

Select the texture, change Texture Type to Texture , change Wrap Mode to Repeat then click Apply . 选择纹理,将Texture Type更改为Texture ,将Wrap Mode更改为Repeat然后单击应用

Get the MeshRenderer of the Mesh then get Material of the 3D Object from the MeshRenderer .Use ropeMat.SetTextureScale to change the the tile of the texture. 获取MeshMeshRenderer ,然后从MeshRenderer获取3D对象的Material 。使用ropeMat.SetTextureScale更改纹理的平铺。 For example, when changing the xTile and yTile value of the code below, the texture of the Mesh will be tiled. 例如,当更改下面代码的xTileyTile值时,将对Mesh的纹理进行平铺。

public float xTile, yTile;
public GameObject rope;
Material ropeMat;

void Start()
{
    ropeMat = rope.GetComponent<MeshRenderer>().material;
}

void Update()
{
    ropeMat.SetTextureScale("_MainTex", new Vector2(xTile, yTile));
}

Now, you have to find a way to map the xTile and yTile values to the size of the Mesh . 现在,您必须找到一种将xTileyTile值映射到Mesh大小的方法。 It's not simple. 这并不简单。 Here is complete method to calculate what value xTile and yTile should be when re-sizing the mesh/rope. 是一种完整的方法,可用于在调整网格/绳索大小时计算xTileyTile值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM