-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathraymarcher.js
205 lines (150 loc) · 5.71 KB
/
raymarcher.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var RayMarcher = function(){
var tl = new THREE.TextureLoader();
var cl = new THREE.CubeTextureLoader();
var rc = new THREE.Raycaster();
var mouse = new THREE.Vector2();
function RayMarcher( distance, precision ){
this.distance = distance || 50;
this.precision = precision || 0.01;
//scene setup
this.scene = new THREE.Scene();
this.renderer = new THREE.WebGLRenderer();
this.renderer.setSize( window.innerWidth, window.innerHeight );
this.domElement = this.renderer.domElement;
//used only to render the scene
this.renderCamera = new THREE.OrthographicCamera(-1,1,1,-1,1/Math.pow( 2, 53 ),1);
//geometry setup
this.geom = new THREE.BufferGeometry();
this.geom.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array([ -1,-1,0, 1,-1,0, 1,1,0, -1, -1, 0, 1, 1, 0, -1, 1, 0]), 3 ) );
this.mesh = new THREE.Mesh( this.geom, null );
this.scene.add( this.mesh );
//some helpers
this.camera = new THREE.PerspectiveCamera( 60, 1, 0.1,1 );
this.target = new THREE.Vector3();
return this;
}
function loadFragmentShader( fragmentUrl, callback )
{
this.loaded = false;
var scope = this;
var req = new XMLHttpRequest();
req.open( "GET", fragmentUrl );
req.onload = function (e) {
scope.setFragmentShader( e.target.responseText, callback );
};
req.send();
return this;
}
function setFragmentShader( fs, cb ){
this.startTime = Date.now();
this.mesh.material = this.material = new THREE.ShaderMaterial({
uniforms :{
resolution:{ type:"v2", value:new THREE.Vector2( this.width, this.height ) },
time:{ type:"f", value:0 },
randomSeed:{ type:"f", value:Math.random() },
fov:{ type:"f", value:45 },
camera:{ type:"v3", value:this.camera.position },
target:{ type:"v3", value:this.target },
raymarchMaximumDistance:{ type:"f", value:this.distance },
raymarchPrecision:{ type:"f", value:this.precision}
},
vertexShader : "void main() {gl_Position = vec4( position, 1.0 );}",
fragmentShader : fs,
transparent:true
});
this.update();
if( cb != null )cb( this );
this.loaded = true;
return this;
}
function setTexture( name, url ){
if( this.material == null )
{
throw new Error("material not initialised, use setFragmentShader() first.");
}
rm.loaded = false;
var scope = this;
this.material.uniforms[ name ] = {type:'t', value:null };
tl.load( url, function(texture){
scope.material.uniforms[ name ].value = texture;
scope.material.needsUpdate = true;
scope.loaded = true;
texture.needsUpdate = true;
});
return this;
}
function setCubemap( name, urls ){
if( this.material == null )
{
throw new Error("material not initialised, use setFragmentShader() first.");
}
rm.loaded = false;
var scope = this;
this.material.uniforms[ name ] = {type:'t', value:null };
cl.load( urls, function(texture) {
scope.material.uniforms[ name ].value = texture;
scope.material.needsUpdate = true;
scope.loaded = true;
texture.needsUpdate = true;
});
}
function setUniform( name, type, value ){
if( this.material == null )
{
throw new Error("material not initialised, use setFragmentShader() first.");
}
this.material.uniforms[ name ] = {type:type, value:value };
return this;
}
function getUniform( name ){
if( this.material == null )
{
console.warn("raymarcher.getUniform: material not initialised, use setFragmentShader() first.");
return null;
}
return this.material.uniforms[name];
}
function setSize( width, height ){
this.width = width;
this.height = height;
this.renderer.setSize( width, height );
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
if( this.material != null )
{
this.material.uniforms.resolution.value.x = width;
this.material.uniforms.resolution.value.y = height;
}
return this;
}
function update(){
if( this.material == null )return;
this.material.uniforms.time.value = ( Date.now() - this.startTime ) * .001;
this.material.uniforms.randomSeed.value = Math.random();
this.material.uniforms.fov.value = this.camera.fov * Math.PI / 180;
this.material.uniforms.raymarchMaximumDistance.value = this.distance;
this.material.uniforms.raymarchPrecision.value = this.precision;
this.material.uniforms.camera.value = this.camera.position;
this.material.uniforms.target.value = this.target;
this.camera.lookAt( this.target );
}
function render(){
if( this.loaded )
{
this.update();
this.renderer.render( this.scene, this.renderCamera );
}
}
var _p = RayMarcher.prototype;
_p.constructor = RayMarcher;
_p.loadFragmentShader = loadFragmentShader;
_p.setFragmentShader = setFragmentShader;
_p.setTexture = setTexture;
_p.setCubemap = setCubemap;
_p.setUniform = setUniform;
_p.getUniform = getUniform;
_p.setSize = setSize;
_p.update = update;
_p.render = render;
return RayMarcher;
}();