【Shader Graph】図形を回転させる

はじめに

前回の記事で作った平面図形を描画するシェーダーに回転を追加する

Shader Graphで図形を回転させる方法

Rotateノードを使ってPolarCoordinatesを回転させる。
Unitで回転量の単位を選べる(Radian or Degrees)。

Rotate ノード | Shader Graph | 10.0.0-preview.27

https://raw.githubusercontent.com/sotanmochi/ProceduralShapesShaderPack/main/Docs/Others/PolarCoordinates_Rotate.gif

シェーダー作成

環境

  • Unity 2020.3.18f1
  • Universal Render Pipeline 10.6.0
  • Shader Graph 10.6.0

Graph

https://raw.githubusercontent.com/sotanmochi/ProceduralShapesShaderPack/main/Docs/Cardioid/Cardioid_Rotate_Graph.png

出力結果

https://raw.githubusercontent.com/sotanmochi/ProceduralShapesShaderPack/main/Docs/Cardioid/Cardioid_Rotate.gif

図形の回転を制御するスクリプトのサンプル

using UnityEngine;
using UnityEngine.UI;

namespace ProceduralShapesShaderPack.Samples
{
    public class ShapeRotation : MonoBehaviour
    {
        [SerializeField] private Renderer _targetShape;
        [SerializeField] private Slider _slider;

        private Material _material;
        private int _rotationPropertyId;

        void Awake()
        {
            _rotationPropertyId = Shader.PropertyToID("Rotation_Degrees");
            _material = _targetShape.material;

            _slider.onValueChanged.AddListener(value => 
            {
                _material.SetFloat(_rotationPropertyId, value);
            });
        }
    }
}

アセット

github.com

github.com