Примеры использования vrayGLSL |
Пример 10: Визуализация каналов
Search Keywords: GLSL, vrayGLSL, shading language
Этот пример генерирует материал, выглядящий как "металлик".
Визуализированный шейдер Параметры шейдера
Исходный код шейдера:
File: cool_metallic.frag #version 110
uniform vr_Color front_color = vr_Color(1.0, 1.0, 0.0, 1.0);
uniform vr_Color background_color = vr_Color(1.0, 0.0, 1.0, 1.0);
uniform float amount = 0.8;
uniform float diffuse_scalar = 0.7;
uniform float specular_scalar = 0.06;
uniform float specular_shininess = 200.0;
float phong_specular(vec3 light_dir, vec3 view_dir, vec3 normal, float specular_shininess) {
vec3 reflection = reflect(view_dir, normal);
float cos_rl = clamp(dot(reflection, light_dir), 0.0, 1.0);
return (pow(cos_rl, specular_shininess) * (specular_shininess + 2.0));
}
void main() {
vec3 normal = (gl_FrontFacing) ? vr_Normal : -vr_Normal;
vec3 position = vr_Position;
vec3 direction = vr_Direction;
float dot_dn = clamp(-dot(direction, normal), 0.0, 1.0);
float mix_value = 1.0 - pow(dot_dn, 1.0 / amount);
vec4 diffuse_color = mix(front_color, background_color, mix_value);
vec4 specular_color = vec4(1.0, 1.0, 1.0, 1.0);
vec4 diffuse = vec4(0.0, 0.0, 0.0, 1.0);
vec4 specular = vec4(0.0, 0.0, 0.0, 1.0);
vec4 raw_diffuse = vec4(vec3(diffuse_color) * diffuse_scalar, 1.0);
vec4 raw_specular = vec4(vec3(specular_color) * specular_scalar, 1.0);
vec4 temp = raw_diffuse + raw_specular;
float max_color_comp = max(max(temp.r,temp.g),temp.b);
if (max_color_comp > 1.0) {
raw_diffuse.rgb /= max_color_comp;
raw_specular.rgb /= max_color_comp;
}
vr_LightIterator light;
for (int i = 0; i < vr_NumLights; ++i) {
vr_evalLight(i, position, normal, light);
float cos_nl = clamp(light.dot_nl, 0.0, 1.0);
if (cos_nl > 0.0) {
diffuse += vec4(vec3(cos_nl) * light.contribution, 0.0);
float specular_coeff = phong_specular(light.direction, direction, normal, specular_shininess);
specular += vec4(vec3(specular_coeff * cos_nl) * light.contribution, 0.0);
}
}
diffuse *= raw_diffuse;
specular *= raw_specular;
vec3 gi_contribution = vr_irradiance(normal, 1.0);
diffuse += vec4(raw_diffuse.rgb * gi_contribution, 0.0);
gl_FragColor = diffuse + specular;
}
Этот пример создаёт шейдер с цветными полосками.
Визуализированный шейдер Параметры шейдера
Исходный код шейдера:
File: generator_strip.frag #version 110
uniform vr_Color strip_color = vr_Color(1.0, 0.5, 1.0, 1.0);
uniform vr_Color background_color = vr_Color(1.0, 1.0, 0.2, 1.0);
uniform float width = 0.5;
uniform float fuzz = 0.5;
uniform float scale = 5.0;
uniform float diffuse_scalar = 0.7;
uniform float specular_scalar = 0.06;
uniform float specular_shininess = 10.0;
float phong_specular(vec3 light_dir, vec3 view_dir, vec3 normal, float specular_shininess) {
vec3 reflection = reflect(view_dir, normal);
float cos_rl = clamp(dot(reflection, light_dir), 0.0, 1.0);
return (pow(cos_rl, specular_shininess) * (specular_shininess + 2.0));
}
void main() {
vec3 normal = (gl_FrontFacing) ? vr_Normal : -vr_Normal;
vec3 position = vr_Position;
vec3 direction = vr_Direction;
float scaled_coord_t = fract(gl_TexCoord[0].t * scale);
float fract1 = clamp(scaled_coord_t / fuzz, 0.0, 1.0);
float fract2 = clamp((scaled_coord_t - width) / fuzz, 0.0, 1.0);
fract1 *= (1.0 - fract2);
fract1 = smoothstep(0.0, 1.0, fract1);
vec4 diffuse_color = mix(strip_color, background_color, fract1);
vec4 specular_color = vec4(1.0, 1.0, 1.0, 1.0);
vec4 diffuse = vec4(0.0, 0.0, 0.0, 1.0);
vec4 specular = vec4(0.0, 0.0, 0.0, 1.0);
vec4 raw_diffuse = vec4(vec3(diffuse_color) * diffuse_scalar, 1.0);
vec4 raw_specular = vec4(vec3(specular_color) * specular_scalar, 1.0);
vec4 temp = raw_diffuse + raw_specular;
float max_color_comp = max(max(temp.r,temp.g),temp.b);
if (max_color_comp > 1.0) {
raw_diffuse.rgb /= max_color_comp;
raw_specular.rgb /= max_color_comp;
}
vr_LightIterator light;
for(int i = 0; i < vr_NumLights; ++i) {
vr_evalLight(i, position, normal, light);
float cos_nl = clamp(light.dot_nl, 0.0, 1.0);
if (cos_nl > 0.0) {
diffuse += vec4(vec3(cos_nl) * light.contribution, 0.0);
float specular_coeff = phong_specular(light.direction, direction, normal, specular_shininess);
specular += vec4(vec3(specular_coeff * cos_nl) * light.contribution, 0.0);
}
}
diffuse *= raw_diffuse;
specular *= raw_specular;
vec3 gi_contribution = vr_irradiance(normal, 1.0);
diffuse += vec4(raw_diffuse.rgb * gi_contribution, 0.0);
gl_FragColor = diffuse + specular;
}
Глянцевитое стекло
Визуализированный шейдер Параметры шейдера
Исходный код шейдера:
File: glossy_glass.frag #version 110
const int requiredSamples = 8;
uniform float refraction_ior = 1.44;
uniform float amount = 0.1;
uniform float blend = 1.0;
void main() {
vec3 normal = (gl_FrontFacing) ? vr_Normal : -vr_Normal;}
vec3 direction = vr_Direction;
vr_TraceOptions options; options.rayGroup = VR_SPECULAR_DISPERSAL;
options.ior = refraction_ior;
options.normal = normal;if((vr_RayFlags & VR_INDIRECT_FLAG) != 0) {
options.rayType = VR_TRACE_REFLECT;}
vec4 reflection = vr_trace(options);
options.rayType = VR_TRACE_REFRACT;
vec4 refraction = vr_trace(options);
float cos_dn = clamp(-dot(direction, normal), 0.0, 1.0);
float lerpFactor = (1.0 - pow(cos_dn, 1.0 / blend));
gl_FragColor = mix(refraction, reflection, lerpFactor);
return;
vec3 tangent = vr_TexTangent;
vec3 binormal = vr_TexBinormal;
int numSuperSamples = vr_NumSuperSamples;
int superSampleIndex = vr_SuperSampleIndex;
float sum = 0.0;
float weights[requiredSamples];
vec4 samples[requiredSamples];
vec4 result = vec4(0.0);
// determine how much more samples will be taken for this hit point
int nSamples = (requiredSamples > numSuperSamples) ? requiredSamples : numSuperSamples; // max
nSamples /= numSuperSamples;
int startSampleIndex = superSampleIndex * nSamples;
options.rayGroup = VR_GLOSSY_DISPERSAL;
for(int i = 0; i < nSamples; ++i) {
vec3 randomSample = vr_randomSample(startSampleIndex + i, nSamples);}
vec2 offset = randomSample.xy - 0.5;
offset *= (2.0 * amount);
vec3 bumpedNormal = normal + offset.x * tangent + offset.y * binormal;
options.normal = bumpedNormal;
options.rayType = VR_TRACE_REFLECT; vec4 reflection = vr_trace(options);
options.rayType = VR_TRACE_REFRACT; vec4 refraction = vr_trace(options);
float cos_dn = clamp(-dot(direction, bumpedNormal), 0.0, 1.0);
float lerpFactor = (1.0 - pow(cos_dn, 1.0 / blend));
samples[i] = mix(refraction, reflection, lerpFactor);
weights[i] = dot(normal, bumpedNormal);
sum += weights[i];
float inverseSum = 1.0 / sum;
for(int i = 0; i < nSamples; ++i) {
weights[i] *= inverseSum;}
result += weights[i] * samples[i];
gl_FragColor = result;
Этот шейдер реализует модифицированную модель Фонга (Phong) с нарушением нормалей.
Визуализированный шейдер Параметры шейдера
Исходный код шейдера:
File: glossy_phong.frag #version 110
uniform float amount = 0.1;
uniform float diffuse_scalar = 0.7;
uniform vr_Color diffuse_color = vr_Color(0.9, 0.2, 0.8, 1.0);
uniform float specular_scalar = 0.06;
uniform float specular_shininess = 20.0;
const int requiredSamples = 16;
float phong_specular(vec3 light_dir, vec3 direction, vec3 normal) {
vec3 reflection = reflect(direction, normal);}
float cos_rl = clamp(dot(reflection, light_dir), 0.0, 1.0);
return (pow(cos_rl, specular_shininess) * (specular_shininess + 2.0));
void main() {
bool isGIRay = ((vr_RayFlags & VR_INDIRECT_FLAG) != 0);}
vec3 normal = (gl_FrontFacing) ? vr_Normal : -vr_Normal;
vec3 direction = vr_Direction;
vec3 position = vr_Position;
vec4 raw_diffuse = vec4(vec3(diffuse_color) * diffuse_scalar, 1.0);
vec4 raw_specular = vec4(vec3(specular_scalar), 1.0);
vec4 temp = raw_diffuse + raw_specular;
float max_color_comp = max(max(temp.r,temp.g),temp.b);
if (max_color_comp > 1.0) {
raw_diffuse.rgb /= max_color_comp;}
raw_specular.rgb /= max_color_comp;
vec4 diffuse_contrib = vec4(0.0, 0.0, 0.0, 1.0);
vec4 specular_contrib = vec4(0.0, 0.0, 0.0, 1.0);
// if this is a GI ray do not try to estimate glossy highlights accurately
if(isGIRay) {
vr_LightIterator light;} else {
for(int j = 0; j < vr_NumLights; ++j) {
vr_evalLight(j, position, normal, light);}
float cos_nl = clamp(light.dot_nl, 0.0, 1.0);
if (cos_nl > 0.0) {
diffuse_contrib += vec4(cos_nl * light.contribution, 0.0);}
float specular_coeff = phong_specular(light.direction, direction, normal);
specular_contrib += vec4(specular_coeff * cos_nl * light.contribution, 0.0);
vec3 tangent = vr_TexTangent;}
vec3 binormal = vr_TexBinormal;
int numSuperSamples = vr_NumSuperSamples;
int superSampleIndex = vr_SuperSampleIndex;
float sum = 0.0;
float weights[requiredSamples];
vec4 specular_samples[requiredSamples];
// determine how much more samples will be taken for this hit point
int nSamples = (requiredSamples > numSuperSamples) ? requiredSamples : numSuperSamples; // max
nSamples /= numSuperSamples;
int startSampleIndex = superSampleIndex * nSamples;
for(int i = 0; i < nSamples; ++i) {
vec3 randomSample = vr_randomSample(startSampleIndex + i, nSamples);}
vec2 offset = (randomSample.xy - 0.5) * 2.0;
offset *= amount;
vec3 bumpedNormal = normalize(normal + offset.x * tangent + offset.y * binormal);
vr_LightIterator light;
for(int j = 0; j < vr_NumLights; ++j) {
vr_evalLight(j, position, bumpedNormal, light);}
float cos_nl = clamp(light.dot_nl, 0.0, 1.0);
if(cos_nl > 0.0) {
diffuse_contrib += vec4(cos_nl * light.contribution, 0.0);}
float specular_coeff = phong_specular(light.direction, direction, bumpedNormal);
specular_samples[i] += vec4(specular_coeff * light.contribution, 0.0);
weights[i] = dot(normal, bumpedNormal);
sum += weights[i];
float inverseSum = 1.0 / sum;
for(int i = 0; i < nSamples; ++i) {
weights[i] *= inverseSum;}
specular_contrib += weights[i] * specular_samples[i];
diffuse_contrib /= float(nSamples);
diffuse_contrib += vec4(vr_irradiance(normal, 1.0), 0.0);
diffuse_contrib *= raw_diffuse;
specular_contrib *= raw_specular;
gl_FragColor = diffuse_contrib + specular_contrib;
Этот шейдер реализует размытое отражение на основании модели Фонга (Phong):
Визуализированный шейдер Параметры шейдера
Исходный код шейдера:
File: glossy_reflection.frag #version 110
uniform float amount = 0.1;
const int requiredSamples = 16;
void main() {
vec3 normal = (gl_FrontFacing) ? vr_Normal : -vr_Normal;}
vr_TraceOptions options;
options.rayType = VR_TRACE_REFLECT;
options.rayGroup = VR_SPECULAR_DISPERSAL;
options.normal = normal;
if((vr_RayFlags & VR_INDIRECT_FLAG) != 0) {
gl_FragColor = vr_trace(options);}
return;
vec3 tangent = vr_TexTangent;
vec3 binormal = vr_TexBinormal;
int numSuperSamples = vr_NumSuperSamples;
int superSampleIndex = vr_SuperSampleIndex;
float sum = 0.0;
float weights[requiredSamples];
vec4 samples[requiredSamples];
vec4 result = vec4(0.0);
// determine how much more samples will be taken for this hit point
int nSamples = (requiredSamples > numSuperSamples) ? requiredSamples : numSuperSamples; // max
nSamples /= numSuperSamples;
int startSampleIndex = superSampleIndex * nSamples;
options.rayGroup = VR_GLOSSY_DISPERSAL;
for(int i = 0; i < nSamples; ++i) {
vec3 randomSample = vr_randomSample(startSampleIndex + i, nSamples);}
vec2 offset = (randomSample.xy - 0.5) * 2.0;
offset *= amount;
vec3 bumpedNormal = normalize(normal + offset.x * tangent + offset.y * binormal);
options.normal = bumpedNormal;
samples[i] = vr_trace(options);
weights[i] = dot(normal, bumpedNormal);
sum += weights[i];
float inverseSum = 1.0 / sum;
for(int i = 0; i < nSamples; ++i) {
weights[i] *= inverseSum;}
result += weights[i] * samples[i];
gl_FragColor = result;
Этот шейдер реализует модель Oren-Nayar для прямого освещения.
Визуализированный шейдер Параметры шейдера
Исходный код шейдера:
File: oren_nayar.frag #version 110
uniform vr_Color diffuse_color = vr_Color(1.0, 1.0, 0.0, 1.0);
uniform float diffuse_scalar = 0.7;
uniform vr_Color specular_color = vr_Color(1.0, 1.0, 1.0 ,1.0);
uniform float specular_scalar = 0.06;
uniform float diffuse_deviation = 0.5;
uniform float specular_shininess = 10.0;
float orennayar_diffuse(vec3 light_dir, vec3 view_dir, vec3 normal) {
float sigma_sqred = diffuse_deviation * diffuse_deviation;}
float A = 1.0 - (sigma_sqred / (2.0 * (sigma_sqred + 0.33)));
float B = (0.45 * sigma_sqred) / (sigma_sqred + 0.09);
float cosThetaI = dot(light_dir, normal);
float cosThetaO = -dot(view_dir, normal);
float sinThetaI = sqrt(max(0.0, 1.0 - (cosThetaI * cosThetaI)));
float sinThetaO = sqrt(max(0.0, 1.0 - (cosThetaO * cosThetaO)));
float cosPhiI = dot(light_dir, vr_TexTangent);
float cosPhiO = -dot(view_dir, vr_TexTangent);
float sinPhiI = sqrt(max(0.0, 1.0 - (cosPhiI * cosPhiI)));
float sinPhiO = sqrt(max(0.0, 1.0 - (cosPhiO * cosPhiO)));
float temp = max(0.0, (cosPhiI * cosPhiO) + (sinPhiI * sinPhiO));
float sinAlpha;
float tanBeta;
if (cosThetaI > cosThetaO) {
sinAlpha = sinThetaO;}
tanBeta = (sinThetaI / cosThetaI);
else {
sinAlpha = sinThetaI;}
tanBeta = (sinThetaO / cosThetaO);
float result = (A + (B * temp * sinAlpha * tanBeta));
return clamp(result, 0.0, 1.0);
float phong_specular(vec3 light_dir, vec3 view_dir, vec3 normal) {
vec3 reflection = reflect(view_dir, normal);}
float cos_rl = clamp(dot(reflection, light_dir), 0.0, 1.0);
return (pow(cos_rl, specular_shininess) * (specular_shininess + 2.0));
void main() {
}
vec3 normal = (gl_FrontFacing) ? vr_Normal : -vr_Normal;
vec4 diffuse = vec4(0.0, 0.0, 0.0, 1.0);
vec4 specular = vec4(0.0, 0.0, 0.0, 1.0);
vec4 diffuse_contrib = vec4(vec3(diffuse_color) * diffuse_scalar, 1.0);
vec4 specular_contrib = vec4(vec3(specular_color) * specular_scalar, 1.0);
vec4 temp = diffuse_contrib + specular_contrib;
float max_color_component = max(max(temp.r,temp.g),temp.b);
if (max_color_component > 1.0) {
diffuse_contrib.rgb /= max_color_component;}
specular_contrib.rgb /= max_color_component;
vec3 view_dir = vr_Direction;
vr_LightIterator lightIter;
for(int i = 0; i < vr_NumLights; ++i) {
vr_evalLight(i, vr_Position, normal, lightIter);}
float cos_nl = clamp(lightIter.dot_nl, 0.0, 1.0);
if (cos_nl > 0.0) {
float diffuse_term = orennayar_diffuse(lightIter.direction, view_dir, normal);}
diffuse += vec4(vec3(diffuse_term * cos_nl) * lightIter.contribution, 0.0);
float specular_term = phong_specular(lightIter.direction, view_dir, normal);
specular += vec4(vec3(specular_term * cos_nl) * lightIter.contribution, 0.0);
diffuse *= diffuse_contrib;
specular *= specular_contrib;
vec3 gi_contrib = vr_irradiance(normal, 1.0);
diffuse += vec4(vec3(diffuse_contrib) * gi_contrib, 0.0);
gl_FragColor = diffuse + specular;
Этот шейдер изменяет свой цвет на основании фрактальной текстуры.
Визуализированный шейдер Параметры шейдера
Исходный код шейдера:
File: turbulence.frag #version 110
uniform float noise_scale = 2.0;
uniform float noise_gain = 0.8;
uniform float noise_lacunarity = 3.0;
uniform vr_Color color1 = vr_Color(1.0, 1.0, 0.0, 1.0);
uniform vr_Color color2 = vr_Color(1.0, 0.0, 0.0, 1.0);
uniform float diffuse_scalar = 0.8;
uniform float specular_scalar = 0.02;
uniform float specular_shininess = 10.0;
const int octaves_count = 4;
float phong_specular(vec3 light_dir, vec3 view_dir, vec3 normal, float specular_shininess) {
vec3 reflection = reflect(view_dir, normal);}
float cos_rl = clamp(dot(reflection, light_dir), 0.0, 1.0);
return (pow(cos_rl, specular_shininess) * (specular_shininess + 2.0));
void main() {
vec3 normal = (gl_FrontFacing) ? vr_Normal : -vr_Normal;}
vec3 position = vr_Position;
vec3 direction = vr_Direction;
vec3 noise_position = vec3(gl_TexCoord[0]);
vec3 scaled_position = noise_position * vec3(noise_scale);
float noise_amplitude = 2.0;
float noise_frequency = 0.5;
vec3 noise_value = vec3(0.0);
if((vr_RayFlags & VR_INDIRECT_FLAG) != 0) {
noise_value = vec3(0.5);}
else {
for (int i = 0; i < octaves_count; ++i) {}
noise_value += abs(noise3(scaled_position * vec3(noise_frequency))) * vec3(noise_amplitude);}
noise_frequency *= noise_lacunarity;
noise_amplitude *= noise_gain;
vec4 diffuse_color = mix(color1, color2, vec4(noise_value, 1.0));
vec4 specular_color = vec4(1.0, 1.0, 1.0, 1.0);
vec4 diffuse = vec4(0.0, 0.0, 0.0, 1.0);
vec4 specular = vec4(0.0, 0.0, 0.0, 1.0);
vec4 raw_diffuse = vec4(vec3(diffuse_color) * diffuse_scalar, 1.0);
vec4 raw_specular = vec4(vec3(specular_color) * specular_scalar, 1.0);
vec4 temp = raw_diffuse + raw_specular;
float max_color_comp = max(max(temp.r,temp.g),temp.b);
if (max_color_comp > 1.0) {
raw_diffuse.rgb /= max_color_comp;}
raw_specular.rgb /= max_color_comp;
vr_LightIterator light;
for(int i = 0; i < vr_NumLights; ++i) {
vr_evalLight(i, position, normal, light);}
float cos_nl = clamp(light.dot_nl, 0.0, 1.0);
if (cos_nl > 0.0) {
diffuse += vec4(vec3(cos_nl) * light.contribution, 0.0);}
float specular_coeff = phong_specular(light.direction, direction, normal, specular_shininess);
specular += vec4(vec3(specular_coeff * cos_nl) * light.contribution, 0.0);
diffuse *= raw_diffuse;
specular *= raw_specular;
vec3 gi_contribution = vr_irradiance(normal, 1.0);
diffuse += vec4(raw_diffuse.rgb * gi_contribution, 0.0);
gl_FragColor = diffuse + specular;
Материал основан на модели BRDF Блинна (Blinn).
Визуализированный шейдер Параметры шейдера
Исходный код шейдера:
File: blinn_material.frag #version 110
uniform sampler2D diffuseTex;
uniform vr_Color specular = vr_Color(1.0, 1.0, 1.0, 1.0);
uniform float highlightGlossiness = 0.8;
uniform sampler2D anisotropyTex;
void main() {
vec3 normal = (gl_FrontFacing) ? vr_Normal : -vr_Normal;}
vec2 uv_coords = vec2(gl_TexCoord[0]);
vr_DiffuseBRDFSettings diffuseSettings;
diffuseSettings.color = vec3(texture2D(diffuseTex, uv_coords));
diffuseSettings.normal = normal;
vr_brdf_diffuse(diffuseSettings);
vr_GlossyBRDFSettings blinnSettings;
blinnSettings.color = vec3(specular);
blinnSettings.normal = normal;
blinnSettings.highlightGlossiness = highlightGlossiness;
blinnSettings.anisotropy = 2.0 * texture2D(anisotropyTex, uv_coords).x - 1.0;
blinnSettings.traceReflections = false;
vr_brdf_blinn(blinnSettings);
gl_FragColor = vec4(vec3(0.0), 1.0);
Прозрачный материал, использующий вывод альфа-канала.
Визуализированный шейдер Параметры шейдера
Исходный код шейдера:
File: transparent_material.frag #version 110
uniform vr_Color diffuse = vr_Color(0.6, 0.2, 1.0, 1.0);
uniform sampler2D transparency;
void main() {
vec3 normal = (gl_FrontFacing) ? vr_Normal : -vr_Normal;}
vr_DiffuseBRDFSettings diffuseSettings;
diffuseSettings.color = vec3(diffuse);
diffuseSettings.normal = normal;
vr_brdf_diffuse(diffuseSettings);
vec3 textureSample = vec3(texture2D(transparency, vec2(gl_TexCoord[0]))); float alpha = dot(textureSample, vec3(0.3333333));
gl_FragColor = vec4(vec3(0.0), alpha);
Шейдер предназначен для визуализации каналов в V-Ray VFB.
Визуализированный шейдерr Параметры шейдера
Исходный код шейдера:
File: render_channel.frag #version 110
uniform vr_Color diffuse = vr_Color(0.3, 0.8, 0.2, 1.0);
__channel vec3 NormalSpace = vec3(0.314);
void main() {
vec3 normal = (gl_FrontFacing) ? vr_Normal : -vr_Normal;}
NormalSpace = normal;
vr_DiffuseBRDFSettings diffuseSettings;
diffuseSettings.color = vec3(diffuse);
diffuseSettings.normal = normal;
vr_brdf_diffuse(diffuseSettings);
gl_FragColor.a = 1.0;
Перевод © Black Sphinx, 2008-2013. All rights reserved.