Changeset View
Changeset View
Standalone View
Standalone View
intern/cycles/render/nodes.cpp
| Show First 20 Lines • Show All 625 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| typedef struct SunSky { | typedef struct SunSky { | ||||
| /* sun direction in spherical and cartesian */ | /* sun direction in spherical and cartesian */ | ||||
| float theta, phi; | float theta, phi; | ||||
| /* Parameter */ | /* Parameter */ | ||||
| float radiance_x, radiance_y, radiance_z; | float radiance_x, radiance_y, radiance_z; | ||||
| float config_x[9], config_y[9], config_z[9], nishita_data[9]; | float config_x[9], config_y[9], config_z[9], nishita_data[10]; | ||||
| } SunSky; | } SunSky; | ||||
| /* Preetham model */ | /* Preetham model */ | ||||
| static float sky_perez_function(float lam[6], float theta, float gamma) | static float sky_perez_function(float lam[6], float theta, float gamma) | ||||
| { | { | ||||
| return (1.0f + lam[0] * expf(lam[1] / cosf(theta))) * | return (1.0f + lam[0] * expf(lam[1] / cosf(theta))) * | ||||
| (1.0f + lam[2] * expf(lam[3] * gamma) + lam[4] * cosf(gamma) * cosf(gamma)); | (1.0f + lam[2] * expf(lam[3] * gamma) + lam[4] * cosf(gamma) * cosf(gamma)); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 100 Lines • ▼ Show 20 Lines | static void sky_texture_precompute_hosek(SunSky *sunsky, | ||||
| /* Free sky_state */ | /* Free sky_state */ | ||||
| arhosekskymodelstate_free(sky_state); | arhosekskymodelstate_free(sky_state); | ||||
| } | } | ||||
| /* Nishita improved */ | /* Nishita improved */ | ||||
| static void sky_texture_precompute_nishita(SunSky *sunsky, | static void sky_texture_precompute_nishita(SunSky *sunsky, | ||||
| bool sun_disc, | bool sun_disc, | ||||
| float sun_size, | float sun_size, | ||||
| float sun_intensity, | |||||
| float sun_elevation, | float sun_elevation, | ||||
| float sun_rotation, | float sun_rotation, | ||||
| int altitude, | float altitude, | ||||
| float air_density, | float air_density, | ||||
| float dust_density) | float dust_density) | ||||
| { | { | ||||
| /* sample 2 sun pixels */ | /* sample 2 sun pixels */ | ||||
| float pixel_bottom[3]; | float pixel_bottom[3]; | ||||
| float pixel_top[3]; | float pixel_top[3]; | ||||
| float altitude_f = (float)altitude; | |||||
| nishita_skymodel_precompute_sun( | nishita_skymodel_precompute_sun( | ||||
| sun_elevation, sun_size, altitude_f, air_density, dust_density, pixel_bottom, pixel_top); | sun_elevation, sun_size, altitude, air_density, dust_density, pixel_bottom, pixel_top); | ||||
| /* limit sun rotation between 0 and 360 degrees */ | |||||
| float offset = M_2PI_F * (int)(fabsf(sun_rotation) / M_2PI_F); | |||||
| if (sun_rotation > 0.0f) | |||||
| sun_rotation = M_2PI_F - (sun_rotation - offset); | |||||
| else | |||||
| sun_rotation = -sun_rotation - offset; | |||||
lukasstockner97: Hm, this seems unnecessarily complicated. I think something based on fmodf() should be cleaner. | |||||
| /* send data to svm_sky */ | /* send data to svm_sky */ | ||||
| sunsky->nishita_data[0] = pixel_bottom[0]; | sunsky->nishita_data[0] = pixel_bottom[0]; | ||||
| sunsky->nishita_data[1] = pixel_bottom[1]; | sunsky->nishita_data[1] = pixel_bottom[1]; | ||||
| sunsky->nishita_data[2] = pixel_bottom[2]; | sunsky->nishita_data[2] = pixel_bottom[2]; | ||||
| sunsky->nishita_data[3] = pixel_top[0]; | sunsky->nishita_data[3] = pixel_top[0]; | ||||
| sunsky->nishita_data[4] = pixel_top[1]; | sunsky->nishita_data[4] = pixel_top[1]; | ||||
| sunsky->nishita_data[5] = pixel_top[2]; | sunsky->nishita_data[5] = pixel_top[2]; | ||||
| sunsky->nishita_data[6] = sun_elevation; | sunsky->nishita_data[6] = sun_elevation; | ||||
| sunsky->nishita_data[7] = M_2PI_F - sun_rotation; | sunsky->nishita_data[7] = sun_rotation; | ||||
| sunsky->nishita_data[8] = sun_disc ? sun_size : 0.0f; | sunsky->nishita_data[8] = sun_disc ? sun_size : 0.0f; | ||||
| sunsky->nishita_data[9] = sun_intensity; | |||||
| } | } | ||||
| NODE_DEFINE(SkyTextureNode) | NODE_DEFINE(SkyTextureNode) | ||||
| { | { | ||||
| NodeType *type = NodeType::add("sky_texture", create, NodeType::SHADER); | NodeType *type = NodeType::add("sky_texture", create, NodeType::SHADER); | ||||
| TEXTURE_MAPPING_DEFINE(SkyTextureNode); | TEXTURE_MAPPING_DEFINE(SkyTextureNode); | ||||
| static NodeEnum type_enum; | static NodeEnum type_enum; | ||||
| type_enum.insert("preetham", NODE_SKY_PREETHAM); | type_enum.insert("preetham", NODE_SKY_PREETHAM); | ||||
| type_enum.insert("hosek_wilkie", NODE_SKY_HOSEK); | type_enum.insert("hosek_wilkie", NODE_SKY_HOSEK); | ||||
| type_enum.insert("nishita_improved", NODE_SKY_NISHITA); | type_enum.insert("nishita_improved", NODE_SKY_NISHITA); | ||||
| SOCKET_ENUM(type, "Type", type_enum, NODE_SKY_NISHITA); | SOCKET_ENUM(type, "Type", type_enum, NODE_SKY_NISHITA); | ||||
| SOCKET_VECTOR(sun_direction, "Sun Direction", make_float3(0.0f, 0.0f, 1.0f)); | SOCKET_VECTOR(sun_direction, "Sun Direction", make_float3(0.0f, 0.0f, 1.0f)); | ||||
| SOCKET_FLOAT(turbidity, "Turbidity", 2.2f); | SOCKET_FLOAT(turbidity, "Turbidity", 2.2f); | ||||
| SOCKET_FLOAT(ground_albedo, "Ground Albedo", 0.3f); | SOCKET_FLOAT(ground_albedo, "Ground Albedo", 0.3f); | ||||
| SOCKET_BOOLEAN(sun_disc, "Sun Disc", true); | SOCKET_BOOLEAN(sun_disc, "Sun Disc", true); | ||||
| SOCKET_FLOAT(sun_size, "Sun Size", 0.009512f); | SOCKET_FLOAT(sun_size, "Sun Size", 0.009512f); | ||||
| SOCKET_FLOAT(sun_intensity, "Sun Intensity", 1.0f); | |||||
| SOCKET_FLOAT(sun_elevation, "Sun Elevation", M_PI_2_F); | SOCKET_FLOAT(sun_elevation, "Sun Elevation", M_PI_2_F); | ||||
| SOCKET_FLOAT(sun_rotation, "Sun Rotation", 0.0f); | SOCKET_FLOAT(sun_rotation, "Sun Rotation", 0.0f); | ||||
| SOCKET_INT(altitude, "Altitude", 0); | SOCKET_FLOAT(altitude, "Altitude", 0.0f); | ||||
| SOCKET_FLOAT(air_density, "Air", 1.0f); | SOCKET_FLOAT(air_density, "Air", 1.0f); | ||||
| SOCKET_FLOAT(dust_density, "Dust", 1.0f); | SOCKET_FLOAT(dust_density, "Dust", 1.0f); | ||||
| SOCKET_FLOAT(ozone_density, "Ozone", 1.0f); | SOCKET_FLOAT(ozone_density, "Ozone", 1.0f); | ||||
| SOCKET_IN_POINT( | SOCKET_IN_POINT( | ||||
| vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED); | vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED); | ||||
| SOCKET_OUT_COLOR(color, "Color"); | SOCKET_OUT_COLOR(color, "Color"); | ||||
| Show All 11 Lines | void SkyTextureNode::compile(SVMCompiler &compiler) | ||||
| ShaderOutput *color_out = output("Color"); | ShaderOutput *color_out = output("Color"); | ||||
| SunSky sunsky; | SunSky sunsky; | ||||
| if (type == NODE_SKY_PREETHAM) | if (type == NODE_SKY_PREETHAM) | ||||
| sky_texture_precompute_preetham(&sunsky, sun_direction, turbidity); | sky_texture_precompute_preetham(&sunsky, sun_direction, turbidity); | ||||
| else if (type == NODE_SKY_HOSEK) | else if (type == NODE_SKY_HOSEK) | ||||
| sky_texture_precompute_hosek(&sunsky, sun_direction, turbidity, ground_albedo); | sky_texture_precompute_hosek(&sunsky, sun_direction, turbidity, ground_albedo); | ||||
| else if (type == NODE_SKY_NISHITA) { | else if (type == NODE_SKY_NISHITA) { | ||||
| /* clamp altitude to avoid intersection issues */ | |||||
| altitude = 1000.0f * clamp(altitude, 0.001f, 59.999f); | |||||
| sky_texture_precompute_nishita(&sunsky, | sky_texture_precompute_nishita(&sunsky, | ||||
| sun_disc, | sun_disc, | ||||
| sun_size, | sun_size, | ||||
| sun_intensity, | |||||
| sun_elevation, | sun_elevation, | ||||
| sun_rotation, | sun_rotation, | ||||
| altitude, | altitude, | ||||
| air_density, | air_density, | ||||
| dust_density); | dust_density); | ||||
| /* precomputed texture image parameters */ | /* precomputed texture image parameters */ | ||||
| ImageManager *image_manager = compiler.scene->image_manager; | ImageManager *image_manager = compiler.scene->image_manager; | ||||
| ImageParams impar; | ImageParams impar; | ||||
| ▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | else { | ||||
| compiler.add_node(__float_as_uint(sunsky.nishita_data[0]), | compiler.add_node(__float_as_uint(sunsky.nishita_data[0]), | ||||
| __float_as_uint(sunsky.nishita_data[1]), | __float_as_uint(sunsky.nishita_data[1]), | ||||
| __float_as_uint(sunsky.nishita_data[2]), | __float_as_uint(sunsky.nishita_data[2]), | ||||
| __float_as_uint(sunsky.nishita_data[3])); | __float_as_uint(sunsky.nishita_data[3])); | ||||
| compiler.add_node(__float_as_uint(sunsky.nishita_data[4]), | compiler.add_node(__float_as_uint(sunsky.nishita_data[4]), | ||||
| __float_as_uint(sunsky.nishita_data[5]), | __float_as_uint(sunsky.nishita_data[5]), | ||||
| __float_as_uint(sunsky.nishita_data[6]), | __float_as_uint(sunsky.nishita_data[6]), | ||||
| __float_as_uint(sunsky.nishita_data[7])); | __float_as_uint(sunsky.nishita_data[7])); | ||||
| compiler.add_node(__float_as_uint(sunsky.nishita_data[8]), handle.svm_slot(), 0, 0); | compiler.add_node(__float_as_uint(sunsky.nishita_data[8]), | ||||
| __float_as_uint(sunsky.nishita_data[9]), | |||||
| handle.svm_slot(), | |||||
| 0); | |||||
| } | } | ||||
| tex_mapping.compile_end(compiler, vector_in, vector_offset); | tex_mapping.compile_end(compiler, vector_in, vector_offset); | ||||
| } | } | ||||
| void SkyTextureNode::compile(OSLCompiler &compiler) | void SkyTextureNode::compile(OSLCompiler &compiler) | ||||
| { | { | ||||
| tex_mapping.compile(compiler); | tex_mapping.compile(compiler); | ||||
| SunSky sunsky; | SunSky sunsky; | ||||
| if (type == NODE_SKY_PREETHAM) | if (type == NODE_SKY_PREETHAM) | ||||
| sky_texture_precompute_preetham(&sunsky, sun_direction, turbidity); | sky_texture_precompute_preetham(&sunsky, sun_direction, turbidity); | ||||
| else if (type == NODE_SKY_HOSEK) | else if (type == NODE_SKY_HOSEK) | ||||
| sky_texture_precompute_hosek(&sunsky, sun_direction, turbidity, ground_albedo); | sky_texture_precompute_hosek(&sunsky, sun_direction, turbidity, ground_albedo); | ||||
| else if (type == NODE_SKY_NISHITA) { | else if (type == NODE_SKY_NISHITA) { | ||||
| /* clamp altitude to avoid intersection issues */ | |||||
| altitude = 1000.0f * clamp(altitude, 0.001f, 59.999f); | |||||
| sky_texture_precompute_nishita(&sunsky, | sky_texture_precompute_nishita(&sunsky, | ||||
| sun_disc, | sun_disc, | ||||
| sun_size, | sun_size, | ||||
| sun_intensity, | |||||
| sun_elevation, | sun_elevation, | ||||
| sun_rotation, | sun_rotation, | ||||
| altitude, | altitude, | ||||
| air_density, | air_density, | ||||
| dust_density); | dust_density); | ||||
| /* precomputed texture image parameters */ | /* precomputed texture image parameters */ | ||||
| ImageManager *image_manager = compiler.scene->image_manager; | ImageManager *image_manager = compiler.scene->image_manager; | ||||
| ImageParams impar; | ImageParams impar; | ||||
| impar.interpolation = INTERPOLATION_LINEAR; | impar.interpolation = INTERPOLATION_CUBIC; | ||||
| impar.extension = EXTENSION_EXTEND; | impar.extension = EXTENSION_EXTEND; | ||||
| /* precompute sky texture */ | /* precompute sky texture */ | ||||
| if (handle.empty()) { | if (handle.empty()) { | ||||
| SkyLoader *loader = new SkyLoader( | SkyLoader *loader = new SkyLoader( | ||||
| sun_elevation, altitude, air_density, dust_density, ozone_density); | sun_elevation, altitude, air_density, dust_density, ozone_density); | ||||
| handle = image_manager->add_image(loader, impar); | handle = image_manager->add_image(loader, impar); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 6,112 Lines • Show Last 20 Lines | |||||
Hm, this seems unnecessarily complicated. I think something based on fmodf() should be cleaner.