Skip to content

Commit 6da7e68

Browse files
committed
Even more optimization for dot stimuli, and bug fix
1 parent 37d076e commit 6da7e68

3 files changed

Lines changed: 8 additions & 22 deletions

File tree

Assets/Scripts/DotStimulus/DotManager.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class DotManager : MonoBehaviour
2020
private Bounds _bounds;
2121
private const float BoundsRange = 5.0f;
2222
private static readonly int ShaderProperties = Shader.PropertyToID("_Properties");
23+
private static readonly int ParentLocalToWorld = Shader.PropertyToID("parentLocalToWorld");
2324

2425
public void Awake()
2526
{
@@ -38,18 +39,9 @@ public void Start()
3839
localPosition = new Vector3(localPosition.x, localPosition.y, stimulusSettings.stimDepthMeters);
3940
worldTransform.localPosition = localPosition;
4041
}
41-
42-
public void OnEnable()
43-
{
44-
for (var i = 0; i < _dots.Length; i++)
45-
{
46-
_shaderData.BufferLocalToWorld(transform.localToWorldMatrix, i);
47-
}
48-
}
49-
42+
5043
public void Update()
5144
{
52-
var localStimulusToWorld = transform.localToWorldMatrix;
5345
for (var i = 0; i < _dots.Length; i++)
5446
{
5547
_dots[i].UpdateDot();
@@ -59,6 +51,7 @@ public void Update()
5951
var meshPropertiesBuffer = _shaderData.MeshPropertiesBuffer;
6052
meshPropertiesBuffer.SetData(_shaderData.MeshProps);
6153
dotMeshMaterial.SetBuffer(ShaderProperties, meshPropertiesBuffer);
54+
dotMeshMaterial.SetMatrix(ParentLocalToWorld, transform.localToWorldMatrix);
6255
Graphics.DrawMeshInstancedIndirect(dotMesh, 0, dotMeshMaterial, _bounds, _shaderData.ArgsBuffer);
6356
}
6457

Assets/Scripts/DotStimulus/DotShaderData.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class DotShaderData
77
public MeshProperties[] MeshProps { get; }
88
public ComputeBuffer MeshPropertiesBuffer { get; private set; }
99
public ComputeBuffer ArgsBuffer { get; private set; }
10-
10+
1111
public DotShaderData(Mesh dotMesh, int numDots)
1212
{
1313
MeshProps = new MeshProperties[numDots];
@@ -24,12 +24,7 @@ public DotShaderData(Mesh dotMesh, int numDots)
2424
ArgsBuffer = new ComputeBuffer(1, args.Length * sizeof(uint), ComputeBufferType.IndirectArguments);
2525
ArgsBuffer.SetData(args);
2626
}
27-
28-
public void BufferLocalToWorld(Matrix4x4 stimulusCenterLocalToWorld, int i)
29-
{
30-
MeshProps[i].parentLocalToWorld = stimulusCenterLocalToWorld;
31-
}
32-
27+
3328
public void UpdateMeshPropertiesBuffer(Dot[] dots, int i)
3429
{
3530
MeshProps[i].localPosition = dots[i].Position;
@@ -48,12 +43,10 @@ public void ClearBuffers()
4843
public struct MeshProperties {
4944
public Vector3 localPosition;
5045
public float localScale;
51-
public Matrix4x4 parentLocalToWorld;
5246

5347
public static int Size()
5448
{
55-
return (sizeof(float) * 4 * 4) +
56-
(sizeof(float) * 4);
49+
return (sizeof(float) * 4);
5750
}
5851
}
5952
}

Assets/Shaders/UnlitDotInstanceShader.shader

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
struct MeshProperties {
3636
float3 localPosition;
3737
float localScale;
38-
float4x4 parentLocalToWorld;
3938
};
39+
uniform float4x4 parentLocalToWorld;
4040

4141
StructuredBuffer<MeshProperties> _Properties;
4242

@@ -63,7 +63,7 @@
6363
const float4x4 localTransform = mul(localPosition, localScale);
6464

6565
const float4x4 worldTransform =
66-
mul(_Properties[instanceID].parentLocalToWorld, localTransform);
66+
mul(parentLocalToWorld, localTransform);
6767

6868
const float4 pos = mul(worldTransform, v.vertex);
6969
o.vertex = UnityWorldToClipPos(pos);

0 commit comments

Comments
 (0)