You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[page:Function callback] — required. Will be called when load completes. The arguments will be the loaded [page:Object3D] and the loaded [page:Array materials].<br/>
39
-
[page:String texturePath] — optional. If not specified, textures will be assumed to be in the same folder as the Javascript model file.
[page:JSONLoader context] — The [page:JSONLoader] instance<br/>
45
-
[page:String url] — required<br/>
46
-
[page:Function callback] — required. This function will be called with the loaded model as an instance of [page:Geometry geometry] when the load is completed.<br/>
47
-
[page:String texturePath] — Base path for textures.<br/>
48
-
[page:Function callbackProgress] — Will be called while load progresses. The argument will be an [page:Object] containing two attributes: .[page:Integer total] and .[page:Integer loaded] bytes.
49
-
</div>
50
-
<div>
51
-
Begin loading from url and call <em>callback</em> with the parsed response content.
[page:Function callback] — required. Will be called when load completes. The arguments will be the loaded [page:Object3D] and the loaded [page:Array materials].<br/>
39
+
[page:String texturePath] — optional. If not specified, textures will be assumed to be in the same folder as the Javascript model file.
Copy file name to clipboardExpand all lines: docs/api/materials/ShaderMaterial.html
+21-46Lines changed: 21 additions & 46 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
<!DOCTYPE html>
2
2
<htmllang="en">
3
3
<head>
4
-
<metacharset="utf-8" />
4
+
<metacharset="utf-8" />
5
5
<basehref="../../" />
6
6
<scriptsrc="list.js"></script>
7
7
<scriptsrc="page.js"></script>
@@ -16,9 +16,13 @@ <h1>[name]</h1>
16
16
<ul>
17
17
<li>implement an effect not included with any of the built-in [page:Material materials]</li>
18
18
<li>combine many objects into a single [page:Geometry] or [page:BufferGeometry] in order to improve performance</li>
19
-
<li>associate custom data with individual vertices ("custom attributes")</li>
20
19
</ul>
21
-
Note that a ShaderMaterial will only be rendered properly by [page:WebGLRenderer], since the GLSL code in the vertexShader and fragmentShader properties must be compiled and run on the GPU using WebGL.
20
+
There are the following notes to bear in mind when using a *ShaderMaterial*:
21
+
22
+
<ul>
23
+
<li>A *ShaderMaterial* will only be rendered properly by [page:WebGLRenderer], since the GLSL code in the *vertexShader* and *fragmentShader* properties must be compiled and run on the GPU using WebGL.</li>
24
+
<li>As of THREE r72, directly assigning attributes in a *ShaderMaterial* is no longer supported. A [page:BufferGeometry] instance (instead of a [page:Geometry] instance) must be used instead, using [page:BufferAttribute] instances to define custom attributes.</li>
25
+
</ul>
22
26
</div>
23
27
24
28
<h3>Example</h3>
@@ -67,66 +71,51 @@ <h3>Built-in attributes and uniforms</h3>
67
71
</p>
68
72
69
73
<h3>Custom attributes and uniforms</h3>
70
-
Custom attributes and uniforms must be declared both in your GLSL shader code (within *vertexShader* and/or *fragmentShader*), <emph>and</emph>in the *attributes* and *uniforms* properties of your ShaderMaterial. The declaration in the material is necessary to ensure [page:WebGLRenderer] passes your attribute/uniform data in a buffer to the GPU when the shader is run. Note that *varying*s only need to be declared within the shader code (not within the material).
74
+
<p>Both custom attributes and uniforms must be declared in your GLSL shader code (within *vertexShader* and/or *fragmentShader*). Custom uniforms must be defined in <emph>both</emph> the *uniforms* property of your *ShaderMaterial*, whereas any custom attributes must be defined via [page:BufferAttribute] instances. Note that *varying*s only need to be declared within the shader code (not within the material).
71
75
</p>
72
-
<p>
73
-
To declare a custom attribute, use the *attributes* property of the material:
74
-
<code>
75
-
attributes: {
76
-
vertexOpacity: { type: 'f', value: [] }
77
-
}
78
-
</code>
79
-
Each attribute must have a *type* property and a *value* property.
76
+
77
+
<p>To declare a custom attribute, please reference the [page:BufferGeometry] page for an overview, and the [page:BufferAttribute] page for a detailed look at the *BufferAttribute* API.</p>
78
+
<p>When creating your attributes, each typed array that you create to hold your attribute's data must be a multiple of your data type's size. For example, if your attribute is a [page:Vector3 THREE.Vector3] type, and you have 3000 vertices in your [page:BufferGeometry], your typed array value must be created with a length of 3000 * 3, or 9000 (one value per-component). A table of each data type's size is shown below for reference:</p>
The way attribute data is stored depends on whether you're using [page:BufferGeometry] or [page:Geometry]:
118
-
<ul>
119
-
<li>When using [page:Geometry], attribute data is stored directly on the <emph>material</emph>, using the attribute's *value* array; each element of *value* should correspond to one vertex. To update an attribute, set the *needsUpdate* flag to true on the material attribute:
<li>When using [page:BufferGeometry], attribute data is stored within a [page:BufferAttribute] on the geometry itself, and the *value* within the material is ignored. To update an attribute, set the *needsUpdate* flag to true on the [page:BufferAttribute] of the geometry:
Note that attribute buffers are <emph>not</emph> refreshed automatically when their values change. To update custom attributes, set the *needsUpdate* flag to true on the [page:BufferAttribute] of the geometry (see [page:BufferGeometry] for further details).
where *type* is a <ahref="#uniform-types">uniform type string</a>, and *value* is the value of the uniform. Names must match the name of the uniform, as defined in the GLSL code. Note that uniforms are refreshed on every frame, so updating the value of the uniform will immediately update the value available to the GLSL code.
233
222
</div>
234
223
235
-
<h3>[property:Object attributes]</h3>
236
-
<div>
237
-
<p>
238
-
Object specifying the custom attributes to be passed to the shader code; keys are attribute names, values are definitions of the form
239
-
<code>
240
-
{ type: 'f', value: [1.0, 0.5, 2.0, ...] }
241
-
</code>
242
-
where *type* is an <ahref="#attribute-types">attribute type string</a>, and *value* is an array containing an attribute value for each vertex in the geometry (or *undefined* if using [page:BufferGeometry]). Names must match the name of the attribute, as defined in the GLSL code.
243
-
</p>
244
-
<p>
245
-
Note that attribute buffers are <emph>not</emph> refreshed automatically when their values change; if using [page:Geometry], set <code>needsUpdate = true</code> on the attribute definition. If using [page:BufferGeometry], set <code>needsUpdate = true</code> on the [page:BufferAttribute].
246
-
</p>
247
-
</div>
248
-
249
224
<h3>[property:Object defines]</h3>
250
225
<div>
251
226
Defines custom constants using *#define* directives within the GLSL code for both the vertex shader and the fragment shader; each key/value pair yields another directive:
0 commit comments