У нас имеется: 1. 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<Sphere> spheres = new List<Sphere>(); private ComputeBuffer sphereBuffer; struct Sphere { public Vector3 origin; public float radius; public Vector3 color; public float padding; // Align structure to 16 bytes } private void Awake() { _camera = GetComponent<Camera>(); 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<Sphere> { new Sphere { origin = new Vector3(0, 0, 0), radius = 1, color = new Vector3(1, 0, 0) }, new Sphere { origin = new Vector3(2, 0, 0), radius = 1, color = new Vector3(0, 1, 0) }, new Sphere { origin = new Vector3(-2, 0, 0), radius = 1, color = new Vector3(0, 0, 1) } }; sphereBuffer = new ComputeBuffer(spheres.Count, sizeof(float) * 8); // 4 floats for position/color + 2 floats for radius/padding 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); } 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 padding; };
RWTexture2D Result; Texture2D _SkyboxTexture; SamplerState sampler_SkyboxTexture;
cbuffer SceneData { matrix _CameraToWorld; matrix _CameraInverseProjection; int _NumSpheres; }
StructuredBuffer _Spheres;
bool RaySphereIntersection(float3 origin, float3 direction, Sphere sphere, out float hitDistance) { float3 oc = origin - sphere.origin; float a = dot(direction, direction); float b = dot(oc, direction) * 2.0; float c = dot(oc, oc) - sphere.radius * sphere.radius; float discriminant = b * b - 4.0 * a * c;
- Assistant