Skip to content

Commit 17729fa

Browse files
committed
beach ball planet as a work-around for #1
1 parent fbb1522 commit 17729fa

4 files changed

Lines changed: 84 additions & 57 deletions

File tree

img/moon_1024.jpg

-233 KB
Binary file not shown.

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
</script>
7777
<script data-src="raytracer.glsl" data-name="raytracer" type="x-shader/x-fragment"></script>
7878

79+
<script src="procedural-textures.js"></script>
7980
<script src="main.js"></script>
8081

8182
</body>

main.js

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ var container, stats;
55
var camera, scene, renderer, cameraControls, shader = null;
66
var uniforms;
77

8-
var TEX_RES = 2*1024;
9-
108
function Shader(mustacheTemplate) {
119
// Compile-time shader parameters
1210
this.parameters = {
@@ -65,44 +63,13 @@ function degToRad(a) { return Math.PI * a / 180.0; }
6563
checkLoaded();
6664
});
6765

68-
texLoader.load('img/moon_1024.jpg', function(tex) {
69-
textures.moon = tex;
70-
checkLoaded();
71-
});
72-
73-
textures.accretion_disk = renderDataTexture(TEX_RES, TEX_RES/4, accretionDiskTexture1D);
74-
textures.stars = renderDataTexture(TEX_RES*2, TEX_RES, starBackgroundTexture);
66+
textures.moon = ProceduralTextures.beachBall();
67+
textures.accretion_disk = ProceduralTextures.accretionDisk();
68+
textures.stars = ProceduralTextures.starBackground();
7569

7670
checkLoaded();
7771
})();
7872

79-
function renderDataTexture(width, height, renderer) {
80-
// Generate random noise texture
81-
var size = width * height;
82-
var data = new Uint8Array( 3 * size );
83-
84-
function to8bit(f) {
85-
return Math.round(Math.max(0, Math.min(f, 1.0))*255);
86-
}
87-
88-
var i = 0;
89-
for (var y=0; y<height; y++) {
90-
for (var x=0; x<width; x++) {
91-
var col = renderer(x/width,y/height);
92-
data[i++] = to8bit(col.r);
93-
data[i++] = to8bit(col.g);
94-
data[i++] = to8bit(col.b);
95-
}
96-
}
97-
98-
var dt = new THREE.DataTexture( data, width, height, THREE.RGBFormat);
99-
dt.magFilter = THREE.LinearFilter;
100-
dt.minFilter = THREE.LinearFilter;
101-
dt.needsUpdate = true;
102-
103-
return dt;
104-
}
105-
10673
function init(textures) {
10774

10875
container = document.createElement( 'div' );
@@ -195,27 +162,6 @@ function setupGUI() {
195162
gui.add(shader.parameters, 'time_scale').min(0);
196163
}
197164

198-
function starBackgroundTexture(x,y) {
199-
200-
var prob = 5.0 / TEX_RES;
201-
prob *= Math.cos((y-0.5)*Math.PI);
202-
203-
var s = Math.random();
204-
205-
if (s < prob) {
206-
s /= prob;
207-
return { r: s, g: s, b: s };
208-
}
209-
210-
return { r: 0, g: 0, b: 0 };
211-
}
212-
213-
function accretionDiskTexture1D(x, y) {
214-
var s = x*Math.exp(-x*4.0)*(1.0-x) * Math.pow((Math.sin(x*Math.PI*20)+1.0)*0.5,0.1) * 20.0;
215-
if (Math.ceil(y*50)%2 === 0) s *= 0.7;
216-
return { r: s, g: s*0.8, b: s*0.5 };
217-
}
218-
219165
function onWindowResize( event ) {
220166

221167
renderer.setSize( window.innerWidth, window.innerHeight );

procedural-textures.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
function renderDataTexture(width, height, renderer) {
3+
// Generate random noise texture
4+
var size = width * height;
5+
var data = new Uint8Array( 3 * size );
6+
7+
function to8bit(f) {
8+
return Math.round(Math.max(0, Math.min(f, 1.0))*255);
9+
}
10+
11+
var i = 0;
12+
for (var y=0; y<height; y++) {
13+
for (var x=0; x<width; x++) {
14+
var col = renderer(x/width,y/height);
15+
data[i++] = to8bit(col.r);
16+
data[i++] = to8bit(col.g);
17+
data[i++] = to8bit(col.b);
18+
}
19+
}
20+
21+
var dt = new THREE.DataTexture( data, width, height, THREE.RGBFormat);
22+
dt.magFilter = THREE.LinearFilter;
23+
dt.minFilter = THREE.LinearFilter;
24+
dt.needsUpdate = true;
25+
26+
return dt;
27+
}
28+
29+
ProceduralTextures = {
30+
31+
beachBall: function() {
32+
33+
var colors = [
34+
{ r: 1, g: 0, b: 0 },
35+
{ r: 1, g: 1, b: 1 },
36+
{ r: 0, g: 0.5, b: 0 },
37+
{ r: 1, g: 1, b: 1 },
38+
{ r: 0, g: 0, b: 1 },
39+
{ r: 1, g: 1, b: 1 }
40+
];
41+
42+
var dt = renderDataTexture(colors.length, 1, function(x,y) {
43+
return colors[Math.floor(x*colors.length)];
44+
});
45+
dt.magFilter = THREE.NearestFilter;
46+
dt.minFilter = THREE.NearestFilter;
47+
return dt;
48+
},
49+
50+
accretionDisk: function() {
51+
52+
var TEX_RES = 2048;
53+
54+
return renderDataTexture(TEX_RES, TEX_RES/4, function(x,y) {
55+
var s = x*Math.exp(-x*4.0)*(1.0-x) * Math.pow((Math.sin(x*Math.PI*20)+1.0)*0.5,0.1) * 20.0;
56+
if (Math.ceil(y*50)%2 === 0) s *= 0.7;
57+
return { r: s, g: s*0.8, b: s*0.5 };
58+
});
59+
},
60+
61+
starBackground: function() {
62+
63+
var TEX_RES = 2*1024;
64+
65+
return renderDataTexture(TEX_RES*2, TEX_RES, function(x,y) {
66+
67+
var prob = 5.0 / TEX_RES;
68+
prob *= Math.cos((y-0.5)*Math.PI);
69+
70+
var s = Math.random();
71+
72+
if (s < prob) {
73+
s /= prob;
74+
return { r: s, g: s, b: s };
75+
}
76+
77+
return { r: 0, g: 0, b: 0 };
78+
});
79+
}
80+
};

0 commit comments

Comments
 (0)