11/**
22 * @module vim-loader/materials
3+ * This module provides custom materials for visualizing and isolating objects in VIM.
34 */
45
56import * 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 */
1419export 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 } )
0 commit comments