-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreepyTreeMaterial.js
More file actions
96 lines (64 loc) · 2.67 KB
/
CreepyTreeMaterial.js
File metadata and controls
96 lines (64 loc) · 2.67 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
CreepyTreeMaterial = function ( parameters ) {
parameters.shading = THREE.SmoothShading;
THREE.ShaderMaterial.call( this, parameters );
this.shading = THREE.FlatShading;
this.wireframe = false;
this.wireframeLinewidth = 1;
this.setValues( parameters );
var shaders = THREE.ShaderLib[ 'normal' ];
this.uniforms = THREE.UniformsUtils.clone( shaders.uniforms );
this.uniforms.growth = { type: 'f', value: parameters.growth };
this.uniforms.radius = { type: 'f', value: parameters.radius };
this.uniforms.growPeriod = { type: 'f', value: parameters.growPeriod };
this.vertexShader = [
"#define PI 3.14159265359",
"varying vec3 vNormal;",
"varying float vDepth;",
"varying float screenZ;",
"uniform float growPeriod;",
"uniform float radius;",
"uniform float growth;",
"void main() {",
"float growDone = growth - growPeriod;",
"vDepth = uv2.x;",
// "float vAge = max(0.0, (growth - vDepth));",
"float growthProgress = float(vDepth < growDone) * 1.0",
,"+ float(vDepth >= growDone && vDepth < growth) * (growth - vDepth) / growPeriod;",
"float thicknessFactor = float(vDepth < growDone) * 1.0"
,"+ float(vDepth >= growDone) * (0.5 - cos( PI * growthProgress ) / 2.0);",
"float thickness = radius * thicknessFactor;",
"vec3 extrudedPosition = position + thickness * normal;",
//"vec3 extrudedPosition = position + 10.0 * growth * normal;",
"vec4 mvPosition = modelViewMatrix * vec4( extrudedPosition, 1.0 );",
// outputs
"gl_Position = projectionMatrix * mvPosition;",
"screenZ = gl_Position.z / gl_Position.w;",
"}"
].join("\n");
this.fragmentShader = [
"uniform float opacity;",
"uniform float growth;",
"varying vec3 vNormal;",
"varying float vDepth;",
"varying float screenZ;",
"void main() {",
// discard if haven't grown here yet
"if(vDepth > growth) discard;",
// visualize screenZ
//"float color = screenZ * 0.002;",
"float color = screenZ * 0.2;",
"gl_FragColor = vec4( color, color, color, float(vDepth < growth) );",
// visualize vDepth
//"gl_FragColor = vec4( vDepth, vDepth, vDepth, float(vDepth < growth) );",
"}"
].join("\n");
};
CreepyTreeMaterial.prototype = Object.create( THREE.ShaderMaterial.prototype );
CreepyTreeMaterial.prototype.clone = function () {
var material = new THREE.MeshNormalMaterial();
THREE.Material.prototype.clone.call( this, material );
material.shading = this.shading;
material.wireframe = this.wireframe;
material.wireframeLinewidth = this.wireframeLinewidth;
return material;
};