Unityで2つのテクスチャを合成(度合い設定可)するシェーダーを書いてみる
Unityシェーダ入門】テクスチャをブレンドして自然な地形を表示する - おもちゃラボ を参照して2つのテクスチャーを混ぜて表示するシェーダーを書いてみたのでメモしておく。
環境
- Unity 5.6.3p1
作ったシェーダーについて
シェーダーを適用したマテリアルで2つのテクスチャとブレンドの度合いを設定できる。
Blendが0.5の場合の表示。
0でAを表示、1.0でBを表示する。
シェーダーの作り方
[Create] => [Shader] => [Standard Surface Shader]でBlendという名前のシェーダーを作成しコードを以下のように修正する。
Shader "Custom/Blend" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Main Texture", 2D) = "white" {}
_SubTex("Sub Texture", 2D) = "white" {} // 追加
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
_Blend("Blend", Range(0,1)) = 0.0 // 追加
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
sampler2D _SubTex; // 追加
struct Input {
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
half _Blend; // 追加
fixed4 _Color;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_CBUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_CBUFFER_END
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
fixed4 c2 = tex2D(_SubTex, IN.uv_MainTex) * _Color; // 追加
o.Albedo = lerp(c.rgb, c2.rgb, _Blend); // 修正
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
スクリプトからの設定
以下のような感じでシェーダーの変数を設定できる。
// 変数の設定
GetComponent<Renderer>().material.SetTexutre("_MainTex", texture);
GetComponent<Renderer>().material.SetTexutre("_SubTex", texture);
GetComponent<Renderer>().material.SetFloat("_Blend", 0.5f);
// 変数の取得
GetComponent<Renderer>().material.GetFloat("_Blend");
Author And Source
この問題について(Unityで2つのテクスチャを合成(度合い設定可)するシェーダーを書いてみる), 我々は、より多くの情報をここで見つけました https://qiita.com/mechamogera/items/9341d935377cf3d3255f著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .