Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.10 KB | None | 0 0
  1.  
  2.  
  3. #define M_PI 3.1415926535897932384626433832795
  4. const float infinity = 999999999.0;
  5. vec3 view_dir = vec3(0.0, -0.1, -1.0);
  6. vec3 view_pos = vec3(0.0, 3.5, 17.0);
  7.  
  8. const int MAX_DEPTH = 5;
  9. const int NUM_OF_OBJECTS = 6;
  10. const int NUM_OF_LIGHTS = 2;
  11. const int samples = 8;
  12. const int MAX_FRAMES_BLEND = 200;
  13. const float GAMMA = 2.2;
  14.  
  15.  
  16. const float bias = 0.000001;
  17. const float minimum_collision_distance = 0.00001;
  18. const float fov = 90.0 * M_PI / 180.0;
  19.  
  20. const float EV = 10.0;
  21. const float light_intensity_scale = 1.0;
  22. const float light_directionality = 0.75;
  23. const float alpha_for_diffCosW = 1000.0;
  24.  
  25. struct Ray{
  26. vec3 m_dir;
  27. vec3 m_or;
  28. vec3 energy;
  29. float ior;
  30. };
  31.  
  32. struct Material{
  33. vec3 color;
  34. float ambient;
  35. float spec;
  36. float emissivity;
  37. };
  38.  
  39. struct Object{
  40. int type;
  41. vec3 pos;
  42. float radius;
  43. //type 0: circle
  44. //type 1: sphere
  45. //type 2: plane
  46. //type 3: triangle
  47. vec3 normal;
  48.  
  49. Material mat;
  50.  
  51. };
  52.  
  53. float seed = 0.0;
  54. vec2 uv = vec2(0.0);
  55.  
  56. float random() {
  57. return fract(sin(dot(uv, mat2(cos(iTime), sin(iTime), -sin(iTime), cos(iTime)) * vec2(12.9898, 78.233)) + seed++) * 43758.5453);
  58. }
  59.  
  60. mat3 make_rot_matrix_from_normals(vec3 _u,vec3 _v){
  61. float c = dot(_u, _v);
  62. mat3 _R = mat3(-1.0);
  63. if (c == 1.0){
  64. return mat3(1.0);
  65. }
  66. if (c != 1.0){
  67. vec3 _a = cross(_u, _v);
  68. mat3 _a_cross = mat3(0.0);
  69. _a_cross[1][0] = -_a.z;
  70. _a_cross[2][0] = _a.y;
  71. _a_cross[0][1] = _a.z;
  72. _a_cross[2][1] = -_a.x;
  73. _a_cross[0][2] = -_a.y;
  74. _a_cross[1][2] = _a.x;
  75.  
  76. mat3 a_cross_sq = _a_cross * _a_cross;
  77.  
  78. _R = mat3(1.0) + _a_cross + a_cross_sq * (1.0 / (1.0 + c));
  79.  
  80. }
  81. return _R;
  82. }
  83.  
  84. vec3 diff_cos_weighted_direction(float _spec, vec3 _n){
  85. if (_spec == 1.0){
  86. return _n;
  87. }
  88.  
  89. float m = pow(alpha_for_diffCosW, _spec * _spec);
  90.  
  91. float _u = random();
  92. float _v = random();
  93.  
  94. float cosT = pow(_u, 1.0 / (1.0 + m));
  95. float sinT = sqrt(1.0 - cosT * cosT);
  96. float phi = 2.0 * M_PI * _v;
  97.  
  98. vec3 step1 = vec3(sinT * cos(phi), sinT * sin(phi), cosT);
  99.  
  100. //rotate to normal plane
  101.  
  102. vec3 rotated_vec = make_rot_matrix_from_normals(vec3(0.0, 0.0, 1.0), _n) * step1;
  103.  
  104. return normalize(rotated_vec);
  105.  
  106.  
  107. }
  108.  
  109. Ray create_new_ray(vec3 _dir, vec3 _pos){
  110. return Ray(_dir, _pos, vec3(1.0),1.0);
  111.  
  112.  
  113. }
  114.  
  115. struct Light{
  116. vec3 pos;
  117. vec3 color;
  118. };
  119.  
  120. Object scene[NUM_OF_OBJECTS];
  121. Object lights[NUM_OF_LIGHTS];
  122.  
  123. vec3 interpolated_ray_dir(vec2 _xy, float w, float h){
  124. float aspect_ratio = w/h;
  125. float x = (2.0 * (_xy.x +0.5)/w - 1.0);
  126. float y = -(1.0-2.0*(_xy.y+0.5)/h)/aspect_ratio;
  127.  
  128. float D = 1.0 / tan(fov/2.0);
  129.  
  130. return normalize(vec3(x,y,0) + D * view_dir);
  131. }
  132.  
  133. vec3 get_point_at_distance(Ray _ray, float d){
  134. return _ray.m_or + d * _ray.m_dir;
  135. }
  136.  
  137.  
  138. float intersect(Ray _ray, Object _obj){
  139. float d = infinity;
  140. if (_obj.type == 0 ) // circle
  141. {
  142. float d_t = dot(_obj.pos - _ray.m_or,_obj.normal)/dot(_obj.normal,_ray.m_dir);
  143. float ip_to_midpt = length(get_point_at_distance(_ray,d_t) - _obj.pos);
  144. if (ip_to_midpt < _obj.radius)
  145. {
  146. return d = d_t;
  147. }
  148. }
  149.  
  150. if (_obj.type == 1){
  151. vec3 r_t_c = _ray.m_or - _obj.pos;
  152. float p1 = -dot(_ray.m_dir, r_t_c);
  153. float p2sqr = p1 * p1 - dot(r_t_c, r_t_c) + _obj.radius * _obj.radius;
  154. if (p2sqr < 0.0){
  155. return infinity;
  156. }
  157. float p2 = sqrt(p2sqr);
  158. float _d = min(p1 - p2, p1 + p2);
  159. return _d > 0.0? _d : infinity;
  160. }
  161. if (_obj.type == 2){
  162. float _N = dot(_obj.normal, _ray.m_dir);
  163. float _d = infinity;
  164. if (abs(_N) < bias){
  165. return infinity;
  166. }
  167. else{
  168. vec3 or_to_plane = _obj.pos - _ray.m_or;
  169. _d = dot(or_to_plane,_obj.normal) / _N;
  170. }
  171. if (_d > 0.0){
  172. return _d;
  173. }
  174. else{
  175. return infinity;
  176. }
  177. }
  178.  
  179. return d;
  180. }
  181.  
  182.  
  183. vec3 get_normal(vec3 _point, Object _obj){
  184. vec3 n = vec3(0.0, 0.0, 1.0);
  185.  
  186. if (_obj.type == 0){
  187. return normalize(_obj.normal);
  188. }
  189. if (_obj.type == 1){
  190. return normalize(_point - _obj.pos);
  191. }
  192. if (_obj.type == 2){
  193. return _obj.normal;
  194. }
  195. return n;
  196. }
  197.  
  198.  
  199. vec2 intersect_scene(inout Ray _ray, Object [NUM_OF_OBJECTS] _scene){
  200. float closest = infinity;
  201. int nearest_obj = -1;
  202. for(int i = 0; i< NUM_OF_OBJECTS; i++){
  203. float dist = intersect(_ray, _scene[i]);
  204. if (dist < closest){
  205. if(dist > minimum_collision_distance){
  206. closest = dist;
  207. nearest_obj = i;
  208. }
  209. }
  210. }
  211. return vec2(float(nearest_obj), closest);
  212. }
  213.  
  214. vec3 trace(inout Ray _ray, Object [NUM_OF_OBJECTS] _scene, int _depth){
  215. vec3 acquired_color = vec3(0.0, 0.0, 0.0);
  216. if (_depth <= MAX_DEPTH){
  217. vec2 intersection_info = intersect_scene(_ray, _scene);
  218. int intersection_id = int(intersection_info.x);
  219. float intersection_distance = intersection_info.y;
  220. if (intersection_distance < infinity){
  221. float distance_sq = intersection_distance * intersection_distance;
  222. vec3 intersection_point = get_point_at_distance(_ray, intersection_distance);
  223. vec3 surf_norm = get_normal(intersection_point,_scene[intersection_id]);
  224. float n_dot_r = dot(surf_norm, _ray.m_dir);
  225.  
  226.  
  227. if (_scene[intersection_id].mat.emissivity > 0.0){
  228. acquired_color += _scene[intersection_id].mat.emissivity * _scene[intersection_id].mat.color * abs(n_dot_r) ;
  229.  
  230. }
  231.  
  232.  
  233.  
  234. //random ray towards light
  235. vec3 diffuse_direct_col = vec3(0.0);
  236. for(int k =0;k<1;k++){
  237. for(int i=0;i<NUM_OF_OBJECTS;i++){
  238. //make towards an object if it is emissive
  239. if(_scene[i].mat.emissivity > 0.0){
  240. //Cast a ray towards an object that is emissive
  241. vec3 pt_to_light_vec0 = normalize(_scene[i].pos - intersection_point);
  242.  
  243. vec3 random_dir = diff_cos_weighted_direction(light_directionality, pt_to_light_vec0);
  244.  
  245. Ray r_to_light = create_new_ray(random_dir, intersection_point + bias * surf_norm);
  246.  
  247. vec2 towards_light_intersection_info = intersect_scene(r_to_light, _scene);
  248. int index_collision = int(towards_light_intersection_info.x);
  249. // The ray has collided with something, let's check its distance and the emissivity of the collided object
  250.  
  251. if(towards_light_intersection_info.y < infinity){
  252. if(_scene[index_collision].mat.emissivity > 0.0){
  253. float n_dot_li = dot(random_dir, surf_norm);
  254. if(n_dot_li > 0.0){
  255. float prob_weight = 0.5 - cos(pow(light_directionality,4.0)*M_PI)*0.5;
  256. float dist_to_light = length(_scene[i].pos - intersection_point);
  257. float di_fo = 1.0 / (dist_to_light*dist_to_light) * (prob_weight) + (1.0 - prob_weight);
  258. diffuse_direct_col += n_dot_li * di_fo * _scene[index_collision].mat.color * _scene[index_collision].mat.emissivity;
  259. }
  260. }
  261. }
  262. }
  263. }
  264. }
  265.  
  266. acquired_color += diffuse_direct_col * _scene[intersection_id].mat.color * (1.0 -_scene[intersection_id].mat.spec) ;
  267.  
  268. //reflect
  269. float dice = random();
  270. _ray.m_or = intersection_point + bias * surf_norm;
  271. _ray.m_dir = diff_cos_weighted_direction( _scene[intersection_id].mat.spec, reflect(_ray.m_dir, surf_norm));
  272. _ray.energy *= _scene[intersection_id].mat.color;
  273.  
  274.  
  275. }
  276. else{
  277. _ray.energy = vec3(0.0);
  278. return vec3(0.0);
  279. }
  280. }
  281.  
  282. return acquired_color;
  283. }
  284.  
  285. void make_scene(){
  286. float default_ambient = 0.000;
  287. float freq = 5.0;
  288. //always add glowing object to lights
  289. //material: color amb spec em
  290. //white light
  291. Material white = Material(vec3(1.0, 1.0, 1.0), default_ambient, 0.0,1.0);
  292. //matte white
  293. Material matte_white = Material(vec3(1.0), default_ambient, 0.0, default_ambient);
  294. //glowing orange
  295. Material orange = Material(vec3(1.0, 0.4, 0.0), default_ambient, 0.0, default_ambient);
  296. //teal glowing
  297. float glow = 0.5*cos(freq*iTime)+0.5;
  298. Material teal = Material(vec3(0.0, 1.0, 1.0), default_ambient, 0.5, default_ambient);
  299. Material gold = Material(vec3(0.83, 0.68, 0.216), default_ambient, 1.0, default_ambient);
  300. Material orange_glow = orange;
  301. orange_glow.emissivity = 0.1;
  302.  
  303. Material green = Material(vec3(0.0, 1.0, 0.0), default_ambient, 0.2, default_ambient);
  304. //object
  305. //type pos radius normal material
  306. float lulz = 1.0;
  307.  
  308. float x_pos = 0.0;
  309. float y_pos = 3.0;
  310. //x_pos = lulz*pow(sin(iTime),3.0);
  311. //y_pos = lulz/ 16.0 *(16.0 * cos(iTime) - 5.0*cos(2.0*iTime) - 2.0 * cos(3.0*iTime) - cos(4.0*iTime));
  312.  
  313. scene[0] = Object(1, vec3(x_pos, y_pos, -3.0), 1.0, vec3(0.0, 0.0, 0.0), orange_glow);
  314. scene[1] = Object(1, vec3(0.0, 0.5, 0.0), 1.0, vec3(0.0, 0.0, 0.0), matte_white);
  315. scene[2] = Object(2, vec3(0.0, -0.5, 0.0), infinity, vec3(0.0, 1.0, 0.0), matte_white);
  316. scene[3] = Object(1, vec3(-2.0, 0.5, 0.0), 1.0, vec3(0.0, 0.0, 0.0), green);
  317. scene[4] = Object(1, vec3(2.0, 0.5, 0.0), 1.0, vec3(0.0, 0.0, 0.0), gold);
  318. scene[5] = Object(1, vec3(0.0, 5.0, 0.0), 1.0, vec3(0.0, 0.0, 0.0), white);
  319.  
  320.  
  321. lights[0] = scene[0];
  322. lights[1] = scene[5];
  323. }
  324.  
  325.  
  326. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  327. {
  328. // process_input();
  329. make_scene();
  330. // Normalized pixel coordinates (from 0 to 1)UV = fragCoord;
  331. seed=iTime;
  332. uv = fragCoord;
  333.  
  334. float _w = iResolution.x;
  335. float _h = iResolution.y;
  336.  
  337. vec3 ray_dir_from_view = interpolated_ray_dir(uv, _w,_h);
  338.  
  339. Ray pixel_ray = Ray(ray_dir_from_view, view_pos, vec3(1.0),1.0);
  340.  
  341. vec3 out_col = vec3(0.0);
  342.  
  343. for(int k = 0; k< samples; k++){
  344. pixel_ray = Ray(ray_dir_from_view, view_pos, vec3(1.0),1.0);
  345. for(int i=0;i<MAX_DEPTH;i++){
  346. out_col += pixel_ray.energy * trace(pixel_ray, scene, i);
  347. seed++;
  348. }
  349. }
  350. out_col /= float(samples);
  351.  
  352. // blend with previous
  353. vec2 UV = fragCoord.xy / iResolution.xy;
  354. vec4 prev_col = texture(iChannel0, UV);
  355. float weight_prev = 1.0;
  356. float weight_new = 1.0;
  357. vec4 new_frag = vec4(weight_prev * prev_col.rgb + weight_new * out_col, prev_col.a+weight_new);
  358. if(!all(lessThanEqual(iMouse.zw, vec2(0.0)))){
  359. new_frag = vec4(out_col, 1.0);
  360. }
  361.  
  362. // Output to screen
  363. fragColor = new_frag;
  364.  
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement