Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class VertexColorParticles : MonoBehaviour {
- public int debrisAmount = 10;
- public LayerMask layerMask;
- public Transform debrisParticleTransform;
- private ParticleSystem _debrisParticleSystem;
- void Awake()
- {
- _debrisParticleSystem = debrisParticleTransform.GetComponent<ParticleSystem>();
- }
- public void CreateDebrisParticles(RaycastHit hit)
- {
- debrisParticleTransform.position = hit.point;
- debrisParticleTransform.rotation = Quaternion.LookRotation(hit.normal);
- Color sampledColor = SampleColour(hit);
- _debrisParticleSystem.startColor = sampledColor;
- _debrisParticleSystem.Emit(debrisAmount);
- }
- Color SampleColour(RaycastHit hit)
- {
- Transform objectHit = hit.transform;
- Mesh _mesh = objectHit.GetComponent<MeshFilter>().mesh;
- if (_mesh != null)
- {
- int[] triangles = _mesh.triangles;
- Color[] hitColors = _mesh.colors;
- if (hitColors.Length != 0)
- {
- //Vertex indices
- int t1 = triangles[(hit.triangleIndex*3) + 0];
- int t2 = triangles[(hit.triangleIndex*3) + 1];
- int t3 = triangles[(hit.triangleIndex*3) + 2];
- //Colors
- Color c1 = hitColors[t1];
- Color c2 = hitColors[t2];
- Color c3 = hitColors[t3];
- Vector3 b = hit.barycentricCoordinate;
- var sampleColor = c1*b.x + c2*b.y + c3*b.z;
- return sampleColor;
- }
- }
- return Color.grey;
- }
- void Update()
- {
- if (Input.GetMouseButtonDown(0))
- {
- RaycastHit hit;
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask.value))
- {
- CreateDebrisParticles(hit);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment