Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by RSpicer · Sep 19, 2014 at 09:43 PM · shaderproceduraldx11

Graphics.DrawProcedural from OnRenderObject

I'm trying to re-implement Microsoft's "WpfD3DInterop" Kinect 1.8 sample project in Unity3d 4.5. This project uses DirectX11 to procedurally generate geometry based on the depth map returned by the Kinect sensor. As an added feature, I want the Kinect-generated geometry to be drawn relative to an empty GameObject in the world (representing the Kinect sensor's translation/orientation).

As near as I can determine, the best way to go about re-implementing this is

  1. Use a Compute Shader to transform the 1d short[] array returned by the Kinect into a RWTexture2D and write that to a Unity RenderTexture.

  2. Rewrite the MS-supplied shader to use RWTexture2D instead of Texture2D (because I can bind a RWTexture2D using Graphics.SetRandomWriteTarget, but can't bind a Texture2D).

  3. Rewrite the MS-supplied shader to use uniforms for the modelview-projection matrix and scale properties instead of a cbuffer, as Unity does not appear to have a built-in way to set cbuffer values.

I have validated that step 1 is working by writing a much simpler shader that simply binds the RenderTexture as a sampler and transforms the value into [0..1] half values and renders the depthmap to a quad.

I can't validate steps 2 and 3, because I can't seem to make anything draw procedurally. I'm including my OnWillRenderObject() code below -- this GameObject also has a MeshFilter (containing a default Quad) and a MeshRenderer attached, and I've validated that OnWillRenderObject() is being called by inserting a Debug.Log into this snippet, and observing Debug.Log output.

     public Material computeMaterial;

     private RenderTexture m_depthTexture;

     void OnRenderObject()
     {
         Matrix4x4 mvp = Camera.current.projectionMatrix * Camera.current.worldToCameraMatrix * transform.localToWorldMatrix;
         Graphics.ClearRandomWriteTargets();
         computeMaterial.SetPass(0);
         computeMaterial.SetMatrix("ViewProjection", mvp);
         computeMaterial.SetVector("XYScale", new Vector4(1.2f, 1f));
         Graphics.SetRandomWriteTarget(1, m_depthTexture);
         Graphics.DrawProcedural(MeshTopology.Points, 640 * 480);
      }

and my shader contains

 RWTexture2D<int> txDepth : register(u1);

 //--------------------------------------------------------------------------------------
 // Constant Buffer Variables
 //--------------------------------------------------------------------------------------
 uniform matrix ViewProjection;
 uniform float4 XYScale;

I'm not getting any output, or any errors. Is anything obviously wrong with my setup? It's entirely possible that I'm doing something ridiculously wrong with my drawing code, or that there are bugs in my shaders. Any suggestions appreciated!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by RSpicer · Oct 07, 2014 at 07:38 PM

I eventually resolved this set of bugs. There's no single answer I can point to since the result I saw was the result of several errors, but I'll offer the following observations:

1) It's very easy to make typos when referencing compute shader kernels and named buffers from MonoBehaviours. These types of errors fail silently unless you look at the debug output from the shaders in the Unity inspector, and/or print debug output in your MonoBehaviour to make sure the values returned by things like GetKernel() are sane.

2) RGBAFloat colors are best bitmasked with 0xefff when you're trying to convert them to unorm floats.

3) Be very careful with indexing when iterating over 1d arrays that you want to make 2d.

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Unity 5 tesselation problem 1 Answer

Is it possible to fake Global Illumination within textures? 1 Answer

New Logical Blend Operations?! 0 Answers

How to create an procedural texture in shader graph for many objects, and save it? 0 Answers

SOLVED: Calculate sphere UV from position (without discontinuity) 1 Answer


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