你在該行color = vec3(depth)
輸出的顏色。函數vec3的這個變體創建了一個向量,其中所有3個部分都等於輸入值。您也可以使用它來創建3部分矢量:color = vec3(red, geen, blue)
。如果您還想將Alpha值傳遞給片段着色器,則必須將其更改爲vec4(紅色,綠色,藍色,alpha),並且還要在頂點着色器和片段着色器中將varying vec3 color
的聲明更改爲varying vec4 color
。
ChromaDepth用於從深度計算正確的紅色,綠色和藍色值的算法已發佈here。這是相關部分:
//Definition of 3d_red component of the color. The value show be between 1
//and 0 over the Range of 0 to 0.75. It should be 0 for all Ranges greater
//than 0.75. From 0 to 0.75 it is calculated by Red_func.
define Red_Range Range/0.9
define Red_func
(-2.13*Red_Range^4-1.07*Red_Range^3+0.133*Red_Range^2+0.0667*Red_Range+1)
define Cc (Red_func <0 || Red_Range>0.75 ? 0:1)
define Dd (Red_func >1 ? 1:0)
define 3d_red (Red_Range<0.75 ? Red_func:Cc*Dd)
//Definition of 3d_green component of the color. The value should be between
//0 and 1 over the Range of 0 to 1, starting from 0, rising to 1, then falling
//to 0 again. It should be 0 at both extremes of Range.
define Green_func1 (1.6*Range^2+1.2*Range)
define Green_func2 (3.2*Range^2-6.8*Range+3.6)
define 3d_green (Range<=0.5 ? Green_func1:Green_func2)
//Definition of 3d_blue component of the color. The value should rise from
//0 at a Range of 0.5 up to 1 at a Range of 1. Below Range 0.5 the value
//must be 0.
define Blue_func (-4.8*Range^2+9.2*Range-3.4)
define 3d_blue (Range>0.5 ? Blue_func:0)
這些函數的輸入值是「範圍」,這是你所說的在你的代碼「深度」(0.0最接近和1.0是最遠)。