Advertisement
JontePonte

mandelbulb master double

Dec 26th, 2024
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MandelbulbMain : MonoBehaviour
  6. {
  7. public float power;
  8. public int iterations;
  9. public float spread;
  10. public float scale;
  11. public ComputeShader mandelbulbShader;
  12. public Light mainLight;
  13.  
  14. RenderTexture renderTexture;
  15.  
  16. ComputeBuffer powerBuf;
  17. ComputeBuffer scaleBuf;
  18. ComputeBuffer spreadBuf;
  19.  
  20. void Start()
  21. {
  22. renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
  23. renderTexture.enableRandomWrite = true;
  24. renderTexture.Create();
  25. mandelbulbShader.SetTexture(0, "Result", renderTexture);
  26. mandelbulbShader.SetFloat("maxDst", 300);
  27. mandelbulbShader.SetFloat("minStepSize", 0.01f);
  28.  
  29. powerBuf = CreateDoubleBuffer(1, power);
  30. scaleBuf = CreateDoubleBuffer(1, scale);
  31. spreadBuf = CreateDoubleBuffer(1, spread);
  32. mandelbulbShader.SetBuffer(0, "power", powerBuf);
  33. mandelbulbShader.SetBuffer(0, "spread", spreadBuf);
  34. mandelbulbShader.SetBuffer(0, "scale", scaleBuf);
  35. }
  36.  
  37. void OnRenderImage(RenderTexture src, RenderTexture dest)
  38. {
  39. Vector3 lightDir = -mainLight.transform.forward;
  40. mandelbulbShader.SetTexture(0, "Source", src);
  41. mandelbulbShader.SetFloat("power", power);
  42. mandelbulbShader.SetFloat("spread", spread);
  43. mandelbulbShader.SetFloat("scale", scale);
  44. mandelbulbShader.SetInt("maxIterations", iterations);
  45. mandelbulbShader.SetFloats("lightDir", new float[] { lightDir.x, lightDir.y, lightDir.z });
  46. mandelbulbShader.SetMatrix("_CameraToWorld", Camera.main.cameraToWorldMatrix);
  47. mandelbulbShader.SetMatrix("_CameraInverseProjection", Camera.main.projectionMatrix.inverse);
  48. mandelbulbShader.Dispatch(0, renderTexture.width / 8, renderTexture.height / 8, 1);
  49.  
  50. Graphics.Blit(renderTexture, dest);
  51. }
  52.  
  53. void OnDestroy()
  54. {
  55. powerBuf.Dispose();
  56. scaleBuf.Dispose();
  57. spreadBuf
  58. }
  59.  
  60. ComputeBuffer CreateDoubleBuffer(int strideMult, double data)
  61. {
  62. var buf = new ComputeBuffer(1, sizeof(double) * strideMult);
  63. buf.SetData(new double[] { data });
  64. return buf;
  65.  
  66. }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement