Skip to content

Commit cbbe323

Browse files
committed
updated appearance tutorial and examples, including a new simple texture example
1 parent 1a1ffa9 commit cbbe323

File tree

2 files changed

+106
-19
lines changed

2 files changed

+106
-19
lines changed
4.15 KB
Loading

src/templates/pages/learn/getting-started-in-webgl-appearance.hbs

Lines changed: 106 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,21 @@ slug: learn/
6666

6767
<p>The camera is an essential piece of a 3D scene; it gives us the sense of space and dimension that we are
6868
often looking for in 3D. In p5.js, the WebGL mode provides us with a perspective camera by default, but we can
69-
change this using <a class="code" href="{{root}}/reference/#/p5/perspective">perspective()</a> or <a class="code" href="{{root}}/reference/#/p5/ortho">ortho()</a>.</p>
69+
change this using <a class="code" href="{{root}}/reference/#/p5/perspective">perspective()</a> or <a
70+
class="code" href="{{root}}/reference/#/p5/ortho">ortho()</a>.</p>
7071

7172
<img style="padding:60px;" src='{{assets}}/learn/basic3D/images/cameraTypeIllustration.png'
7273
alt="an illustration showing the difference between perspective and orthographic camera types">
7374

74-
<p>A <em>perspective camera</em> skews objects so they appear to get smaller as they get further away, vanishing at a
75-
single point in the distance. This is in contrast to an <em>orthographic camera</em>, where the geometry stays the same
75+
<p>A <em>perspective camera</em> skews objects so they appear to get smaller as they get further away, vanishing
76+
at a
77+
single point in the distance. This is in contrast to an <em>orthographic camera</em>, where the geometry stays
78+
the same
7679
size as it gets further away and has no vanishing point. </p>
7780

7881
<p>Constantly moving and adjusting the camera in code can be tedious, especially when you are experimenting with
79-
ideas. p5.js has a special camera method, <a class="code" href="{{root}}/reference/#/p5/orbitControl">orbitControl()</a>, that can be used to zoom, pan,
82+
ideas. p5.js has a special camera method, <a class="code"
83+
href="{{root}}/reference/#/p5/orbitControl">orbitControl()</a>, that can be used to zoom, pan,
8084
and position the camera using the mouse.</p>
8185

8286
<p>The following camera methods are available in p5.js:</p>
@@ -90,10 +94,11 @@ slug: learn/
9094
sensitivityZ)</a></li>
9195
</ul>
9296

93-
<p>
94-
A scene can have multiple cameras, but only one camera can be active at a time. Sketches in p5.js will default
95-
to having a single perspective camera, but that can be changed by calling either <a class="code">perspective()</a>
96-
(with new parameters) or <a class="code">ortho()</a>. <a class="code">camera()</a> can be used to change the
97+
<p>
98+
A scene can have multiple cameras, but only one camera can be active at a time. Sketches in p5.js will default
99+
to having a single perspective camera, but that can be changed by calling either <a
100+
class="code">perspective()</a>
101+
(with new parameters) or <a class="code">ortho()</a>. <a class="code">camera()</a> can be used to change the
97102
position of the active camera and the position that the camera is looking at.
98103
</p>
99104

