Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 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 AkosDucz · Dec 30, 2021 at 03:18 PM · shadersgpuquaternionsinstancing

GPU instancing shader wrong rotation,Unity GPU instancing shader wrong rotation

I am trying to make unity render lots of (1M) cubes all with different position, color and rotation. Here is my shader. Positions and colors work, but the rotations are incorrect. The problem mostly manifests when rotation on the x and z axes over 180 degrees.

 Shader "Instanced/InstancedSurfaceShader" {
     Properties {
         _MainTex ("Albedo (RGB)", 2D) = "white" {}
         _Glossiness ("Smoothness", Range(0,1)) = 0.5
         _Metallic ("Metallic", Range(0,1)) = 0.0
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         LOD 200
 
         CGPROGRAM
         // Physically based Standard lighting model
         #pragma surface surf Standard addshadow fullforwardshadows
         #pragma multi_compile_instancing
         #pragma instancing_options procedural:setup
 
         sampler2D _MainTex;
 
         struct Input {
             float2 uv_MainTex;
         };
 
     #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
         StructuredBuffer<float4> positionBuffer;
         StructuredBuffer<float4> colorBuffer;
         StructuredBuffer<float4> rotationBuffer;
     #endif
         void setup()
         {
 
     #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
         float4 position     = positionBuffer[unity_InstanceID];
         float4 q             = rotationBuffer[unity_InstanceID];
         float qr            = q[0];
         float qi            = q[1];
         float qj            = q[2];
         float qk            = q[3];
  
         float4x4 rotation;
         float4x4 translation = {
             1,0,0,position.x,
             0,1,0,position.y,
             0,0,1,position.z,
             0,0,0,1
         };
  
         // quaternion to matrix
         // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/
         // https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Quaternion-derived_rotation_matrix
  
         rotation[0][0]            = 1.0f - 2.0f*qj*qj - 2.0f*qk*qk;
         rotation[0][1]            = 2.0f*(qi*qj - qk*qr);
         rotation[0][2]            = 2.0f*(qi*qk + qj*qr);
         rotation[0][3]            = 0.0f;
  
         rotation[1][0]            = 2.0f*(qi*qj+qk*qr);
         rotation[1][1]            = 1.0f - 2.0f*qi*qi - 2.0f*qk*qk;
         rotation[1][2]            = 2.0f*(qj*qk - qi*qr);
         rotation[1][3]            = 0.0f;
  
         rotation[2][0]            = 2.0f*(qi*qk - qj*qr);
         rotation[2][1]            = 2.0f*(qj*qk + qi*qr);
         rotation[2][2]            = 1.0f - 2.0f*qi*qi - 2.0f*qj*qj;
         rotation[2][3]            = 0.0f;
  
         rotation[3][0]            = 0.0f;
         rotation[3][1]            = 0.0f;
         rotation[3][2]            = 0.0f;
         rotation[3][3]            = 1.0f;
  
         unity_ObjectToWorld = mul(translation, rotation);
         //unity_WorldToObject = inverse(unity_ObjectToWorld);
          
         // inverse transform matrix
         // taken from richardkettlewell's post on
         // https://forum.unity3d.com/threads/drawmeshinstancedindirect-example-comments-and-questions.446080/
  
         float3x3 w2oRotation;
         w2oRotation[0] = unity_ObjectToWorld[1].yzx * unity_ObjectToWorld[2].zxy - unity_ObjectToWorld[1].zxy * unity_ObjectToWorld[2].yzx;
         w2oRotation[1] = unity_ObjectToWorld[0].zxy * unity_ObjectToWorld[2].yzx - unity_ObjectToWorld[0].yzx * unity_ObjectToWorld[2].zxy;
         w2oRotation[2] = unity_ObjectToWorld[0].yzx * unity_ObjectToWorld[1].zxy - unity_ObjectToWorld[0].zxy * unity_ObjectToWorld[1].yzx;
  
         float det = dot(unity_ObjectToWorld[0], w2oRotation[0]);
  
         w2oRotation = transpose(w2oRotation);
  
         w2oRotation *= rcp(det);
  
         float3 w2oPosition = mul(w2oRotation, -unity_ObjectToWorld._14_24_34);
 
         unity_WorldToObject._11_21_31_41 = float4(w2oRotation._11_21_31, 0.0f);
         unity_WorldToObject._12_22_32_42 = float4(w2oRotation._12_22_32, 0.0f);
         unity_WorldToObject._13_23_33_43 = float4(w2oRotation._13_23_33, 0.0f);
         unity_WorldToObject._14_24_34_44 = float4(w2oPosition, 1.0f);
 
     #endif
 
 
         }
 
         half _Glossiness;
         half _Metallic;
         UNITY_INSTANCING_BUFFER_START(Props)
            UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
         UNITY_INSTANCING_BUFFER_END(Props)
         void surf (Input IN, inout SurfaceOutputStandard o) {
             fixed4 c = {0,0,255,0};//tex2D (_MainTex, IN.uv_MainTex);
             #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
                 c=colorBuffer[unity_InstanceID];
             #endif
             o.Albedo = c.rgb;
             o.Metallic = _Metallic;
             o.Smoothness = _Glossiness;
             o.Alpha = c.a;
         }
         ENDCG
     }
     FallBack "Diffuse"
 }

Maybe something wrong with the matrix calculation?

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

155 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

Related Questions

Cant find Standard Surface Shader (Instanced) in Unity 2 Answers

GPU Instancing with textures 1 Answer

How do I enable GPU Instancing on the Sprites-Default shader? 1 Answer

Shader graph and GPU instancing 0 Answers

Difference between dynamic batching and gpu instancing? 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