Skip to content

Commit e913570

Browse files
committed
Texture mapping for surfaces
1 parent f8a4505 commit e913570

16 files changed

+278
-57
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "vendor/ThreeRTT.js"]
88
path = vendor/ThreeRTT.js
99
url = https://github.com/unconed/ThreeRTT.js
10+
[submodule "vendor/console-extras"]
11+
path = vendor/console-extras
12+
url = https://github.com/unconed/console-extras.js.git

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ Bezier Surface
326326
doubleSided: true, // Whether the mesh is double sided
327327
flipSided: false, // Whether to flip a single sided mesh
328328
shaded: true, // Whether to shade the surface
329+
map: null, // Texture Map (pass in THREE.Texture)
330+
mapOpacity: 0, // Opacity of texture map
329331
})
330332
```
331333

@@ -370,6 +372,8 @@ Surface
370372
doubleSided: true, // Whether the mesh is double sided
371373
flipSided: false, // Whether to flip a single sided mesh
372374
shaded: true, // Whether to shade the surface
375+
map: null, // Texture Map (pass in THREE.Texture)
376+
mapOpacity: 0, // Opacity of texture map
373377
})
374378
```
375379

build/MathBox-bundle.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -43488,6 +43488,8 @@ MathBox.Style.prototype = {
4348843488
worldRotation: new THREE.Vector3(),
4348943489
worldPosition: new THREE.Vector3(),
4349043490
zIndex: 0.0,
43491+
43492+
mapOpacity: 0,
4349143493
};
4349243494
},
4349343495

