Posts

Showing posts with the label vector3

Compute Shader, read from calculated buffer

 Use case - get the GPU to calculate a bunch of X,Y,Z coordinates for an arbitrary number of instanced prefabs in Unity.  On the Unity C# side, we need : To specify a compute shader, a handle for the compute shader, a buffer that we'll be writing to in the shader, a prefab, the number of prefabs we want, an array for the instanced prefabs, an array for the coordinate data. Any extra variables we want to pass over, eg time. On the compute shader side we need : To specify a read-write buffer, instead of a texture that we created in the previous post. The buffer will be of float3 type, which is how HLSL calls a vector3. That's actually it... Here's the Compute Shader code - #pragma kernel boxMove RWStructuredBuffer<float3> yesBlad; float time; [numthreads(64,1,1)] void boxMove (uint3 id : SV_DispatchThreadID) {     float xpos = (float)id.x;     float ypos = sin(id.x+time)*50;     float zpos = cos(id.x +time)* 50;    ...

setting VFX graph properties using C#

How to set values for your custom properties in VFX Graph. Yeah, there are binders, but I find they expect a rather precise  way of working.. Making your own scripts forces Unity to do what you actually want haha. In my VFX graph, I have a VECTOR 3 value named "realSpawnPos" which I'm using as the spawn position for some particles. It's important to note that "position", whilst it is a Vector3.. doesn't like receiving Vector 3 values. So make sure you set it up as a Vector 3 in the VFX graph. Further to this - Make sure in the VFX Graph, your position node is set to WORLD, if you need to set/get/whatever the world position and not local... your objects are probably nested in a million groups/transforms..   using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.VFX; public class bindPosition : MonoBehaviour {     public GameObject target_thing;     public VisualEffect visualEffect;     void U...