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

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

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

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

Q&A

解決済

1回答

4738閲覧

[Unity] 2つの動画を同期して再生する方法

shimazu

総合スコア38

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

0グッド

1クリップ

投稿2018/07/31 14:35

編集2018/08/01 05:53

Unityで視差動画を作成しています。
知りたいのは、種類の違う2つの動画を同期して再生する方法です。

現在、右目用の動画と左目用の動画2つを用意し重ねて再生しています。
その際、2つの動画がずれることがあります。
コードは以下のようになっています。

csharp

1 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class Movie_Script : MonoBehaviour { 7 public MovieTexture movie_l; 8 public MovieTexture movie_r; 9 public Renderer renderer; 10 void Start () { 11 12 renderer = GetComponent<Renderer>(); 13 movie_l = (MovieTexture)renderer.material.GetTexture("_Movie_L"); 14 movie_r = (MovieTexture)renderer.material.GetTexture("_Movie_R"); 15 16 movie_l.Play(); 17 movie_r.Play(); 18 } 19}

shader

1 2Shader "Unlit/Movie_sample" 3{ 4 Properties 5 { 6 _MainTex ("Texture", 2D) = "white" {} 7 _Movie_L("Movie_Left", 2D) = "white"{} 8 _Movie_R("Movie_Right", 2D) = "black"{} 9 } 10... 11}

行ったこと。
2つの動画を横に並べて1つの動画とし、再生する際には左右で切り抜いて2枚のtextureを取得してから設定する。
(上と下で方人が違います。こっちはVideoPlayerを使ってます)
ただ、この場合、TextureをTexture2Dへ変換するのに時間がかかるようで、fps=10程度まで落ちる(上は60程度)。

sharp

