-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Better shader docs #3154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better shader docs #3154
Changes from 1 commit
c024413
2f67e5a
47e22ec
6a370ca
8d0c325
948b2e2
a387b47
d6ac66a
d0c268d
b2459d1
c3fc4d2
a451ab4
072cda2
50966cd
392f796
9653524
948b3d1
16ae6e5
27c6ddb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -335,39 +335,53 @@ We can register custom shader materials for appearances and effects using | |
| `AFRAME.registerShader`. | ||
|
|
||
| [example]: https://codepen.io/machenmusik/pen/WZyQNj | ||
| Here is an [example][example] CodePen with step-by-step commentary. | ||
| Let's walk through an [example CodePen][example] with step-by-step commentary. | ||
|
|
||
| As always, we need to include the A-Frame script. | ||
| Here, we use RawGit to include the latest master version. | ||
|
|
||
| ```js | ||
| <!-- Include A-Frame latest master distribution, via RawGit. --> | ||
| <!-- Really should be in <head>; in CodePen body for simple viewing. --> | ||
| <script src="https://rawgit.com/aframevr/aframe/master/dist/aframe-master.min.js"></script> | ||
| ``` | ||
|
|
||
| <!-- Components and shaders go after A-Frame, but before the scene declaration. --> | ||
| Next, we define any custom components and shaders we may want to use, | ||
| after A-Frame, but before the scene declaration. | ||
| Here, we begin our `my-custom` shader. | ||
| The schema declares any parameters for the shader. | ||
| ```js | ||
| <script> | ||
| AFRAME.registerShader('my-custom', { | ||
| // The schema declares any parameters for the shader. | ||
| schema: { | ||
| // Where relevant, it is customary to support color. | ||
| // The A-Frame schema uses `type:'color'`, so it will parse string values like 'white' or 'red. | ||
| // `is:'uniform'` tells A-Frame this should appear as uniform value in the shader(s). | ||
| ``` | ||
| Where relevant, it is customary to support at least two data values: | ||
|
|
||
| - `color`: A-Frame uses `type:'color'`, so it will parse string values like 'white' or 'red. | ||
| - `opacity`: This is commonly used for fading in and out. | ||
|
|
||
| `is:'uniform'` tells A-Frame this should appear as uniform value in the shader(s). | ||
|
|
||
| ```js | ||
| color: {type:'color', is:'uniform', default:'red'}, | ||
| // It is customary to support opacity, for fading in and out. | ||
| opacity: {type:'number', is:'uniform', default:1.0} | ||
| }, | ||
|
|
||
| // Setting raw to true uses THREE.RawShaderMaterial instead of ShaderMaterial, | ||
| // so your shader strings are used as-is, for advanced shader usage. | ||
| // Here, we want the usual prefixes with GLSL constants etc., | ||
| // so we set it to false. | ||
| // (Which is also the default, so we could have omitted it). | ||
| ``` | ||
|
|
||
| Setting raw to true uses THREE.RawShaderMaterial instead of ShaderMaterial, | ||
| so your shader strings are used as-is, for advanced shader usage. | ||
|
||
| Here, we want the usual prefixes with GLSL constants etc., | ||
| so we set it to false. | ||
| (Which is also the default, so we could have omitted it). | ||
| ```js | ||
| raw: false, | ||
| ``` | ||
|
|
||
| Here, we're going to use the default vertex shader by omitting vertexShader. | ||
| But note that if your fragment shader cares about texture coordinates, | ||
| the vertex shader should set uniform values to use in the fragment shader. | ||
|
|
||
| // Here, we're going to use the default vertex shader by omitting vertexShader. | ||
| // But note that if your fragment shader cares about texture coordinates, | ||
| // the vertex shader should set uniform values to use in the fragment shader. | ||
|
|
||
| // Since almost every WebVR-capable browser supports ES6, | ||
| // define our fragment shader as a multi-line string. | ||
| Since almost every WebVR-capable browser supports ES6, | ||
| we define our fragment shader as a multi-line string. | ||
| ```js | ||
| fragmentShader: | ||
| ` | ||
| // Use medium precision. | ||
|
|
@@ -391,7 +405,11 @@ AFRAME.registerShader('my-custom', { | |
| ` | ||
| }); | ||
| </script> | ||
| ``` | ||
|
|
||
| Here is the A-Frame scene declaration that uses our `my-custom` shader: | ||
|
|
||
| ``` | ||
| <!-- Declaration of the A-Frame scene. --> | ||
| <a-scene> | ||
| <!-- Black skybox at effectively infinite distance. --> | ||
|
|
@@ -406,6 +424,8 @@ AFRAME.registerShader('my-custom', { | |
| </a-scene> | ||
| ``` | ||
|
|
||
| Again, here is a link to view the [example CodePen][example]. | ||
|
|
||
| ### `registerShader` | ||
|
|
||
| Like components, custom materials have schema and lifecycle handlers. | ||
|
|
@@ -451,6 +471,7 @@ The uniform types supported by A-Frame are summarized in the table below. | |
| Note that `time` can eliminate the need for a `tick()` handler in many cases. | ||
|
|
||
| | A-Frame Type | THREE Type | GLSL Shader Type | | ||
| |--------------|------------|------------------| | ||
| | array | v3 | vec3 | | ||
| | color | v3 | vec3 | | ||
| | int | i | int | | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason we need to be suggesting the master version in the docs?
@ngokevin do versions get automatically find/replaced when we do a release?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No reason other than not having to change it every release.