using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.VFX; [ExecuteAlways] public class computeTest : MonoBehaviour { public bool executeInEditMode = false; public bool updateTexture = false; public bool createTexture = false; public int texResolution = 256; public Material testMaterial; int kernel; public ComputeShader debugCompute; public RenderTexture debugTexture; public string materialProperty = "_Texture3D"; [Tooltip("Visual effect whose property to set with the output SDF texture")] public VisualEffect vfxOutput; public string vfxProperty = "Texture3D"; #if UNITY_EDITOR private void OnValidate() { Awake(); } #endif private void Initialize() { //kernel = debugCompute.FindKernel("CSMain"); kernel = debugCompute.FindKernel("DebugSphere"); } // Start is called before the first frame update void Awake() { Initialize(); } void CreateTexture() { debugTexture = new RenderTexture(texResolution, texResolution, // 0, RenderTextureFormat.ARGBHalf); 0, RenderTextureFormat.RFloat); debugTexture.dimension = UnityEngine.Rendering.TextureDimension.Tex3D; debugTexture.enableRandomWrite = true; debugTexture.useMipMap = false; debugTexture.anisoLevel = 1; debugTexture.volumeDepth = texResolution; debugTexture.Create(); } void UpdateTexture() { int texSize = debugTexture.width; debugCompute.SetTexture(kernel, "Result", debugTexture); int dispatchCubeSize = debugTexture.width; debugCompute.SetInt("dispatchCubeSide", dispatchCubeSize); debugCompute.Dispatch(kernel, numGroups(debugTexture.width, 8), numGroups(debugTexture.height, 8), numGroups(debugTexture.volumeDepth, 8)); if (testMaterial) { if (testMaterial.HasProperty(materialProperty)) { testMaterial.SetTexture(materialProperty, debugTexture); } else { Debug.Log("Test material doesn't have propoerty "+materialProperty); } } if (vfxOutput) { if (!vfxOutput.HasTexture(vfxProperty)) { Debug.LogError(string.Format("Vfx Output doesn't have property {0}", vfxProperty)); } else { vfxOutput.SetTexture(vfxProperty, debugTexture); } } } // number of groups for a dispatch with totalThreads and groups of size // numThreadsForDim int numGroups(int totalThreads, int groupSize) { // return (totalThreads + (groupSize - 1)) / groupSize; return (totalThreads) / groupSize; } // Update is called once per frame void Update() { if (createTexture) { createTexture = false; CreateTexture(); } if (updateTexture) { updateTexture = false; UpdateTexture(); } } }