forked from gefercan/webGL_threejs_exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaFrame_guide_template2.html
More file actions
700 lines (637 loc) · 36.5 KB
/
Copy pathaFrame_guide_template2.html
File metadata and controls
700 lines (637 loc) · 36.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>A-Frame & Three.js Elements and Templates</title>
<!-- Inter Font -->
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<!-- Tailwind CSS CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Base font for the document */
:root {
font-family: 'Inter', sans-serif;
}
/* Styling for code blocks to make them distinct and readable */
.code-block {
background-color: #1f2937; /* Dark gray background */
color: #f9fafb; /* Light text color */
padding: 1rem;
border-radius: 0.5rem;
overflow-x: auto; /* Enable horizontal scrolling for long lines */
font-family: monospace; /* Monospaced font for code */
font-size: 0.875rem; /* text-sm */
}
.code-block pre {
margin: 0;
white-space: pre-wrap; /* Wrap long lines of code */
word-break: break-all; /* Break words if they are too long */
}
/* Container for visual previews, adding a consistent look */
.preview-container {
border: 1px solid #e5e7eb; /* Light gray border */
border-radius: 0.5rem;
overflow: hidden; /* Hide anything overflowing the rounded corners */
background-color: #f9fafb; /* Light background for previews */
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06); /* Subtle shadow */
position: relative; /* Needed for absolute positioning of UI overlays */
}
/* Specific styling for A-Frame scene preview */
.aframe-scene-preview {
width: 100%;
height: 300px; /* Fixed height for A-Frame scene */
border-radius: 0.5rem; /* Apply rounded corners */
}
/* Specific styling for Three.js canvas preview */
.threejs-canvas-preview {
display: block; /* Remove extra space below the canvas */
width: 100%;
height: 300px; /* Fixed height for Three.js canvas */
border-radius: 0.5rem; /* Apply rounded corners */
}
/* Styling for the responsive UI overlay example */
.responsive-ui-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent dark overlay */
color: white;
font-size: 1.5rem;
text-align: center;
pointer-events: none; /* Allows clicks to pass through to the A-Frame scene */
z-index: 10; /* Ensure it's above the A-Frame scene */
}
/* Make buttons within the overlay clickable */
.responsive-ui-overlay button {
pointer-events: auto; /* Override parent's pointer-events: none */
background-color: #4f46e5; /* Indigo-600 */
color: white;
padding: 0.75rem 1.5rem;
border-radius: 9999px; /* Fully rounded button */
cursor: pointer;
border: none;
transition: background-color 0.3s ease; /* Smooth hover effect */
}
.responsive-ui-overlay button:hover {
background-color: #4338ca; /* Darker indigo on hover */
}
</style>
<!-- A-Frame CDN: Core library for WebVR/AR experiences -->
<script src="https://aframe.io/releases/1.5.0/aframe.min.js"></script>
<!-- A-Frame Extras: Provides useful components like environment (optional) -->
<script src="https://cdn.jsdelivr.net/gh/donmccurdy/[email protected]/dist/aframe-extras.min.js"></script>
<!-- Three.js CDN: Low-level 3D graphics library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<!-- Three.js OrbitControls: Enables interactive camera movement with mouse -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
<!-- Three.js GLTFLoader: Loader for glTF 3D models -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/loaders/GLTFLoader.js"></script>
</head>
<body class="bg-gray-100 text-gray-800 p-4 sm:p-6 md:p-8">
<!-- Main Container: Centers content and applies basic styling -->
<div class="max-w-6xl mx-auto bg-white shadow-lg rounded-xl p-6 sm:p-8 md:p-10">
<!-- Main Title of the Document -->
<h1 class="text-3xl sm:text-4xl font-bold text-center mb-8 text-indigo-700">
A-Frame & Three.js Elements and Templates
</h1>
<!-- Section for A-Frame Examples -->
<section class="mb-12">
<h2 class="text-2xl sm:text-3xl font-semibold mb-6 text-indigo-600 border-b pb-3">
A-Frame (WebVR/AR Framework)
</h2>
<p class="mb-8 text-gray-700">
A-Frame is an open-source web framework for building virtual reality (VR) and
augmented reality (AR) experiences. It's built on top of HTML and the DOM,
making it easy to get started with 3D and VR content using familiar web technologies.
It abstracts away much of the complexity of WebGL and Three.js.
</p>
<!-- Grid layout for A-Frame examples -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<!-- A-Frame Basic Elements & Scene Setup -->
<div class="p-4 bg-gray-50 rounded-lg shadow-sm flex flex-col md:col-span-2">
<h3 class="text-xl font-medium mb-4">Basic Elements & Scene Setup</h3>
<div class="mb-4">
<p class="font-semibold mb-2">Preview (Click and drag to look around, use WASD to move):</p>
<div class="preview-container">
<!--
A-Frame Scene: The root element for all VR content.
'embedded' attribute prevents A-Frame from taking over the whole page.
-->
<a-scene class="aframe-scene-preview" embedded>
<!--
Camera: Defines the user's viewpoint in the scene.
'look-controls' enables mouse/touch camera rotation.
'wasd-controls' enables movement with WASD keys.
'position' sets the initial camera location.
-->
<a-entity camera look-controls wasd-controls position="0 1.6 0">
<!--
Cursor: A visual indicator that appears at the center of the camera's view.
Useful for indicating what the user is looking at.
-->
<a-cursor></a-cursor>
</a-entity>
<!-- Basic Geometries (3D Primitives) -->
<!-- Box: A cube or rectangular prism -->
<a-box position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9"></a-box>
<!-- Sphere: A round 3D object -->
<a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E"></a-sphere>
<!-- Cylinder: A cylindrical object -->
<a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D"></a-cylinder>
<!-- Plane: A flat 2D surface in 3D space, often used as a ground -->
<a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4"></a-plane>
<!-- Sky: A large sphere that acts as the background, typically for skyboxes or solid colors -->
<a-sky color="#ECECEC"></a-sky>
<!-- Lights: Illuminate the scene. Without lights, objects will appear black. -->
<!-- Ambient Light: Provides soft, omnidirectional lighting, no shadows -->
<a-light type="ambient" color="#CCC"></a-light>
<!-- Directional Light: Simulates light from a distant source (e.g., sun), casts shadows -->
<a-light type="directional" color="#FFF" intensity="0.6" position="-1 1 1"></a-light>
</a-scene>
</div>
</div>
<div>
<p class="font-semibold mb-2">Code:</p>
<div class="code-block">
<pre><code><!-- Include A-Frame CDN in your <head> -->
<script src="https://aframe.io/releases/1.5.0/aframe.min.js"></script>
<!-- A-Frame Scene: The root element for all VR content -->
<a-scene embedded>
<!-- Camera: Defines the user's viewpoint -->
<a-entity camera look-controls wasd-controls position="0 1.6 0">
<!-- Cursor: A visual indicator for interaction -->
<a-cursor></a-cursor>
</a-entity>
<!-- Basic Geometries -->
<!-- Box: A cube or rectangular prism -->
<a-box position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9"></a-box>
<!-- Sphere: A round 3D object -->
<a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E"></a-sphere>
<!-- Cylinder: A cylindrical object -->
<a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D"></a-cylinder>
<!-- Plane: A flat 2D surface in 3D space -->
<a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4"></a-plane>
<!-- Sky: A large sphere that acts as the background -->
<a-sky color="#ECECEC"></a-sky>
<!-- Lights: Illuminate the scene -->
<!-- Ambient Light: Provides soft, even lighting -->
<a-light type="ambient" color="#CCC"></a-light>
<!-- Directional Light: Simulates light from a distant source (e.g., sun) -->
<a-light type="directional" color="#FFF" intensity="0.6" position="-1 1 1"></a-light>
</a-scene></code></pre>
</div>
</div>
<p class="mt-4 text-gray-700">
This example demonstrates how to set up a basic A-Frame scene (`a-scene`)
and include fundamental 3D primitives like `a-box`, `a-sphere`, `a-cylinder`,
and `a-plane`. The `a-sky` component sets the background color, and `a-light`
elements provide illumination. The `a-entity camera` with `look-controls`
and `wasd-controls` allows for desktop navigation, while `a-cursor` provides
a visual indicator for interaction.
</p>
</div>
<!-- A-Frame 360 Image -->
<div class="p-4 bg-gray-50 rounded-lg shadow-sm flex flex-col">
<h3 class="text-xl font-medium mb-4">360 Image (Panorama)</h3>
<div class="mb-4">
<p class="font-semibold mb-2">Description:</p>
<p class="text-gray-700">
The `a-sky` component is versatile and can be used to display 360-degree
panoramic images. Simply set its `src` attribute to the URL of your
equirectangular image. This creates an immersive background for your VR scene.
The preview for this element is integrated into the main A-Frame scene above
by changing the `a-sky` color.
</p>
</div>
<div>
<p class="font-semibold mb-2">Code:</p>
<div class="code-block">
<pre><code><!-- To display a 360 image, use <a-sky> with an image source. -->
<!-- Replace 'your-360-image.jpg' with the URL of your equirectangular 360 image. -->
<a-scene>
<!--
src: URL to your 360 equirectangular image.
Example: https://cdn.aframe.io/360-image-gallery/img/sechelt.jpg
-->
<a-sky src="https://cdn.aframe.io/360-image-gallery/img/sechelt.jpg"></a-sky>
<a-camera></a-camera>
</a-scene></code></pre>
</div>
</div>
</div>
<!-- A-Frame 360 Video -->
<div class="p-4 bg-gray-50 rounded-lg shadow-sm flex flex-col">
<h3 class="text-xl font-medium mb-4">360 Video</h3>
<div class="mb-4">
<p class="font-semibold mb-2">Description:</p>
<p class="text-gray-700">
Similar to 360 images, `a-videosphere` allows you to embed 360-degree
videos as the background. You typically define the video in `a-assets`
and then reference it. Be aware of browser autoplay policies, which
often require a user gesture (like a click) to start video playback.
A live preview is not shown to avoid autoplay issues and scene conflicts.
</p>
</div>
<div>
<p class="font-semibold mb-2">Code:</p>
<div class="code-block">
<pre><code><!-- For 360 video, use <a-videosphere>. -->
<!-- First, define your video asset within <a-assets> for preloading -->
<a-assets>
<!--
id: Unique identifier for the video asset.
autoplay: Attempts to play video automatically (may be blocked by browsers).
loop: Loops the video playback.
src: URL to your 360 video file (e.g., MP4).
-->
<video id="my-360-video" autoplay loop="true" src="your-360-video.mp4"></video>
</a-assets>
<a-scene>
<!-- Then, reference the video asset using its ID with <a-videosphere> -->
<a-videosphere src="#my-360-video"></a-videosphere>
<a-camera></a-camera>
</a-scene>
<!-- Note: Autoplay requires user interaction in many browsers. -->
<!-- You might need a button to start video playback on user click. --></code></pre>
</div>
</div>
</div>
<!-- A-Frame Responsive UI (HTML Overlay) -->
<div class="p-4 bg-gray-50 rounded-lg shadow-sm flex flex-col md:col-span-2">
<h3 class="text-xl font-medium mb-4">Responsive UI (HTML Overlay)</h3>
<div class="mb-4">
<p class="font-semibold mb-2">Preview (Overlayed on A-Frame scene):</p>
<div class="preview-container relative">
<!-- A-Frame Scene for the background -->
<a-scene class="aframe-scene-preview" embedded>
<a-entity camera look-controls position="0 1.6 0"></a-entity>
<a-box position="0 1 -3" color="#FF0000"></a-box>
<a-sky color="#ADD8E6"></a-sky>
</a-scene>
<!-- HTML Overlay positioned absolutely on top of the scene -->
<div class="responsive-ui-overlay">
<div class="p-4 bg-black bg-opacity-70 rounded-lg text-center">
<p class="text-lg font-bold mb-2">Welcome to VR!</p>
<!-- Button within the overlay, made clickable via CSS -->
<button onclick="alert('Button Clicked! This is a standard JS alert, replace with custom modal if needed.')">Enter Experience</button>
</div>
</div>
</div>
</div>
<div>
<p class="font-semibold mb-2">Code:</p>
<div class="code-block">
<pre><code><!-- A-Frame Scene (as a base for the overlay) -->
<div class="preview-container relative">
<a-scene class="aframe-scene-preview" embedded>
<a-entity camera look-controls position="0 1.6 0"></a-entity>
<a-box position="0 1 -3" color="#FF0000"></a-box>
<a-sky color="#ADD8E6"></a-sky>
</a-scene>
<!-- Responsive HTML Overlay -->
<div class="responsive-ui-overlay">
<div class="p-4 bg-black bg-opacity-70 rounded-lg text-center">
<p class="text-lg font-bold mb-2">Welcome to VR!</p>
<button onclick="console.log('Button Clicked!')">Enter Experience</button>
</div>
</div>
</div>
<!-- Basic CSS for the overlay (add to your <style> block) -->
<style>
.responsive-ui-overlay {
position: absolute; /* Position relative to its parent .preview-container */
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center; /* Center horizontally */
align-items: center; /* Center vertically */
background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent overlay */
color: white;
font-size: 1.5rem;
text-align: center;
pointer-events: none; /* By default, clicks pass through to the A-Frame scene */
z-index: 10; /* Ensure it's on top */
}
.responsive-ui-overlay button {
pointer-events: auto; /* Make the button itself clickable */
background-color: #4f46e5;
color: white;
padding: 0.75rem 1.5rem;
border-radius: 9999px;
cursor: pointer;
border: none;
transition: background-color 0.3s ease;
}
.responsive-ui-overlay button:hover {
background-color: #4338ca;
}
</style></code></pre>
</div>
</div>
<p class="mt-4 text-gray-700">
While A-Frame has components for in-VR UI, a common and effective way
to implement responsive UI (especially for pre-VR entry screens or 2D overlays)
is to use standard HTML and CSS positioned on top of the A-Frame scene.
This allows you to leverage all standard web development tools for UI.
Using `pointer-events: none;` on the overlay allows interaction with the
underlying A-Frame scene, while specific interactive elements (like buttons)
can have `pointer-events: auto;` to be clickable.
</p>
</div>
<!-- A-Frame Hand Tracking -->
<div class="p-4 bg-gray-50 rounded-lg shadow-sm flex flex-col">
<h3 class="text-xl font-medium mb-4">Hand Tracking / Controllers</h3>
<div class="mb-4">
<p class="font-semibold mb-2">Description:</p>
<p class="text-gray-700">
A-Frame provides built-in support for WebXR controllers and hand tracking.
By adding `hand-controls` or specific controller components like
`oculus-touch-controls` to your camera entity, A-Frame will automatically
render the controllers and enable basic interactions. You can then add
custom components (like the `clickable` example in the code) to make your 3D
objects respond to controller events (e.g., `click`, `triggerdown`).
This functionality requires a compatible VR headset and browser to fully experience.
A live preview is not provided as it requires specific hardware.
</p>
</div>
<div>
<p class="font-semibold mb-2">Code:</p>
<div class="code-block">
<pre><code><!-- A-Frame Scene -->
<a-scene>
<!--
Camera with default WebXR hand tracking/controllers.
A-Frame automatically detects and renders common VR controllers (e.g., Oculus Touch, Valve Index).
Add 'hand-controls' for both left and right hands.
-->
<a-entity camera="active: true"
hand-controls="hand: left"
hand-controls="hand: right">
</a-entity>
<!-- Alternatively, for specific controller models (e.g., Oculus Touch) -->
<!-- <a-entity oculus-touch-controls="hand: left"></a-entity> -->
<!-- <a-entity oculus-touch-controls="hand: right"></a-entity> -->
<!-- Add interactable objects for testing with controllers -->
<a-box position="0 1 -2" color="blue" clickable></a-box>
<a-sphere position="0.5 1.5 -2.5" color="green" clickable></a-sphere>
<a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4"></a-plane>
<a-sky color="#ECECEC"></a-sky>
</a-scene>
<!-- Example of a simple 'clickable' component for interaction -->
<script>
// Register a custom A-Frame component named 'clickable'
AFRAME.registerComponent('clickable', {
init: function () {
// Add an event listener for the 'click' event (triggered by cursor or controller)
this.el.addEventListener('click', function (evt) {
console.log('Clicked on:', evt.target.id || evt.target.tagName);
// Change the color of the clicked object randomly
evt.target.setAttribute('color', '#' + Math.floor(Math.random()*16777215).toString(16));
});
}
});
</script></code></pre>
</div>
</div>
</div>
<!-- A-Frame Model Viewer (GLTF) -->
<div class="p-4 bg-gray-50 rounded-lg shadow-sm flex flex-col">
<h3 class="text-xl font-medium mb-4">3D Model Viewer (GLTF)</h3>
<div class="mb-4">
<p class="font-semibold mb-2">Description:</p>
<p class="text-gray-700">
A-Frame makes it easy to load 3D models in the glTF format (`.gltf` or `.glb`)
using the `gltf-model` component. It's recommended to define your model in
`a-assets` for preloading and caching, which improves performance. This allows for
complex 3D assets to be integrated into your VR scenes with minimal code.
You can adjust its position, rotation, and scale just like any other A-Frame entity.
A live preview is not provided as it relies on external model files.
</p>
</div>
<div>
<p class="font-semibold mb-2">Code:</p>
<div class="code-block">
<pre><code><!-- A-Frame Scene -->
<a-scene>
<!-- Assets: Define your 3D model here for preloading -->
<a-assets>
<!--
id: Unique identifier for the asset.
src: URL to your glTF model file.
Example: A simple tree model from A-Frame's CDN.
-->
<a-asset-item id="tree-model" src="https://cdn.aframe.io/models/gltf/tree/tree.gltf"></a-asset-item>
</a-assets>
<!-- Use the gltf-model component to load the model -->
<!--
src: References the asset by its ID (e.g., #tree-model).
position: Sets the model's position in 3D space.
scale: Adjusts the model's size.
-->
<a-gltf-model src="#tree-model" position="0 0 -3" scale="0.5 0.5 0.5"></a-gltf-model>
<!-- Basic environment for context -->
<a-plane position="0 0 -4" rotation="-90 0 0" width="10" height="10" color="#AAA"></a-plane>
<a-sky color="#ECECEC"></a-sky>
<a-entity camera look-controls position="0 1.6 0"></a-entity>
<a-light type="ambient" color="#CCC"></a-light>
<a-light type="directional" color="#FFF" intensity="0.6" position="-1 1 1"></a-light>
</a-scene></code></pre>
</div>
</div>
</div>
</div>
</section>
<!-- Section for Three.js Examples -->
<section class="mb-12">
<h2 class="text-2xl sm:text-3xl font-semibold mb-6 text-indigo-600 border-b pb-3">
Three.js (Low-Level 3D Graphics Library)
</h2>
<p class="mb-8 text-gray-700">
Three.js is a powerful JavaScript library that simplifies the process of creating
and displaying animated 3D computer graphics in a web browser using WebGL.
It provides a higher degree of control over the 3D rendering pipeline compared
to A-Frame, but consequently requires more explicit coding for scene setup,
object creation, and interactions.
</p>
<!-- Grid layout for Three.js examples -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<!-- Three.js Basic Setup & Elements -->
<div class="p-4 bg-gray-50 rounded-lg shadow-sm flex flex-col md:col-span-2">
<h3 class="text-xl font-medium mb-4">Basic Scene, Elements & Animation</h3>
<div class="mb-4">
<p class="font-semibold mb-2">Preview (Click and drag to orbit camera):</p>
<div class="preview-container">
<!-- Canvas element where Three.js will render the 3D scene -->
<canvas id="threejs-canvas" class="threejs-canvas-preview"></canvas>
</div>
</div>
<div>
<p class="font-semibold mb-2">Code:</p>
<div class="code-block">
<pre><code><!-- Include Three.js CDN in your <head> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<!-- Include OrbitControls for camera interaction -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
<!-- Canvas element for Three.js rendering -->
<canvas id="myThreeJsCanvas" style="width: 100%; height: 300px; display: block;"></canvas>
<script>
// 1. Scene: Where all objects, cameras, and lights are placed
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf0f0f0); // Set a light gray background
// 2. Camera: Defines the perspective from which the scene is viewed
// PerspectiveCamera(FOV, Aspect Ratio, Near Plane, Far Plane)
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
// 3. Renderer: Renders the scene using WebGL
// antialias: true for smoother edges
const renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('myThreeJsCanvas'), antialias: true });
// Set renderer size to match the canvas element's client dimensions
renderer.setSize(document.getElementById('myThreeJsCanvas').clientWidth, document.getElementById('myThreeJsCanvas').clientHeight);
renderer.setPixelRatio(window.devicePixelRatio); // Handle high-DPI screens
// OrbitControls: Allows camera to orbit around a target using mouse
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // Provides a smooth, inertia-like feel
controls.dampingFactor = 0.25;
controls.screenSpacePanning = false; // Prevents panning in screen space
controls.maxPolarAngle = Math.PI / 2; // Prevents camera from going below the ground
// Basic Geometries & Materials
// Box: Create a cube geometry
const boxGeometry = new THREE.BoxGeometry(1, 1, 1);
// MeshStandardMaterial: A material that reacts to lights (PBR material)
const boxMaterial = new THREE.MeshStandardMaterial({ color: 0x00ff00 }); // Green color
const box = new THREE.Mesh(boxGeometry, boxMaterial);
box.position.x = -1.5; // Position the box to the left
scene.add(box); // Add the box to the scene
// Sphere: Create a sphere geometry
const sphereGeometry = new THREE.SphereGeometry(0.75, 32, 16);
const sphereMaterial = new THREE.MeshStandardMaterial({ color: 0xff0000 }); // Red color
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.position.x = 1.5; // Position the sphere to the right
scene.add(sphere); // Add the sphere to the scene
// Plane (Ground): Create a large flat plane
const planeGeometry = new THREE.PlaneGeometry(10, 10);
const planeMaterial = new THREE.MeshStandardMaterial({ color: 0xaaaaaa, side: THREE.DoubleSide });
const plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = Math.PI / 2; // Rotate the plane to be horizontal
plane.position.y = -1; // Position it below the objects
scene.add(plane); // Add the plane to the scene
// Lights
// AmbientLight: Illuminates all objects equally from all directions
const ambientLight = new THREE.AmbientLight(0x404040); // Soft white light
scene.add(ambientLight);
// DirectionalLight: Simulates sunlight - light from a distant source
const directionalLight = new THREE.DirectionalLight(0xffffff, 1); // White light with full intensity
directionalLight.position.set(5, 5, 5).normalize(); // Position the light and normalize its direction
scene.add(directionalLight);
// Set initial camera position
camera.position.z = 5;
camera.position.y = 2;
// Make camera look at the center of the scene
controls.target.set(0, 0, 0);
// Animation Loop: Renders the scene continuously
function animate() {
requestAnimationFrame(animate); // Request the next frame
// Rotate the box
box.rotation.x += 0.01;
box.rotation.y += 0.01;
// Rotate the sphere
sphere.rotation.y += 0.02;
controls.update(); // Update controls (important if damping/auto-rotate is enabled)
renderer.render(scene, camera); // Render the scene with the camera
}
// Handle window resize: Adjust camera aspect ratio and renderer size
window.addEventListener('resize', () => {
const canvas = document.getElementById('myThreeJsCanvas');
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix(); // Update camera's projection matrix after aspect ratio change
renderer.setSize(canvas.clientWidth, canvas.clientHeight); // Resize the renderer
});
// Start the animation loop once the window has loaded
window.onload = function () {
animate();
}
</script></code></pre>
</div>
</div>
<p class="mt-4 text-gray-700">
This Three.js example demonstrates the core components of a 3D scene:
`Scene`, `Camera`, and `Renderer`. It creates a basic box and sphere
(`BoxGeometry`, `SphereGeometry`) with materials (`MeshStandardMaterial`),
adds a ground plane, and sets up lighting (`AmbientLight`, `DirectionalLight`).
The `OrbitControls` enable interactive camera movement using the mouse.
The `animate` function runs a continuous loop for rendering and updating
object properties, creating simple animations.
</p>
</div>
<!-- Three.js Loading Models -->
<div class="p-4 bg-gray-50 rounded-lg shadow-sm flex flex-col">
<h3 class="text-xl font-medium mb-4">Loading 3D Models (GLTFLoader)</h3>
<div class="mb-4">
<p class="font-semibold mb-2">Description:</p>
<p class="text-gray-700">
Three.js uses `GLTFLoader` (and other specific loaders for different formats)
to import 3D models into your scene. You create an instance of the loader
and then call its `load` method, providing the model's URL and callback
functions for success, progress, and error handling. Once loaded, the
`gltf.scene` object can be added directly to your Three.js scene.
Remember to include the `GLTFLoader.js` script from the Three.js examples.
A live preview is not provided as it relies on external model files.
</p>
</div>
<div>
<p class="font-semibold mb-2">Code:</p>
<div class="code-block">
<pre><code><!-- Include GLTFLoader CDN in your <head> -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/loaders/GLTFLoader.js"></script>
<script>
// Assuming you have your scene, camera, renderer, and lights already set up
// ... (previous scene, camera, renderer, lights setup from the basic example) ...
// Create an instance of GLTFLoader
const loader = new THREE.GLTFLoader();
// Load a glTF resource
loader.load(
// resource URL: Replace 'path/to/your/model.gltf' with the actual URL of your 3D model
'path/to/your/model.gltf',
// called when the resource is loaded successfully
function (gltf) {
// Add the loaded model's scene (a THREE.Group) to your Three.js scene
scene.add(gltf.scene);
// gltf object contains various properties:
// gltf.animations: Array<THREE.AnimationClip> - Animations data
// gltf.scene: THREE.Group - The loaded 3D model's main group
// gltf.scenes: Array<THREE.Group> - All scenes in the glTF file
// gltf.cameras: Array<THREE.Camera> - Cameras defined in the glTF file
// gltf.asset: Object - Metadata about the glTF asset
// Optional: Adjust model position/scale after loading if needed
gltf.scene.position.set(0, 0, 0); // Set position
gltf.scene.scale.set(1, 1, 1); // Set scale
// gltf.scene.rotation.set(0, Math.PI / 2, 0); // Set rotation
},
// called while loading is progressing
function (xhr) {
// Log loading progress to the console
console.log((xhr.loaded / xhr.total * 100) + '% loaded');
},
// called when loading has errors
function (error) {
// Log any errors that occur during loading
console.error('An error happened while loading the model:', error);
}
);
// ... (rest of your animation loop and resize handler, if part of the same script) ...
</script></code></pre>
</div>
</div>
</div>
</div>
</section>
</div>
</body>
</html>