Hi, I tried a technique described here: http://en.wikibooks.org/wiki/Cg_Programming/Unity/Billboards, but if i create two sprites in the scene, positions of the sprites are wrong: Also I tried to create billboard shader that will rotate my sprite here, but cannot finish it. Please help me fix this problem. I create sprite from Unity3d script, but there are a lot of objects in my scene, and when i try to rotate each sprite on each update, application performance is very low. So I want to create a shader that will rotate object (plane), around his center, so he is always visible to camera. Here is the shader code, that should implement this: Code (csharp): float3 look = normalize(ObjSpaceViewDir(v.vertex).xyz); float3 up = float3(0.0f, 1.0f, 0.0f); float3 right = normalize(cross(look, up)); float4x4 rotationMatrix = float4x4(float4(right.x, up.x, -look.x, 0), float4(right.y, up.y, -look.y, 0), float4(right.z, up.z, -look.z, 0), float4(v.vertex.xyz, 1)); float4 finalposition = mul(rotationMatrix, v.vertex); float4 pos = float4(finalposition.xyz, 1); o.vertex = mul(UNITY_MATRIX_MVP, pos); But it doesn't works. Please point me to bug in this code. Thanks in advance.
It is more likely that you'll get help if you ask a specific question. That way the solution might also helpful to other people visiting the forum. If you want someone to fix the shader for you, you could consider requesting help on the commercial forum. [Edit] Your edited first post looks better now.
Thank you for your answer, but i have found some similar question, and of course solutions for this issue. But i really can't find any solution that works. I just try to change position of my sprite, and sprite appears in some strange locations. So I tried to implement this shader by myself, and when i understand that i can't get good implementation, i decided to ask the question here. Also i put my implementation here, and i hope that a lot of people knows solution for this issue. So, i of course do not need someone to create shader for me from A to Z.
I thinks that, setting world matrix for 999999 object on each frame on update is not the best solution. And even removing colliders will not help in this situation
Never know what exactly what you want,here is my code,which will keep mesh size based distance to camera,and rotation Code (csharp): Shader "Tut/Project/Billboard_2" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" } pass{ Cull Off ZTest Always Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; struct v2f { float4 pos:SV_POSITION; float2 texc:TEXCOORD0; }; v2f vert(appdata_base v) { v2f o; float4 ori=mul(UNITY_MATRIX_MV,float4(0,0,0,1)); float4 vt=v.vertex; //vt.y=vt.z; float2 r1=float2(_Object2World[0][0],_Object2World[0][2]); float2 r2=float2(_Object2World[2][0],_Object2World[2][2]); float2 vt0=vt.x*r1; vt0+=vt.z*r2; vt.xy=vt0; vt.z=0; vt.xyz+=ori.xyz;//result is vt.z==ori.z ,so the distance to camera keeped ,and screen size keeped o.pos=mul(UNITY_MATRIX_P,vt); o.texc=v.texcoord; return o; } float4 frag(v2f i):COLOR { return tex2D(_MainTex,i.texc); } ENDCG }//endpass } }
Never know what exactly what you want,here is my code,which will keep mesh size based distance to camera,and rotation Code (csharp): Shader "Tut/Project/Billboard_2" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" } pass{ Cull Off ZTest Always Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; struct v2f { float4 pos:SV_POSITION; float2 texc:TEXCOORD0; }; v2f vert(appdata_base v) { v2f o; float4 ori=mul(UNITY_MATRIX_MV,float4(0,0,0,1)); float4 vt=v.vertex; //vt.y=vt.z; float2 r1=float2(_Object2World[0][0],_Object2World[0][2]); float2 r2=float2(_Object2World[2][0],_Object2World[2][2]); float2 vt0=vt.x*r1; vt0+=vt.z*r2; vt.xy=vt0; vt.z=0; vt.xyz+=ori.xyz;//result is vt.z==ori.z ,so the distance to camera keeped ,and screen size keeped o.pos=mul(UNITY_MATRIX_P,vt); o.texc=v.texcoord; return o; } float4 frag(v2f i):COLOR { return tex2D(_MainTex,i.texc); } ENDCG }//endpass } }
Hope this would be help and here is a more simple code,just size in view sapce will keeped,not rotation Code (csharp): v2f vert(appdata_base v) { v2f o; float4 ori=mul(UNITY_MATRIX_MV,float4(0,0,0,1)); float4 vt=v.vertex; vt.y=vt.z; vt.z=0; vt.xyz+=ori.xyz;//result is vt.z==ori.z ,so the distance to camera keeped ,and screen size keeped o.pos=mul(UNITY_MATRIX_P,vt); o.texc=v.texcoord; return o; } just worked for mesh has pivot in center,like default plane
Thanks again, Lulucifer. Shader works fine. The only restriction, i should mention in your shader, that you cannot assign the SAME material for objects, user need to create separate materials for each object. I think this is the issue of the Unity3d. But, thank you a lot!
I know that, I had asked why,but didnt get answers I have try again, and they(billborads) are fine now, dont know why. maybe something cached by unity
Ah, you have got the same issues too. So, now i can say that even this works nice, if i create separate materials for objects. So, yes, there is some mega bug
Did you guys managed to fix the intance issue? For now I just wrote a simple script that update rotation of a mehs on Update, but it seems overkill with a lot of sprites and would rather use a shader for that. Also is possible to not rotate on Y in the shader? Sorry for question I'm kinda noob when comes to shaders.
Yeah it works, but as you can expect drawcalls raise and the benefit to have one single material is just gone. But I guess isn't fixable.
Hi guys i am working with ur shader and its working awsmly but some of my trees r disappear and the appear agian i dnt knw what is this its like a lights blinking effect can u guys guide me that what i am doing worng
Is there a solution to this at all? I'm generating billboards procedurally, and I need a solution that works in shader. Like the posters before me, the code here works as long as the billboards do not overlap, if they do, they translate to the inverse of their transform, not helpful. Again, as mentioned, using separate shaders fixes this issue, but I'm generating hundreds of billboards, to keep my draw count manageable, hundreds of materials is not going to work.