Align objects to 3D geometry DX11 StreamOut

Hi all,

I’ve been playing around with the DX11 geometry shaders. In particular the StreamOut_Rebind example from the girlpower folder of the DX11 pack.

Currently all box instances that are created in the Box_Batcher shader face in the same direction. I would like to modify the shader so that each box is rotated in the direction of the vertex normal (or the interpolated normal for vertices with the same xyz coordinates).

Since I’m quite new to HLSL I haven’t figured out how to modify the code. I guess I somehow need to multiply output.pos with a transform matrix that considers the normals.

Any suggestions?

Attached is a patch of the result that I want to achieve but done in the old-fashioned way. Would be great to achieve the same thing inside the DX11 shader directly.

AlignToGeometry.v4p (13.6 kB)

1 Like

why don’t u post ur progress with that shader?
anyways for stuff like sphere it’s pretty easy for something more then that needs testing…

use tangent vectors (2 vectors pointing in the incrementing direction of U and V coordinates in object space and are perpendicular to normal vector) (comes with TANGENT and BINORMAL semantics if the 3D model supports it). using tangents and normal you can construct a 3x3 matrix which you can use to multiply your object with. note if UV coordinates are distorted (incrementing ratio between U and V != 1) and you don’t want your objects distorted you have to select one of the tangents and do cross product between it and the normal and use the resulting vector instead of the other tangent for the 3x3 matrix.
also note that currently primitives in vvvv-DX11 doesn’t come with tangent vectors so you have to calculate them which is a completely different and longer story.

Thanks microdee. I’ll give that a try.

Btw, how can I confirm that a mesh comes with TANGENT and BINORMAL? I know exporting from other 3d software can be tricky sometimes…so I want to double check my files after I bring them into vvvv.
I tested a couple of different files using VertexDeclaration and noticed some odd behaviour. In one case (see screenshot) I imported the exact same file (aka astroBoy) using the Assimp nodes as well as the COLLADA nodes. For the Assimp approach VertexDecleration included TANGENT and BINORMAL whereas for the collada approach it did not. How can that be? I’m using the exact same file, yet there seems to be different per vertex data. Any suggestions?

if you want to work with dx11 streamout you should use dx11 assimp (SceneFile) and to check wether the mesh has tangents or not, use InputLayout and InputElement Split nodes. It might be possible that old dx9 collada node didn’t interpret tangents but i doubt that. anyway if you export a mesh with dae or fbx and the mesh had texture coordinates and normals the 3D software probably calculated the tangents for you too.

The assimp scene node uses this preset when loading the scene: Assimp: postprocess.h File Reference
As you can see aiProcess_CalcTangentSpace is set so all scenes loaded with the assimp nodes will contain those tangents and binormals.

I’ve tried to write that shader, I’m a bit confused about the transform order, I should be close to the solution. But there is an error somewhere, I don’t know if in the DAE model or in my shaders.

I’ve found this article as reference http://renderdan.blogspot.it/2006/05/rotation-matrix-from-axis-vectors.html

I attach everything.

alignToGeometry.zip (9.6 kB)

If you want to be able to use generative meshes, or anything without tangents and binormals you can also use a look at function. Use your normal for the direction and the up vector as you like (0,1,0 usually).

float3x3 lookat(float3 dir,float3 up=float3(0,1,0))
{
	float3 z=normalize(dir);float3 x=normalize(cross(up,z));float3 y=normalize(cross(z,x));return float3x3(x,y,z);
}

I feel stupid, there’s some problem, maybe with the Blender’s DAE, I don’t know, I’ve already met this problem and never really solved. The geometry shader read more vertices than the vertices count in Blender. I’ve got a lot of double vertices with slighty different normal vectors.

The lookat function return a not well aligned rotation, it depends on wich doubleVertex takes.

In the attached zip there’s the updated patch, the meshes and some screenshot from Blender.

(edit: I opened with a text editor the DAE models and Blender does not export Tangent and Bitangent, so the only approach for me is the lookat method wich still work bad because of the double verices created by VVVV)

alignToGeometry_2.zip (656.5 kB)

You do have per face normals in your meshes. I’m not a blender user but I’d say this is probably what you are running in to:
https://developer.blender.org/T44900

This is a screen from your shader’s lookat technique as is with a model with smooth per vertex normals. Keep in mind that is still drawing multiple instances per vertex (think of it as one for each triangle point at that location- & triangles points share locations). They all point the same direction so you don’t see the extras.

yea collada and blender aren’t friends, at list when i try to use blender for it. however OBJ is pretty solid for that purpose, since u won’t have additional transforms