This repository was archived by the owner on Jan 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
121 lines (102 loc) · 3.11 KB
/
script.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
let scene,
camera,
renderer,
effect,
cube,
ambientLight,
pointLight;
let mouse = {
x: 0,
y: 0
};
init();
render();
function init() {
// Scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0xFFFFFF);
// Renderer
// setup container
/*
container = document.createElement('div');
container.style.position = 'absolute';
container.style.left = '0px';
container.style.top = '0px';
container.style.height = '100vh';
container.style.width = '100vw';
container.style.zIndex = '0';
document.querySelector(".three-container").appendChild(container);
*/
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setClearColor(0xFFFFFF)
renderer.setSize( window.innerWidth, window.innerHeight );
effect = new THREE.AsciiEffect(renderer, ' .\'`^",:;Il!i~+_-?][}{1)(|/tfjrxnuvczXYUJCLQ0OZmwsubuccubusB@$');
effect.setSize( window.innerWidth, window.innerHeight );
document.querySelector('.three-container').appendChild(effect.domElement);
// Camera
let screenWidth = window.innerWidth,
screenHeight = window.innerHeight,
viewAngle = 75,
nearDistance = 0.1,
farDistance = 1000;
camera = new THREE.PerspectiveCamera(viewAngle, screenWidth / screenHeight, nearDistance, farDistance);
camera.position.z = 0.75;
camera.lookAt(scene.position);
// Lights
// Ambient light
ambientLight = new THREE.AmbientLight(0x333333, 0.025);
scene.add(ambientLight);
// Point light
pointLight = new THREE.PointLight(0xFFFFFF, 10, 2, 10);
pointLight.position.set(0, 0, 0);
pointLight.castShadow = true;
pointLight.shadow.bias = 0.0001;
pointLight.mapSizeWidth = 2048; // Shadow Quality
pointLight.mapSizeHeight = 2048; // Shadow Quality
scene.add(pointLight);
// Cube
let cubeGeometry = new THREE.BoxGeometry( 0.75, 0.75, 0.75 );
let cubeMaterial = new THREE.MeshPhongMaterial({ // Required For Shadows
color: 0x888888,
specular: 0x000000,
shininess: 10
});
cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.castShadow = true;
cube.receiveShadow = true;
cube.position.set(0, 0, -1);
cube.rotation.x = 10;
cube.rotation.y = 20;
scene.add( cube );
// Listeners
document.addEventListener('mousemove', onMouseMove, false);
window.addEventListener('resize', onResize);
}
// Render Loop
function render() {
requestAnimationFrame( render );
cube.rotation.x += 0.005;
cube.rotation.y += 0.005;
effect.render( scene, camera );
}
// On mouse move
function onMouseMove(event) {
event.preventDefault();
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
let vector = new THREE.Vector3(mouse.x, mouse.y, 1.0);
vector.unproject(camera);
let dir = vector.sub(camera.position).normalize();
let distance = -camera.position.z / dir.z;
let pos = camera.position.clone().add(dir.multiplyScalar(distance));
pointLight.position.copy(pos);
};
// On resize
function onResize() {
let width = window.innerWidth;
let height = window.innerHeight;
renderer.setSize(width, height);
effect.setSize(width, height);
camera.aspect = width/height;
camera.updateProjectionMatrix();
}