Created
September 18, 2019 13:13
-
-
Save mafikes/ae9f23a677ecc3af0a3a759fc681b38d to your computer and use it in GitHub Desktop.
SimplePainting.cs
This file contains 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 UnityEngine; | |
using QuizCannersUtilities; | |
using PlaytimePainter; | |
using System.Collections.Generic; | |
using PlaytimePainter.Examples; | |
using System.Linq; | |
using PlayerAndEditorGUI; | |
public class SimpleGazeCursor : MonoBehaviour, IPEGI | |
{ | |
private Vector3 prevPos; | |
private bool firstMouseDwn = false; | |
// Painting | |
public BrushConfig brush = new BrushConfig(); | |
readonly List<ImageMeta> _texturesNeedUpdate = new List<ImageMeta>(); | |
// Use this for initialization | |
void Start () { | |
} | |
// Update is called once per frame | |
void Update () { | |
if (Input.GetMouseButton(0)) | |
{ | |
Paint(); | |
firstMouseDwn = true; | |
} | |
if(Input.GetMouseButtonUp(0)) | |
{ | |
Debug.Log("Mouse up"); | |
firstMouseDwn = false; | |
Paint(); | |
} | |
} | |
private void Paint() | |
{ | |
RaycastHit hit; | |
var ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
if (Physics.Raycast(ray, out hit)) | |
{ | |
var receivers = hit.transform.GetComponentsInParent<PaintingReceiver>(); | |
if (receivers.Length == 0) return; | |
int subMesh; | |
var receiver = receivers[0]; | |
// IF FEW SubMeshes | |
if (hit.collider.GetType() == typeof(MeshCollider)) | |
{ | |
subMesh = ((MeshCollider)hit.collider).sharedMesh.GetSubMeshNumber(hit.triangleIndex); | |
if (receivers.Length > 1) | |
{ | |
var mats = receiver.Renderer.materials; | |
var material = mats[subMesh % mats.Length]; | |
receiver = receivers.FirstOrDefault(r => r.Material == material); | |
} | |
} | |
else | |
subMesh = receiver.materialIndex; | |
// ACTUAL PAINTING | |
if (!receiver) return; | |
var tex = receiver.GetTexture(); | |
if (!tex) return; | |
var rendTex = (receiver.texture.GetType() == typeof(RenderTexture)) ? (RenderTexture)receiver.texture : null; | |
bool setMouseDwn = false; | |
if (firstMouseDwn == false) | |
{ | |
prevPos = hit.point; | |
setMouseDwn = true; | |
} | |
// WORLD SPACE BRUSH | |
if (rendTex) | |
{ | |
var st = new StrokeVector() | |
{ | |
posTo = hit.point, | |
posFrom = prevPos, | |
mouseDwn = setMouseDwn, | |
}; | |
switch (receiver.type) | |
{ | |
case PaintingReceiver.RendererType.Regular when receiver.meshFilter: | |
{ | |
var mat = receiver.Material; | |
if (mat && mat.IsAtlased()) | |
BrushTypeSphere.PaintAtlased(rendTex, receiver.gameObject, | |
receiver.originalMesh ? receiver.originalMesh : receiver.meshFilter.sharedMesh, brush, st, new List<int> { subMesh }, (int)mat.GetFloat(PainterDataAndConfig.ATLASED_TEXTURES)); | |
else | |
BrushTypeSphere.Paint(rendTex, receiver.gameObject, | |
receiver.originalMesh ? receiver.originalMesh : | |
receiver.meshFilter.sharedMesh, brush, st, | |
new List<int> { subMesh }); | |
break; | |
} | |
} | |
} | |
prevPos = hit.point; | |
} | |
} | |
private void LateUpdate() | |
{ | |
foreach (var t in _texturesNeedUpdate) | |
t.SetAndApply(); // True for Mipmaps. But best to disable mipmaps on textures or set this to false | |
_texturesNeedUpdate.Clear(); | |
} | |
#if !NO_PEGI | |
bool Documentation() | |
{ | |
"I can paint on objects with PaintingReceiver script and:".nl(); | |
"Mesh Collider + any Texture".nl(); | |
"Skinned Mesh + any Collider + Render Texture".nl(); | |
"Also its better to use textures without mipMaps".nl(); | |
"Render Texture Painting will fail if material has tiling or offset".nl(); | |
"Editing will be symmetrical if mesh is symmetrical".nl(); | |
"Brush type should be Sphere".nl(); | |
("I was going to make only one component painting method at first, but later it became clear that it's best to have this set up on both: receiving and shooting ends." + | |
"This doesn't need PlaytimePainter component but still needs Painting Receiver script").writeHint(); | |
return false; | |
} | |
public bool Inspect() | |
{ | |
var changed = false; | |
pegi.toggleDefaultInspector(); | |
pegi.fullWindowDocumentationClickOpen(Documentation); | |
//if ("Fire!".Click().nl()) | |
//Paint2(); | |
brush.Targets_PEGI().nl(ref changed); | |
brush.Mode_Type_PEGI().nl(ref changed); | |
brush.ColorSliders().nl(ref changed); | |
//if (brush.targetIsTex2D) | |
//{ | |
// "Script expects Render Texture terget".writeWarning(); | |
// pegi.nl(); | |
// if ("Switch to Render Texture".Click()) | |
// brush.targetIsTex2D = false; | |
//} | |
//else if (brush.GetBrushType(false).GetType() != typeof(BrushTypeSphere)) | |
//{ | |
// "This script works with Sphere Brush only".writeWarning(); | |
// if ("Switch to Sphere Brush".Click()) | |
// brush.SetBrushType(false, BrushTypeSphere.Inst); | |
//} | |
//if (!brush.PaintingRGB) | |
//"Enable RGB, disable A to use faster Brush Shader (if painting to RenderTexture).".writeHint(); | |
return changed; | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment