質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

1566閲覧

UnityでEffectカメラのRenderingがうまく行かない

motokanou

総合スコア13

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2021/01/25 01:18

編集2021/01/25 13:32

カメラのrenderingで二つのマテリアルのeffectを行いたいのですが、うまくいきません。

何が悪いのかよくわからないので、情報がここはどうなっているのか聞いてもらえれば嬉しいです。

MetaballCameraEffect.cs
cutOutMaterial_1, cutOutMaterial_2はshaderは同じで、異なったmaterialです。

c#

1using System; 2using UnityEngine; 3 4namespace UnityStandardAssets.ImageEffects 5{ 6 [ExecuteInEditMode] 7 public class MetaballCameraEffect : MonoBehaviour 8 { 9 /// Blur iterations - larger number means more blur. 10 public int iterations = 3; 11 12 /// Blur spread for each iteration. Lower values 13 /// give better looking blur, but require more iterations to 14 /// get large blurs. Value is usually between 0.5 and 1.0. 15 public float blurSpread = 0.6f; 16 17 18 // The blur iteration shader. 19 // Basically it just takes 4 texture samples and averages them. 20 // By applying it repeatedly and spreading out sample locations 21 // we get a Gaussian blur approximation. 22 23 public Shader blurShader = null; 24 25 static Material m_Material = null; 26 protected Material material 27 28 { 29 get 30 { 31 if (m_Material == null) 32 { 33 m_Material = new Material(blurShader); 34 m_Material.hideFlags = HideFlags.DontSave; 35 } 36 return m_Material; 37 } 38 } 39 40 public Material cutOutMaterial_1; 41 public Material cutOutMaterial_2; 42 43 public Camera bgCamera; 44 45 46 RenderTexture bgTargetTexture; 47 48 private void OnEnable() 49 { 50 if (Screen.width > 0 && Screen.height > 0) { 51 bgTargetTexture = new RenderTexture (Screen.width, Screen.height, 16); 52 //Debug.Log(Screen.width); 53 //Debug.Log(Screen.height); 54 bgCamera.targetTexture = bgTargetTexture; 55 //Debug.Log(bgCamera.targetTexture.name); 56 } 57 } 58 59 protected void OnDisable() 60 { 61 if (m_Material) 62 { 63 DestroyImmediate(m_Material); 64 } 65 } 66 protected void Start() 67 { 68 // Disable if we don't support image effects 69 if (!SystemInfo.supportsImageEffects) 70 { 71 enabled = false; 72 return; 73 } 74 // Disable if the shader can't run on the users graphics card 75 if (!blurShader || !material.shader.isSupported) 76 { 77 enabled = false; 78 return; 79 } 80 81 // Disable if the shader can't run on the users graphics card 82 if (!cutOutMaterial_1.shader.isSupported) 83 { 84 enabled = false; 85 return; 86 } 87 88 if (!cutOutMaterial_2.shader.isSupported) 89 { 90 enabled = false; 91 return; 92 } 93 94 } 95 96 // Performs one blur iteration. 97 public void FourTapCone(RenderTexture source, RenderTexture dest, int iteration) 98 { 99 float off = 0.5f + iteration * blurSpread; 100 Graphics.BlitMultiTap(source, dest, material, 101 new Vector2(-off, -off), 102 new Vector2(-off, off), 103 new Vector2(off, off), 104 new Vector2(off, -off) 105 ); 106 } 107 108 // Downsamples the texture to a quarter resolution. 109 private void DownSample4x(RenderTexture source, RenderTexture dest) 110 { 111 float off = 1.0f; 112 Graphics.BlitMultiTap(source, dest, material, 113 new Vector2(-off, -off), 114 new Vector2(-off, off), 115 new Vector2(off, off), 116 new Vector2(off, -off) 117 ); 118 } 119 120 121 // Called by the camera to apply the image effect 122 RenderTexture buffer; 123 //RenderTexture buffer3; 124 RenderTexture buffer2; 125 void OnRenderImage(RenderTexture source, RenderTexture destination) 126 { 127 //if(!Application.isPlaying) 128 // return; 129 130 int rtW = source.width / 4; 131 int rtH = source.height / 4; 132 buffer = RenderTexture.GetTemporary(rtW, rtH, 0); 133 // Copy source to the 4x4 smaller texture. 134 DownSample4x(source, buffer); 135 136 137 // Blur the small texture 138 for (int i = 0; i < iterations; i++) 139 { 140 buffer2 = RenderTexture.GetTemporary (rtW, rtH, 0); 141 FourTapCone (buffer, buffer2, i); 142 RenderTexture.ReleaseTemporary (buffer); 143 buffer = buffer2; 144 } 145 146 147 //Debug.Log(material.name); 148 Graphics.Blit(bgTargetTexture, destination); // background 149 Graphics.Blit(buffer, destination, cutOutMaterial_2); // water 150 //RenderTexture.ReleaseTemporary(buffer); 151 Graphics.Blit(buffer, destination, cutOutMaterial_1); // water 152 RenderTexture.ReleaseTemporary(buffer); 153 154 155 } 156 } 157}

実行すると、左の土管からですGameobjectにcutOutMaterial_1のeffectだけがかかって、右からのGameobhectにかけたいcutOutMaterial_2のeffectはかかってない感じです。

イメージ説明

### 解決済み
回答どうりにやればできました。
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

2DMetaballsのコードを基になさっていますでしょうかね?
あのコードを見てみた感じですと、おそらくMetaballCameraEffectを改造して複数のマテリアルに対応できるようにするよりも、マテリアル毎にメタボールのシステムをセットアップした方が手軽なような気がしました。

  • Custom/Transparent Cutoutシェーダーを使ったマテリアルを2つ作る(仮にメタボールマテリアルA、メタボールマテリアルBとする)。
  • レイヤーを2つ作る(仮にメタボールレイヤーA、メタボールレイヤーBとする)。それぞれのマテリアルと対応するメタボール構成要素オブジェクト(ご質問者さんの図でいうところの土管から滴る粒子)には、これらレイヤーを割り当てる。
  • カメラを3つ用意する(仮にバックグラウンドカメラ、メタボールカメラA、メタボールカメラBとする)。
  • バックグラウンドカメラのCulling MaskからはメタボールレイヤーA、メタボールレイヤーBを除外する。
  • メタボールカメラAのCulling MaskはメタボールレイヤーAのみとする。Depthはバックグラウンドカメラより大きくし、バックグラウンドカメラの後にレンダリングを行わせる。MetaballCameraEffectをアタッチし、Cut Out MaterialにはメタボールマテリアルAを、Bg Cameraにはバックグラウンドカメラをセットする。
  • メタボールカメラBのCulling MaskはメタボールレイヤーBのみとする。DepthはメタボールカメラAより大きくし、メタボールカメラAの後にレンダリングを行わせる。MetaballCameraEffectをアタッチし、Cut Out MaterialにはメタボールマテリアルBを、Bg CameraにはメタボールカメラAをセットする。

という構成はいかがでしょうか。バックグラウンドカメラの映像を下地にメタボールAを合成し、それを下地にさらにメタボールBを合成してやろうという思惑です。

図

投稿2021/01/25 11:44

Bongo

総合スコア10807

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

motokanou

2021/01/25 13:29

ありがとうございます!できました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問