source: trunk/Utilities/DirectShowReferenceSource/Sample/Unity01/UnityProject/Assets/UseRenderingPlugin.cs @ 937

Last change on this file since 937 was 638, checked in by roman, 7 years ago

Media source texture reader and Unity 3D sample project

File size: 3.0 KB
Line 
1// on OpenGL ES there is no way to query texture extents from native texture id
2#if (UNITY_IPHONE || UNITY_ANDROID) && !UNITY_EDITOR
3        #define UNITY_GLES_RENDERER
4#endif
5
6
7using UnityEngine;
8using System;
9using System.Collections;
10using System.Runtime.InteropServices;
11
12
13public class UseRenderingPlugin : MonoBehaviour
14{
15        // Native plugin rendering events are only called if a plugin is used
16        // by some script. This means we have to DllImport at least
17        // one function in some active script.
18        // For this example, we'll call into plugin's SetTimeFromUnity
19        // function and pass the current time so the plugin can animate.
20
21#if UNITY_IPHONE && !UNITY_EDITOR
22        [DllImport ("__Internal")]
23#else
24        [DllImport ("RenderingPlugin")]
25#endif
26        private static extern void SetTimeFromUnity(float t);
27
28
29        // We'll also pass native pointer to a texture in Unity.
30        // The plugin will fill texture data from native code.
31#if UNITY_IPHONE && !UNITY_EDITOR
32        [DllImport ("__Internal")]
33#else
34        [DllImport ("RenderingPlugin")]
35#endif
36#if UNITY_GLES_RENDERER
37        private static extern void SetTextureFromUnity(System.IntPtr texture, int w, int h);
38#else
39        private static extern void SetTextureFromUnity(System.IntPtr texture);
40#endif
41
42
43#if UNITY_IPHONE && !UNITY_EDITOR
44        [DllImport ("__Internal")]
45#else
46        [DllImport("RenderingPlugin")]
47#endif
48        private static extern void SetUnityStreamingAssetsPath([MarshalAs(UnmanagedType.LPStr)] string path);
49
50
51#if UNITY_IPHONE && !UNITY_EDITOR
52        [DllImport ("__Internal")]
53#else
54        [DllImport("RenderingPlugin")]
55#endif
56        private static extern IntPtr GetRenderEventFunc();
57
58
59        IEnumerator Start()
60        {
61                SetUnityStreamingAssetsPath(Application.streamingAssetsPath);
62
63                CreateTextureAndPassToPlugin();
64                yield return StartCoroutine("CallPluginAtEndOfFrames");
65        }
66
67        private void CreateTextureAndPassToPlugin()
68        {
69        // NOTE: https://en.wikipedia.org/wiki/Display_resolution
70
71        // Create a texture
72        Texture2D tex = new Texture2D(2048, 1536, TextureFormat.BGRA32, false);
73                // Set point filtering just so we can see the pixels clearly
74                tex.filterMode = FilterMode.Point;
75                // Call Apply() so it's actually uploaded to the GPU
76                tex.Apply();
77
78                // Set texture onto our matrial
79                GetComponent<Renderer>().material.mainTexture = tex;
80
81                // Pass texture pointer to the plugin
82        #if UNITY_GLES_RENDERER
83                SetTextureFromUnity (tex.GetNativeTexturePtr(), tex.width, tex.height);
84        #else
85                SetTextureFromUnity (tex.GetNativeTexturePtr());
86        #endif
87        }
88
89        private IEnumerator CallPluginAtEndOfFrames()
90        {
91                while (true) {
92                        // Wait until all frame rendering is done
93                        yield return new WaitForEndOfFrame();
94
95                        // Set time for the plugin
96                        SetTimeFromUnity (Time.timeSinceLevelLoad);
97
98                        // Issue a plugin event with arbitrary integer identifier.
99                        // The plugin can distinguish between different
100                        // things it needs to do based on this ID.
101                        // For our simple plugin, it does not matter which ID we pass here.
102                        GL.IssuePluginEvent(GetRenderEventFunc(), 1);
103                }
104        }
105}
Note: See TracBrowser for help on using the repository browser.