II. (40 points) Given the function
$$
f(x)=3\left(\sin ^{3} x+\cos ^{3} x\right)+m(\sin x+\cos x)^{3}
$$

has a maximum value of 2 in $x \in\left[0, \frac{\pi}{2}\right]$. Find the value of the real number $m$.
$$
\begin{array}{l}
\sin ^{3} x+\cos ^{3} x \\
=(\sin x+\cos x)\left[(\sin x+\cos x)^{2}-3 \sin x \cdot \cos x\right] \\
=(\sin x+\cos x)\left\{(\sin x+\cos x)^{2}-\right. \\
\left.\quad \frac{3}{2}\left[(\sin x+\cos x)^{2}-1\right]\right\} .
\end{array}
$$

Let $t=\sin x+\cos x$
$$
=\sqrt{2} \sin \left(x+\frac{\pi}{4}\right) \in[1, \sqrt{2}] \text {. }
$$

Then $f(x)=3 t\left[t^{2}-\frac{3}{2}\left(t^{2}-1\right)\right]+m t^{3}$
$$
=\left(m-\frac{3}{2}\right) t^{3}+\frac{9}{2} t \Delta g(t) \text {. }
$$

By $g^{\prime}(t)=3\left[\left(m-\frac{3}{2}\right) t^{2}+\frac{3}{2}\right]$, there are two cases.
$$
\text { (1) } m \geqslant \frac{3}{2} \text {. }
$$

Since $g^{\prime}(t)>0$, we have
$$
\begin{array}{l}
g(t)_{\max }=g(\sqrt{2})=\sqrt{2}\left(2 m-3+\frac{9}{2}\right)=2 \\
\Rightarrow 2 m-3\sqrt{2}$, i.e., $\frac{3}{2}\left(1-\frac{\sqrt{2}}{2}\right)<m<\frac{3}{2}$,
$$
\begin{array}{l}
g(t)_{\max }=g(\sqrt{2})=\sqrt{2}\left(2 m+\frac{3}{2}\right)=2 \\
\Rightarrow m=-\frac{3}{4}+\frac{\sqrt{2}}{2},
\end{array}
$$

which is a contradiction.
In summary, $m=-1$.