Drawing Board
A studio with a whiteboard everyone can draw on, a pen, a palette of colours, and a Snapshot button that prints the
board into picture frames on the wall with Graphics.Blit.
Get it from the StageTime-Worlds-Examples repository: copy the Worlds/DrawingBoard folder into a Unity
project that has the StageTime World SDK, and open its scene.
| Piece | Built with |
|---|---|
| Board | An ST Drawing Surface on an empty GameObject covering the board's 2 × 1.2 m face; it shows the drawing on the board model's board material (materialIndex) |
| Pen | An ST Pickup with a small trigger on its tip |
| Colours | Swatch buttons: touch one with the pen's tip, or click it while holding the pen |
| Prints | Three Render Textures filled by Graphics.Blit through a print-look material |
The pen
Only the holder's device draws; the surface sends the lines to everyone and to players who join later. The colour is
a [Synced] field, so everyone sees the nib change.
using StageTime.SDK;
using UnityEngine;
/// <summary>
/// A pen (an ST Pickup) that draws on ST Drawing Surfaces when its tip touches them. Only the holder's device
/// draws; the surfaces send the lines to everyone. The colour can be changed by touching a ColourSwatch with its tip: the
/// colour is synced, so everyone sees the tip change.
/// </summary>
public class DrawingPen : StageTimeBehaviour
{
[Tooltip("The surfaces this pen draws on.")]
public STDrawingSurface[] surfaces;
[Tooltip("The tip: lines are drawn where it is.")]
public Transform tip;
[Tooltip("The nib's material, coloured with the current colour. Give each pen its own.")]
public Material tipMaterial;
[Tooltip("Line width in pixels.")]
public float width = 6f;
[Tooltip("How close the tip must be to the surface to draw, in metres.")]
public float reach = 0.015f;
public Color[] palette = { Color.black };
[Synced] private int colourIndex;
private bool held;
private int lastSurface = -1;
private Vector2 lastPoint;
private void Start()
{
ShowColour();
}
public override void OnPickup()
{
held = true;
lastSurface = -1;
}
public override void OnDrop()
{
held = false;
lastSurface = -1;
}
/// <summary>A ColourSwatch trigger saw the tip: only the holder's device acts (every device sees the pen move).</summary>
public void Dip(int index)
{
if (held) SetColour(index);
}
/// <summary>Takes palette colour <paramref name="index"/> for everyone.</summary>
public void SetColour(int index)
{
if (index < 0 || index >= palette.Length || index == colourIndex) return;
Networking.SetOwner(Networking.LocalPlayer, gameObject);
colourIndex = index;
ShowColour();
}
public override void OnDeserialization()
{
ShowColour();
}
private void Update()
{
if (!held) return;
Vector3 position = tip.position;
for (int i = 0; i < surfaces.Length; i++)
{
Vector3 p = surfaces[i].WorldToSurface(position);
if (p.z > reach || p.z < -0.05f || p.x < 0f || p.x > 1f || p.y < 0f || p.y > 1f) continue;
var point = new Vector2(p.x, p.y);
if (lastSurface != i)
{
surfaces[i].DrawPoint(point, width, palette[colourIndex]);
lastSurface = i;
lastPoint = point;
}
else if ((point - lastPoint).sqrMagnitude > 0.000004f) // about 2 mm on a 1 m board
{
surfaces[i].DrawLine(lastPoint, point, width, palette[colourIndex]);
lastPoint = point;
}
return;
}
lastSurface = -1;
}
private void ShowColour()
{
if (tipMaterial != null) tipMaterial.color = palette[colourIndex];
}
}
using StageTime.SDK;
using UnityEngine;
/// <summary>
/// A colour button for a pen: touch it with the pen's tip (the tip's trigger enters it) or click it while holding the
/// pen, and the pen takes this swatch's colour (its number in the pen's palette).
/// </summary>
public class ColourSwatch : StageTimeBehaviour
{
public DrawingPen pen;
[Tooltip("This swatch's colour: its number in the pen's palette.")]
public int colourIndex;
[Tooltip("The pen tip's collider (a small trigger on the tip).")]
public Collider penTip;
private void OnTriggerEnter(Collider other)
{
if (other == penTip) pen.Dip(colourIndex);
}
public override void Interact()
{
pen.SetColour(colourIndex);
}
}
Clear and Snapshot
Clear calls the surface's Clear(). Snapshot copies the board into the next picture frame's Render Texture. The
snapshot count is a [Synced] field, so every device makes the same print when it changes.
Picture frames are local textures, so a player who joins later sees only the newest snapshot.
using StageTime.SDK;
using UnityEngine;
/// <summary>
/// Buttons beside a board: Clear wipes it for everyone, and Snapshot copies it into the next picture frame on the wall
/// with Graphics.Blit through a material (a warm print look). The snapshot count is synced: every device copies the
/// same board into the same frame when it changes. Picture frames are local textures, so a player who joins later sees
/// only the newest snapshot (their copy of the board, taken when they join).
/// </summary>
public class BoardControls : StageTimeBehaviour
{
public STDrawingSurface board;
[Tooltip("Render Textures shown in the picture frames (512 x 256 or so), filled in turn.")]
public RenderTexture[] frames;
[Tooltip("Optional: the material the copy goes through (a Blit shader reading _MainTex).")]
public Material printMaterial;
[Synced] private int snapshots;
private int taken;
/// <summary>Button: clear the board for everyone.</summary>
public void ClearBoard()
{
board.Clear();
}
/// <summary>Button: snapshot for everyone.</summary>
public void Snapshot()
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
snapshots++;
TakeSnapshot();
}
public override void OnDeserialization()
{
TakeSnapshot();
}
private void TakeSnapshot()
{
if (snapshots == taken || frames.Length == 0 || board.texture == null) return;
taken = snapshots;
RenderTexture frame = frames[(snapshots - 1) % frames.Length];
if (printMaterial != null) Graphics.Blit(board.texture, frame, printMaterial);
else Graphics.Blit(board.texture, frame);
}
}