-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
161 lines (137 loc) · 4.61 KB
/
main.js
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
import * as THREE from "three";
import { FontLoader } from "three/examples/jsm/loaders/FontLoader.js";
import { TextGeometry } from "three/examples/jsm/geometries/TextGeometry.js";
import Stats from "three/examples/jsm/libs/stats.module.js";
// Set up container and initial scene
const container = document.createElement("div");
document.body.appendChild(container);
const width = window.innerWidth;
const height = window.innerHeight;
// Create the scene
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x000000);
// Set up camera
const camera = new THREE.PerspectiveCamera(40, width / height, 1, 1000);
camera.position.set(0, 0, 10);
// Add light to the scene
const light = new THREE.DirectionalLight(0xffffff, 3);
light.position.set(0, 0, 1);
scene.add(light);
// Create a cube geometry
const geometry1 = new THREE.BoxGeometry(1, 1, 1);
const material1 = new THREE.MeshBasicMaterial({ color: 0xff0ddd });
const cube = new THREE.Mesh(geometry1, material1);
scene.add(cube);
// Create a line shape
const material2 = new THREE.LineBasicMaterial({ color: 0x0ffddd });
const points = [];
points.push(new THREE.Vector3(0, 0, 0));
points.push(new THREE.Vector3(5, 0, 0));
points.push(new THREE.Vector3(5, 5, 0));
points.push(new THREE.Vector3(-5, 5, 0));
points.push(new THREE.Vector3(-5, 0, 0));
points.push(new THREE.Vector3(0, 0, 0));
const geometry2 = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry2, material2);
scene.add(line);
// Load a font and create the text
const loader = new FontLoader();
loader.load(
"https://threejs.org/examples/fonts/helvetiker_regular.typeface.json",
function (font) {
const textGeometry = new TextGeometry("Hello, I am Jay!", {
font: font,
size: 2,
height: 0.5,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 0.1,
bevelSize: 0.05,
bevelSegments: 5,
});
const textMaterial = new THREE.MeshBasicMaterial({ color: 0xffff00 });
const textMesh = new THREE.Mesh(textGeometry, textMaterial);
textMesh.position.set(-8, -3, 0); // Position the text within the scene
scene.add(textMesh);
}
);
// Set up shadow
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
const gradient = context.createRadialGradient(
canvas.width / 2,
canvas.height / 2,
0,
canvas.width / 2,
canvas.height / 2,
canvas.width / 2
);
gradient.addColorStop(0.1, "rgba(210,210,210,1)");
gradient.addColorStop(1, "rgba(255,255,255,1)");
context.fillStyle = gradient;
context.fillRect(0, 0, canvas.width, canvas.height);
const shadowTexture = new THREE.CanvasTexture(canvas);
const shadowMaterial = new THREE.MeshBasicMaterial({ map: shadowTexture });
const shadowGeo = new THREE.PlaneGeometry(300, 300, 1, 1);
const shadowMesh1 = new THREE.Mesh(shadowGeo, shadowMaterial);
shadowMesh1.position.y = -250;
shadowMesh1.rotation.x = -Math.PI / 2;
scene.add(shadowMesh1);
// Add geometry shapes
const radius = 200;
const geometry3 = new THREE.IcosahedronGeometry(radius, 1);
const material3 = new THREE.MeshPhongMaterial({
color: 0xffffff,
flatShading: true,
vertexColors: true,
shininess: 0,
});
const wireframeMaterial = new THREE.MeshBasicMaterial({
color: 0x000000,
wireframe: true,
transparent: true,
});
const mesh = new THREE.Mesh(geometry3, material3);
const wireframe = new THREE.Mesh(geometry3, wireframeMaterial);
mesh.add(wireframe);
mesh.position.set(400, 0, 0);
scene.add(mesh);
// Renderer
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(width, height);
container.appendChild(renderer.domElement);
// Stats for performance monitoring
const stats = new Stats();
container.appendChild(stats.dom);
// Handle window resize
window.addEventListener("resize", () => {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
});
// Mouse movement for camera interaction
let mouseX = 0,
mouseY = 0;
window.addEventListener("mousemove", (event) => {
mouseX = (event.clientX - window.innerWidth / 2) * 0.05;
mouseY = (event.clientY - window.innerHeight / 2) * 0.05;
});
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Rotate the cube
cube.rotation.x += 0.02;
cube.rotation.y += 0.02;
// Move camera with mouse interaction
camera.position.x += (mouseX - camera.position.x) * 0.05;
camera.position.y += (-mouseY - camera.position.y) * 0.05;
camera.lookAt(scene.position);
renderer.render(scene, camera);
stats.update();
}
// Start the animation
animate();