У нас имеется: C# скрипт для настройки параметров и передачи их в шейдер…
- User
У нас имеется:
C# скрипт для настройки параметров и передачи их в шейдер RayTracingMaster.cs using System.Collections.Generic; using UnityEngine;
public class RayTracingMaster : MonoBehaviour { public ComputeShader rayTracingShader; public Texture skyboxTexture;
private RenderTexture target; private Camera _camera; private List spheres = new List(); private ComputeBuffer sphereBuffer;
struct Sphere { public Vector3 origin; public float radius; public Vector3 color; public float albedo; }
private void Awake() { _camera = GetComponent(); SetupScene(); }
private void OnRenderImage(RenderTexture source, RenderTexture destination) { InitRenderTexture(); SetComputeShaderParameters(); int threadGroupsX = Mathf.CeilToInt(Screen.width / 8.0f); int threadGroupsY = Mathf.CeilToInt(Screen.height / 8.0f); rayTracingShader.Dispatch(0, threadGroupsX, threadGroupsY, 1); Graphics.Blit(target, destination); }
private void SetupScene() { spheres = new List { new Sphere { origin = new Vector3(0, 0, 0), radius = 1, color = new Vector3(1, 0, 0), albedo = 0.9f }, new Sphere { origin = new Vector3(2, 0, 0), radius = 1, color = new Vector3(0, 1, 0), albedo = 0.8f }, new Sphere { origin = new Vector3(-2, 0, 0), radius = 1, color = new Vector3(0, 0, 1), albedo = 0.7f } };
codesphereBuffer = new ComputeBuffer(spheres.Count, sizeof(float) * (3 + 1 + 3 + 1)); // 32 bytes sphereBuffer.SetData(spheres);}
private void InitRenderTexture() { if (target == null || target.width != Screen.width || target.height != Screen.height) { if (target != null) { target.Release(); } target = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear); target.enableRandomWrite = true; target.Create(); } }
private void SetComputeShaderParameters() { rayTracingShader.SetTexture(0, "Result", target); rayTracingShader.SetTexture(0, "_SkyboxTexture", skyboxTexture); rayTracingShader.SetBuffer(0, "_Spheres", sphereBuffer); rayTracingShader.SetMatrix("_CameraToWorld", _camera.cameraToWorldMatrix); rayTracingShader.SetMatrix("_CameraInverseProjection", _camera.projectionMatrix.inverse); rayTracingShader.SetInt("_NumSpheres", spheres.Count); Vector3 lightDirection = new Vector3(1, 0, 1).normalized; rayTracingShader.SetVector("_LightDirection", new Vector4(lightDirection.x, lightDirection.y, lightDirection.z, 0)); rayTracingShader.SetFloat("_LightIntensity", 2.0f); }
private void OnDestroy() { if (sphereBuffer != null) { sphereBuffer.Release(); } } }
Compute Shader для трассировки лучей RayTracing.compute #pragma kernel CSMain
#define M_PI 3.14159265359
struct Sphere { float3 origin; float radius; float3 color; float albedo; };
- Assistant
- User
'Raytrace': recursive functions not allowed in cs_5_0 at kernel CSMain
- Assistant
- User
с этим же кодом как сделать так, чтобы сферы стали прозрачными, отражая только skybox
- Assistant