しゅみぷろ

プログラミングとか

Tessellationを用いたなんちゃってスカルプト

はじめに

何かが衝突した影響でモノが変形したり…といった表現は結構巷にあふれていて、その手法も様々です。 一番シンプルに思いつく実装方法といえば、衝突の際に実際にジオメトリを変形させることでしょうか?

今回は実際の頂点データを弄らず、GPUで頂点を変形してレンダリングさせています。なので、ランタイムでの利用を想定しています。

用途としては…アルミ缶とかに何かをぶつけて適当に凹ませたり…?

あんまり用途は思いつかなかったです。

壁みたいな、実際に頂点の密度が細かくないようなモデルでもいい感じに変形出来るのが良いところでしょうか?

実装

実装といっても、特殊なことは何もありません。ただのDisplacementMappingです。 基本的にはInkPainterの、TessellateHeightTransformサンプルを改造していく感じで作りました。

InkPainter/Assets/InkPainter/Sample/SampleScene at master · EsProgram/InkPainter · GitHub

なので処理的には

  • DisplaementMapに対して、凹凸情報を好きなように書き込む
  • この凹凸情報を参照して変形するShaderを書いてモデルの描画に使うようにする

という感じです。

書き込みはInkPainterの機能を使い回しました。

変形処理ですが、引き伸ばされた辺をいい感じに分割してほしかったので頂点シェーダーで変形後の頂点の辺の長さを考慮してTessellationを行っています。

動画を見ると結構目立つのですが、ポリゴンがポリポリしています(シェーディングの関係でポリゴンが目立つ、陰でポリゴンが際立つ感じ…)。 少々コストはかかりますが、DisplacementMapから動的にNormalMapを生成してやって、それを使ってShadingを行うと良くなりそうに思います。

最後に

コード等公開しようと思いましたが、これ以上更新しようと思うほど楽しくなかったので今回はここまででお蔵入りします。

シンプルなメッシュに対して、ざっくりとした変形は実際に頂点を変形させ、ディティールな変形には今回のような手法を用いるような、ハイブリッドな実装ができれば結構面白そうかなと思いました。

Why is it painted in multiple places?

Introduction

esprog.hatenablog.com

This is an answer to questions about InkPainter.

Why is it painted in multiple places?

f:id:es_program:20170421230929g:plain:w600

You may encounter this situation while using InkPainter.

Is this a bug? Please be relieved, it is not a bug but a specification.

Then why is it painted in an unintended place?

Because, InkPainter paints directly on the texture of the model.

This means that it depends on how the texture is pasted on the model.

To paste the texture into the model, set information on which position of the texture each vertex of the model corresponds to.

This information is called UV or texturecoord, and the modeling process of setting UV is called UV mapping.

Therefore, if there is a surface where UV overlaps, it will be painted in several places. In order to solve this, it is necessary to model so that UV does not overlap. For example, the Unity standard Cube object will display the same place on the texture on all faces.

This is because the Unity standard Cube object has such UV.

f:id:es_program:20170421233452p:plain:w200

UV coordinates set for Unity standard Cube


f:id:es_program:20170421235133g:plain:w600

One side displays the whole texture



On the other hand, this problem does not occur with a Cube object created such that the UVs do not overlap as shown in the developed view of the dice.

f:id:es_program:20170421233511p:plain:w200

Cube where UV is not overlapped


f:id:es_program:20170421235247g:plain:w600

The position of the texture displayed by each face is different