oriels/add/room.hlsl
2022-05-12 19:52:32 -04:00

106 lines
No EOL
2.5 KiB
HLSL

#include "stereokit.hlsli"
//--name = dofdev/room
//--color:color = 1,1,1,1
//--tex_scale = 1
//--diffuse = white
float4 color;
float tex_scale;
Texture2D diffuse : register(t0);
SamplerState diffuse_s : register(s0);
cbuffer BufferData : register(b3) {
float4x4 oriel_matrix;
float3 dimensions;
float time;
};
struct vsIn {
float4 pos : SV_Position;
float3 norm : NORMAL0;
float2 uv : TEXCOORD0;
float4 col : COLOR0;
};
struct psIn {
float4 pos : SV_Position;
float3 world : WORLD;
float2 uv : TEXCOORD0;
float4 color : COLOR0;
float3 camdir : TEXCOORD1;
float3 campos : TEXCOORD2;
uint view_id : SV_RenderTargetArrayIndex;
};
psIn vs(vsIn input, uint id : SV_InstanceID) {
psIn o;
o.view_id = id % sk_view_count;
id = id / sk_view_count;
o.camdir = sk_camera_dir[o.view_id].xyz;
o.campos = sk_camera_pos[o.view_id].xyz;
o.world = mul(input.pos, sk_inst[id].world).xyz;
o.pos = mul(float4(o.world, 1), sk_viewproj[o.view_id]);
float3 normal = normalize(mul(input.norm, (float3x3)sk_inst[id].world));
o.uv = input.uv * tex_scale;
o.color = color * input.col * sk_inst[id].color;
// o.color.rgb *= Lighting(normal);
return o;
}
float sdBox(float3 p, float3 b) {
float3 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}
float sdSphere(float3 p, float r) {
return length(p) - r;
}
float raymarch(float3 ro, float3 rd) {
ro = mul(float4(ro, 1), oriel_matrix).xyz;
rd = mul(float4(rd, 0), oriel_matrix).xyz;
float dist = 0.0;
for (int i = 0; i < 256; i++) {
float3 pos = ro + dist * rd;
float step = sdBox(pos, dimensions / 2.0);
// float step = sdSphere(pos, dimensions.y / 2.0);
if (step < 0.0001 || dist > 100) break; // 100 == distmax
dist += step;
}
return dist;
}
float4 ps(psIn input) : SV_TARGET {
float4 col = diffuse.Sample(diffuse_s, input.uv);
float3 ro = input.campos;
float3 rd = normalize(input.world - ro);
float ol = raymarch(ro, rd);
clip(-(100 - (ol + 1)));
// if ((100 - (ol + 1)) > 0) {
// col *= 0.1;
// }
ro += ol * rd;
// clip((distance(input.campos, input.world) - distance(input.campos, ro)) * -1);
// if ((distance(input.campos, input.world) - distance(input.campos, ro)) >= 0) {
// col *= 0.1;
// }
// if (input.world.y < bufferCenter.y) {
// col *= 0.1;
// }
// float value = (col.r + col.r + col.g + col.g + col.g + col.b) / 6;
// return float4(value, value, value, 1);
return col;
}