简体   繁体   English

如何在Unity5中渲染高度图?

[英]How to render evolving heightmap in Unity5?

I generate a grayscale heightmap in Unity, and I would like to render it. 我在Unity中生成一个灰度高度图,我想渲染它。 Similar to a terrain in Unity, but not using the terrain component and with the ability to change very often without too much performance loss. 与Unity中的地形类似,但不使用地形组件,并且具有经常更改的能力而不会造成太多性能损失。

Currently I generate a grid mesh (in c#) and then alter the vertexes each frame in a script. 目前,我生成了一个网格网格(在C#中),然后在脚本中每帧更改顶点。 This, however, is slow, and limits the resolution of my heightmap (Unity only allows meshes up to 65000 vertices). 但是,这很慢,并且限制了我的高度图的分辨率(Unity仅允许网格化多达65000个顶点)。

So I was wondering, is there a better way to render this heightmap? 所以我想知道,是否有更好的方法来渲染此高度图?

Notes: 笔记:

  • The heightmap is changed almost every frame. 几乎每帧都更改高度图。
  • I use one channel of my texture (currently the red one) to store the height. 我使用纹理的一个通道(当前为红色通道)存储高度。
  • The resolution is a main problem, it should allow 512, 1024 and up. 分辨率是一个主要问题,应允许512、1024或更高。
  • I'm working in Unity5 on mac. 我正在Mac上的Unity5中工作。

Thanks a lot! 非常感谢!

I found how to do this a while ago, but forgot to post my solution. 我前一阵子发现了如何做,但是忘了发布我的解决方案。 So here it is over a year later, maybe it'll help someone else: 因此,在一年后的今天,也许会对其他人有所帮助:

You can simply add a vertex function into any shader: 您可以简单地将顶点函数添加到任何着色器中:

First add something like: #pragma surface surf Standard fullforwardshadows vertex:vert (the vertex:vert part is important, a similar line will probably already be in the shader, so just add that to that line). 首先添加以下内容: #pragma surface surf Standard fullforwardshadows vertex:vertvertex:vert部分很重要,着色器中可能已经有一条类似的线,因此只需将其添加到该线中即可)。

Then create the vertex function with the name you have it ("vert" in my case): 然后使用您拥有的名称创建顶点函数(在我的情况下为“ vert”):

void vert(inout appdata_full v) {
    float4 wsr = tex2Dlod(_MyTexture, v.texcoord);
    v.vertex.y = wsr.r + wsr.g + wsr.b;
}

You'll also need a sampler in there somewhere (I just put it above the vert function) 您还需要在某处放置一个采样器(我只是将其放在vert函数上方)

sampler2D _MyTexture;

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

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