# Real-Time Cloth Simulation Using Compute Shader in Unity3D for AR/VR Contents
## 2 Related Work
### Implicit Constraint Enforcement  
In research by [PERSON] et al. [23], the implicit constraint enforcement method predicts the correct direction of the constraint forces by using future time-step. A list of positions of 3n size is used, where \(q\) = [\(x_{1}\), \(y_{1}\), \(z_{1}\), \(x_{2}\), \(y_{2}\), \(z_{2}\)... \(x_{n}\), \(y_{n}\), \(z_{n}\)]\({}^{T}\), and where n is the total number of all nodes. The distance constraint \(\Phi\) based on Euclidean equation can be defined as follows:  
\[\Phi=\left(x_{1}-x_{2}\right)^{2}+\left(y_{1}-y_{2}\right)^{2}+\left(z_{1}-z_ {2}\right)^{2}-r^{2} \tag{3}\]  
where, \(r\) is the initialing distance between 2 nodes. Then the list of m distance constraints can be written as the following equation:  
\[\Phi(q,t)=\Big{[}\Phi^{1}(q,t),\Phi^{2}(q,t),\dots\Phi^{m}(q,t)\Big{]}^{T} \tag{4}\]  
The partial differentiation on distance constraint concerning subscript \(q\) obtains a Jacobian matrix \(\Phi_{q}\) of size of m \(\times\) 3n. This can be calculated using the following equation:  
\[\Phi_{q}(q,t)=\left[\begin{array}{cccc}A&B&0&\dots&0\\ 0&C&D&\dots&0\\ \vdots&\vdots&\vdots&\ddots&\vdots\end{array}\right]\]  
\[\begin{array}{c}A=\big{|}2(x_{0}-C_{x}),2\big{(}y_{0}-C_{y}\big{)},2(z_{0}- C_{z})\big{|},\\ B=\big{|}-2(x_{0}-C_{x}),-2\big{(}y_{0}-C_{y}\big{)},-2(z_{0}-C_{z})\big{|},\\ C=\big{|}2(x_{1}-C_{x}),2\big{(}y_{2}-C_{y}\big{)},2(z_{2}-C_{z})\big{|},\\ D=\big{|}-2(x_{1}-C_{x}),-2\big{(}y_{1}-C_{y}\big{)},-2(z_{1}-C_{z})\big{|}, \end{array} \tag{5}\]  
where, \(x_{0},y_{0},z_{0}\) are the current position of a node, and \(C_{x}\), \(C_{y}\), \(C_{z}\) are the fixed position of the node. The system of constraint force equation can be written as follows:  
\[M\ddot{q}+\Phi_{q}^{T}\lambda=F^{A} \tag{6}\]  
where, \(M\) is the mass matrix and the diagonal element stores the node's mass, \(F^{A}\) is the accumulation of gravity force and constraint forces acting on the node, \(\Phi_{q}^{T}\) is the transpose of the Jacobian matrix, and \(\lambda\) is the Lagrange multiplier vector of size m. The equation of time integration can be defined as follows in the equation:  
\[\dot{q}(t+\Delta t)=\dot{q}(t)-\Delta tM^{-1}\Phi_{q}^{T}\lambda+\Delta tM^{-1 }F^{A}(q,t) \tag{7}\]\[q(t+\Delta t)=q(t)+\Delta t\dot{q}(t+\Delta t) \tag{8}\]  
The integration time-step is \(\Delta t\), so the constraint function of the next time is treated implicitly, as follows in Equation (9):  
\[\Phi(q(t+\Delta t),t+\Delta t)=0 \tag{9}\]  
A truncated, first-order Taylor series can be used to estimate Equation (9), and the new constraint function of next time can be written as follows:  
\[\Phi(q(t),t)+\Phi_{q}(q(t),t)(q(t+\Delta t)-q(t))+\Phi_{t}(q(t),t)\Delta t=0 \tag{10}\]  
To eliminate \(q(t+\Delta t)\), Equations (7) and (8) are substituted into Equation (9). Putting all of this together, we can solve the Lagrange multiplier by solving a system of linear equations, as follows in Equation (11):  
\[\Phi_{q}(q,t)M^{-1}\Phi_{q}^{T}\lambda=\frac{1}{\Delta t^{2}}\Phi(q,t)+\frac{1 }{\Delta t}\ \Phi_{t}(q,t)+\Phi_{q}(q,t)\bigg{(}\frac{1}{\Delta t}\dot{q}(t)+M^{-1}F^{A}(q,t)\bigg{)} \tag{11}\]  
Equation (11) is in the form of a linear system problem, and can be solved by a precise and effective method. The conjugate gradient is the most well-suited algorithm for this kind of system [24]. Equation (11) can be written as \(A\lambda=B\). The conjugate gradient method is given by the following equation. The prediction vector, \(\lambda\) initializes with 0. Equation (12) can written as \(r_{0}=p_{0}=B\). The routine to correct a prediction vector with \(i\) number of iterations can be written as follows:  
\[r_{0}=p_{0}=B-A\lambda \tag{12}\]  
\[\alpha_{i}=\frac{r_{i}^{T}r_{i}}{p_{i}^{T}Ap_{i}} \tag{13}\]  
\[\lambda_{i+1}=\lambda_{i}+\alpha_{i}p_{i} \tag{14}\]  
\[r_{i+1}=r_{i}-\alpha_{i}Ap_{i} \tag{15}\]  
\[\beta_{i}=\frac{r_{i+1}^{T}r_{i+1}}{r_{i}^{T}r_{i}} \tag{16}\]  
\[p_{i+1}=r_{i+1}+\beta_{i}p_{i} \tag{17}\]