added TNTC paint system shader

This commit is contained in:
André Cardoso
2021-02-22 19:20:18 +01:00
parent c34062c024
commit 687e053e5f
49 changed files with 17229 additions and 840 deletions

View File

@@ -0,0 +1,17 @@
using UnityEngine;
public class CollisionPainter : MonoBehaviour{
public Color paintColor;
public float radius = 1;
public float strength = 1;
public float hardness = 1;
private void OnCollisionStay(Collision other) {
Paintable p = other.collider.GetComponent<Paintable>();
if(p != null){
Vector3 pos = other.contacts[0].point;
PaintManager.instance.paint(p, pos, radius, hardness, strength, paintColor);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2be8a48416f841d4fac024c4f1994147
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,36 @@
using UnityEngine;
public class MousePainter : MonoBehaviour{
public Camera cam;
[Space]
public bool mouseSingleClick;
[Space]
public Color paintColor;
public float radius = 1;
public float strength = 1;
public float hardness = 1;
void Update(){
bool click;
click = mouseSingleClick ? Input.GetMouseButtonDown(0) : Input.GetMouseButton(0);
if (click){
Vector3 position = Input.mousePosition;
Ray ray = cam.ScreenPointToRay(position);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100.0f)){
Debug.DrawRay(ray.origin, hit.point - ray.origin, Color.red);
transform.position = hit.point;
Paintable p = hit.collider.GetComponent<Paintable>();
if(p != null){
PaintManager.instance.paint(p, hit.point, radius, hardness, strength, paintColor);
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1abde833d7186b14285c3eb5913989ea
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,56 @@
using UnityEngine;
using UnityEngine.Rendering;
public class PaintManager : Singleton<PaintManager>{
public Shader texturePaint;
public Shader extendIslands;
int positionID = Shader.PropertyToID("_PainterPosition");
int hardnessID = Shader.PropertyToID("_Hardness");
int strengthID = Shader.PropertyToID("_Strength");
int radiusID = Shader.PropertyToID("_Radius");
int blendOpID = Shader.PropertyToID("_BlendOp");
int colorID = Shader.PropertyToID("_PainterColor");
int textureID = Shader.PropertyToID("_MainTex");
Material paintMaterial;
Material extendMaterial;
CommandBuffer command;
void Start(){
paintMaterial = new Material(texturePaint);
extendMaterial = new Material(extendIslands);
command = new CommandBuffer();
command.name = "CommmandBuffer - " + gameObject.name;
}
public void paint(Paintable paintable, Vector3 pos, float radius = 1f, float hardness = .5f, float strength = .5f, Color? color = null){
RenderTexture mask = paintable.getMask();
RenderTexture extend = paintable.getExtend();
RenderTexture support = paintable.getSupport();
Renderer rend = paintable.getRenderer();
paintMaterial.SetVector(positionID, pos);
paintMaterial.SetFloat(hardnessID, hardness);
paintMaterial.SetFloat(strengthID, strength);
paintMaterial.SetFloat(radiusID, radius);
paintMaterial.SetTexture(textureID, support);
paintMaterial.SetColor(colorID, color ?? Color.red);
command.SetRenderTarget(mask);
command.DrawRenderer(rend, paintMaterial, 0);
command.SetRenderTarget(support);
command.Blit(mask, support);
command.SetRenderTarget(extend);
command.Blit(mask, extend, extendMaterial);
Graphics.ExecuteCommandBuffer(command);
command.Clear();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 97807551a49343d4a84e6d24e81cff4f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,45 @@
using UnityEngine;
using UnityEngine.Rendering;
public class Paintable : MonoBehaviour {
const int TEXTURE_SIZE = 1024;
RenderTexture extendIslandsRenderTexture;
RenderTexture maskRenderTexture;
RenderTexture supportTexture;
Renderer rend;
int maskTextureID = Shader.PropertyToID("_MaskTexture");
public RenderTexture getMask() => maskRenderTexture;
public RenderTexture getExtend() => extendIslandsRenderTexture;
public RenderTexture getSupport() => supportTexture;
public Renderer getRenderer() => rend;
void Start() {
maskRenderTexture = new RenderTexture(TEXTURE_SIZE, TEXTURE_SIZE, 0);
extendIslandsRenderTexture = new RenderTexture(TEXTURE_SIZE, TEXTURE_SIZE, 0);
extendIslandsRenderTexture.filterMode = FilterMode.Bilinear;
supportTexture = new RenderTexture(TEXTURE_SIZE, TEXTURE_SIZE, 0);
supportTexture.filterMode = FilterMode.Bilinear;
rend = GetComponent<Renderer>();
rend.material.SetTexture(maskTextureID, extendIslandsRenderTexture);
CommandBuffer command = new CommandBuffer();
command.name = "CommandBuffer - " + gameObject.name;
command.SetRenderTarget(maskRenderTexture);
command.SetRenderTarget(extendIslandsRenderTexture);
command.SetRenderTarget(supportTexture);
Graphics.ExecuteCommandBuffer(command);
}
void OnDisable(){
maskRenderTexture.Release();
extendIslandsRenderTexture.Release();
supportTexture.Release();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0c3ea44eef20b0b4396ebe666c9a6eeb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,36 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticlesController: MonoBehaviour{
public Color paintColor;
public float minRadius = 0.05f;
public float maxRadius = 0.2f;
public float strength = 1;
public float hardness = 1;
[Space]
ParticleSystem part;
List<ParticleCollisionEvent> collisionEvents;
void Start(){
part = GetComponent<ParticleSystem>();
collisionEvents = new List<ParticleCollisionEvent>();
var pr = part.GetComponent<ParticleSystemRenderer>();
Color c = new Color(pr.material.color.r, pr.material.color.g, pr.material.color.b, .8f);
paintColor = c;
}
void OnParticleCollision(GameObject other) {
int numCollisionEvents = part.GetCollisionEvents(other, collisionEvents);
Paintable p = other.GetComponent<Paintable>();
if(p != null){
for (int i = 0; i< numCollisionEvents; i++){
Vector3 pos = collisionEvents[i].intersection;
float radius = Random.Range(minRadius, maxRadius);
PaintManager.instance.paint(p, pos, radius, hardness, strength, paintColor);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a81a5c75c78d6364ea8c1a6f121e3a6c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 607eab570f0165c429ab607f92c6f610
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,52 @@
using UnityEngine;
public class Singleton<T> : MonoBehaviour where T: MonoBehaviour{
public static bool verbose = false;
public static bool keepAlive = true;
private static T _instance = null;
public static T instance {
get {
if(_instance == null){
_instance = GameObject.FindObjectOfType<T>();
if(_instance == null){
var singletonObj = new GameObject();
singletonObj.name = typeof(T).ToString();
_instance = singletonObj.AddComponent<T>();
}
}
return _instance;
}
}
static public bool isInstanceAlive{
get { return _instance != null; }
}
public virtual void Awake(){
if (_instance != null){
if(verbose)
Debug.Log("SingleAccessPoint, Destroy duplicate instance " + name + " of " + instance.name);
Destroy(gameObject);
return;
}
_instance = GetComponent<T>();
if(keepAlive){
DontDestroyOnLoad(gameObject);
}
if (_instance == null){
if(verbose)
Debug.LogError("SingleAccessPoint<" + typeof(T).Name + "> Instance null in Awake");
return;
}
if(verbose)
Debug.Log("SingleAccessPoint instance found " + instance.GetType().Name);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a81968a323aba6a44b0bb536a20ba0ce
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: