proofreading in progress

pull/28/head
kynd 9 years ago
parent 84375443a5
commit 5ea89031bb

@ -5,29 +5,28 @@
Finally! We have been building skills for this moment! You have learned most of the GLSL foundations, types and functions. You have practiced your shaping equations over and over. Now is the time to put it all together. You are up for this challenge! In this chapter you'll learn how to draw simple shapes in a parallel procedural way.
ついにこの時が来ました私たちはこの瞬間のために技を磨いてきたのです。あなたはGLSLの基礎を、型、関数についてのほぼ全てを身につけました。シェイピング関数も繰り返し練習してきました。今こそ学んだ全てを合わせる時です。この挑戦に受けて立ちましょう。この章ではシンプルな形をGPUの並列処理ならではの方法で描く方法を学びます。
ついにこの時が来ました。私たちはこの瞬間のために技を磨いてきたのです。あなたはGLSLの基礎の大部分、値の型と関数について学びました。シェイピング関数も繰り返し練習してきました。今こそ学んだ全てを掛け合わせる時です。この挑戦を受けて立ちましょう。この章では並列処理ならではの方法でシンプルな形を描く方法を学びます。
### Rectangle
### 長方形
Imagine we have grid paper like we used in math classes and our homework is to draw a square. The paper size is 10x10 and the square is supposed to be 8x8. What will you do?
数学の授業で使ったような方眼紙があって、そこに正方形を描く宿題が出たと想像してください。紙のサイズは10x10で正方形は8x8にする必要があります。あなたならどうしますか。
数学の授業で使ったような方眼紙があって、そこに正方形を描く宿題が出たと想像してください。紙のサイズは10×10で正方形は8×8にする必要があります。あなたならどうしますか。
![](grid_paper.jpg)
You'd paint everything except the first and last rows and the first and last column, right?
一番上と下の行、両端の列を除いてすべてを塗りつぶしませばよいですね。
一番上と下の行、左右両端の列を除いてすべてを塗りつぶせばよいですね。
How does this relate to shaders? Each little square of our grid paper is a thread (a pixel). Each little square knows its position, like the coordinates of a chess board. In previous chapters we mapped *x* and *y* to the *red* and *green* color channels, and we learned how to use the narrow two dimensional territory between 0.0 and 1.0. How can we use this to draw a centered square in the middle of our billboard?
これがシェーダーになんの関係があるのでしょう。方眼紙の小さな正方形一つ一つはスレッド(ピクセル)です。それぞれの小さな正方形は自分の位置をチェス盤上の行と列のように把握しています。以前の章で私たちは x と y を赤と緑の色のチャンネルに割り当てました。私たちはまた 0.0 から 1.0 の間の狭い二次元の領域を使う方法も学んできました。この知識を生かして描画領域の中央に正方形を描くにはどうすれば良いでしょうか。
これがシェーダーになんの関係があるのでしょう。方眼紙の小さな正方形1つ1つはスレッド(ピクセル)です。それぞれの小さな正方形は自分の位置をチェス盤上の行と列のように把握しています。以前の章で私たちは x と y を赤と緑の色のチャンネルに割り当てました。また 0.0 から 1.0 の間の狭い二次元の領域を使う方法も学んできました。描画領域の中央に正方形を描くのに、これらの知識をどう活かせばよいでしょうか。
Let's start by sketching pseudocode that uses ```if``` statements over the spatial field. The principles to do this are remarkably similar to how we think of the grid paper scenario.
描画する範囲についての ```if``` 文を使った疑似コードから初めてみましょう。基本的な考え方は方眼紙に絵を描くときととてもよく似ています。
描画する範囲についての ```if``` 文を使った疑似コードから始めてみましょう。基本的な考え方は方眼紙の宿題ととてもよく似ています。
```glsl
if ( (X GREATER THAN 1) AND (Y GREATER THAN 1) )
@ -38,7 +37,7 @@ Let's start by sketching pseudocode that uses ```if``` statements over the spati
Now that we have a better idea of how this will work, lets replace the ```if``` statement with [```step()```](../glossary/?search=step), and instead of using 10x10 lets use normalized values between 0.0 and 1.0:
理解が進んだところで ```if``` 文を [```step()```](../glossary/?search=step) に置き換えて、10x10のマス目の代わりに 0.0 から 1.0 の正規化された値を使ってみましょう。
方針がまとまったところで、 ```if``` 文を [```step()```](../glossary/?search=step) に置き換えて、10×10のマス目の代わりに 0.0 から 1.0 に正規化された値を使ってみましょう。
```glsl
@ -61,13 +60,13 @@ void main(){
The [```step()```](../glossary/?search=step) function will turn every pixel below 0.1 to black (```vec3(0.0)```) and the rest to white (```vec3(1.0)```) . The multiplication between ```left``` and ```bottom``` works as a logical ```AND``` operation, where both must be 1.0 to return 1.0 . This draws two black lines, one on the bottom and the other on the left side of the canvas.
[```step()```](../glossary/?search=step) 関数がxかyが0.1以下になるピクセルを黒(```vec3(0.0)```)に、他のすべてのピクセルを白(```vec3(1.0)```)に変えてくれます。```left``` と ```bottom``` の掛け算は ```AND``` 演算の役割をして、両方ともの値が1.0だった場合にのみ1.0を返します。結果として描画領域の下側と左側に2本の黒い線が引かれることになります。
[```step()```](../glossary/?search=step) 関数は、 x か y が 0.1 より小さくなるピクセルを黒(```vec3(0.0)```)に、他のすべてのピクセルを白(```vec3(1.0)```)に変えてくれます。```left``` と ```bottom``` の掛け算は ```AND``` 演算の役割をして、両方ともの値が 1.0 だった場合にのみ 1.0 を返します。結果として描画領域の下側と左側に2本の黒い線が引かれることになります。
![](rect-01.jpg)
In the previous code we repeat the structure for each axis (left and bottom). We can save some lines of code by passing two values directly to [```step()```](../glossary/?search=step) instead of one. That looks like this:
このコードではそれぞれの軸に対して、つまり下側と左側で同じ構造の繰り返しがあります。[```step()```](../glossary/?search=step) に1つづつ値を渡す代わりに、2つの値を一度に渡せば数行分のコードを節約することができます。下記のコードを見てください。
このコードではそれぞれの軸(左側と下側)に対して同じ構造を繰り返しています。[```step()```](../glossary/?search=step) に1つずつ値を渡す代わりに、2つの値を一度に渡せば数行分のコードを節約することができます。下記を見てください。
```glsl
vec2 borders = step(vec2(0.1),st);
@ -76,17 +75,18 @@ In the previous code we repeat the structure for each axis (left and bottom). We
So far, weve only drawn two borders (bottom-left) of our rectangle. Let's do the other two (top-right). Check out the following code:
ここまでではまだ2本の辺下側と左しか描けていません。もう2本にも手をつけましょう。
ここまでではまだ2本の辺左側と下しか描けていません。もう2本にも手をつけましょう。
<div class="codeAndCanvas" data="rect-making.frag"></div>
Uncomment and see how we invert the ```st``` coordinates and repeat the same [```step()```](../glossary/?search=step) function. That way the ```vec2(0.0,0.0)``` will be in the top right corner. This is the digital equivalent of flipping the page and repeating the previous procedure.
コメントを外して、```st``` 座標を反転させてから同じ [```step()```](../glossary/?search=step) 関数を繰り返す様子をご覧ください。こうすると ```vec2(0.0,0.0)``` が右上の角になります。紙をひっくり返して作業を繰り返すのと同じことをデジタルに行っているわけです。
コメントを外すと、```st``` 座標を反転させてから同じ [```step()```](../glossary/?search=step) 関数を繰り返す様子が見られます。こうすると ```vec2(0.0,0.0)``` が右上の角になります。紙をひっくり返して作業を繰り返すのと同じことをデジタルに行っているわけです。
![](rect-02.jpg)
Take note that in *lines 18 and 22* all of the sides are being multiplied together. This is equivalent to writing:
18行目と22行目で全ての辺の値が掛け合わされていることに注目してください。これは下記のように書くこともできます。
```glsl
@ -97,14 +97,15 @@ Take note that in *lines 18 and 22* all of the sides are being multiplied togeth
Interesting right? This technique is all about using [```step()```](../glossary/?search=step) and multiplication for logical operations and flipping the coordinates.
面白いでしょう。[```step()```](../glossary/?search=step) を使いこなし、掛け算を使って論理演算して、さらに座標をひっくり返すとこんなことができるのです。
面白いでしょう。[```step()```](../glossary/?search=step) を使いこなし、掛け算を使って論理演算して、さらに座標をひっくり返すとこんなことができるのです。
Before going forward, try the following exercises:
さらに先に進む前に下記に挑戦しましょう。
さらに先に進む前に下記の課題に挑戦しましょう。
* Change the size and proportions of the rectangle.
* 長方形の大きさと縦横を変えてみましょう。
* 長方形の大きさと縦横比を変えてみましょう。
* Experiment with the same code but using [```smoothstep()```](../glossary/?search=smoothstep) instead of [```step()```](../glossary/?search=step). Note that by changing values, you can go from blurred edges to elegant smooth borders.
@ -116,7 +117,7 @@ Before going forward, try the following exercises:
* Choose the implementation you like the most and make a function of it that you can reuse in the future. Make your function flexible and efficient.
* 実験のなかから気に入ったコードを選び、それを関数に変えて再利用できるようにしましょう。効率的でフレキシブルな関数を考えましょう。
* 課題の中から気に入ったコードを選び、それを関数に変えて再利用できるようにしましょう。効率的でフレキシブルな関数を考えましょう。
* Make another function that just draws the outline of a rectangle.
@ -124,7 +125,7 @@ Before going forward, try the following exercises:
* How do you think you can move and place different rectangles in the same billboard? If you figure out how, show off your skills by making a composition of rectangles and colors that resembles a [Piet Mondrian](http://en.wikipedia.org/wiki/Piet_Mondrian) painting.
* どうすれば1つの描画領域に幾つかの違う長方形を動かして配置することができると思いますか。良い方法を考え出せたら[モンドリアン](http://en.wikipedia.org/wiki/Piet_Mondrian)の絵のようなパターンを作ってみせてください。
* どうすれば1つの描画領域の異なる位置に、複数の長方形を配置することができると思いますか。良い方法を考え出せたら[モンドリアン](http://en.wikipedia.org/wiki/Piet_Mondrian)の絵のようなパターンを作ってみせてください。
![Piet Mondria - Tableau (1921)](mondrian.jpg)
@ -138,7 +139,7 @@ It's easy to draw squares on grid paper and rectangles on cartesian coordinates,
How? Let's start by going back to math class and the grid paper, where we opened a compass to the radius of a circle, pressed one of the compass points at the center of the circle and then traced the edge of the circle with a simple spin.
どうすればいいのでしょう。数学の教室に戻って方眼紙の上にコンパスを半径の長さに開いたところから始めましょう。片方の端を円の中心に置いて回転させて円の淵をなぞります。
どうすればいいのでしょう。数学の教室に戻って方眼紙の上にコンパスを半径の長さに開いたところから始めましょう。片方の端を円の中心に置いて1回転させて円の淵をなぞります。
![](compass.jpg)
@ -150,14 +151,14 @@ Translating this to a shader where each square on the grid paper is a pixel impl
There are several ways to calculate that distance. The easiest one uses the [```distance()```](../glossary/?search=distance) function, which internally computes the [```length()```](../glossary/?search=length) of the difference between two points (in our case the pixel coordinate and the center of the canvas). The ```length()``` function is nothing but a shortcut of the [hypotenuse equation](http://en.wikipedia.org/wiki/Hypotenuse) that uses square root ([```sqrt()```](../glossary/?search=sqrt)) internally.
距離を計算するには幾つかの方法があります。最も簡単なのは [```distance()```](../glossary/?search=distance) 関数を使うことです。この関数は内部的に2つの点の差を取りその [```length()```](../glossary/?search=length) を計算します(今回のケースではピクセルの座標と描画領域の中心が対象です)。```length()``` 関数は[ピタゴラスの定理](https://ja.wikipedia.org/wiki/%E6%96%9C%E8%BE%BA)のショートカットで内部的に平方根([```sqrt()```](../glossary/?search=sqrt))を使います。
距離を計算するには幾つかの方法があります。最も簡単なのは [```distance()```](../glossary/?search=distance) 関数を使うことです。この関数は内部的に2つの点の差を取りその [```length()```](../glossary/?search=length) を計算します(今回のケースではピクセルの座標と描画領域の中心対象にします)。```length()``` 関数は[ピタゴラスの定理](https://ja.wikipedia.org/wiki/%E6%96%9C%E8%BE%BA)のショートカットで内部的に平方根[```sqrt()```](../glossary/?search=sqrt)を使います。
![](hypotenuse.png)
You can use [```distance()```](../glossary/?search=distance), [```length()```](../glossary/?search=length) or [```sqrt()```](../glossary/?search=sqrt) to calculate the distance to the center of the billboard. The following code contains these three functions and the non-surprising fact that each one returns exactly same result.
[```distance()```](../glossary/?search=distance), [```length()```](../glossary/?search=length), [```sqrt()```](../glossary/?search=sqrt) のいずれかを使って中心までの距離を計算することができます。下記のコードにはこれら3つの関数が全て含まれていますが、当然ながら全く同じ結果になります。
[```distance()```](../glossary/?search=distance), [```length()```](../glossary/?search=length), [```sqrt()```](../glossary/?search=sqrt) のいずれかを使って中心までの距離を計算することができます。下記のコードにはこれら3つの関数が全て含まれていますが、当然ながら全く同じ結果になります。
* Comment and uncomment lines to try the different ways to get the same result.
* コメントを付け替えて、違う方法を使っても同じ結果が得られることを確かめましょう。
@ -165,40 +166,39 @@ You can use [```distance()```](../glossary/?search=distance), [```length()```](.
In the previous example we map the distance to the center of the billboard to the color brightness of the pixel. The closer a pixel is to the center, the lower (darker) value it has. Notice that the values don't get too high because from the center ( ```vec2(0.5, 0.5)``` ) the maximum distance barely goes over 0.5. Contemplate this map and think:
このサンプルでは中心からの距離をピクセルの明るさに割り当てました。中心に近いピクセルほど値が低く(暗く)なります。中心( ```vec2(0.5, 0.5)``` からの距離は最大でルート2の半分訳注原文で0.5はとなっていますが誤りです。)にしかならないため一番明るいところでも真っ白にはなりません。このサンプルをよく見て考えてみてください。
このサンプルでは中心からの距離をピクセルの明るさに割り当てました。中心に近いピクセルほど値が低く(暗く)なります。中心( ```vec2(0.5, 0.5)``` からの距離は最大でルート2の半分にしかならないため一番明るいところでも真っ白にはなりません。このサンプルをよく見て考えてみてください。
訳注原文では最大の距離は0.5はとなっていますが誤りです。中心から描画領域の角までの距離は ```sqrt(2.0)``` です。)
* What you can infer from it?
* 何か気づいたことはありますか。
* How we can use this to draw a circle?
* これをどう使えばが描けるでしょう。
* これをどう使えばが描けるでしょう。
* Modify the above code in order to contain the entire circular gradient inside the canvas.
* コードを書き換えて円形のグラデーションの全体が領域に収まるようにしてください。
* コードを書き換えて円形のグラデーションの全体が描画領域に収まるようにしてください。
### Distance field
### ディスタンスフィールド
We can also think of the above example as an altitude map, where darker implies taller. The gradient shows us something similar to the pattern made by a cone. Imagine yourself on the top of that cone. The horizontal distance to the edge of the cone is 0.5. This will be constant in all directions. By choosing where to “cut” the cone you will get a bigger or smaller circular surface.
上のサンプルは、暗い色ほど標高が高いことを示した、高低差を表す地図だと考えることもできます。このグラデーションは円錐のようなものを表しています。円錐の頂上に立ったところを想像してください。円錐の縁までの距離は2分のルート2訳注原文では0.5となっています。)です。これはどの方角でも同じです。どこで円錐を「輪切りに」するかを決めることで、大きさの違う円を得ることができます。
上のサンプルは、暗い色ほど標高が高いことを示した、高低差を表す地図だと考えることもできます。このグラデーションは円錐のようなものを表しています。円錐の頂上に立ったところを想像してください。円錐の縁までの距離は 1.0 です訳注原文では0.5となっていますが、明るさが1.0に
なるところを縁と考えると 1.0 です)。これはどの方角でも同じです。どこで円錐を「輪切りに」するかを決めることで、大きさの違う円を得ることができます。
![](distance-field.jpg)
Basically we are using a re-interpretation of the space (based on the distance to the center) to make shapes. This technique is known as a “distance field” and is used in different ways from font outlines to 3D graphics.
要するにここでは空間を中心からの距離に基づいて解釈し直すことによって形を作っています。この手法は ディスタンスフィールドと呼ばれ、フォントのアウトラインから3Dグラフィクスまで様々な用途に使われています。
要するにここでは空間を中心からの距離に基づいて解釈し直すことによって形を作っています。この手法はディスタンスフィールドと呼ばれ、フォントのアウトラインから3Dグラフィクスまで様々な用途に使われています。
(訳注distance field の適当な訳語が見当たらなかったのでカタカナで「ディスタンスフィールド」とします。おおざっぱに、点や図形からの距離を空間上にマップしたものだと考えてください。)
Try the following exercises:
下記の課題に挑戦しましょう。
* Use [```step()```](../glossary/?search=step) to turn everything above 0.5 to white and everything below to 0.0.
* [```step()```](../glossary/?search=step) を使って、0.5以上を超える全て白に、それ以外を黒にしてください。
* [```step()```](../glossary/?search=step) を使って、0.5 以上を全て白に、それ以外を黒にしてください。
* Inverse the colors of the background and foreground.
* 背景と図形の色を反転させてください。
* Using [```smoothstep()```](../glossary/?search=smoothstep), experiment with different values to get nice smooth borders on your circle.
@ -213,7 +213,7 @@ Try the following exercises:
* 円を移動させることはできますか。円を別の場所に動かして、さらにもう1つの円を別の場所に描くことはできますか。
* What happens if you combine distances fields together using different functions and operations?
* 複数のディスタンスフィールドを色々なる演算や関数を使って組み合わせると何が起きるでしょう。
* 様々な演算や関数を使って、複数のディスタンスフィールドを組み合わせると何が起きるでしょう。
```glsl
pct = distance(st,vec2(0.4)) + distance(st,vec2(0.6));
@ -243,7 +243,8 @@ In terms of computational power the [```sqrt()```](../glossary/?search=sqrt) fun
Distance fields can be used to draw almost everything. Obviously the more complex a shape is, the more complicated its equation will be, but once you have the formula to make distance fields of a particular shape it is very easy to combine and/or apply effects to it, like smooth edges and multiple outlines. Because of this, distance fields are popular in font rendering, like [Mapbox GL Labels](https://www.mapbox.com/blog/text-signed-distance-fields/), [Matt DesLauriers](https://twitter.com/mattdesl) [Material Design Fonts](http://mattdesl.svbtle.com/material-design-on-the-gpu) and [as is describe on Chapter 7 of iPhone 3D Programming, OReilly](http://chimera.labs.oreilly.com/books/1234000001814/ch07.html#ch07_id36000921).
ディスタンスフィールドを使ってほとんどどんなものでも描くことができます。もちろん形が複雑なほどより複雑な計算式が必要ですが、一旦ある形のディスタンスフィールドを作り出すための式を手に入れたら、複数の形を組み合わせることや、縁をぼかしたり何重にも線を引い引いたりと様々なエフェクトをかけたりすることはとても簡単にできます。そのため下記の例に見られるようにディスタンスフィールドはフォントのレンダリングによく使われています。
ディスタンスフィールドを使うと、ほぼどんなものでも描くことができます。もちろん形が複雑なほどより計算式がもより複雑になりますが、一旦ある形のディスタンスフィールドを作り出すための式を手に入れれば、複数の形を組み合わせたり、縁をぼかしたり何重にも線を引いたり様々なエフェクトをかけたりすることはとても簡単にできます。そのため下記の例に見られるようにディスタンスフィールドはフォントのレンダリングによく使われています。
* [Mapbox GL Labels](https://www.mapbox.com/blog/text-signed-distance-fields/)
* [Matt DesLauriers](https://twitter.com/mattdesl)
* [Material Design Fonts](http://mattdesl.svbtle.com/material-design-on-the-gpu)
@ -257,20 +258,21 @@ Take a look at the following code.
We start by moving the coordinate system to the center and shrinking it in half in order to remap the position values between -1 and 1. Also on *line 24* we are visualizing the distance field values using a [```fract()```](../glossary/?search=fract) function making it easy to see the pattern they create. The distance field pattern repeats over and over like rings in a Zen garden.
まず座標系を中心に動かして半分の大きさに縮小することで値が -1 から 1の間に収まるようにします訳注16行目の式の 2.と 1.を書き換えて見ると理解の助けになります。24行目では [```fract()```](../glossary/?search=fract) 関数を使ってディスタンスフィールドの形が見やすいように視覚化しています。ディスタンスフィールドは禅寺の庭
まず座標系を中心に動かして半分の大きさに縮小することで値が -1 から 1の間に収まるようにします訳注16行目の式の ```2.``` ```1.``` を書き換えて見ると理解の助けになります。24行目では [```fract()```](../glossary/?search=fract) 関数を使ってディスタンスフィールドが作り出すパターンが見やすいように視覚化しています。ディスタンスフィールドは禅寺の庭
のような繰り返しの模様を作り出します。
Lets take a look at the distance field formula on *line 19*. There we are calculating the distance to the position on ```(.3,.3)``` or ```vec3(.3)``` in all four quadrants (thats what [```abs()```](../glossary/?search=abs) is doing there).
19行目のディスタンスフィールドを作り出す式を見てみましょう。4つの象限すべてで ```vec3(.3)```、つまり ```(.3,.3)``` までの
距離を計算しています([```abs()```](../glossary/?search=abs) を使って座標を絶対値にしてから距離を求めています)
距離を計算しています([```abs()```](../glossary/?search=abs) を使って座標を絶対値にしてから距離を求めています)
If you uncomment *line 20*, you will note that we are combining the distances to these four points using the [```min()```](../glossary/?search=min) to zero. The result produces an interesting new pattern.
20行目のコメントを外すと [```min()```](../glossary/?search=min) を使って0と比べた最低値を求めることで4つの店への距離を組み合わせた場合を見ることができます。面白い新しい形を作り出します
20行目のコメントを外すと 0 との [```min()```](../glossary/?search=min) を使って、4つの点への距離を組み合わせた場合を見ることができます。これは新く面白いパターンを作り出します。
Now try uncommenting *line 21*; we are doing the same but using the [```max()```](../glossary/?search=max) function. The result is a rectangle with rounded corners. Note how the rings of the distance field get smoother the further away they get from the center.
さて21行目のコメントを外して見ましょう。[```max()```](../glossary/?search=max) を使っていること以外は同じです。結果は角丸の四角形になります。ディスタンスフィールドの輪は中心から遠ざかるほど滑らかなグラデーションになっています
さて21行目のコメントを外して見ましょう。[```max()```](../glossary/?search=max) を使っていること以外は同じです。結果は角丸の四角形になります。ディスタンスフィールドの輪は中心から遠ざかるほど半径が大きくなっていることに注目しましょう
Finish uncommenting *lines 27 to 29* one by one to understand the different uses of a distance field pattern.
@ -284,7 +286,7 @@ Finish uncommenting *lines 27 to 29* one by one to understand the different uses
In the chapter about color we map the cartesian coordinates to polar coordinates by calculating the *radius* and *angles* of each pixel with the following formula:
色についての章ではそれぞれのピクセルについて、下記の式を使って半径と角度を計算することでデカルト座標を極座標に変換しました。
色についての章ではそれぞれのピクセルについて、下記の式を使って半径と角度を計算することでデカルト座標を極座標に変換しました。
```glsl
vec2 pos = vec2(0.5)-st;

Loading…
Cancel
Save