-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenGridHelper.js
More file actions
165 lines (134 loc) · 4.1 KB
/
OpenGridHelper.js
File metadata and controls
165 lines (134 loc) · 4.1 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
/**
* Author: Vishwajeet Mane
* References
* 1. https://github.com/Fyrestar/THREE.InfiniteGridHelper/blob/master/InfiniteGridHelper.js
* 2. https://dev.to/javiersalcedopuyo/simple-infinite-grid-shader-5fah
*/
/**
* Notes
* 1. Area is the size of the grid
* 2. Visible Area is the portion of the grid that is visible with the raidal blur
* 3. Currently, the grid is only visible in the xz plane
*/
/**
* TODO
* 1. Do Grid Calculation According To Axes
*/
let vAxes = "xy";
import * as THREE from 'three';
function generateGridPoints(axes, size) {
const gridPoints = [];
gridPoints.push(1, 0, 1);
gridPoints.push(-1, 0, 1);
gridPoints.push(-1, 0, -1);
gridPoints.push(1, 0, -1);
gridPoints.push(1, 0, 1);
const offsets = [];
const area = size;
const areaNeg = -area;
for (let i = areaNeg/2; i < area/2; i++) {
for (let j = areaNeg/2; j < area/2; j++) {
offsets.push(i, 0, j);
}
}
return {
gridPoints: new Float32Array(gridPoints),
offset: new Float32Array(offsets)
};
}
class Grid {
constructor(axes="xzy", color = new THREE.Vector3(0, 0, 0), size = 50, visibleArea = 25, polka = false) {
const axesArray = axes.substring(0, 2);
vAxes = axesArray;
const shader = Shader;
shader.uniforms.lineColor.value = color;
shader.uniforms.visibleArea.value = visibleArea;
shader.uniforms.polka.value = polka;
const gridMaterial = new THREE.RawShaderMaterial({
name: shader.name,
uniforms: shader.uniforms,
vertexShader: vertexShaderFunc(),
fragmentShader: fragmentShaderFun(),
side: THREE.DoubleSide,
forceSinglePass: true,
transparent: true
});
const gridData = generateGridPoints(axes, size);
const gridPosition = gridData.gridPoints;
const offsets = gridData.offset;
const gridInstancedGeometry = new THREE.InstancedBufferGeometry();
gridInstancedGeometry.instanceCount = offsets.length / 3;
gridInstancedGeometry.setAttribute('position', new THREE.Float32BufferAttribute(gridPosition, 3));
gridInstancedGeometry.setAttribute('offset', new THREE.InstancedBufferAttribute(new Float32Array(offsets), 3));
if (polka) {
gridMaterial.uniforms.polkaTexture = { value: new THREE.TextureLoader().load('https://threejs.org/examples/textures/sprites/disc.png') };
const grid = new THREE.Points(gridInstancedGeometry, gridMaterial);
grid.frustumCulled = false;
return grid;
}
else {
const grid = new THREE.Line(gridInstancedGeometry, gridMaterial);
grid.frustumCulled = false;
return grid;
}
}
}
function fragmentShaderFun() {
return `
precision highp float;
uniform vec3 lineColor;
float near = 0.1;
uniform float visibleArea;
varying vec3 vPosition;
uniform vec3 cameraPosition;
uniform bool polka;
uniform float polpkSize;
uniform sampler2D polkaTexture;
void main() {
float dist = distance(vPosition, cameraPosition);
float alpha = 1.0 - smoothstep(near, visibleArea, dist);
if (polka) {
gl_FragColor = vec4( lineColor, alpha );
gl_FragColor = gl_FragColor * texture2D( polkaTexture, gl_PointCoord );
}
else {
gl_FragColor = vec4(lineColor, alpha);
}
}
`;
}
function vertexShaderFunc() {
return `
precision highp float;
attribute vec3 offset;
attribute vec3 position;
varying vec3 vPosition;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform vec3 cameraPosition;
uniform float visibleArea;
uniform vec3 lineColor;
vec3 worldPosition;
uniform bool polka;
uniform float polkaSize;
void main() {
worldPosition = position + offset;
worldPosition.${vAxes} += cameraPosition.${vAxes} - mod(cameraPosition.${vAxes}, 1.0);
vPosition = worldPosition;
if (polka) {
gl_PointSize = polkaSize;
}
gl_Position = projectionMatrix * modelViewMatrix * vec4(vPosition, 1.0);
}
`;
}
const Shader = {
name: 'OpenGridHelper',
uniforms: {
'lineColor': { value: new THREE.Vector3(0, 0, 0) },
'visibleArea': { value: 25 },
'polka': { value: false },
'polkaSize': { value: 3 }
}
}
export { Grid, Shader }