SHARE
TWEET

Untitled

a guest Nov 14th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5.  
  6.  
  7. [ExecuteInEditMode]
  8. public class MetaballShaderPass : MonoBehaviour
  9. {
  10.     MeshRenderer meshRenderer;
  11.     public Camera mainCamera;
  12.     // Start is called before the first frame update
  13.     void Start()
  14.     {
  15.         meshRenderer = GetComponent<MeshRenderer>();
  16.     }
  17.  
  18.     // Update is called once per frame
  19.     void Update()
  20.     {
  21.        
  22.         GameObject[] gos = GameObject.FindGameObjectsWithTag("Metaball");
  23.  
  24.         List<Vector4> positions = gos.Select( (go) => {
  25.             return new Vector4(
  26.                 go.transform.position.x,
  27.                 go.transform.position.y,
  28.                 go.transform.position.z,
  29.                 0);
  30.         }).ToList();
  31.  
  32.         while(positions.Count < 256){
  33.             positions.Add(new Vector4());
  34.         }
  35.  
  36.         List<Vector4> colors = gos.Select( (go) => {
  37.             SpriteRenderer sr = go.GetComponent<SpriteRenderer>();
  38.             return new Vector4(
  39.                 sr.color.r,
  40.                 sr.color.g,
  41.                 sr.color.b,
  42.                 sr.color.a);
  43.         }).ToList();
  44.         while(colors.Count < 256){
  45.             colors.Add(new Vector4());
  46.         }
  47.  
  48.         meshRenderer.sharedMaterial.SetVectorArray("_Metaballs", positions);
  49.         meshRenderer.sharedMaterial.SetVectorArray("_MetaColors", colors);
  50.         meshRenderer.sharedMaterial.SetFloat("_MetaballsSize", positions.Count);
  51.  
  52.         meshRenderer.sharedMaterial.SetVector("viewport", new Vector2(mainCamera.orthographicSize * mainCamera.aspect * 2, mainCamera.orthographicSize * 2));
  53.         meshRenderer.sharedMaterial.SetVector("cameraPosition", mainCamera.transform.position);
  54.         meshRenderer.sharedMaterial.SetFloat("cameraZoom", mainCamera.transform.localScale.x);
  55.  
  56.     }
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top