Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.67 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 = 2;
  9. const int NUM_OF_OBJECTS = 5;
  10. const int NUM_OF_LIGHTS = 3;
  11. const int samples = 2;
  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.99;
  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;
  229. }
  230.  
  231.  
  232.  
  233. //random ray towards light
  234. vec3 diffuse_direct_col = vec3(0.0);
  235. for(int k =0;k<1;k++){
  236. for(int i=0;i<NUM_OF_LIGHTS;i++){
  237. //make ray towards this light
  238. vec3 pt_to_light_vec0 = normalize(lights[i].pos - intersection_point);
  239.  
  240. vec3 random_dir = diff_cos_weighted_direction(light_directionality, pt_to_light_vec0);
  241.  
  242. Ray r_to_light = create_new_ray(random_dir, intersection_point + bias * surf_norm);
  243.  
  244. vec2 towards_light_intersection_info = intersect_scene(r_to_light, _scene);
  245. int index_collision = int(towards_light_intersection_info.x);
  246.  
  247. if(towards_light_intersection_info.y < infinity){
  248. if(_scene[index_collision].mat.emissivity > 0.0){
  249. float n_dot_li = dot(random_dir, surf_norm);
  250. if(n_dot_li > 0.0){
  251. float prob_weight = 0.5 - cos(pow(light_directionality,4.0)*M_PI)*0.5;
  252. float dist_to_light = length(lights[i].pos - intersection_point);
  253. float di_fo = n_dot_li / (dist_to_light*dist_to_light) * (prob_weight) + (1.0 - prob_weight);
  254. diffuse_direct_col += _scene[index_collision].mat.color * _scene[index_collision].mat.emissivity * di_fo;
  255. }
  256. }
  257. }
  258. }
  259. }
  260.  
  261. acquired_color += diffuse_direct_col * _scene[intersection_id].mat.color / float(NUM_OF_LIGHTS);
  262.  
  263. //reflect
  264. _ray.m_or = intersection_point + bias * surf_norm;
  265. _ray.m_dir = diff_cos_weighted_direction( _scene[intersection_id].mat.spec, reflect(_ray.m_dir, surf_norm));
  266. float dice = random();
  267. if(dice < _scene[intersection_id].mat.spec){
  268. //pure reflective
  269. _ray.energy *= _scene[intersection_id].mat.color ;
  270. }
  271. else{
  272. _ray.energy *= _scene[intersection_id].mat.color* _scene[intersection_id].mat.ambient;
  273. }
  274.  
  275.  
  276. }
  277. else{
  278. _ray.energy = vec3(0.0);
  279. return vec3(0.0);
  280. }
  281. }
  282.  
  283. return acquired_color;
  284. }
  285.  
  286. void make_scene(){
  287. float default_ambient = 0.10;
  288. float freq = 5.0;
  289. //always add glowing object to lights
  290. //material: color amb spec em
  291. //white light
  292. Material white = Material(vec3(1.0, 1.0, 1.0), default_ambient, 0.0,1.0);
  293. //matte white
  294. Material matte_white = Material(vec3(1.0, 1.0, 1.0), default_ambient, 0.0, 0.0);
  295. //glowing orange
  296. Material orange = Material(vec3(1.0, 0.4, 0.0), default_ambient, 0.5, 0.1);
  297. //teal glowing
  298. float glow = 0.5*cos(freq*iTime)+0.5;
  299. Material teal = Material(vec3(0.0, 1.0, 1.0), default_ambient, 0.5, 0.3*glow+0.1);
  300. Material gold = Material(vec3(0.83, 0.68, 0.216), default_ambient, 0.75, 0.0);
  301. //object
  302. //type pos radius normal material
  303. float lulz = 1.0;
  304.  
  305. float x_pos = 0.0;
  306. float y_pos = 3.0;
  307. x_pos = lulz*pow(sin(iTime),3.0);
  308. 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));
  309.  
  310. scene[0] = Object(1, vec3(x_pos, y_pos+7.0, 0.0), 0.2, vec3(0.0, 0.0, 0.0), white);
  311. scene[1] = Object(1, vec3(0.0, 0.5, 0.0), 1.0, vec3(0.0, 0.0, 0.0), teal);
  312. scene[2] = Object(2, vec3(0.0, -0.5, 0.0), infinity, vec3(0.0, 1.0, 0.0), matte_white);
  313. scene[3] = Object(1, vec3(-2.0, 0.5, 0.0), 1.0, vec3(0.0, 0.0, 0.0), orange);
  314. scene[4] = Object(1, vec3(2.0, 0.5, 0.0), 1.0, vec3(0.0, 0.0, 0.0), gold);
  315.  
  316.  
  317. lights[0] = scene[0];
  318. lights[1] = scene[3];
  319. lights[2] = scene[1];
  320. }
  321.  
  322.  
  323. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  324. {
  325. // process_input();
  326. make_scene();
  327. // Normalized pixel coordinates (from 0 to 1)UV = fragCoord;
  328. seed=iTime;
  329. uv = fragCoord;
  330.  
  331. float _w = iResolution.x;
  332. float _h = iResolution.y;
  333.  
  334. vec3 ray_dir_from_view = interpolated_ray_dir(uv, _w,_h);
  335.  
  336. Ray pixel_ray = Ray(ray_dir_from_view, view_pos, vec3(1.0),1.0);
  337.  
  338. vec3 out_col = vec3(0.0);
  339.  
  340. for(int k = 0; k< samples; k++){
  341. pixel_ray = Ray(ray_dir_from_view, view_pos, vec3(1.0),1.0);
  342. for(int i=0;i<MAX_DEPTH;i++){
  343. out_col += pixel_ray.energy * trace(pixel_ray, scene, i);
  344. seed++;
  345. }
  346. }
  347. out_col /= float(samples);
  348.  
  349. // blend with previous
  350. vec2 UV = fragCoord.xy / iResolution.xy;
  351. vec4 prev_col = texture(iChannel0, UV);
  352. float weight_prev = 1.0;
  353. float weight_new = 1.0;
  354. vec4 new_frag = vec4(weight_prev * prev_col.rgb + weight_new * out_col, prev_col.a+0.2);
  355. if(!all(lessThanEqual(iMouse.zw, vec2(0.0)))){
  356. new_frag = vec4(out_col, 1.0);
  357. }
  358.  
  359. // Output to screen
  360. fragColor = new_frag;
  361.  
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement