Skip to content

Commit 5cb2d95

Browse files
authored
Merge pull request #36 from vimaec/sroberge/isolation
Isolation update
2 parents 030e455 + ff4d864 commit 5cb2d95

31 files changed

+462
-712
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vim-web",
3-
"version": "0.3.42-dev.5",
3+
"version": "0.3.42-dev.14",
44
"description": "A demonstration app built on top of the vim-webgl-viewer",
55
"type": "module",
66
"files": [

src/vim-web/core-viewers/webgl/images.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @module vim-loader/materials
3+
* This module provides materials for rendering specific visualization modes in VIM.
4+
*/
5+
6+
import * as THREE from 'three'
7+
8+
/**
9+
* Creates a material for the ghost effect in isolation mode.
10+
*
11+
* - **Non-visible items**: Rendered as transparent objects using a customizable fill color.
12+
* - **Visible items**: Completely excluded from rendering.
13+
* - Designed for use with instanced or merged meshes.
14+
* - Includes clipping plane support, vertex colors, and transparency.
15+
*
16+
* @returns {THREE.ShaderMaterial} A custom shader material for the ghost effect.
17+
*/
18+
export function createGhostMaterial() {
19+
return new THREE.ShaderMaterial({
20+
userData: {
21+
isGhost: true
22+
},
23+
uniforms: {
24+
// Uniform controlling the overall transparency of the non-visible objects.
25+
opacity: { value: 0.001 },
26+
// Uniform specifying the fill color for non-visible objects.
27+
fillColor: { value: new THREE.Vector3(0, 0, 0) }
28+
},
29+
// Render only the front side of faces to prevent drawing internal geometry.
30+
side: THREE.FrontSide,
31+
// Enable support for vertex colors.
32+
vertexColors: true,
33+
// Enable transparency for the material.
34+
transparent: true,
35+
// Enable clipping planes for geometry slicing.
36+
clipping: true,
37+
// Prevent writing to the depth buffer for proper blending of transparent objects.
38+
depthWrite: false,
39+
// Perform depth testing to ensure correct rendering order.
40+
depthTest: true,
41+
vertexShader: /* glsl */ `
42+
#include <clipping_planes_pars_vertex>
43+
44+
// Attribute to determine if an object or vertex should be visible.
45+
// Used as an instance attribute for instanced meshes or a vertex attribute for merged meshes.
46+
attribute float ignore;
47+
48+
void main() {
49+
// Standard transformations to calculate vertex position.
50+
#include <begin_vertex>
51+
#include <project_vertex>
52+
#include <clipping_planes_vertex>
53+
54+
// Hide objects or vertices where the 'ignore' attribute is set to 0.
55+
if (ignore == 0.0) {
56+
// Push the vertex far out of view, effectively hiding it.
57+
gl_Position = vec4(1e20, 1e20, 1e20, 1.0);
58+
}
59+
}
60+
`,
61+
fragmentShader: /* glsl */ `
62+
#include <clipping_planes_pars_fragment>
63+
64+
// Uniform controlling the transparency level of the material.
65+
uniform float opacity;
66+
// Uniform specifying the fill color for non-visible objects.
67+
uniform vec3 fillColor;
68+
69+
void main() {
70+
// Handle clipping planes to discard fragments outside the defined planes.
71+
#include <clipping_planes_fragment>
72+
// Set the fragment color to the specified fill color and opacity.
73+
gl_FragColor = vec4(fillColor, opacity);
74+
}
75+
`
76+
});
77+
}

src/vim-web/core-viewers/webgl/loader/materials/isolationMaterial.ts

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/vim-web/core-viewers/webgl/loader/materials/simpleMaterial.ts

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,55 @@
11
/**
22
* @module vim-loader/materials
3+
* This module provides custom materials for visualizing and isolating objects in VIM.
34
*/
45

56
import * as THREE from 'three'
67

78
/**
8-
* Material for isolation mode
9-
* Non visible item appear as transparent.
10-
* Visible items are flat shaded with a basic pseudo lighting.
11-
* Supports object coloring for visible objects.
12-
* Non-visible objects use fillColor.
9+
* Creates a material for isolation mode.
10+
*
11+
* - **Non-visible items**: Completely excluded from rendering by pushing them out of view.
12+
* - **Visible items**: Rendered with flat shading and basic pseudo-lighting.
13+
* - **Object coloring**: Supports both instance-based and vertex-based coloring for visible objects.
14+
*
15+
* This material is optimized for both instanced and merged meshes, with support for clipping planes.
16+
*
17+
* @returns {THREE.ShaderMaterial} A custom shader material for isolation mode.
1318
*/
1419
export function createSimpleMaterial () {
1520
return new THREE.ShaderMaterial({
16-
uniforms: {
17-
opacity: { value: 0.1 },
18-
fillColor: { value: new THREE.Vector3(0, 0, 0) }
19-
},
21+
side: THREE.DoubleSide,
22+
// No uniforms are needed for this shader.
23+
uniforms: {},
24+
// Enable vertex colors for both instanced and merged meshes.
2025
vertexColors: true,
21-
// transparent: true,
26+
// Enable support for clipping planes.
2227
clipping: true,
2328
vertexShader: /* glsl */ `
24-
2529
#include <common>
2630
#include <logdepthbuf_pars_vertex>
2731
#include <clipping_planes_pars_vertex>
28-
32+
2933
// VISIBILITY
30-
// Instance or vertex attribute to hide objects
31-
// Used as instance attribute for instanced mesh and as vertex attribute for merged meshes.
34+
// Determines if an object or vertex should be visible.
35+
// Used as an instance attribute for instanced meshes or as a vertex attribute for merged meshes.
3236
attribute float ignore;
3337
34-
// Passed to fragment to discard them
35-
varying float vIgnore;
38+
// LIGHTING
39+
// Passes the vertex position to the fragment shader for lighting calculations.
3640
varying vec3 vPosition;
3741
38-
3942
// COLORING
43+
// Passes the color of the vertex or instance to the fragment shader.
4044
varying vec3 vColor;
4145
42-
// attribute for color override
43-
// merged meshes use it as vertex attribute
44-
// instanced meshes use it as an instance attribute
46+
// Determines whether to use instance color (1.0) or vertex color (0.0).
47+
// For merged meshes, this is used as a vertex attribute.
48+
// For instanced meshes, this is used as an instance attribute.
4549
attribute float colored;
4650
47-
// There seems to be an issue where setting mehs.instanceColor
48-
// doesn't properly set USE_INSTANCING_COLOR
49-
// so we always use it as a fix
51+
// Fix for a known issue where setting mesh.instanceColor does not properly enable USE_INSTANCING_COLOR.
52+
// This ensures that instance colors are always used when required.
5053
#ifndef USE_INSTANCING_COLOR
5154
attribute vec3 instanceColor;
5255
#endif
@@ -57,46 +60,56 @@ export function createSimpleMaterial () {
5760
#include <clipping_planes_vertex>
5861
#include <logdepthbuf_vertex>
5962
60-
// VISIBILITY
61-
// Set frag ignore from instance or vertex attribute
62-
vIgnore = ignore;
63+
// If ignore is greater than 0, hide the object by moving it far out of view.
64+
if (ignore > 0.0) {
65+
gl_Position = vec4(1e20, 1e20, 1e20, 1.0);
66+
return;
67+
}
6368
6469
// COLORING
70+
// Default to the vertex color.
6571
vColor = color.xyz;
6672
67-
// colored == 1 -> instance color
68-
// colored == 0 -> vertex color
73+
// Blend instance and vertex colors based on the colored attribute.
74+
// colored == 1.0 -> use instance color.
75+
// colored == 0.0 -> use vertex color.
6976
#ifdef USE_INSTANCING
70-
vColor.xyz = colored * instanceColor.xyz + (1.0f - colored) * color.xyz;
77+
vColor.xyz = colored * instanceColor.xyz + (1.0 - colored) * color.xyz;
7178
#endif
7279
73-
gl_Position.z = -10.0f;
74-
7580
// LIGHTING
76-
vPosition = vec3(mvPosition ) / mvPosition .w;
81+
// Pass the model-view position to the fragment shader for lighting calculations.
82+
vPosition = vec3(mvPosition) / mvPosition.w;
7783
}
7884
`,
7985
fragmentShader: /* glsl */ `
86+
#include <common>
87+
#include <logdepthbuf_pars_fragment>
8088
#include <clipping_planes_pars_fragment>
81-
varying float vIgnore;
89+
90+
91+
// Position and color data passed from the vertex shader.
8292
varying vec3 vPosition;
8393
varying vec3 vColor;
8494
8595
void main() {
8696
#include <clipping_planes_fragment>
97+
#include <logdepthbuf_fragment>
8798
88-
if (vIgnore > 0.0f){
89-
discard;
90-
}
91-
else{
92-
gl_FragColor = vec4(vColor.x, vColor.y, vColor.z, 1.0f);
93-
94-
// LIGHTING
95-
vec3 normal = normalize( cross(dFdx(vPosition), dFdy(vPosition)) );
96-
float light = dot(normal, normalize(vec3(1.4142f, 1.732f, 2.2360f)));
97-
light = 0.5 + (light *0.5);
98-
gl_FragColor.xyz *= light;
99-
}
99+
// Set the fragment color to the interpolated vertex or instance color.
100+
gl_FragColor = vec4(vColor, 1.0);
101+
102+
// LIGHTING
103+
// Compute a pseudo-normal using screen-space derivatives of the vertex position.
104+
vec3 normal = normalize(cross(dFdx(vPosition), dFdy(vPosition)));
105+
106+
// Apply simple directional lighting.
107+
// Normalize the light direction for consistent shading.
108+
float light = dot(normal, normalize(vec3(1.4142, 1.732, 2.236)));
109+
light = 0.5 + (light * 0.5); // Adjust light intensity to range [0.5, 1.0].
110+
111+
// Modulate the fragment color by the lighting intensity.
112+
gl_FragColor.xyz *= light;
100113
}
101114
`
102115
})

src/vim-web/core-viewers/webgl/loader/materials/standardMaterial.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export function createBasicOpaque () {
2727
vertexColors: true,
2828
flatShading: true,
2929
side: THREE.DoubleSide,
30-
31-
//shininess: 20
3230
})
3331
}
3432

@@ -39,7 +37,6 @@ export function createBasicOpaque () {
3937
export function createBasicTransparent () {
4038
const mat = createBasicOpaque()
4139
mat.transparent = true
42-
//mat.shininess = 70
4340
return mat
4441
}
4542

0 commit comments

Comments
 (0)