Rotating quads to face the camera?

Node-based Shader Editor

Rotating quads to face the camera?

Postby CyanSlinky » Fri Jul 13, 2018 1:02 pm

So i have multiple disconnected quads that face up in a single mesh, can i somehow displace their vertices to look at the camera instead or another vector?

Image
CyanSlinky
 
Posts: 6
Joined: Sun Jan 21, 2018 7:18 pm

Re: Rotating quads to face the camera?

Postby Amplify_Borba » Fri Jul 13, 2018 2:20 pm

Hey there!

Could you share a few additional details, such as how you're generating the quads?

I believe that this would usually be scripted, not controlled by the shader, however, it ultimately depends on your requirements.
Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Amplify_Borba
 
Posts: 1239
Joined: Mon Jul 24, 2017 9:50 am

Re: Rotating quads to face the camera?

Postby CyanSlinky » Fri Jul 13, 2018 4:55 pm

I'm procedurally generating the quads through Procedural Toolkit, which isn't required but i use it for convenience.

Here's a code snippet :

Code: Select all
using ProceduralToolkit;

public class Pointfield : MonoBehaviour
{
   private Transform myTF;

   private MeshDraft meshDraft = new MeshDraft();
      
   private MeshFilter   meshFilter;
   private MeshRenderer meshRenderer;
   private MeshCollider meshCollider;

   public int pointCount = 100;
   public float pointRadius = 0.2f;
   public float radius = 10;
      
   private void Start()
   {
      myTF = GetComponent<Transform>();
         
      meshFilter   = myTF.GetComponent<MeshFilter>();
      meshRenderer = myTF.GetComponent<MeshRenderer>();
      meshCollider = myTF.GetComponent<MeshCollider>();
         
      Create();
   }

   private void Create()
   {
      for(int i = 0; i < pointCount; i++)
      {
         Vector3 position = Random.insideUnitSphere * radius;

         Vector3 c1 = position + (((Vector3.back + Vector3.left) / 2) * pointRadius);
         Vector3 c2 = position + (((Vector3.forward + Vector3.left) / 2) * pointRadius);
         Vector3 c3 = position + (((Vector3.forward + Vector3.right) / 2) * pointRadius);
         Vector3 c4 = position + (((Vector3.back + Vector3.right) / 2) * pointRadius);
            
         MeshDraft quad = MeshDraft.Quad(c1, c2, c3, c4);

         meshDraft.Add(quad);
      }
         
      Render();
   }

   private void Render()
   {
      meshFilter.mesh = meshDraft.ToMesh();
   }
}


I would think that rotating each quad through a for loop and updating the mesh every time would tank the framerate if the quads are in the thousands, maybe I'm wrong but i would think that doing it through shaders would be faster?
CyanSlinky
 
Posts: 6
Joined: Sun Jan 21, 2018 7:18 pm

Re: Rotating quads to face the camera?

Postby Amplify_Borba » Mon Jul 16, 2018 11:01 am

Unfortunately, even tough we provide the means to offset vertices, that is not something that you can easily setup in your shader as the way you control geometry is fairly limited. It really depends on how you intend to use it, conceivably you probably deform them to a certain point but, given the precise control you're looking for, it seems to be outside the scope of our editor.

Have you considered using particles or a billboard for the planes? Instancing might help mitigate possible performance issues.

Apologies for the inconvenience.
Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Amplify_Borba
 
Posts: 1239
Joined: Mon Jul 24, 2017 9:50 am

Re: Rotating quads to face the camera?

Postby CyanSlinky » Mon Jul 16, 2018 2:01 pm

Thanks for the response, I've looked into making this work via particles but it gets slower based on how many particles there are, maybe it could work through gpu instancing but i'm not entirely sure how i enable that feature. I could check it out at some point.

Anyways over the weekend i found a billboard shader that does exactly what i want, i would ideally like to convert it into amplify shader if at all possible, so i can add additional features to the shader. I'm not very experienced with the written shader language so i don't really know where to begin with converting this shader or if it's even possible.

