Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

GLSL Programming/Unity/Curved Glass

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

250px|thumb|left|Crystal balls are examples of curved, transparent surfaces. This tutorial covers refraction mapping and its implementation with cube maps. It is a variation of Reflecting Surfaces, which should be read first. Refraction Mapping In Reflecting Surfaces, we reflected view rays and then performed texture lookups in a cube map in the reflected direction. Here, we refract view rays at a curved, transparent surface and then perform the lookups with the refracted direction. The effect will ignore the second refraction when the ray leaves the transparent object again; however, many people hardly notice the differences since such refractions are usually not part of our daily life. Instead of the reflect function, we are using the refract function; thus, the fragment shader could be: #ifdef FRAGMENT void main() { float refractiveIndex = 1.5; vec3 refractedDirection = refract(normalize(viewDirection), normalize(normalDirection), 1.0 / refractiveIndex); gl_FragColor = textureCube(_Cube, refractedDirection); } #endif Note that refract takes a third argument, which is the refractive index of the outside medium (e.g. 1.0 for air) divided by the refractive index of the object (e.g. 1.5 for some kinds of glass). Also note that the first argument has to be normalized, which isn't necessary for reflect. Complete Shader Code With the adapted fragment shader, the complete shader code becomes: Shader "GLSL shader with refraction mapping" { Properties { _Cube ("Environment Map", Cube) = "" {} } SubShader { Pass { GLSLPROGRAM // User-specified uniforms uniform samplerCube _Cube; // The following built-in uniforms // are also defined in "UnityCG.glslinc", // i.e. one could #include "UnityCG.glslinc" uniform vec3 _WorldSpaceCameraPos; // camera position in world space uniform mat4 _Object2World; // model matrix uniform mat4 _World2Object; // inverse model matrix // Varyings varying vec3 normalDirection; varying vec3 viewDirection; #ifdef VERTEX void main() { mat4 modelMatrix = _Object2World; mat4 modelMatrixInverse = _World2Object; // unity_Scale.w // is unnecessary because we normalize vectors normalDirection = normalize(vec3( vec4(gl_Normal, 0.0) * modelMatrixInverse)); viewDirection = vec3(modelMatrix * gl_Vertex - vec4(_WorldSpaceCameraPos, 1.0)); gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } #endif #ifdef FRAGMENT void main() { float refractiveIndex = 1.5; vec3 refractedDirection = refract(normalize(viewDirection), normalize(normalDirection), 1.0 / refractiveIndex); gl_FragColor = textureCube(_Cube, refractedDirection); } #endif ENDGLSL } } } Summary Congratulations. This is the end of another tutorial. We have seen: How to adapt reflection mapping to refraction mapping using the refract instruction. Further Reading If you still want to know more about reflection mapping and cube maps, you should read Reflecting Surfaces. about the refract instruction, you could look it up in the “OpenGL ES Shading Language 1.0.17 Specification” available at the “Khronos OpenGL ES API Registry”.