Guest User

Point Cloud Script

a guest
Sep 3rd, 2020
3,366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.VFX;
  5.  
  6. public class PointCloudRenderer : MonoBehaviour {
  7.     Texture2D texColor;
  8.     Texture2D texPosScale;
  9.     VisualEffect vfx;
  10.     uint resolution = 2048;
  11.  
  12.     public float particleSize = 0.1f;
  13.     bool toUpdate = false;
  14.     uint particleCount = 0;
  15.  
  16.     private void Start() {
  17.         vfx = GetComponent<VisualEffect>();
  18.     }
  19.  
  20.     private void Update() {
  21.         if (toUpdate) {
  22.             toUpdate = false;
  23.  
  24.             vfx.Reinit();
  25.             vfx.SetUInt(Shader.PropertyToID("ParticleCount"), particleCount);
  26.             vfx.SetTexture(Shader.PropertyToID("TexColor"), texColor);
  27.             vfx.SetTexture(Shader.PropertyToID("TexPosScale"), texPosScale);
  28.             vfx.SetUInt(Shader.PropertyToID("Resolution"), resolution);
  29.         }
  30.     }
  31.  
  32.     public void SetParticles(Vector3[] positions, Color[] colors) {
  33.         texColor = new Texture2D(positions.Length > (int)resolution ? (int)resolution : positions.Length, Mathf.Clamp(positions.Length / (int)resolution, 1, (int)resolution), TextureFormat.RGBAFloat, false);
  34.         texPosScale = new Texture2D(positions.Length > (int)resolution ? (int)resolution : positions.Length, Mathf.Clamp(positions.Length / (int)resolution, 1, (int)resolution), TextureFormat.RGBAFloat, false);
  35.         int texWidth = texColor.width;
  36.         int texHeight = texColor.height;
  37.  
  38.         for (int y = 0; y < texHeight; y++) {
  39.             for (int x = 0; x < texWidth; x++) {
  40.                 int index = x + y * texWidth;
  41.                 texColor.SetPixel(x, y, colors[index]);
  42.                 var data = new Color(positions[index].x, positions[index].y, positions[index].z, particleSize);
  43.                 texPosScale.SetPixel(x, y, data);
  44.             }
  45.         }
  46.  
  47.         texColor.Apply();
  48.         texPosScale.Apply();
  49.         particleCount = (uint)positions.Length;
  50.         toUpdate = true;
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment