using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Rendering.Universal;
namespace GrassCutter
{
///
/// The story takes, `s_`: the trailer told the way the game opens. The real opening film through the player's eyes with
/// the phone and the Director on it, and at the same moments a close-up of him in his studio, filling the frame; his
/// assistant in the ear; a twister and a saucer as they come in the game; and his other calls, close up, for the cut.
/// Every sound the game asks for while a take rolls goes into the take's sounds.txt (frame, name, volume, pitch, metres
/// from the camera), so the edit can lay the voices and effects exactly where they fell.
///
public partial class Trailer
{
const int CW = 1920, CH = 1080;
/// A second camera rolling with a take, into a folder of its own.
class Also
{
public string Name;
public Camera Cam;
public RenderTexture Rt;
public Renderer Hide, Show;
}
readonly List _also = new List();
Texture2D _closeRead;
/// The take keeps only its second cameras.
bool _onlyAlso;
StreamWriter _sounds;
int _frameNow;
string _takeName;
/// A landscape close-up of a booth's sitter: centred on `centreY` (metres up the sitter), `height` metres tall,
/// in front of the wide mission control that the upright booth picture is too narrow for.
Also CloseUp(string name, Booth b, float centreY, float height)
{
if (b == null || !b.Root) return null;
const float fov = 20f;
float dist = height * 0.5f / Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);
var go = new GameObject("CloseUp " + name);
go.transform.SetParent(b.Root, false);
go.transform.localPosition = new Vector3(0f, centreY, dist);
go.transform.localRotation = Quaternion.LookRotation(Vector3.back, Vector3.up);
var cam = go.AddComponent();
cam.fieldOfView = fov;
cam.nearClipPlane = 0.2f;
cam.farClipPlane = dist + 10f;
cam.cullingMask = 1 << Studio.Layer;
cam.clearFlags = CameraClearFlags.SolidColor;
cam.backgroundColor = new Color(0.1f, 0.16f, 0.3f);
cam.enabled = false;
var data = go.AddComponent();
data.renderShadows = false;
data.renderPostProcessing = false;
var rt = new RenderTexture(CW, CH, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB) { antiAliasing = 8, name = "CloseUp " + name };
rt.Create();
cam.targetTexture = rt;
// the wide wall, further back than the booth's own, shown only to this camera
var wall = GameObject.CreatePrimitive(PrimitiveType.Quad);
Destroy(wall.GetComponent());
wall.name = "WideBackdrop";
wall.layer = Studio.Layer;
wall.transform.SetParent(b.Root, false);
const float back = 2.6f;
float wallH = 2f * (dist + back) * Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad) * 1.06f;
var bg = AssetBank.I.Tex("T_Dir_BgWide");
float aspect = bg ? bg.width / (float)bg.height : 16f / 9f;
float qh = Mathf.Max(wallH, wallH * (CW / (float)CH) / aspect);
// the desks at the sitter's waist, as in the upright picture
wall.transform.localPosition = new Vector3(0f, centreY + (0.97f - centreY) * 0.6f, -back);
wall.transform.localRotation = Quaternion.LookRotation(Vector3.back, Vector3.up);
wall.transform.localScale = new Vector3(qh * aspect, qh, 1f);
var wm = new Material(AssetBank.I.Mat("M_Studio"));
wm.SetFloat("_Lit", 0f);
if (bg) wm.SetTexture("_BaseMap", bg);
wm.SetColor("_BaseColor", new Color(0.82f, 0.86f, 0.95f));
var wr = wall.GetComponent();
wr.sharedMaterial = wm;
wr.enabled = false;
var old = b.Root.Find("Backdrop");
_props.Add(go);
_props.Add(wall);
return new Also { Name = name, Cam = cam, Rt = rt, Hide = old ? old.GetComponent() : null, Show = wr };
}
void RollAlso(int f)
{
if (_also.Count == 0) return;
if (!_closeRead) _closeRead = new Texture2D(CW, CH, TextureFormat.RGB24, false, false);
foreach (var a in _also)
{
if (a == null || !a.Cam) continue;
if (a.Hide) a.Hide.enabled = false;
if (a.Show) a.Show.enabled = true;
a.Cam.Render();
if (a.Hide) a.Hide.enabled = true;
if (a.Show) a.Show.enabled = false;
var was = RenderTexture.active;
RenderTexture.active = a.Rt;
_closeRead.ReadPixels(new Rect(0, 0, CW, CH), 0, 0, false);
RenderTexture.active = was;
// each take's close-ups in a folder of their own: take@camera
var dir = Path.Combine(Folder, _takeName + "@" + a.Name);
Directory.CreateDirectory(dir);
File.WriteAllBytes(Path.Combine(dir, f.ToString("00000") + ".jpg"), _closeRead.EncodeToJPG(95));
}
}
void Listen(string take)
{
Directory.CreateDirectory(Path.Combine(Folder, take));
_sounds = new StreamWriter(Path.Combine(Folder, take, "sounds.txt"));
Sfx.Heard = (name, vol, pitch, pos) =>
{
float d = pos.HasValue && _cam ? Vector3.Distance(pos.Value, _cam.transform.position) : 0f;
_sounds?.WriteLine($"{_frameNow}\t{name}\t{vol:0.###}\t{pitch:0.###}\t{d:0.#}");
};
}
void StopListening()
{
Sfx.Heard = null;
_sounds?.Close();
_sounds = null;
}
void ClearAlso()
{
_also.Clear();
_onlyAlso = false;
}
static Booth BossBooth => Studio.Get("SM_Dir_Boss", 540, 1184, 0.97f, 0.72f);
static Booth AsstBooth => Studio.Get("SM_Dir_Assistant", 600, 760, 1.04f, 0.62f);
IEnumerator Story()
{
if (!Wanted("s_")) yield break;
Sky(0.45f, false);
// the meadow furry far out, for the shots from above
GrassField.FurReach = 3.5f;
Eyes(true);
var boss = CloseUp("s_boss", BossBooth, 1.13f, 1.0f);
var asst = CloseUp("s_asst", AsstBooth, 1.0f, 0.8f);
// the opening film, as a new game shows it, and the Director close up beside it
if (Wanted("s_intro"))
{
// a new crew's phone: no money yet
Cheats.Run("money set 0");
IntroCinematic.NoSkipHint = true;
IntroCinematic.Again();
float t0 = Time.time;
while (!IntroCinematic.Running && Time.time - t0 < 5f) yield return null;
_also.Add(boss);
yield return Take("s_intro", 57f, 60, null, 0f, null, true);
ClearAlso();
// her hello as the film lets go, and her close up
_also.Add(asst);
yield return Take("s_after", 16f, 60, null, 0f, null, true);
ClearAlso();
}
Assistant.Hush();
Cheats.Run("money set 90000000");
// no beginner's hints talking over the rest
Cheats.Run("ship 60");
yield return new WaitForSeconds(0.5f);
// a twister across the meadow under a storm, coming at the lens
if (Wanted("s_storm"))
{
Eyes(false);
Cheats.Run("weather storm 6");
Sky(0.3f, false);
var cam = On(60f, 30f, 1.5f);
var from = On(64f, 78f);
Ts.ServerTwister(from);
Ts.TwisterFrom.Value = from;
Ts.TwisterDir.Value = Vector3.ProjectOnPlane(cam - from, Planet.Up(from)).normalized;
yield return Take("s_storm", 9f, 60, t => new Lens(cam, Vector3.Lerp(Ts.TwisterPos(Ts.Now), On(62f, 60f, 1f), 0.3f) + Planet.Up(cam) * 6f, 58f), 3f);
Ts.TwisterAt.Value = 0d;
Assistant.Hush();
Eyes(true);
}
// through the eyes: the warning in the ear, the funnel coming, and up you go
if (Wanted("s_thrown"))
{
Assistant.Hush();
Assistant.Forget();
Me.Tools.Equip(ToolId.Scythe);
var from = On(100f, 70f);
Ts.ServerTwister(from);
Ts.TwisterFrom.Value = from;
var meet = On(100f, 40f);
Ts.TwisterDir.Value = Vector3.ProjectOnPlane(meet - from, Planet.Up(from)).normalized;
// stand where the funnel will be in 7.5 s
var spot = Ts.TwisterPos(Ts.TwisterAt.Value + 7.5d);
Me.Teleport(spot + Planet.Up(spot) * 0.2f);
yield return null;
bool flung = false;
var p0 = Me.transform.position;
_also.Add(asst);
yield return Take("s_thrown", 15f, 60, null, 0.2f, k =>
{
if (!flung && (Me.transform.position - p0).magnitude > 2f) flung = true;
if (!flung) Me.LookAt(Ts.TwisterPos(Ts.Now) + Planet.Up(p0) * 3.5f);
}, true);
ClearAlso();
Ts.TwisterAt.Value = 0d;
Cheats.Run("weather clear 600");
yield return new WaitForSeconds(4f);
Assistant.Hush();
}
// the saucer: from the grass as it comes down, from above as it draws
if (Wanted("s_ufo"))
{
Eyes(false);
Sky(0.25f, true);
var spot = On(-60f, 70f);
Ts.ServerUfo(spot);
double at = Ts.UfoAt.Value;
var low = On(-56f, 42f, 1.1f);
yield return Take("s_ufo", 11f, 60, t => new Lens(low, Vector3.Lerp(On(-60f, 70f, 30f), On(-60f, 70f, 6f), Ease(Mathf.Clamp01(t * 1.2f))), 52f), 0f);
while (Ts.Now - at < 11d) yield return null;
yield return Take("s_ufo_top", 10f, 60, t => new Lens(On(-60f + 16f - t * 3f, 70f - 14f, 20f + t * 3f), On(-60f, 70f, 2f), 52f), 0f);
while (Ts.UfoOn) yield return null;
Assistant.Hush();
Eyes(true);
}
// the Director rings about it while you stand and watch it come down
if (Wanted("s_ufocall"))
{
Me.Tools.Equip(ToolId.Scythe);
yield return Stand(-120f, 40f, 8f);
var spot = On(-120f, 72f);
Ts.ServerUfo(spot);
Assistant.Hush();
yield return new WaitForSeconds(0.2f);
Assistant.Hush();
DirectorCall.Quiet = false;
DirectorCall.Call("dir_ufo");
DirectorCall.Quiet = true;
_also.Add(boss);
yield return Take("s_ufocall", 17f, 60, null, 0f, k => Me.LookAt(Vector3.Lerp(On(-120f, 72f, 26f), On(-120f, 72f, 6f), Ease(Mathf.Clamp01(k * 1.6f)))), true);
ClearAlso();
while (Ts.UfoOn) yield return null;
Assistant.Hush();
}
// his other calls, close up only
foreach (var line in new[] { "dir_belts", "dir_combine", "dir_m1", "dir_m100" })
{
string take = "s_" + line;
if (!Wanted(take)) continue;
var c = CloseUp("s_boss", BossBooth, 1.13f, 1.0f);
_also.Add(c);
_onlyAlso = true;
float length = AssetBank.I.Clip(line) ? AssetBank.I.Clip(line).length : 10f;
StartCoroutine(DirectorCall.Speak(line, true));
yield return Take(take, length + 1.6f, 60, null, 0f, null, false);
ClearAlso();
while (DirectorCall.Busy) yield return null;
}
Eyes(false);
Sky(0.42f, false);
GrassField.FurReach = 1f;
}
}
}