forked from aframevr/a-painter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrainbow.js
More file actions
120 lines (102 loc) · 4.28 KB
/
rainbow.js
File metadata and controls
120 lines (102 loc) · 4.28 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
/* globals AFRAME THREE */
(function(){
var vertexShader = "varying vec2 vUv; \
void main() { \
vUv = uv; \
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); \
}";
var fragmentShader = "uniform sampler2D tDiffuse; \
uniform float amount; \
uniform float time; \
varying vec2 vUv; \
\
vec3 hsv2rgb(vec3 c) { \
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); \
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); \
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); \
} \
\
void main() { \
float h = mod(vUv.x - time / 3000.0, 1.0); \
vec4 color = vec4(hsv2rgb(vec3(h, 1.0, 0.5)), 1.0); \
gl_FragColor = color; \
}";
var material = new THREE.ShaderMaterial({
vertexShader: vertexShader,
fragmentShader: fragmentShader,
side: THREE.DoubleSide,
uniforms: {
time: {type: 'f', value: 0}
}
});
AFRAME.registerBrush('line-rainbow',
{
init: function (color, brushSize) {
this.idx = 0;
this.geometry = new THREE.BufferGeometry();
this.vertices = new Float32Array(this.options.maxPoints * 3 * 3);
this.indices = new Uint32Array(this.options.maxPoints * 4.5 * 4.5)
this.uvs = new Float32Array(this.options.maxPoints * 2 * 2);
this.linepositions = new Float32Array(this.options.maxPoints);
this.geometry.setDrawRange(0, 0);
this.geometry.setAttribute('position', new THREE.BufferAttribute(this.vertices, 3).setUsage(THREE.DynamicDrawUsage));
this.geometry.setIndex(new THREE.BufferAttribute(this.indices, 3).setUsage(THREE.DynamicDrawUsage));
this.geometry.setAttribute('uv', new THREE.BufferAttribute(this.uvs, 2).setUsage(THREE.DynamicDrawUsage));
this.geometry.setAttribute('lineposition', new THREE.BufferAttribute(this.linepositions, 1).setUsage(THREE.DynamicDrawUsage));
this.material = material;
var mesh = new THREE.Mesh(this.geometry, this.material);
mesh.frustumCulled = false;
mesh.vertices = this.vertices;
this.object3D.add(mesh);
this.drawing = document.querySelector('.a-drawing');
this.drawing.object3D.add(this.object3D);
},
addPoint: (function () {
var direction = new THREE.Vector3();
var posA = new THREE.Vector3();
var posB = new THREE.Vector3();
var auxDir = new THREE.Vector3();
return function (position, orientation, pointerPosition, pressure, timestamp) {
var uv = 0;
for (i = 0; i < this.data.numPoints; i++) {
this.uvs[uv++] = i / (this.data.numPoints - 1);
this.uvs[uv++] = 0;
this.uvs[uv++] = i / (this.data.numPoints - 1);
this.uvs[uv++] = 1;
}
direction.set(1, 0, 0);
direction.applyQuaternion(orientation);
direction.normalize();
posA.copy(pointerPosition);
posB.copy(pointerPosition);
var brushSize = this.data.size * pressure;
posA.add(auxDir.copy(direction).multiplyScalar(brushSize / 2));
posB.add(auxDir.copy(direction).multiplyScalar(-brushSize / 2));
this.vertices[this.idx++] = posA.x;
this.vertices[this.idx++] = posA.y;
this.vertices[this.idx++] = posA.z;
this.vertices[this.idx++] = posB.x;
this.vertices[this.idx++] = posB.y;
this.vertices[this.idx++] = posB.z;
// Same principle as line brush strips
if (this.idx > 6) {
this.geometry.index.setXYZ(this.idx / 3 - 4, this.idx / 3 - 4, this.idx / 3 - 3, this.idx / 3 - 2);
this.geometry.index.setXYZ(this.idx / 3 - 3, this.idx / 3 - 2, this.idx / 3 - 3, this.idx / 3 - 1);
}
this.geometry.attributes.position.needsUpdate = true;
this.geometry.index.needsUpdate = true;
this.geometry.attributes.uv.needsUpdate = true;
this.geometry.setDrawRange(0, this.data.numPoints * 2 * 6);
return true;
}
})(),
tick: function(timeOffset, delta) {
this.material.uniforms.time.value = timeOffset;
},
undo: function () {
this.drawing.object3D.children.pop();
}
},
{thumbnail:'brushes/thumb_rainbow.png', maxPoints: 3000}
);
})();