Exemple #1
0
    void UpdateVideoRenderer()
    {
        if (!useDedicatedServer || !Network.isServer)
        {
            VideoFrame videoFrame = new VideoFrame();
            //Initialize Videoplane, BackgroundCamera etc.
            if (!videoInitialized)
            {
                if (videoInitialized = videoSubscriber.getPixels(videoFrame))
                {
                    width = videoFrame.getWidth();
                    height = videoFrame.getHeight();

                    videoObject = new GameObject("VideoObject");
                    videoMesh = videoObject.AddComponent<MeshFilter>();
                    float horScale = 1.0f;// width;
                    float verScale = 1.0f;//height;
                    Vector3[] newVertices = { new Vector3(0, 0, 0), new Vector3(0, verScale, 0), new Vector3(horScale, verScale, 0), new Vector3(horScale, 0, 0) };
                    Vector2[] newUV = { new Vector2(1, 1), new Vector2(1, 0), new Vector2(0, 0), new Vector2(0, 1) };
                    int[] newTriangles = { 0, 1, 2, 2, 3, 0 };

                    videoMesh.mesh.vertices = newVertices;
                    videoMesh.mesh.uv = newUV;
                    videoMesh.mesh.triangles = newTriangles;
                    videoMesh.mesh.RecalculateNormals();

                    videoMeshRenderer = videoObject.AddComponent<MeshRenderer>();
                    videoObject.layer = 10;
                    videoMeshRenderer.enabled = true;
                    imagePlaneMaterial = new Material(Shader.Find("Diffuse"));
                    videoMeshRenderer.material = imagePlaneMaterial;

                    tex = new Texture2D(width/imageScaleFactor, height/imageScaleFactor, TextureFormat.RGB24, false);//new Texture2D(width, height);
                    this.imagePlaneMaterial.SetTexture("_MainTex", tex);

                    //flip y-Axis
                    Vector3 _positionCamera=positionCamera;
                    _positionCamera.y = -_positionCamera.y;

                    createForeGroundCamera(_positionCamera, size);
                    createBackGroundCamera(_positionCamera, size);
                    createVideoPlane(_positionCamera, size);
                }
            }
            //Update the texture every time there is a new video-frame
            if (videoInitialized)
            {
                uint currentCounter = videoSubscriber.getUpdateCounter();
                if (m_updateCounter != currentCounter)
                {
                    m_updateCounter = currentCounter;
                    videoSubscriber.getPixels(videoFrame);
                    int texId=tex.GetNativeTextureID();
                    if (! videoFrame.set(texId))//Works only in OpenGL
                    {
                        Color[] val = new Color[width/imageScaleFactor * height/imageScaleFactor];

                   		float[] color = new float[3];
                        for (int y = 0; y < height; y++)
                        {
                            for (int x = 0; x < width; x++)
                            {
                                if ((y < height) && (x < width))
                                {
                                    videoFrame.getPixel(x, y, out color[0], out color[1], out color[2]);
                                }
                                else
                                {
                                    color[0] = 0.0f;
                                    color[1] = 0.0f;
                                    color[2] = 0.0f;
                                }
                                val[(y/imageScaleFactor * width/imageScaleFactor) + x/imageScaleFactor] = new Color(color[0], color[1], color[2]);
                                //val[y * width + x] = new Color(color[0], color[1], color[2]);
                            }
                        }

                        tex.SetPixels(val);
                        tex.Apply();
                    }
                }
            }
        }
    }