Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Jun 16, 2016 at 08:46 PM by krasi_intugame for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by krasi_intugame · Mar 03, 2016 at 01:01 PM · 5.3

Get the output of the main camera and pass it to a native plugin

Edit: The issue is now resolved. There were a few DirectX errors and mismatches like the texture format that you get from Unity and so fort. Just had to enable the debug flag of the DX device and see what's really going on.

I am trying to copy (in a efficient manner, not leaving the GPU, etc.) the output of the main camera to a native plugin I am working on. I'll have to do some shenanigans in there, but for now I'm trying to open a window and render the same picture in the plugin. The issue I'm having is I get only some gray-ish rectangle, and not the actual camera output. Here's my approach:

In Unity I created a camera that is child to the main camera. I added a script to that camera which makes it render to a texture and passes the texture to my native plugin (based on THE native plugin example):

 IEnumerator Start()
     {
         myCamera = gameObject.GetComponent<Camera>();
         if (myCamera != null && myCamera.pixelHeight > 0 && myCamera.pixelWidth > 0)
         {
             CreateTextureAndPassToPlugin();
             yield return StartCoroutine("CallPluginAtEndOfFrames");
         }
     }
 
 private void CreateTextureAndPassToPlugin()
     {
         var myRT = new RenderTexture(myCamera.pixelWidth, myCamera.pixelHeight, 24, RenderTextureFormat.ARGB32);
 
         myRT.Create();
 
         myCamera.targetTexture = myRT;
         InitNativePlugin(myRT.GetNativeTexturePtr());
     }
 
 private IEnumerator CallPluginAtEndOfFrames()
     {
         while (true)
         {
             yield return new WaitForEndOfFrame();
             
             GL.IssuePluginEvent(GetRenderEventFunc(), 1);
         }
     }

Then, in my native plugin I get the Unity device and context and map the content of the texture I get from Unity to be able to render it on my window:

 IUnityGraphicsD3D11* d3d11 = s_UnityInterfaces->Get<IUnityGraphicsD3D11>();
 ID3D11Device* unityDevice = d3d11->GetDevice();
 
 D3D11_TEXTURE2D_DESC uDesc;
 unityTex->GetDesc(&uDesc);
 
 ComPtr<ID3D11DeviceContext> unityContext;
 unityDevice->GetImmediateContext(&unityContext);
 
 D3D11_TEXTURE2D_DESC copyTexDesc;
 
 copyTexDesc.Width = uDesc.Width;
 copyTexDesc.Height = uDesc.Height;
 copyTexDesc.MipLevels = 1;
 copyTexDesc.ArraySize = 1;
 copyTexDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
 copyTexDesc.SampleDesc.Count = 1;
 copyTexDesc.SampleDesc.Quality = 0;
 
 copyTexDesc.Usage = D3D11_USAGE_STAGING;
 copyTexDesc.BindFlags = 0;
 copyTexDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
 copyTexDesc.MiscFlags = 0;
 
 ComPtr<ID3D11Texture2D> txResult;
 
 hr = unityDevice->CreateTexture2D(&copyTexDesc, nullptr, &txResult);
 
 D3D11_BOX box;
 box.left = 0;
 box.right = uDesc.Width;
 box.top = 0;
 box.bottom = uDesc.Height;
 box.front = 0;
 box.back = 1;
 
 unityContext->CopySubresourceRegion(txResult, 0, 0, 0, 0, unityTex, 0, &box);
 
 D3D11_MAPPED_SUBRESOURCE unityMapped;
 
 hr = unityContext->Map(txResult, 0, D3D11_MAP_READ, 0, &unityMapped);

For some reason I end up getting only gray from the pixels in unityMapped.pData (see below).alt text Do you think my approach is correct? Any ideas? Thanks!

output.png (3.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by krasi_intugame · Jun 16, 2016 at 08:43 PM

Yes. It's working now. There were a few DirectX errors and mismatches like the texture format that you get from Unity and so fort. Just had to enable the debug flag of the DX device and see what's really going on.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AtGfx · Apr 04, 2017 at 05:14 PM 0
Share

Hi, I try to make the exact same thing as you do, the exact same way... And I get the same problem :) Can you give a bit more details on how you resolve it ? In my case, the copy goes well in DirectX (I put a check on each call on the returned value, but the data inside my mappedData is always 0). Thank you !

avatar image jobigoud · Apr 18, 2017 at 08:42 AM 0
Share

@krasi_intugame: you should at least post your final working D3D11_TEXTURE2D_DESC to improve this answer.

avatar image AtGfx jobigoud · Apr 21, 2017 at 10:08 AM 0
Share

Simply change

 copyTexDesc.Format = DXGI_FOR$$anonymous$$AT_B8G8R8A8_UNOR$$anonymous$$;

with

 copyTexDesc.Format = uDesc.Format;

and you will get the correct texture description according to the format you declared in the behaviour with :

 var myRT = new RenderTexture(myCamera.pixelWidth, myCamera.pixelHeight, 24, RenderTextureFormat.ARGB32);

In the example above the RenderTexture is declared as ARGB32 while the copyTextDesc is declared as BGRA32.

avatar image
0

Answer by wqyuwss · Jun 16, 2016 at 07:35 PM

do you get solution?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Jittery Frame Rate with 120ms+ for PartilceSystem.GeometryJob execution 4 Answers

unity5.2版本烘焙Terrain, 阴影不清晰。 1 Answer

Cloth physics on a rigged character 1 Answer

Loading screen on 5.3 (problem after loading the first scene) 1 Answer

Scripts will not load Visual Studio 2015 as of 5.3.1f1 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges