Last active
February 11, 2021 18:14
-
-
Save andybak/e783e9e7df0dc768c4e7a8bc773de936 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Conway; | |
using UnityEngine; | |
using Random = System.Random; | |
namespace TiltBrush.AndyB | |
{ | |
public class FooTool : BaseTool | |
{ | |
//the parent of all of our tool's visual indicator objects | |
private GameObject m_toolDirectionIndicator; | |
//whether this tool should follow the controller or not | |
private bool m_LockToController; | |
//the controller that this tool is attached to | |
private Transform m_BrushController; | |
//Init is similar to Awake(), and should be used for initializing references and other setup code | |
public override void Init() | |
{ | |
base.Init(); | |
m_toolDirectionIndicator = transform.GetChild(0).gameObject; | |
} | |
//What to do when the tool is enabled or disabled | |
public override void EnableTool(bool bEnable) | |
{ | |
base.EnableTool(bEnable); | |
if (bEnable) | |
{ | |
m_LockToController = m_SketchSurface.IsInFreePaintMode(); | |
if (m_LockToController) | |
{ | |
m_BrushController = InputManager.m_Instance.GetController(InputManager.ControllerName.Brush); | |
} | |
EatInput(); | |
} | |
// Make sure our UI reticle isn't active. | |
SketchControlsScript.m_Instance.ForceShowUIReticle(false); | |
} | |
//What to do when the tool is hidden / shown | |
public override void HideTool(bool bHide) | |
{ | |
base.HideTool(bHide); | |
m_toolDirectionIndicator.SetActive(!bHide); | |
} | |
//What to do when all the tools run their update functions. Note that this is separate from Unity's Update script | |
//All input handling should be done here | |
override public void UpdateTool() | |
{ | |
base.UpdateTool(); | |
// Angle the pointer according to the user-defined pointer angle. | |
var rAttachPoint = App.Scene.ActiveCanvas.AsCanvas[InputManager.Brush.Geometry.ToolAttachPoint]; | |
Vector3 pos = rAttachPoint.translation; | |
Quaternion rot = rAttachPoint.rotation; | |
//keep the tool angle correct | |
m_toolDirectionIndicator.transform.localRotation = Quaternion.Euler(PointerManager.m_Instance.FreePaintPointerAngle, 0f, 0f); | |
if (InputManager.m_Instance.GetCommandDown(InputManager.SketchCommands.Activate)) | |
{ | |
var polyHelper = gameObject.GetComponent<PolyhydraBrushHelper>(); | |
polyHelper.Generate(); | |
var brush = PointerManager.m_Instance.MainPointer.CurrentBrush; | |
uint time = 0; | |
float pressure = 10; | |
foreach (var (face, faceIndex) in polyHelper.poly.Faces.WithIndex()) | |
{ | |
float lineLength = 0; | |
var controlPoints = new List<PointerManager.ControlPoint>(); | |
var edges = face.GetHalfedges(); | |
edges.Add(edges[0]); | |
foreach (var edge in edges) | |
{ | |
controlPoints.Add(new PointerManager.ControlPoint | |
{ | |
m_Pos = pos + edge.Vertex.Position, | |
m_Orient = Quaternion.LookRotation(face.Normal, Vector3.up), | |
m_Pressure = pressure, | |
m_TimestampMs = time++ | |
}); | |
lineLength += edge.Vector.magnitude; // TODO Does this need scaling? Should be in Canvas space | |
} | |
var stroke = new Stroke | |
{ | |
m_Type = Stroke.Type.NotCreated, | |
m_IntendedCanvas = App.Scene.ActiveCanvas, | |
m_BrushGuid = brush.m_Guid, | |
m_BrushScale = 1f, | |
m_BrushSize = PointerManager.m_Instance.MainPointer.BrushSize01, | |
// m_Color = PointerManager.m_Instance.MainPointer.GetCurrentColor(), | |
m_Color = polyHelper.GetFaceColor(faceIndex), | |
m_Seed = 0, | |
m_ControlPoints = controlPoints.ToArray(), | |
}; | |
stroke.m_ControlPointsToDrop = Enumerable.Repeat(false, stroke.m_ControlPoints.Length).ToArray(); | |
stroke.Uncreate(); | |
stroke.Recreate(null, App.Scene.ActiveCanvas); | |
SketchMemoryScript.m_Instance.MemorizeBatchedBrushStroke( | |
stroke.m_BatchSubset, | |
stroke.m_Color, | |
stroke.m_BrushGuid, | |
stroke.m_BrushSize, | |
stroke.m_BrushScale, | |
stroke.m_ControlPoints.ToList(), | |
stroke.m_Flags, | |
WidgetManager.m_Instance.ActiveStencil, | |
lineLength, | |
stroke.m_Seed | |
); | |
} | |
} | |
} | |
//The actual Unity update function, used to update transforms and perform per-frame operations | |
void Update() | |
{ | |
// If we're not locking to a controller, update our transforms now, instead of in LateUpdate. | |
if (!m_LockToController) | |
{ | |
UpdateTransformsFromControllers(); | |
} | |
} | |
override public void LateUpdateTool() | |
{ | |
base.LateUpdateTool(); | |
UpdateTransformsFromControllers(); | |
} | |
private void UpdateTransformsFromControllers() | |
{ | |
// Lock tool to camera controller. | |
if (m_LockToController) | |
{ | |
transform.position = m_BrushController.position; | |
transform.rotation = m_BrushController.rotation; | |
} | |
else | |
{ | |
transform.position = SketchSurfacePanel.m_Instance.transform.position; | |
transform.rotation = SketchSurfacePanel.m_Instance.transform.rotation; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment