-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
173 lines (121 loc) · 4.57 KB
/
Copy pathtest.html
File metadata and controls
173 lines (121 loc) · 4.57 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - sprites</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body, html { padding:0; margin:0; overflow: hidden }
</style>
</head>
<body>
<script type="module">
import * as THREE from '/js/three.module.js';
class SpriteMulti extends THREE.Sprite {
constructor( material ) {
super();
this.type = 'Sprite';
let _geometry = new THREE.BufferGeometry();
let s = 1.0 / 8.0;
let x = Math.random()*8|0;
let y = Math.random()*8|0;
const float32Array = new Float32Array( [
-0.5, -0.5, 0, (x+0)*s, (y+0)*s,
0.5, -0.5, 0, (x+1)*s, (y+0)*s,
0.5, 0.5, 0, (x+1)*s, (y+1)*s,
-0.5, 0.5, 0, (x+0)*s, (y+1)*s
] );
const interleavedBuffer = new THREE.InterleavedBuffer( float32Array, 5 );
_geometry.setIndex( [ 0, 1, 2, 0, 2, 3 ] );
_geometry.setAttribute( 'position', new THREE.InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
_geometry.setAttribute( 'uv', new THREE.InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
this.geometry = _geometry;
this.material = ( material !== undefined ) ? material : new THREE.SpriteMaterial();
this.center = new THREE.Vector2( 0.5, 0.5 );
}
}
let camera, scene, renderer;
let cameraOrtho, sceneOrtho;
let group;
init();
animate();
function init() {
const width = window.innerWidth;
const height = window.innerHeight;
camera = new THREE.PerspectiveCamera( 60, width / height, 1, 2100 );
camera.position.z = 1000;
cameraOrtho = new THREE.OrthographicCamera( - width / 2, width / 2, height / 2, - height / 2, 1, 10 );
cameraOrtho.position.z = 10;
scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x000000, 500, 1000 );
sceneOrtho = new THREE.Scene();
// create sprites
const amount = 200;
const radius = 500;
const textureLoader = new THREE.TextureLoader();
let map = textureLoader.load( "/img/glyphs_128x128.png");
group = new THREE.Group();
const mat = new THREE.SpriteMaterial( { map: map, color: 0xffffff, fog: true } );
for ( let a = 0; a < amount; a ++ ) {
const x = Math.random() - 0.5;
const y = Math.random() - 0.5;
const z = Math.random() - 0.5;
let material;
let sprite = new SpriteMulti( mat );
sprite.position.set( x, y, z );
sprite.position.normalize();
sprite.position.multiplyScalar( radius );
group.add( sprite );
}
scene.add( group );
// renderer
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.autoClear = false; // To allow render overlay on top of sprited sphere
document.body.appendChild( renderer.domElement );
//
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
cameraOrtho.left = -width / 2;
cameraOrtho.right = width / 2;
cameraOrtho.top = height / 2;
cameraOrtho.bottom = -height / 2;
cameraOrtho.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
const time = Date.now() / 1000;
for ( let i = 0, l = group.children.length; i < l; i ++ ) {
const sprite = group.children[ i ];
const material = sprite.material;
const scale = Math.sin( time + sprite.position.x * 0.01 ) * 0.3 + 1.0;
let imageWidth = 1;
let imageHeight = 1;
if ( material.map && material.map.image && material.map.image.width ) {
imageWidth = material.map.image.width;
imageHeight = material.map.image.height;
}
// sprite.material.rotation += 0.1 * ( i / l );
sprite.scale.set( 64.0, 64.0, 1.0 );
}
group.rotation.x = time * 0.5;
group.rotation.y = time * 0.75;
group.rotation.z = time * 1.0;
renderer.clear();
renderer.render( scene, camera );
renderer.clearDepth();
renderer.render( sceneOrtho, cameraOrtho );
}
</script>
</body>
</html>