| |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
-> 游戏开发 -> 《Unity Shader 入门精要》MyProject: letsStartShader笔记 03 -> 正文阅读 |
|
[游戏开发]《Unity Shader 入门精要》MyProject: letsStartShader笔记 03 |
1. Texture for Diffusestep 1 : prepare resources? ? ? ? Load the texture in your shader:
step 2 : prepare containers? ? ? ? Notice how the uv coordinates are stored.
step 3 : sample the picture and?calculate the color????????tex2D(Texture from sampler2D, uv coordinate)? returns rgba(fixed4) per coordinate.
2. Bump Mapping (the MOST IMPORTANT !!!)0.1 . Why bump mapping?? ? ? ? We calculate light and shadow by dot(lightDir, normalDir). Imagine that the normals on a??same plane are different. Now you will get a plane with different gray levels (occlusion). In general,?it?requires a lot of resources to get such a bumpy surface, but now you can achieve it by using shader tricks! shader tricks! 0.2 . coordinate range difference? ? ? ? tex2D(tex, uv) returns rgba in [0, 1], but the coordinate range of normal is [-1, 1]. So you need : 0.3 saturate()? ? ? ? Well, at least I think it's jut for security to use this now, including the use of inverse transpose matrix in the normal conversion above. I still need a better?understanding of this?in the future.?
1. tangent-space normal map????????We want to store every?normal of the model?in the texture map.?Naturally we may think of that in object space. But tangent-space normal map is used more often. ????????In short, tangent space allows you to freely control the direction of the new normal through relative coordinates. And you can compress the value of Z direction in tangent space (when orthonormal). 2. where?to calculate Lighting Model? ? ? ? You've got all changed normals in tangent space by?a?tangent-space normal map. Next you need to calculate the light and shadow. You'd better use the following two methods to calculate: ? ? ? ? · Calculate in tangent space. You need to transform the lightDir and viewDir into tangent space. By this way you can get all containers ready?before fragment shader starts, and never have to change them. ? ? ? ? · Calculate in world space. You need to transform all changed normal into world space. You need an extra matrix in fragment shader. Sometimes we use this method because some calculation couldn't be done in tangent space (something about cubemap). A. Calculate in tangent spacereminderTANGENT (float4 xyzw) ? ? ? ??3D object's each tangent in object space, corresponding?to each normal. w is used to determine the direction of?B axis. (let's see something about linear algebra...) x or T?axis: tangent? ? ? ?y?or B?axis: binormal? ? ? ? z?or N?axis: normal? ? ? ? they are orthonormal matrix : tangent?space --> object?space????????==》????????matrix : object space --> tangent space ????????inverse???????? step 1 : prepare resources
step 2 : prepare containers?? ? ? ? TEXCOORDn in v2f stores two sets of coordinate. But for most time, they point to a same coordinate, because MainTex and BumpMap's original pictures are usually the same. ????????The way you calculate the lighting model has changed. You no longer need worldPos and worldNormal, because you've give the right Directions of light and view.
step 3 : sample the picture and?calculate the color? ? ? ? When you mark?a texture as "Normal map", Unity will compress?it. If compressed by DXT5nm, when tex2D() return the rgba, a --> T axis?component,? g --> B axis?component, r and b will be deserted. ? ? ? ? UnpackNormal(packedNormal) returns?xyz of the changed normals. After this, _BumpScale changes them?again.
B. Calculate in world?spacereminder? ? ? ? To transform the changed normals from tangent space to world space, you need to know how to express every Tangent, Binormal and Normal in world space. Differences from calculations in tangent space1. v2f? ? ? ? Before fragment shader, you don't need to calculate anything about lightDir and viewDir. Instead, you need to give the matrix that transform directions from tangent space to world space.
2.vertex shader????????The float3 of TBN in world space is just the expression of the basis of tangent space.
3.fragment shader? ? ? ? ?Calculate the lightDir and viewDir in fragment shader. Transform bump normals from tangent space to world space.?Note that the calculation about angle (dot()) is always finished in fragment shader.
From this page, the original book no longer gives the complete code (I modified part of the code in frag() to run it successfully) , so I will not provide the final code as well. Please try it yourself! 3. Ramp texture? ? ? ? Now dot() doesn't determine the light and shadow, but directly determines the color on a ramp texture.
4. Mask Texture? ? ? ? Control the specular level by a value.
tips: I'm not sure?if it's appropriate to call Semantics?'container', but ... well, at least not completely wrong :) |
|
|
上一篇文章 下一篇文章 查看所有文章 |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年1日历 | -2025/1/17 5:58:44- |
|
网站联系: qq:121756557 email:121756557@qq.com IT数码 |