Skip to content

The World Camera

Aiekick edited this page Jul 14, 2019 · 4 revisions

you can have a camera for manipulate easily your shader

Consider this minimal Sdf Shader :

@UNIFORMS

uniform vec2(buffer) size;

@VERTEX

layout(location = 0) in vec2 a_position; // Current vertex position
out vec2 uv;
void main()
{
	uv = a_position * 2.0 - 1.0;
	gl_Position = vec4(uv, 0.0, 1.0);
}

@FRAGMENT

layout(location = 0) out vec4 fragColor;
in vec2 uv;

float map(vec3 p)
{
	return length(max(abs(p) - 1.0,0.0))-0.1;
}

vec3 getNor(vec3 p, float k)
{
	vec3 eps = vec3(k, 0., 0.);
	vec3 nor = vec3(
		map(p + eps.xyy) - map(p - eps.xyy),
		map(p + eps.yxy) - map(p - eps.yxy),
		map(p + eps.yyx) - map(p - eps.yyx));
	return normalize(nor);
}

void main(void)
{
	vec2 v = uv; v.x *= size.x/size.y;
	
	vec3 ro = vec3(0,0,-5);
	vec3 rd = normalize(vec3(v,1));

	vec3 col = vec3(0.0);

	float s = 1., d = 0., md = 50.;
	for (int i = 0; i < 100; i++)
	{
		if (s < 0.001 || d > 50.) break;
		s = map(ro + rd * d);
		d += s * 0.8;
	}
	
	if (d < md)
	{
		vec3 p = ro + rd * d;
		vec3 n = getNor(p, 0.01);
		col = n * 0.5 + 0.5;
	}
	
	fragColor = vec4(col,1);
}

you get that :

NoodlesPlate_MSVC_Debug_2019-07-14_04-03-40

Use the world camera for ray origin and ray direction.

First, Insert << #include "space3d.glsl" >> just after the tag @FRAGMENT

@FRAGMENT
#include "space3d.glsl"

Then, replace the lines

vec3 ro = vec3(0,0,-5);
vec3 rd = normalize(vec3(v,1));

by

vec3 ro = getRayOrigin();
vec3 rd = getRayDirection();

after code save, you get the hability to use camera for :

  • rotate around y axis with left mouse
  • zoom with the right button
  • the camera pane is in the panel Config

2019-07-14_04-00-20

Clone this wiki locally