@@ -44579,6 +44581,7 @@ MathBox.Surface.prototype = _.extend(new MathBox.Primitive(null), {
4457944581
doubleSided: true,
4458044582
flipSided: false,
4458144583
shaded: true,
44584+
map: null,
4458244585
style: {}//,
4458344586
};
4458444587
},
@@ -44621,7 +44624,8 @@ MathBox.Surface.prototype = _.extend(new MathBox.Primitive(null), {
4462144624
doubleSided: options.doubleSided,
4462244625
flipSided: options.flipSided,
4462344626
shaded: options.shaded,
44624-
dynamic: options.live
44627+
dynamic: options.live,
44628+
map: options.map,
4462544629
}, style);
4462644630
this.line = new MathBox.Renderable.Mesh(geometry, {
4462744631
type: 'mesh',
@@ -44795,6 +44799,7 @@ MathBox.BezierSurface.prototype = _.extend(new MathBox.Surface(null), {
4479544799
doubleSided: true,
4479644800
flipSided: false,
4479744801
shaded: true,
44802+
map: null,
4479844803
style: {}//,
4479944804
};
4480044805
},
@@ -45015,9 +45020,14 @@ MathBox.Renderable.prototype = {
4501545020
}
4501645021

4501745022
if (this.material) {
45023+
options = this.get();
45024+
45025+
// Apply texture
45026+
if (options.map) {
45027+
this.material.uniforms.texture.value = options.map;
45028+
}
4501845029

4501945030
// Set double sided / culling order.
45020-
options = this.get();
4502145031
this.material.side = options.doubleSided ? THREE.DoubleSide :
4502245032
THREE.FrontSide;
4502345033
options = { flipSided: (options.doubleSided && options.flipSided) ? -1 : 1 };

build/MathBox-bundle.min.js

+15-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/MathBox-core.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,8 @@ MathBox.Style.prototype = {
16411641
worldRotation: new THREE.Vector3(),
16421642
worldPosition: new THREE.Vector3(),
16431643
zIndex: 0.0,
1644+
1645+
mapOpacity: 0,
16441646
};
16451647
},
16461648

@@ -2732,6 +2734,7 @@ MathBox.Surface.prototype = _.extend(new MathBox.Primitive(null), {
27322734
doubleSided: true,
27332735
flipSided: false,
27342736
shaded: true,
2737+
map: null,
27352738
style: {}//,
27362739
};
27372740
},
@@ -2774,7 +2777,8 @@ MathBox.Surface.prototype = _.extend(new MathBox.Primitive(null), {
27742777
doubleSided: options.doubleSided,
27752778
flipSided: options.flipSided,
27762779
shaded: options.shaded,
2777-
dynamic: options.live
2780+
dynamic: options.live,
2781+
map: options.map,
27782782
}, style);
27792783
this.line = new MathBox.Renderable.Mesh(geometry, {
27802784
type: 'mesh',
@@ -2948,6 +2952,7 @@ MathBox.BezierSurface.prototype = _.extend(new MathBox.Surface(null), {
29482952
doubleSided: true,
29492953
flipSided: false,
29502954
shaded: true,
2955+
map: null,
29512956
style: {}//,
29522957
};
29532958
},
@@ -3168,9 +3173,14 @@ MathBox.Renderable.prototype = {
31683173
}
31693174

31703175
if (this.material) {
3176+
options = this.get();
3177+
3178+
// Apply texture
3179+
if (options.map) {
3180+
this.material.uniforms.texture.value = options.map;
3181+
}
31713182

31723183
// Set double sided / culling order.
3173-
options = this.get();
31743184
this.material.side = options.doubleSided ? THREE.DoubleSide :
31753185
THREE.FrontSide;
31763186
options = { flipSided: (options.doubleSided && options.flipSided) ? -1 : 1 };

build/MathBox-core.min.js

+15-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/MathBox.glsl.html

+16-2
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@
312312
// Fragment shader: render a shaded surface fragment.
313313
uniform vec3 color;
314314
uniform float opacity;
315+
uniform float mapOpacity;
316+
317+
uniform sampler2D texture;
318+
varying vec2 vUV;
319+
315320
varying vec3 vNormal;
316321
varying vec3 vViewPosition;
317322
uniform float flipSided;
@@ -337,18 +342,27 @@
337342
float dotNormalHalf = max(dot(normal, halfVector), 0.0);
338343
float specular = max(pow(dotNormalHalf, shininess), 0.0);
339344

340-
gl_FragColor = vec4(color * diffuse + specular * .3, opacity);
345+
vec4 sample = texture2D(texture, vUV);
346+
vec3 textured = color + (color * sample.xyz - color) * mapOpacity * sample.w;
347+
gl_FragColor = vec4(textured * diffuse + specular * .3, opacity);
341348
}
342349
</script>
343350

344351
<script type="application/x-glsl" id="fragmentSolid">
345352
// Fragment shader: render a solid surface fragment.
346353
uniform vec3 color;
347354
uniform float opacity;
355+
uniform float mapOpacity;
356+
357+
uniform sampler2D texture;
358+
varying vec2 vUV;
348359

349360
void fragmentSolid() {
350361
if (opacity < 0.01) discard;
351-
gl_FragColor = vec4(color, opacity);
362+
363+
vec4 sample = texture2D(texture, vUV);
364+
vec3 textured = color + (color * sample.xyz - color) * mapOpacity * sample.w;
365+
gl_FragColor = vec4(textured, opacity);
352366
}
353367
</script>
354368

build/MathBox.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -4612,6 +4612,8 @@ MathBox.Style.prototype = {
46124612
worldRotation: new THREE.Vector3(),
46134613
worldPosition: new THREE.Vector3(),
46144614
zIndex: 0.0,
4615+
4616+
mapOpacity: 0,
46154617
};
46164618
},
46174619

@@ -5703,6 +5705,7 @@ MathBox.Surface.prototype = _.extend(new MathBox.Primitive(null), {
57035705
doubleSided: true,
57045706
flipSided: false,
57055707
shaded: true,
5708+
map: null,
57065709
style: {}//,
57075710
};
57085711
},
@@ -5745,7 +5748,8 @@ MathBox.Surface.prototype = _.extend(new MathBox.Primitive(null), {
57455748
doubleSided: options.doubleSided,
57465749
flipSided: options.flipSided,
57475750
shaded: options.shaded,
5748-
dynamic: options.live
5751+
dynamic: options.live,
5752+
map: options.map,
57495753
}, style);
57505754
this.line = new MathBox.Renderable.Mesh(geometry, {
57515755
type: 'mesh',
@@ -5919,6 +5923,7 @@ MathBox.BezierSurface.prototype = _.extend(new MathBox.Surface(null), {
59195923
doubleSided: true,
59205924
flipSided: false,
59215925
shaded: true,
5926+
map: null,
59225927
style: {}//,
59235928
};
59245929
},
@@ -6139,9 +6144,14 @@ MathBox.Renderable.prototype = {
61396144
}
61406145

61416146
if (this.material) {
6147+
options = this.get();
6148+
6149+
// Apply texture
6150+
if (options.map) {
6151+
this.material.uniforms.texture.value = options.map;
6152+
}
61426153

61436154
// Set double sided / culling order.
6144-
options = this.get();
61456155
this.material.side = options.doubleSided ? THREE.DoubleSide :
61466156
THREE.FrontSide;
61476157
options = { flipSided: (options.doubleSided && options.flipSided) ? -1 : 1 };

0 commit comments

Comments
 (0)