Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MandelbulbMain : MonoBehaviour
- {
- public float power;
- public int iterations;
- public float spread;
- public float scale;
- public ComputeShader mandelbulbShader;
- public Light mainLight;
- RenderTexture renderTexture;
- ComputeBuffer powerBuf;
- ComputeBuffer scaleBuf;
- ComputeBuffer spreadBuf;
- void Start()
- {
- renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
- renderTexture.enableRandomWrite = true;
- renderTexture.Create();
- mandelbulbShader.SetTexture(0, "Result", renderTexture);
- mandelbulbShader.SetFloat("maxDst", 300);
- mandelbulbShader.SetFloat("minStepSize", 0.01f);
- powerBuf = CreateDoubleBuffer(1, power);
- scaleBuf = CreateDoubleBuffer(1, scale);
- spreadBuf = CreateDoubleBuffer(1, spread);
- mandelbulbShader.SetBuffer(0, "power", powerBuf);
- mandelbulbShader.SetBuffer(0, "spread", spreadBuf);
- mandelbulbShader.SetBuffer(0, "scale", scaleBuf);
- }
- void OnRenderImage(RenderTexture src, RenderTexture dest)
- {
- Vector3 lightDir = -mainLight.transform.forward;
- mandelbulbShader.SetTexture(0, "Source", src);
- mandelbulbShader.SetFloat("power", power);
- mandelbulbShader.SetFloat("spread", spread);
- mandelbulbShader.SetFloat("scale", scale);
- mandelbulbShader.SetInt("maxIterations", iterations);
- mandelbulbShader.SetFloats("lightDir", new float[] { lightDir.x, lightDir.y, lightDir.z });
- mandelbulbShader.SetMatrix("_CameraToWorld", Camera.main.cameraToWorldMatrix);
- mandelbulbShader.SetMatrix("_CameraInverseProjection", Camera.main.projectionMatrix.inverse);
- mandelbulbShader.Dispatch(0, renderTexture.width / 8, renderTexture.height / 8, 1);
- Graphics.Blit(renderTexture, dest);
- }
- void OnDestroy()
- {
- powerBuf.Dispose();
- scaleBuf.Dispose();
- spreadBuf
- }
- ComputeBuffer CreateDoubleBuffer(int strideMult, double data)
- {
- var buf = new ComputeBuffer(1, sizeof(double) * strideMult);
- buf.SetData(new double[] { data });
- return buf;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement