// Use this for initialization void Start() { //we need a dummy RenderTexture for camera to let it do off-screen rendering. RenderTexture dummyRT = new RenderTexture(1024, 1024, 24, RenderTextureFormat.ARGB32); dummyRT.antiAliasing = 1; dummyRT.useMipMap = false; dummyRT.autoGenerateMips = false; //Even though we could set the dimension here, it will not work properly anyway. dummyRT.dimension = TextureDimension.Tex2DArray; dummyRT.volumeDepth = 2; dummyRT.Create(); mainCam.targetTexture = dummyRT; mainCam.forceIntoRenderTexture = true; //Very few documentations about this variable, not sure if I set this right. Vector4[] unity_StereoScaleOffset = new Vector4[2]; unity_StereoScaleOffset[0] = new Vector4(1.0f, 1.0f, 0f, 0f); unity_StereoScaleOffset[1] = new Vector4(1.0f, 1.0f, 0.5f, 0f); Shader.SetGlobalVectorArray("unity_StereoScaleOffset", unity_StereoScaleOffset); //Add callback to hack frambuffer //In unity forward rendering path, the BeforeForwardOpaque event will occur first //so, here is the point we change the framebuffer's binding in native plugin. NativePlugin.AddCameraCallbacks(mainCam, CameraEvent.BeforeForwardOpaque); //Clear framebuffer. //In many cases Unity will always clear it for us, this is like a double-check CommandBuffer cb = new CommandBuffer(); cb.ClearRenderTarget(true, true, mainCam.backgroundColor); mainCam.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, cb); float eyeOffset = mainCam.stereoSeparation / 2.0f; eyeOffsetVector = new Vector3(eyeOffset, 0, 0); }
void Awake() { mainCam = GetComponent <Camera>(); if (mainCam == null) { Debug.LogError("Can't find Camera!"); DestroyImmediate(this); return; } //Enable these keywords to let the unity shaders works for single pass stereo rendering Shader.EnableKeyword("STEREO_MULTIVIEW_ON"); Shader.EnableKeyword("UNITY_SINGLE_PASS_STEREO"); //Texture2DArray could only be generated in runtime renderTexture = new Texture2DArray(1024, 1024, 2, TextureFormat.ARGB32, false); System.IntPtr tid = renderTexture.GetNativeTexturePtr(); Debug.Log("Texture ID " + (int)tid); //Tell native plugin the texture id NativePlugin.SetTextureID((int)tid); NativePlugin.SetAntiAliasing(QualitySettings.antiAliasing); }