1 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.Video; 6using UnityEngine.UI; 7 8public class Generate_3D_Movie : MonoBehaviour { 9 [SerializeField] 10 VideoPlayer mPlayerRight; 11 private Renderer render; 12 void Start () { 13 render = GetComponent<Renderer>(); 14 render.material.SetTexture("_MainTex", null); 15 } 16 17 void Update () { 18 if(mPlayerRight.texture == null){ 19 return; 20 } 21 22 Texture2D change_texture_l = getCenterClippedTexture(true, this.ToTexture2D(mPlayerRight.texture)); 23 Texture2D change_texture_r = getCenterClippedTexture(false, this.ToTexture2D(mPlayerRight.texture)); 24 render.material.SetTexture("_LeftTex", change_texture_l); 25 render.material.SetTexture("_RightTex", change_texture_r); 26 } 27 28 Texture2D getCenterClippedTexture(bool _left , Texture2D texture){ 29 if(texture == null){ 30 return null; 31 } 32 Color[] pixel; 33 Texture2D clipTex; 34 int tw = texture.width; 35 int th = texture.height; 36 tw = tw/2; 37 if(_left){ 38 pixel = texture.GetPixels(0, 0, tw, th); 39 } 40 else{ 41 pixel = texture.GetPixels(tw, 0, tw, th); 42 } 43 44 // 横幅,縦幅を指定してTexture2Dを生成 45 clipTex = new Texture2D(tw, th); 46 clipTex.SetPixels(pixel); 47 clipTex.Apply(); 48 return clipTex; 49 } 50 51 Texture2D ToTexture2D(Texture _texture){ 52 if(_texture == null){ 53 Debug.Log(null); 54 return null; 55 } 56 var sw = _texture.width; 57 var sh = _texture.height; 58 var format = TextureFormat.RGBA32; 59 var result = new Texture2D( sw, sh, format, false ); 60 var currentRT = RenderTexture.active; 61 var rt = new RenderTexture( sw, sh, 32 ); 62 Graphics.Blit( _texture, rt ); 63 RenderTexture.active = rt; 64 var source = new Rect( 0, 0, rt.width, rt.height ); 65 result.ReadPixels( source, 0, 0 ); 66 result.Apply(); 67 RenderTexture.active = currentRT; 68 return result; 69 } 70} 71

shader

1Shader "Unlit/Movie_sample" 2{ 3 Properties 4 { 5 //Inspectorからテクスチャを設定 6 _MainTex ("Texture", 2D) = "white" {} 7 _LeftTex ("Left_Texture", 2D) = "black"{} 8 _RightTex ("Right_Texture", 2D) = "gray"{} 9 } 10... 11 float4 frag (v2f i) : SV_Target 12 { 13 float4 col; 14 Pix_no pix_no; 15 float4 RGBA; 16 int index = 0; 17 18 19#if 1 //描画OFF(FPSテスト) 20 21 i.uv.y = 1.0f - i.uv.y; 22 //i.uv.y = 1.0f - i.uv.y; 23 24 pix_no = pixel_no(i.uv); 25 26 if(_Alignment_flg){ 27 RGBA = Alignment_Generate(pix_no, i.uv); 28 } 29 else{ 30 if(_Tracking_OnOff_flg){ 31 index = _Index; 32 } 33 else if(_Continuous_Change_flg) 34 { 35 index = _Continuous_Index; 36 } 37 38 if(index == 0){ 39 RGBA = Pattern0_Generate(pix_no, i.uv); 40 } 41 else if(index == 1){ 42 RGBA = Pattern1_Generate(pix_no, i.uv); 43 } 44... 45 46 } 47 48 //Pixel設定 49 col = RGBA; 50 51 // apply fog 52 //UNITY_APPLY_FOG(i.fogCoord, col); 53#endif 54 55 return col; 56 } 57 58 ENDCG 59 } 60 } 61} 62 63}

調べていく中で関係しそうな資料

https://qiita.com/KentaKato/items/b3d16de8877c688acbd1

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

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

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

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

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

guest

回答1

0

ベストアンサー

Shaderが省略されてるので、修正コードがわかりませんが
Shader修正して左右の2つのTextureからでなく、横に並べた画像の1つのTextureに変更し、Shaderのfragのところにuvをuを0.5倍にして、おそらく画面座標からLeftTexかRightTexを判定しているところがあると思いますので、そこをuに+0.5を足すか足さないかに変更し、1つのTextureから取得するようにすればできると思います。

追記
省略されてる部分がわかりません。どちらが左か右かわかりませんが、Pattern0_Generateで使ってる方をTexture残して、使わなくなったGenerate関数を削除

shader

1 if(_Alignment_flg){ 2 RGBA = Pattern0_Generate(pix_no, i.uv); 3 } 4 else{ 5 if(_Tracking_OnOff_flg){ 6 index = _Index; 7 } 8 else if(_Continuous_Change_flg) 9 { 10 index = _Continuous_Index; 11 } 12 13 if(index == 0){ 14 i.uv.x *=0.5; 15 RGBA = Pattern0_Generate(pix_no, i.uv); 16 } 17 else if(index == 1){ 18 i.uv.x *=0.5; 19 i.uv.x +=0.5; 20 RGBA = Pattern0_Generate(pix_no, i.uv); 21 }

投稿2018/08/01 03:29

編集2018/08/01 09:12
tmp

総合スコア277

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

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

shimazu

2018/08/01 05:56 編集

コメントありがとうございます。 Shaderの方で1枚から2枚を生成できるのですね! Shaderのfragのコードを追加しました。 全てではなくて構いませんので、該当する場所のShaderのコードを教えていただけますか? (おそらくこのような感じというので構いません)
shimazu

2018/08/01 11:26 編集

追記ありがとうござます。手元で試行錯誤してみます。 shaderのファイルです。2つありますが、使うのはどちらか一方です。 Movie_RefactShader_3rdが、2つの動画をinspectorから設定して合成しているものです。 もう1つが、2つの画像を設定しているものです(こちらは毎フレームスクリプトで動画からTextureを取得し、そのTextureを設定するのに使いました)。 やりたいのは1つの動画から、その左右の動画を重ねた1つの動画にすることです。 https://drive.google.com/open?id=1mMxu14YCYffZ-T6YlyM4P86YLtpPo_hu
shimazu

2018/08/01 11:43 編集

(Shaderについては今回初めてコードを触るのですが)Pattern0_Generateの中で左右を判定しているように見えます。 このように設定すれば左右が1つになりました。やり方としてあっていますか? //パターン0 float4 Pattern0_Generate(Pix_no pix_no, float2 pos) { float R, G, B; if(pix_no.R <= 7) { //左から pos.x *= 0.5; R = tex2D(_Movie_L, pos).r; } else { //右から pos.x *= 0.5; pos.x += 0.5; R = tex2D(_Movie_R, pos).r; }
tmp

2018/08/03 09:06

_Movie_Lに、左右の映像があるTextureがセットされると想定すると 右から取り出すときも_Movie_Lからになると思います。 また、左右を判定するpix_noも変更が必要だと思いますが、 左右の合成方法を理解できてないのと、Subpixel番号を理解できてないので、わからないですが、pix_noの計算の float x_size = _LeftTex_TexelSize.z; を半分にする必要があるかもしれません。 当初、画面の1ラインごとに左右を切り替えるのかと思ってたのですが、全然ちがいましたね。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問