Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 KyleJensen72 · Mar 28 at 05:43 PM · shader3drenderinggraphicsrealtime

Raymarching Bug - Rendering 3D texture in real-time

I am working on a project for a client and I am trying to replicate the 3D volume rendering done in the Unity Inspector so I can view changes to 3D volume data in real-time.

Here is an example of what the final output should look like (with and without transparency):

https://imgur.com/a/unboPgk

However, when I render this in real-time using a slightly modified version of the default Raymarching shader provided by Unity - my results are quite different.

https://imgur.com/a/fNMguu4

Here is the shader code being used to render the 3D texture:

 Shader "Unlit/3DVolumeShader"
 {
     Properties
     {
         _Alpha ("Alpha", float) = 0.8
         _StepSize ("Step Size", float) = 0.01
     }
     SubShader
     {
         Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
         Blend One OneMinusSrcAlpha
         LOD 100
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             #define TEX_WIDTH 512
             #define TEX_HEIGHT 64
             #define TEX_LENGTH 384
 
             // Maximum amount of raymarching samples
             #define MAX_STEP_COUNT 128
 
             // Allowed floating point inaccuracy for hit detection
             #define EPSILON 0.00001f
 
             struct appdata
             {
                 float4 vertex : POSITION;
             };
 
             struct v2f
             {
                 float4 vertex : SV_POSITION;
                 float3 objectVertex : TEXCOORD0;
                 float3 vectorToSurface : TEXCOORD1;
             };
 
             float _Alpha;
             float _StepSize;
 
             StructuredBuffer<float4> DensityMap;
             
             v2f vert (appdata v)
             {
                 v2f o;
 
                 // Vertex in object space this will be the starting point of raymarching
                 o.objectVertex = v.vertex;
 
                 // Convert vertex in object space to camera coordinates
                 o.vertex = UnityObjectToClipPos(v.vertex);
 
                 // Calculate vector from camera to vertex in world space
                 float3 worldVertex = mul(unity_ObjectToWorld, v.vertex).xyz;
                 o.vectorToSurface = worldVertex - _WorldSpaceCameraPos;
 
                 return o;
             }
 
             // This method blends colors based on their alpha transparency
             // If color.a == 1 then no blending will occur
             // Otherwise multiply the difference in the alpha's by the new color
             float4 BlendColors(float4 color, float4 newColor)
             {
                 color.rgb += (1.0 - color.a) * newColor.a * newColor.rgb;
                 color.a += (1.0 - color.a) * newColor.a;
                 return color;
             }
 
             fixed4 frag(v2f i) : SV_Target
             {
                 // Start raymarching at the front surface of the object
                 float3 rayOrigin = i.objectVertex;
 
                 // Use vector from camera to object surface to get the ray direction
                 float3 rayDirection = mul(unity_WorldToObject, float4(normalize(i.vectorToSurface), 1));
 
                 float4 color = float4(0, 0, 0, 0);
 
                 float3 bounds = float3(TEX_WIDTH, TEX_LENGTH, TEX_HEIGHT);
                 float3 samplePosition = rayOrigin;
 
                 // Raymarch through object space
                 for (int i = 0; i < MAX_STEP_COUNT; i++)
                 {
                     // Accumulate color only within unit cube bounds
                     if(max(abs(samplePosition.x), max(abs(samplePosition.y), abs(samplePosition.z))) < 0.5f + EPSILON)
                     {
                         // Sample the color at the position in our density map. Add an offset for UV coordinate transformation.
                         // float4 sampledColor = tex3D(_DensityMap, samplePosition + float3(0.5f, 0.5f, 0.5f));
 
                         float3 textureCoord = (samplePosition + float3(0.5f, 0.5f, 0.5f)) * bounds;
                         int index = int(textureCoord.x) + (int(textureCoord.y) * bounds.x) + (int(textureCoord.z) * bounds.x * bounds.y);
 
                         float4 sampledColor = DensityMap[index];
                         sampledColor.a *= _Alpha;
 
                         // Blend the colors based on alpha transparency
                         color = BlendColors(color, sampledColor);
 
                         samplePosition += rayDirection * _StepSize;
                     }
                 }
 
                 return color;
             }
             ENDCG
         }
     }
 }

I am using raymarching to index into the 3D texture array, which is where I think this issue might be occurring. I'm wondering if I might have something wrong with the scaling/coordinate system that Unity uses.

My texture size is 512x64x384 and I am rendering to a cube with scaling 1x0.1x0.75.

Please let me know if there is any additional information required to solve this.

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

0 Replies

· Add your reply
  • Sort: 

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

256 People are following this question.

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

Related Questions

How to render multiple objects at one layer? 1 Answer

How do I draw lines on the edges of meshes? 1 Answer

Convention of matrices passed to shaders 1 Answer

Unity 5 Custom Deferred shader 0 Answers

Shader Overlay Texture 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