Базовый рендеринг сцены в тремя цветными сферами и skybox: 1. Написать *.cs скрипт для…
- User
Базовый рендеринг сцены в тремя цветными сферами и skybox:
- Написать *.cs скрипт для настройки параметров и передачи их в шейдер
Преобразовать id.xy в uv координаты. Создать rayOrigin и rayDirection. Вывести уравнение пересечения луча и сферы.
using System.Collections; using System.Collections.Generic; using UnityEngine;
private ComputeShader _raytracingShader; private RenderTexture _target
private void OnRenderImage(RenderTexture source, Render Texture destination) { Init();
code_raytracingShader.SetTexture(0, "Result", _target); int threadGroupX = Mathf.CeilToInt(Screen.width / 8.0f); int threadGroupY = Mathf.CeilToInt(Screen.height / 8.0f); _raytracingShader.Dispatch(0, threadGroupX, threadGroupY, 1); Graphics.Blit(_target, destination)}
private void Init() { if (_target == null || _target.width != Screen.width || _target.height != Screen.height) { _target?.Release();
code_target = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBFloat); _target.enableRandomWrite = true; _target.Create(); }}
#pragma kernel CSMain
RWTexture2D Result;
[numthreads(8,8,1)] void CSMain (uint3 id : SV_DispatchThreadID) { Result[id.xy] = float4(id.x & id.y, (id.x & 15) / 15.0, (id.y & 15) / 15.0, 0.0); }
uint width, height; Result.GetDimensions(width, height);
float2 uv = float2(id.xy / float2(width, height) * 2.0f - 1.0f); float3 direction = mul(_CameraInverseProjection, float4(uv, 0.0f, 1.0f)).xyz;
Ray ray; ray.origin = mul(_CameraLocalToWorldMatrix, float4(0.0f, 0.0f, 0.0f, 1.0f)).xyz; ray.direction = normalize(mul(_CameraLocalToWorldMatrix, float4(direction, 0.0f)).xyz);
shader.SetMatrix("_CameraLocalToWorldMatrix", camera.cameraToWorldMatrix); shader.SetTexture("_CameraInverseProjection", camera.projectionMatrix.inverse);
Добавление skybox
Texture2D _SkyboxTexture; SampleState sampler_SkyboxTexture;
float theta = acos(ray.direction.y) / -PI; float phi = atan2(ray.direction.x, -ray.direction.z) / -PI * 0.5f;
float4 color = _SkyboxTexture.SampleLevel(sampler_SkyboxTexture, float2(phi, theta), 0);
Создание сфер
private ComputeBuffer _sphereBuffer; private List _sphereList = new List();
- Assistant