위의 프래그먼트 셰이더에서는 항상 두 개의 텍스처를 모두 읽게 됩니다.
Texture Lookup Functions
@@ -767,7 +767,7 @@ derivatives are undefined within non-uniform control flow and for vertex texture
다시 말해 텍스처를 사용하는 경우 항상 텍스처에 접근할 수 있어야 한다는 것입니다.
결과를 조건부로 사용할 수는 있습니다.
예를 들어 아래와 같이 작성하거나
-
+{{#escapehtml}}
vec4 projectedTexColor = texture(u_projectedTexture, projectedTexcoord.xy);
vec4 texColor = texture(u_texture, v_texcoord) * u_colorMult;
@@ -776,14 +776,14 @@ derivatives are undefined within non-uniform control flow and for vertex texture
} else {
gl_FragColor = texColor;
}
-
+{{/escapehtml}}
아래와 같이 작성할 수 있습니다.
-
+{{#escapehtml}}
vec4 projectedTexColor = texture(u_projectedTexture, projectedTexcoord.xy);
vec4 texColor = texture(u_texture, v_texcoord) * u_colorMult;
gl_FragColor = inRange ? projectedTexColor : texColor;
-
+{{/escapehtml}}
하지만 텍스처 접근 자체를 조건부로 만들 수는 없습니다.
어떤 GPU에서는 작동될 수 있지만 모든 GPU에서 작동이 보장되지는 않습니다.
diff --git a/webgl/lessons/ko/webgl1-to-webgl2.md b/webgl/lessons/ko/webgl1-to-webgl2.md
index 5cd9de1e3..5b68e11e6 100644
--- a/webgl/lessons/ko/webgl1-to-webgl2.md
+++ b/webgl/lessons/ko/webgl1-to-webgl2.md
@@ -355,14 +355,14 @@ extension을 사용하는 것처럼 작성하시면 안되고 조금은 수정
WebGL1 extension을 WebGL2처럼 만들기
WebGL1의 extension에 있던 함수들은, WebGL2에서는 extension 없이 사용할 수 있습니다. 예를 들면, WebGL1에서는 아래와 같았지만
-
+{{#escapehtml}}
var ext = gl.getExtension("OES_vertex_array_object");
if (!ext) {
// tell user they don't have the required extension or work around it
} else {
var someVAO = ext.createVertexArrayOES();
}
-
+{{/escapehtml}}
WebGL2 에서는 이렇죠.
@@ -373,7 +373,7 @@ var someVAO = gl.createVertexArray();
좀 어려울 수 있습니다.
한 가지 해결 방법은, 초기화할 때 WebGL1 extension을 WebGL context에 복사하는 것입니다.
그렇게 하면 나머지 코드는 그대로입니다. 예시:
-
+{{#escapehtml}}
const gl = someCanvas.getContext("webgl");
const haveVAOs = getAndApplyExtension(gl, "OES_vertex_array_object");
@@ -412,18 +412,18 @@ function getAndApplyExtension(gl, name) {
}
return ext;
}
-
+{{/escapehtml}}
이제 이 코드는 WebGL1과 WebGL2 에서 대부분 똑같이 동작할 겁니다. 예시:
-
+{{#escapehtml}}
if (haveVAOs) {
var someVAO = gl.createVertexArray();
...
} else {
... do whatever for no VAOs.
}
-
+{{/escapehtml}}
아니면 이런식으로 적어야겠죠.
-
+{{#escapehtml}}
if (haveVAOs) {
if (isWebGL2)
someVAO = gl.createVertexArray();
@@ -434,7 +434,7 @@ if (haveVAOs) {
} else {
... do whatever for no VAOs.
}
-
+{{/escapehtml}}
참고: Vertex Array Objects를 사용하는 경우에는, polyfill을 사용하는 것을 권장합니다. VAO는 대부분의 시스템에서 지원되지만, 지원하지 않는 몇 시스템에서는 polyfill로 해결할 수 있습니다.
코드의 변경 없이 말이죠.
diff --git a/webgl/lessons/pt-br/webgl-2d-vs-3d-library.md b/webgl/lessons/pt-br/webgl-2d-vs-3d-library.md
index 7beb4db89..530f12e7c 100644
--- a/webgl/lessons/pt-br/webgl-2d-vs-3d-library.md
+++ b/webgl/lessons/pt-br/webgl-2d-vs-3d-library.md
@@ -35,7 +35,7 @@ com luzes.
Aqui está o código em three.js para fazer isso
-
+{{#escapehtml}}
// Setup.
renderer = new THREE.WebGLRenderer({canvas: document.querySelector("#canvas")});
c.appendChild(renderer.domElement);
@@ -71,7 +71,7 @@ Aqui está o código em three.js para fazer isso
light2 = new THREE.PointLight(0x0040ff, 2, 0);
light2.position.set(-200, 100, 300);
scene.add(light2);
-
+{{/escapehtml}}
e aqui é exibido.
@@ -79,7 +79,7 @@ e aqui é exibido.
Aqui está o código similar em OpenGL (não ES) para exibir um cubo com 2 luzes.
-
+{{#escapehtml}}
// Setup
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
@@ -132,7 +132,7 @@ Aqui está o código similar em OpenGL (não ES) para exibir um cubo com 2 luzes
*/
glEnd();
-
+{{/escapehtml}}
Observe como precisamos quase nenhum conhecimento de matemática para 3D para qualquer um desses
diff --git a/webgl/lessons/pt-br/webgl1-to-webgl2.md b/webgl/lessons/pt-br/webgl1-to-webgl2.md
index 311eb0b12..2fd8441b2 100644
--- a/webgl/lessons/pt-br/webgl1-to-webgl2.md
+++ b/webgl/lessons/pt-br/webgl1-to-webgl2.md
@@ -346,25 +346,25 @@ de WebGL1 para WebGL2. [Há ainda mais coisas que você pode fazer no WebGL2 emb
Fazendo as extensões WebGL1 parecer WebGL2
As funções que estavam em extensões no WebGL1 estão agora no contexto principal
no WebGL2. Por exemplo, no WebGL
-
+{{#escapehtml}}
var ext = gl.getExtension("OES_vertex_array_object");
if (!ext) {
// Diga ao usuário que ele não têm a extensão necessária ou trabalhar em torno dela
} else {
var someVAO = ext.createVertexArrayOES();
}
-
+{{/escapehtml}}
vs em webgl2
-
+{{#escapehtml}}
var someVAO = gl.createVertexArray();
-
+{{/escapehtml}}
Acomo você pode ver se você deseja que seu código seja executado tanto no WebGL1 quanto no WebGL2,
que pode apresentar alguns desafios.
Uma solução seria copiar extensões WebGL1 ao contexto WebGL em tempo de inicialização.
Dessa forma, o resto do seu código pode permanecer o mesmo. Exemplo:
-
+{{#escapehtml}}
var gl = someCanvas.getContext("webgl");
var haveVAOs = getAndApplyExtension(gl, "OES_vertex_array_object"));
@@ -389,18 +389,18 @@ function getAndApplyExtension(gl, name) {
gl[unprefixedKey] = ext[key];
}
}
-
+{{/escapehtml}}
Agora, seu código pode funcionar da mesma forma em ambos. Exemplo:
-
+{{#escapehtml}}
if (haveVAOs) {
var someVAO = gl.createVertexArray();
...
} else {
... do whatever for no VAOs.
}
-
+{{/escapehtml}}
A alternativa seria ter que fazer algo assim
-
+{{#escapehtml}}
if (haveVAOs) {
if (isWebGL2)
someVAO = gl.createVertexArray();
@@ -411,7 +411,7 @@ if (haveVAOs) {
} else {
... do whatever for no VAOs.
}
-
+{{/escapehtml}}
Nota: No caso dos objetos Vertex Array em particular, sugiro que você use a polyfill
para que você os tenha em todos os lugares. Os VAOs estão disponíveis na maioria dos sistemas.
Aqueles poucos sistemas onde eles não estão disponíveis o polyfill irá lidar com você e seu código
diff --git a/webgl/lessons/webgl-2d-drawimage.md b/webgl/lessons/webgl-2d-drawimage.md
index 2da2fc716..da055016d 100644
--- a/webgl/lessons/webgl-2d-drawimage.md
+++ b/webgl/lessons/webgl-2d-drawimage.md
@@ -467,7 +467,7 @@ it provides.
You might have noticed we're using a unit quad for our positions and those positions of
a unit quad exactly match our texture coordinates. As such we can use the positions
as the texture coordinates.
-
+{{#escapehtml}}
#version 300 es
in vec4 a_position;
-in vec2 a_texcoord;
@@ -481,7 +481,7 @@ void main() {
gl_Position = u_matrix * a_position;
* v_texcoord = (u_textureMatrix * a_position).xy;
}
-
+{{/escapehtml}}
We can now remove the code that setup the texture coordinates and it will
work just the same as before.
{{{example url="../webgl-2d-drawimage-08.html" }}}
diff --git a/webgl/lessons/webgl-2d-vs-3d-library.md b/webgl/lessons/webgl-2d-vs-3d-library.md
index c65b13ff5..a262e1ae7 100644
--- a/webgl/lessons/webgl-2d-vs-3d-library.md
+++ b/webgl/lessons/webgl-2d-vs-3d-library.md
@@ -35,7 +35,7 @@ with lights.
Here's the code in three.js to display this
-
+{{#escapehtml}}
// Setup.
renderer = new THREE.WebGLRenderer({canvas: document.querySelector("#canvas")});
c.appendChild(renderer.domElement);
@@ -71,7 +71,7 @@ Here's the code in three.js to display this
light2 = new THREE.PointLight(0x0040ff, 2, 0);
light2.position.set(-200, 100, 300);
scene.add(light2);
-
+{{/escapehtml}}
and here it is displayed.
@@ -79,7 +79,7 @@ and here it is displayed.
Here's similar code in OpenGL (not ES) to display a cube with 2 lights.
-
+{{#escapehtml}}
// Setup
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
@@ -132,7 +132,7 @@ Here's similar code in OpenGL (not ES) to display a cube with 2 lights.
*/
glEnd();
-
+{{/escapehtml}}
Notice how we need almost no knowledge of 3D math for either of those
examples. Compare that to WebGL. I'm not going to write the code
diff --git a/webgl/lessons/webgl-less-code-more-fun.md b/webgl/lessons/webgl-less-code-more-fun.md
index 33dc8b9c8..3511740a2 100644
--- a/webgl/lessons/webgl-less-code-more-fun.md
+++ b/webgl/lessons/webgl-less-code-more-fun.md
@@ -440,19 +440,19 @@ Next up, [drawing multiple things](webgl-drawing-multiple-things.html).
For those of you familiar with JavaScript you might be wondering if you can use the setters
directly like this.
-
+{{#escapehtml}}
// At initialization time
var uniformSetters = twgl.createUniformSetters(program);
// At draw time
uniformSetters.u_ambient([1, 0, 0, 1]); // set the ambient color to red.
-
+{{/escapehtml}}
The reason this is not a good idea is because when you're working with GLSL you might
modify the shaders from time to time, often to debug. Let's say we were not seeing
anything on the screen in our program. One of the first things I do when nothing
is appearing is to simplify my shaders. For example I might change the fragment shader
to the simplest thing possible
-
+{{#escapehtml}}
#version 300 es
precision highp float;
@@ -492,7 +492,7 @@ void main() {
diffuseColor.a);
* outColor = vec4(0,1,0,1); // <!--- just green
}
-
+{{/escapehtml}}
Notice I just added a line that sets outColor
to a constant color.
Most drivers will see that none of the previous lines in the file actually contribute
to the result. As such they'll optimize out all of our uniforms. The next time we run the program
diff --git a/webgl/lessons/webgl-load-obj-w-mtl.md b/webgl/lessons/webgl-load-obj-w-mtl.md
index 1e923ef9a..71a6bb845 100644
--- a/webgl/lessons/webgl-load-obj-w-mtl.md
+++ b/webgl/lessons/webgl-load-obj-w-mtl.md
@@ -867,7 +867,7 @@ the types of things involved in displaying arbitrary 3D content.
Avoid conditionals in shaders where possible
The traditional advice is to avoid conditionals in shaders. As an example
we could have done something like this
-
+{{#escapehtml}}
uniform bool hasDiffuseMap;
uniform vec4 diffuse;
uniform sampler2D diffuseMap
@@ -878,7 +878,7 @@ uniform sampler2D diffuseMap
effectiveDiffuse *= texture2D(diffuseMap, texcoord);
}
...
-
+{{/escapehtml}}
Conditionals like that are generally discouraged because depending on the
GPU/driver they are often not very performant.
Either do like we did above and try to make the code have no conditionals. We used
diff --git a/webgl/lessons/webgl-planar-projection-mapping.md b/webgl/lessons/webgl-planar-projection-mapping.md
index 2a6af6afb..4d42e8916 100644
--- a/webgl/lessons/webgl-planar-projection-mapping.md
+++ b/webgl/lessons/webgl-planar-projection-mapping.md
@@ -791,21 +791,21 @@ One other thing this type of projection is useful for is
Conditional Texture References
In the fragment shader above we get read both textures
in all cases.
-
+{{#escapehtml}}
vec4 projectedTexColor = texture(u_projectedTexture, projectedTexcoord.xy);
vec4 texColor = texture(u_texture, v_texcoord) * u_colorMult;
float projectedAmount = inRange ? 1.0 : 0.0;
gl_FragColor = mix(texColor, projectedTexColor, projectedAmount);
-
+{{/escapehtml}}
Why didn't we do something like this?
-
+{{#escapehtml}}
if (inRange) {
gl_FragColor = texture(u_projectedTexture, projectedTexcoord.xy);
} else {
gl_FragColor = texture(u_texture, v_texcoord) * u_colorMult;
}
-
+{{/escapehtml}}
From the GLSL ES 3.0 spec Section 8.8
Texture Lookup Functions
@@ -816,7 +816,7 @@ derivatives are undefined within non-uniform control flow and for vertex texture
In other words, if we are going to use textures we must always access them. We can use the results
conditionally. For example we could have written this:
-
+{{#escapehtml}}
vec4 projectedTexColor = texture(u_projectedTexture, projectedTexcoord.xy);
vec4 texColor = texture(u_texture, v_texcoord) * u_colorMult;
@@ -825,14 +825,14 @@ conditionally. For example we could have written this:
} else {
gl_FragColor = texColor;
}
-
+{{/escapehtml}}
or this
-
+{{#escapehtml}}
vec4 projectedTexColor = texture(u_projectedTexture, projectedTexcoord.xy);
vec4 texColor = texture(u_texture, v_texcoord) * u_colorMult;
gl_FragColor = inRange ? projectedTexColor : texColor;
-
+{{/escapehtml}}
But we can't access the textures themselves conditionally. It might work on your GPU but it won't
work on all GPUs.
In any case it's important to know.
diff --git a/webgl/lessons/webgl-scene-graph.md b/webgl/lessons/webgl-scene-graph.md
index fa9029001..29c9c0064 100644
--- a/webgl/lessons/webgl-scene-graph.md
+++ b/webgl/lessons/webgl-scene-graph.md
@@ -439,22 +439,22 @@ function whereas above I made a node.setParent
function. Which way
is arguably a matter of style but I'd argue one objectively better reason
setParent
is better than addChild
is because it makes code like
this impossible.
-
+{{#escapehtml}}
someParent.addChild(someNode);
...
someOtherParent.addChild(someNode);
-
+{{/escapehtml}}
What does that mean? Does someNode
get added to both someParent
and someOtherParent
?
In most scene graphs that's impossible. Does the second call generate an error?
ERROR: Already have parent
. Does it magically remove someNode
from someParent
before
adding it to someOtherParent
? If it does it's certainly not clear from the name addChild
.
setParent
on the other hand has no such issue
-
+{{#escapehtml}}
someNode.setParent(someParent);
...
someNode.setParent(someOtherParent);
-
+{{/escapehtml}}
It's 100% obvious what's happening in this case. Zero ambiguity.
diff --git a/webgl/lessons/webgl1-to-webgl2.md b/webgl/lessons/webgl1-to-webgl2.md
index f882c7c35..ec46c39e0 100644
--- a/webgl/lessons/webgl1-to-webgl2.md
+++ b/webgl/lessons/webgl1-to-webgl2.md
@@ -390,7 +390,7 @@ var someVAO = gl.createVertexArray();
that can present some challenges.
One workaround would be to copy WebGL1 extensions to the WebGL context at init time.
That way the rest of your code can stay the same. Example:
-
+{{#escapehtml}}
const gl = someCanvas.getContext("webgl");
const haveVAOs = getAndApplyExtension(gl, "OES_vertex_array_object");
@@ -429,18 +429,18 @@ function getAndApplyExtension(gl, name) {
}
return ext;
}
-
+{{/escapehtml}}
Now your code can mostly just work the same on both. Example:
-
+{{#escapehtml}}
if (haveVAOs) {
var someVAO = gl.createVertexArray();
...
} else {
... do whatever for no VAOs.
}
-
+{{/escapehtml}}
The alternative would be having to do something like this
-
+{{#escapehtml}}
if (haveVAOs) {
if (isWebGL2)
someVAO = gl.createVertexArray();
@@ -451,7 +451,7 @@ if (haveVAOs) {
} else {
... do whatever for no VAOs.
}
-
+{{/escapehtml}}
Note: In the case of Vertex Array Objects in particular I suggest you use a polyfill
so you'll have them everywhere. VAOs are available on most systems. On those few systems
where they aren't available, the polyfill will handle it for you, and your code
diff --git a/webgl/lessons/zh_cn/webgl-2d-drawimage.md b/webgl/lessons/zh_cn/webgl-2d-drawimage.md
index 3726a578e..4e6a64bab 100644
--- a/webgl/lessons/zh_cn/webgl-2d-drawimage.md
+++ b/webgl/lessons/zh_cn/webgl-2d-drawimage.md
@@ -448,7 +448,7 @@ WebGL 需要上传`x, y`, `x + width, y`, `x, y + height`, 和
就是利用它提供的特性去做创意。
你可能会注意到我们使用的位置单位矩形和纹理坐标刚好匹配,所以我们就可以使用位置作为纹理坐标。
-
+{{#escapehtml}}
#version 300 es
in vec4 a_position;
-in vec2 a_texcoord;
@@ -463,7 +463,7 @@ gl_Position = u_matrix \* a_position;
- v_texcoord = (u_textureMatrix \* a_position).xy;
}
-
+{{/escapehtml}}
现在移除关于纹理坐标设置的代码,得到的结果和之前是相同的。
{{{example url="../webgl-2d-drawimage-08.html" }}}