Changeset View
Changeset View
Standalone View
Standalone View
intern/cycles/kernel/shaders/node_sky_texture.osl
| Show First 20 Lines • Show All 116 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| /* Nishita improved */ | /* Nishita improved */ | ||||
| vector geographical_to_direction(float lat, float lon) | vector geographical_to_direction(float lat, float lon) | ||||
| { | { | ||||
| return vector(cos(lat) * cos(lon), cos(lat) * sin(lon), sin(lat)); | return vector(cos(lat) * cos(lon), cos(lat) * sin(lon), sin(lat)); | ||||
| } | } | ||||
| color sky_radiance_nishita(vector dir, float nishita_data[9], string filename) | color sky_radiance_nishita(vector dir, float nishita_data[10], string filename) | ||||
| { | { | ||||
| /* definitions */ | /* definitions */ | ||||
| float sun_elevation = nishita_data[6]; | float sun_elevation = nishita_data[6]; | ||||
| float sun_rotation = nishita_data[7]; | float sun_rotation = nishita_data[7]; | ||||
| float angular_diameter = nishita_data[8]; | float angular_diameter = nishita_data[8]; | ||||
| float sun_intensity = nishita_data[9]; | |||||
| int sun_disc = angular_diameter > 0; | int sun_disc = angular_diameter > 0; | ||||
| float alpha = 1.0; | float alpha = 1.0; | ||||
| color xyz; | color xyz; | ||||
| /* convert dir to spherical coordinates */ | /* convert dir to spherical coordinates */ | ||||
| vector direction = sky_spherical_coordinates(dir); | vector direction = sky_spherical_coordinates(dir); | ||||
| /* render above the horizon */ | /* render above the horizon */ | ||||
| if (dir[2] >= 0.0) { | if (dir[2] >= 0.0) { | ||||
| /* definitions */ | /* definitions */ | ||||
| vector sun_dir = geographical_to_direction(sun_elevation, sun_rotation + M_PI_2); | vector sun_dir = geographical_to_direction(sun_elevation, sun_rotation + M_PI_2); | ||||
| float sun_dir_angle = acos(dot(dir, sun_dir)); | float sun_dir_angle = acos(dot(dir, sun_dir)); | ||||
| float half_angular = angular_diameter / 2.0; | float half_angular = angular_diameter / 2.0; | ||||
| float dir_elevation = M_PI_2 - direction[0]; | float dir_elevation = M_PI_2 - direction[0]; | ||||
| /* if ray inside sun disc render it, otherwise render sky */ | /* if ray inside sun disc render it, otherwise render sky */ | ||||
| if (sun_dir_angle < half_angular && sun_disc == 1) { | if (sun_dir_angle < half_angular && sun_disc == 1) { | ||||
| /* get 3 pixels data */ | /* get 2 pixels data */ | ||||
| color pixel_bottom = color(nishita_data[0], nishita_data[1], nishita_data[2]); | color pixel_bottom = color(nishita_data[0], nishita_data[1], nishita_data[2]); | ||||
| color pixel_top = color(nishita_data[3], nishita_data[4], nishita_data[5]); | color pixel_top = color(nishita_data[3], nishita_data[4], nishita_data[5]); | ||||
| float y; | float y; | ||||
| /* sun interpolation */ | /* sun interpolation */ | ||||
| if (sun_elevation - half_angular > 0.0) { | if (sun_elevation - half_angular > 0.0) { | ||||
| if ((sun_elevation + half_angular) > 0.0) { | if ((sun_elevation + half_angular) > 0.0) { | ||||
| y = ((dir_elevation - sun_elevation) / angular_diameter) + 0.5; | y = ((dir_elevation - sun_elevation) / angular_diameter) + 0.5; | ||||
| xyz = mix(pixel_bottom, pixel_top, y); | xyz = mix(pixel_bottom, pixel_top, y) * sun_intensity; | ||||
| } | } | ||||
| } | } | ||||
| else { | else { | ||||
| if (sun_elevation + half_angular > 0.0) { | if (sun_elevation + half_angular > 0.0) { | ||||
| y = dir_elevation / (sun_elevation + half_angular); | y = dir_elevation / (sun_elevation + half_angular); | ||||
| xyz = mix(pixel_bottom, pixel_top, y); | xyz = mix(pixel_bottom, pixel_top, y) * sun_intensity; | ||||
| } | } | ||||
| } | } | ||||
| /* limb darkening, coefficient is 0.6f */ | /* limb darkening, coefficient is 0.6f */ | ||||
| float angle_fraction = sun_dir_angle / half_angular; | float angle_fraction = sun_dir_angle / half_angular; | ||||
| float limb_darkening = (1.0 - 0.6 * (1.0 - sqrt(1.0 - angle_fraction * angle_fraction))); | float limb_darkening = (1.0 - 0.6 * (1.0 - sqrt(1.0 - angle_fraction * angle_fraction))); | ||||
| xyz *= limb_darkening; | xyz *= limb_darkening; | ||||
| } | } | ||||
| /* sky */ | /* sky */ | ||||
| else { | else { | ||||
| /* sky interpolation */ | /* sky interpolation */ | ||||
| float x = (direction[1] + M_PI + sun_rotation) / M_2PI; | float x = (direction[1] + M_PI + sun_rotation) / M_2PI; | ||||
| float y = 1.0 - (dir_elevation / M_PI_2); | float y = 1.0 - pow((dir_elevation / M_PI_2), 0.5); | ||||
| if (x > 1.0) { | if (x > 1.0) { | ||||
lukasstockner97: Please use sqrt() here, pow() is very expensive so we should avoid it if possible. | |||||
| x = x - 1.0; | x = x - 1.0; | ||||
| } | } | ||||
| xyz = (color)texture(filename, x, y, "wrap", "clamp", "interp", "linear", "alpha", alpha); | xyz = (color)texture(filename, x, y, "wrap", "clamp", "interp", "linear", "alpha", alpha); | ||||
| } | } | ||||
| } | } | ||||
| /* ground */ | /* ground */ | ||||
| else { | else { | ||||
| if (dir[2] < -0.4) { | if (dir[2] < -0.4) { | ||||
| Show All 23 Lines | shader node_sky_texture(int use_mapping = 0, | ||||
| string type = "hosek_wilkie", | string type = "hosek_wilkie", | ||||
| float theta = 0.0, | float theta = 0.0, | ||||
| float phi = 0.0, | float phi = 0.0, | ||||
| string filename = "", | string filename = "", | ||||
| color radiance = color(0.0, 0.0, 0.0), | color radiance = color(0.0, 0.0, 0.0), | ||||
| float config_x[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, | float config_x[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, | ||||
| float config_y[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, | float config_y[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, | ||||
| float config_z[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, | float config_z[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, | ||||
| float nishita_data[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, | float nishita_data[10] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, | ||||
| output color Color = color(0.0, 0.0, 0.0)) | output color Color = color(0.0, 0.0, 0.0)) | ||||
| { | { | ||||
| vector p = Vector; | vector p = Vector; | ||||
| if (use_mapping) | if (use_mapping) | ||||
| p = transform(mapping, p); | p = transform(mapping, p); | ||||
| if (type == "nishita_improved") | if (type == "nishita_improved") | ||||
| Color = sky_radiance_nishita(p, nishita_data, filename); | Color = sky_radiance_nishita(p, nishita_data, filename); | ||||
| if (type == "hosek_wilkie") | if (type == "hosek_wilkie") | ||||
| Color = sky_radiance_hosek(p, phi, theta, radiance, config_x, config_y, config_z); | Color = sky_radiance_hosek(p, phi, theta, radiance, config_x, config_y, config_z); | ||||
| if (type == "preetham") | if (type == "preetham") | ||||
| Color = sky_radiance_preetham(p, phi, theta, radiance, config_x, config_y, config_z); | Color = sky_radiance_preetham(p, phi, theta, radiance, config_x, config_y, config_z); | ||||
| } | } | ||||
Please use sqrt() here, pow() is very expensive so we should avoid it if possible.