File size: 5,331 Bytes
41791c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System.Collections;
using System.Collections.Generic;
using PhysX5ForUnity;
using UnityEngine;

public class CustomDiffuseMaterialFluidSourceActor : PhysxFluidSourceActor, ICustomFluidActor
{
    public float MaterialState
    {
        get { return m_materialState; }
    }

    public int ParticleMaterialChangeCount
    {
        get { return m_particleMaterialChangeCount; }
        set { m_particleMaterialChangeCount = value; }
    }

    public bool InEpisode
    {
        get { return m_inEpisode; }
        set { m_inEpisode = value; }
    }

    public bool IsActive
    {
        get { return m_isActive; }
        set { m_isActive = value; }
    }

    public int CompletionThreshold
    {
        get { return m_completionThreshold; }
        set { m_completionThreshold = value; }
    }

    public override void ResetObject()
    {
        base.ResetObject();
        m_inEpisode = false;
        m_isActive = false;
        m_lastParticleMaterialChangeCount = 0;
    }

    protected override void UpdateParticleData()
    {
        if (m_inEpisode && m_isActive)
        {
            base.UpdateParticleData();
            switch (m_agent.TaskLesson)
            {
                case 1:
                    m_agent.AddIrrigationReward(m_blood.ParticleMaterialChangeCount - m_lastParticleMaterialChangeCount);
                    m_lastParticleMaterialChangeCount = m_blood.ParticleMaterialChangeCount;
                    if (m_blood.ParticleMaterialChangeCount > m_materialChangeThreshold)
                    {
                        ChangeBloodMaterial();
                    }
                    if (m_blood.ParticleMaterialChangeCount >= m_completionThreshold)
                    {
                        m_agent.AddCompletionReward();
                        m_agent.EndEpisode();
                    }
                    // For evaluation, if the max possible number of particles changed, we can consider it successful but no completion reward added.
                    // if (m_agent.Evaluation && m_blood.ParticleMaterialChangeCount == m_agent.ActiveBloodIndices.Count)
                    // {
                    //     m_agent.AddCompletionReward();
                    //     m_agent.CummulativeReward -= 5;
                    //     m_agent.EndEpisode();
                    // }
                    break;
                case 0:
                    if (m_blood.ParticleMaterialChangeCount > m_materialChangeThreshold)
                    {
                        ChangeBloodMaterial();
                    }
                    break;
            }
            if (m_activeParticleIndices.Count == m_numParticles)
            {
                m_agent.EndEpisode();
            }
        }
        else
        {
            SyncParticleDataGet();
        }

        // Remove particles below the boundary
        bool belowBoundary = false;
        Vector4 teleportPosition = new Vector4(InactiveParticlePosition.x, InactiveParticlePosition.y, InactiveParticlePosition.z, 0f);
        List<int> particlesToRemove = new List<int>();

        foreach (int idx in m_activeParticleIndices)
        {
            Vector4 position = m_particleData.PositionInvMass[idx];
            // in case particle drops below the map
            if (position.y < -10.0f)
            {
                belowBoundary = true;
                m_particleData.SetParticle(idx, teleportPosition, true);
                particlesToRemove.Add(idx);
            }
        }

        if (belowBoundary)
        {
            foreach (int idx in particlesToRemove)
            {
                m_activeParticleIndices.Remove(idx);
            }
            m_particleData.SyncParticlesSet(false);
        }

        belowBoundary = false;
        particlesToRemove.Clear();

        foreach (int idx in m_blood.ActiveParticleIndices)
        {
            Vector4 position = m_blood.ParticleData.PositionInvMass[idx];
            // in case particle drops below the map
            if (position.y < -10.0f)
            {
                belowBoundary = true;
                m_blood.ParticleData.SetParticle(idx, teleportPosition, true);
                particlesToRemove.Add(idx);
            }
        }

        if (belowBoundary)
        {
            foreach (int idx in particlesToRemove)
            {
                m_blood.ActiveParticleIndices.Remove(idx);
            }
            m_blood.ParticleData.SyncParticlesSet(false);
        }
        // Debug.Log(m_agent.CummulativeReward);
    }

    private void ChangeBloodMaterial()
    {
        m_blood.PBDMaterial.Friction = m_pbdMaterial.Friction;
        m_blood.PBDMaterial.Viscosity = m_pbdMaterial.Viscosity;
        m_blood.PBDMaterial.SurfaceTension = m_pbdMaterial.SurfaceTension;
        m_blood.PBDMaterial.Cohesion = m_pbdMaterial.Cohesion;
    }

    [SerializeField]
    private float m_materialState;
    [SerializeField]
    private CustomDiffuseMaterialFluidArrayActor m_blood;
    [SerializeField]
    private IrrigationAgent m_agent;
    [SerializeField]
    private float m_materialChangeThreshold = 100;

    private int m_particleMaterialChangeCount;
    private bool m_inEpisode = false;
    private bool m_isActive = false;
    private int m_lastParticleMaterialChangeCount = 0;
    private int m_completionThreshold;
}