title: Unity: plane defined by three points to quaternions
body: <p>For a study project, I am working on a 3D file in Unity. The aim is to place three markers, draw a plane through the three markers and make a section cut using that plane.
  I am able to define the markers, add a plane, and make a section cut. However, as the section cut requires the plane to be described in quaternions, I have to translate the values from the plane that I have somehow and I don't know how.</p>
  
  <p>The markers that define a plane (sample values):</p>
  
  <pre><code>Vector3 p1 = new Vector3(1.1f, 2.6f, 3.5f);
  Vector3 p2 = new Vector3(.4f,  4.5f, 2.5f);
  Vector3 p3 = new Vector3(6.1f, 6.9f, 1.4f);
  Plane cut  = new Plane(p1, p2, p3);
  </code></pre>
  
  <p>Currently, I define the plane using <a href="https://docs.unity3d.com/ScriptReference/Plane.html" rel="nofollow noreferrer"><code>new Plane</code></a> but I am willing to define it differently (e.g., using <a href="https://docs.unity3d.com/ScriptReference/PrimitiveType.Plane.html" rel="nofollow noreferrer"><code>PrimitiveType.Plane</code></a>). I only use this plane for this reference.
  Next, I add a section cut using an <a href="https://github.com/Dandarawy/Unity3DCrossSectionShader" rel="nofollow noreferrer">existing shader</a>. However, this shader requires a PlaneNormal and a PlanePosition with four values, see the <a href="https://i.stack.imgur.com/rMGhJ.png" rel="nofollow noreferrer">shader screenshot</a>.</p>
  
  <p>In short, my question is: how do I translate the three coordinates that I have to the eight values required for the shader?
  Alternatively, if another shader is better suited here, that is fine as well.</p>
  
  <h1>Solution</h1>
  
  <p>With the help of your answers, I was able to find a solution.
  Using three points, I define a plane and store its normal as a Vector4.</p>
  
  <pre><code>Plane finger = new Plane(p1, p2, p3);
  Vector3 normal = (finger.normal);
  Vector4 planeNormal = Vector4.zero;
  planeNormal.x = normal.x;
  planeNormal.y = normal.y;
  planeNormal.z = normal.z;
  digitNormals[digit-1] = planeNormal;
  </code></pre>
  
  <p>Likewise, I define the position as the average of three points. Therefore, I have written a function <code>AvgVector()</code> that takes the average value of the x, y, and z axis.</p>
  
  <pre><code>Vector3 position = AvgVector(p1, p2, p3);
  Vector4 planePosition = Vector4.zero;
  planePosition.x = position.x;
  planePosition.y = position.y;
  planePosition.z = position.z;
  Quaternion camNormal = Quaternion.LookRotation(normal);
  digitPlanes[digit-1] = planePosition;
  </code></pre>
  
  <p>I store the values of <code>camPos</code> and <code>camNormal</code> in a new GameObject. Later, I grab these values and apply them to my camera.</p>
  
  <pre><code>rend.material.SetVector("_PlaneNormal", digitNormals[dig-1]);
  rend.material.SetVector("_PlanePosition", digitPlanes[dig-1]);
  SectionCamera.transform.position = section.transform.position;
  SectionCamera.transform.LookAt(clone.transform);
  RelocateMarker(c, 3);
  </code></pre>
  
  <p>I know that I use quite some variables and that might limit the usefulness of these snippits. However, the entire code will soon be online at <a href="https://github.com/TimJohDij/marker-maker" rel="nofollow noreferrer">https://github.com/TimJohDij/marker-maker</a>.</p>