Code: Select all
Shader "Custom/GS Billboard"
{
   Properties
   {
      _SpriteTex ("Base (RGB)", 2D) = "white" {}
      _Size ("Size", Range(0, 3)) = 0.5
   }

   SubShader
   {
      Pass
      {
         Tags { "RenderType"="Opaque" }
         LOD 200

         CGPROGRAM
            #pragma target 5.0
            #pragma vertex VS_Main
            #pragma fragment FS_Main
            #pragma geometry GS_Main
            #include "UnityCG.cginc"

            // **************************************************************
            // Data structures                                    *
            // **************************************************************
            struct GS_INPUT
            {
               float4   pos      : POSITION;
               float3   normal   : NORMAL;
               float2  tex0   : TEXCOORD0;
            };

            struct FS_INPUT
            {
               float4   pos      : POSITION;
               float2  tex0   : TEXCOORD0;
            };


            // **************************************************************
            // Vars                                             *
            // **************************************************************

            float _Size;
            float4x4 _VP;
            Texture2D _SpriteTex;
            SamplerState sampler_SpriteTex;

            // **************************************************************
            // Shader Programs                                    *
            // **************************************************************

            // Vertex Shader ------------------------------------------------
            GS_INPUT VS_Main(appdata_base v)
            {
               GS_INPUT output = (GS_INPUT)0;

               output.pos =  mul(unity_ObjectToWorld, v.vertex);
               output.normal = v.normal;
               output.tex0 = float2(0, 0);

               return output;
            }



            // Geometry Shader -----------------------------------------------------
            [maxvertexcount(4)]
            void GS_Main(point GS_INPUT p[1], inout TriangleStream<FS_INPUT> triStream)
            {
               float3 up = UNITY_MATRIX_IT_MV[0].xyz;
               float3 look = _WorldSpaceCameraPos - p[0].pos;
               look.y = 0;
               look = normalize(look);
               float3 right = UNITY_MATRIX_IT_MV[1].xyz;

               float halfS = 0.5f * _Size;

               float4 v[4];
               v[0] = float4(p[0].pos + halfS * right - halfS * up, 1.0f);
               v[1] = float4(p[0].pos + halfS * right + halfS * up, 1.0f);
               v[2] = float4(p[0].pos - halfS * right - halfS * up, 1.0f);
               v[3] = float4(p[0].pos - halfS * right + halfS * up, 1.0f);

               FS_INPUT pIn;
               pIn.pos = UnityObjectToClipPos(v[0]);
               pIn.tex0 = float2(1.0f, 0.0f);
               triStream.Append(pIn);

               pIn.pos = UnityObjectToClipPos(v[1]);
               pIn.tex0 = float2(1.0f, 1.0f);
               triStream.Append(pIn);

               pIn.pos = UnityObjectToClipPos(v[2]);
               pIn.tex0 = float2(0.0f, 0.0f);
               triStream.Append(pIn);

               pIn.pos = UnityObjectToClipPos(v[3]);
               pIn.tex0 = float2(0.0f, 1.0f);
               triStream.Append(pIn);
            }



            // Fragment Shader -----------------------------------------------
            float4 FS_Main(FS_INPUT input) : COLOR
            {
               return _SpriteTex.Sample(sampler_SpriteTex, input.tex0);
            }

         ENDCG
      }
   }
}
CyanSlinky
 
Posts: 6
Joined: Sun Jan 21, 2018 7:18 pm

Re: Rotating quads to face the camera?

Postby Amplify_Borba » Mon Jul 16, 2018 3:29 pm

ASE already includes an implementation of Billboards, you can set them up through either the Billboard node or the Output node parameter.

Since it basically functions as a toggle, unless you want to further customize it, simple enable it in the Output node or add the node and connect it's output to the Local Vertex Offset port and compile the shader to see the results.
Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Amplify_Borba
 
Posts: 1239
Joined: Mon Jul 24, 2017 9:50 am

Re: Rotating quads to face the camera?

Postby CyanSlinky » Mon Jul 16, 2018 7:39 pm

wow can't believe i overlooked that option, sadly though it's not doing what i want it to. It seems to be billboarding the entire mesh as a whole instead of the quads individually.

what the billboard option does :

Image

what the shader i posted above does ( this is exactly what i want, but converted to amplify if possible ) :

Image
CyanSlinky
 
Posts: 6
Joined: Sun Jan 21, 2018 7:18 pm

Re: Rotating quads to face the camera?

Postby Amplify_Borba » Tue Jul 17, 2018 10:58 am

Unfortunately, that shader that you've shared is a custom Geometry shader, which can't be converted to ASE since we don't support this type of shaders yet. Hopefully we should be able to add support for those in the future.

It's possible, however, to add GPU Instancing to Unity's particle system, and we should be adding support for this on our Particles Template as soon as possible!
Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Amplify_Borba
 
Posts: 1239
Joined: Mon Jul 24, 2017 9:50 am

Re: Rotating quads to face the camera?

Postby CyanSlinky » Tue Jul 17, 2018 1:33 pm

Alright, i think you're right that particles are more suited for what i'm doing, i just wanted to see if i could get it to work this way. Thanks for all your help!
CyanSlinky
 
Posts: 6
Joined: Sun Jan 21, 2018 7:18 pm

Re: Rotating quads to face the camera?

Postby Amplify_Borba » Tue Jul 17, 2018 1:40 pm

No problem, happy to help.

Have you had the chance to rate and review Amplify Shader Editor? It would be awesome if you could share your experience with the Unity community, the Unity Asset Store thrives on user interaction and direct feedback.
Every bit helps, and your feedback is extremely valuable to us.

Feel free to get back in touch if you have further issues or questions, thanks!
Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Amplify_Borba
 
Posts: 1239
Joined: Mon Jul 24, 2017 9:50 am


Return to Amplify Shader Editor

Who is online

Users browsing this forum: No registered users and 0 guests

cron