@@ -170,23 +175,27 @@ function draw() {
170175
</li>
171176
<li><a class="code" href="{{root}}/reference/#/p5/pointLight">pointLight()</a>
172177
<ul style="margin-left:15px;">
173-
<li>A point light emits from a single point in all directions, similar to something like a lightbulb. This method accepts a color and a position for the light.</li>
178+
<li>A point light emits from a single point in all directions, similar to something like a lightbulb. This
179+
method accepts a color and a position for the light.</li>
174180
</ul>
175181
</li>
176182
<li><a class="code" href="{{root}}/reference/#/p5/spotLight">spotLight()</a>
177183
<ul style="margin-left:15px;">
178-
<li>A spot light emits from a single point in a single direction. This light is cast in a conical shape and it's radius and concentration can be adjusted.</li>
184+
<li>A spot light emits from a single point in a single direction. This light is cast in a conical shape and
185+
it's radius and concentration can be adjusted.</li>
179186
</ul>
180187
</li>
181188
<li><a class="code" href="{{root}}/reference/#/p5/noLights">noLights()</a>
182189
<ul style="margin-left:15px;">
183-
<li>noLights() makes it so that all subsequent geometry is rendered without any lighting. This can be useful when you want flat, unshaded geometry.</li>
190+
<li>noLights() makes it so that all subsequent geometry is rendered without any lighting. This can be useful
191+
when you want flat, unshaded geometry.</li>
184192
</ul>
185193
</li>
186194
</ul>
187195

188196
<p>
189-
These lights should be used within <a class="code">draw()</a>. Up to 5 lights can be used in a scene simultaneously,
197+
These lights should be used within <a class="code">draw()</a>. Up to 5 lights can be used in a scene
198+
simultaneously,
190199
allowing you to compose a scene with varied and complex lighting sources.
191200
</p>
192201

@@ -241,8 +250,9 @@ function draw() {
241250

242251
<h2 id="materials">Materials and Textures</h2>
243252
<p>3D is not all about geometry, objects can appear differently based on their material. Materials dictate how
244-
light interacts with the geometry and how color (or texture) gets applied to the object. Materials can be really
245-
flexible and each can have unique properties. </p>
253+
light interacts with the geometry and how color (or texture) gets applied to the object. Materials can be
254+
varied,
255+
making objects shiny, rough, or even textured with images. </p>
246256

247257
<p>In p5 we have </p>
248258

@@ -253,9 +263,85 @@ function draw() {
253263
<li><a class="code" href="{{root}}/reference/#/p5/specularMaterial">specularMaterial()</a></li>
254264
</ul>
255265

256-
<p>More custom materials can be achieved through using <a class="code">texture()</a> and shaders. These are a bit
257-
beyond the scope of this tutorial, so make sure to check out the <a
258-
href="{{root}}/learn/introduction-to-shaders.html">Introduction to Shaders</a> page when you are ready. </p>
266+
<p>Try commenting and uncommenting the different lights in this example:</p>
267+
268+
{{!-- material example --}}
269+
<script type="text/p5" data-autoplay data-p5-version="{{ version }}">
270+
function setup() {
271+
createCanvas(innerWidth, innerHeight, WEBGL);
272+
debugMode();
273+
}
274+
275+
function draw() {
276+
background(220);
277+
camera(200, -200, 200);
278+
279+
ambientLight(128)
280+
let locX = mouseX - width / 2;
281+
let locY = mouseY - height / 2;
282+
pointLight(255, 255, 255, locX, locY, 100);
283+
284+
// normalMaterial();
285+
286+
// ambient materials reflect under any light
287+
ambientMaterial(255, 0, 0);
288+
289+
// emissive materials show the same color regardless of light
290+
// emissiveMaterial(0, 255, 0);
291+
292+
// specular materials reflect the color of the light source
293+
// and can vary in 'shininess'
294+
// shininess(10)
295+
// specularMaterial(0, 0, 255);
296+
297+
// box(50);
298+
sphere();
299+
}
300+
</script>
301+
302+
<p>More custom materials can be achieved through using <a class="code">texture()</a>. In short, these are images
303+
that can be mapped onto the surface of a geometry. These textures can be imported from an image and can even be
304+
generated within code using shaders. To map a texture to your geometry, use <a class="code">loadImage()</a>
305+
within <a class="code">preload()</a>, then call <a class="code">texture()</a> before drawing your shape.
306+
</p>
307+
308+
{{!-- texture example --}}
309+
<script type="text/p5" data-autoplay data-p5-version="{{ version }}">
310+
let myTexture;
311+
312+
function preload() {
313+
myTexture = loadImage("{{assets}}/learn/basic3D/images/simpleTexture.png")
314+
// map textures between 0 and 1 to match geometry uvs
315+
textureMode(NORMAL)
316+
}
317+
318+
function setup() {
319+
createCanvas(innerWidth, innerHeight, WEBGL);
320+
}
321+
322+
function draw() {
323+
background(220);
324+
325+
// apply the texture to the box
326+
texture(myTexture)
327+
328+
push()
329+
rotateX(millis()/1000)
330+
rotateY(millis()/700)
331+
box()
332+
pop()
333+
}
334+
</script>
335+
336+
337+
<div class="additionalInformation">
338+
<p>
339+
While they are useful for changing the appearance of your geometry, shaders are a bit beyond the scope of this
340+
tutorial, so make sure to check out the <a href="{{root}}/learn/introduction-to-shaders.html">Introduction to Shaders</a>
341+
page when you are ready.
342+
</p>
343+
</div>
344+
259345

260346
{{!-- sketch illustrating materials --}}
261347
{{!-- <iframe src="{{assets}}/learn/basic3D/materialsExample.html" width="720" height="350">
@@ -267,10 +353,11 @@ function draw() {
267353
<p>The viewpoint of a 3D scene</p>
268354

269355
<h3>Perspective</h3>
270-
<p></p>
356+
<p>A projection that gives the appearance of depth, achieved by making objects in the distance appear smaller.</p>
271357

272358
<h3>Orthographic</h3>
273-
<p></p>
359+
<p>A projection that is <em>orthogonal</em> and unaffected by depth. It gives the appearance of being
360+
two-dimensional. </p>
274361

275362
</main>
276363

0 commit comments

Comments
 (0)