highlight.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // This script handles highlighting of Three.js objects in the 3D scene
  2. ( function () {
  3. 'use strict';
  4. let highlightObject = null;
  5. function cloneMaterial( material ) {
  6. // Skip MeshNormalMaterial
  7. if ( material.isMeshNormalMaterial ) {
  8. return material;
  9. }
  10. // Handle ShaderMaterial and RawShaderMaterial
  11. if ( material.isShaderMaterial || material.isRawShaderMaterial ) {
  12. // Create new material of the same type
  13. const cloned = new material.constructor();
  14. // Override shaders with simple yellow output
  15. const vertexShader = `
  16. ${ material.isRawShaderMaterial ? `attribute vec3 position;
  17. uniform mat4 modelViewMatrix;
  18. uniform mat4 projectionMatrix;
  19. ` : '' }void main() {
  20. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  21. }
  22. `;
  23. const fragmentShader = `
  24. ${ material.isRawShaderMaterial ? `precision highp float;
  25. ` : '' }void main() {
  26. gl_FragColor = vec4( 1.0, 1.0, 0.0, 1.0 );
  27. }
  28. `;
  29. cloned.vertexShader = vertexShader;
  30. cloned.fragmentShader = fragmentShader;
  31. // Override with yellow wireframe settings
  32. cloned.wireframe = true;
  33. cloned.depthTest = false;
  34. cloned.depthWrite = false;
  35. cloned.transparent = true;
  36. cloned.opacity = 1;
  37. cloned.toneMapped = false;
  38. cloned.fog = false;
  39. return cloned;
  40. }
  41. // Create new material of the same type
  42. const cloned = new material.constructor();
  43. // Set yellow color
  44. if ( cloned.color ) {
  45. cloned.color.r = 1;
  46. cloned.color.g = 1;
  47. cloned.color.b = 0;
  48. }
  49. // If material has emissive, set it to yellow
  50. if ( 'emissive' in cloned ) {
  51. cloned.emissive.r = 1;
  52. cloned.emissive.g = 1;
  53. cloned.emissive.b = 0;
  54. }
  55. // Enable wireframe if the material supports it
  56. if ( 'wireframe' in cloned ) {
  57. cloned.wireframe = true;
  58. }
  59. // Render on top, ignoring depth
  60. cloned.depthTest = false;
  61. cloned.depthWrite = false;
  62. cloned.transparent = true;
  63. cloned.opacity = 1;
  64. // Disable tone mapping and fog
  65. cloned.toneMapped = false;
  66. cloned.fog = false;
  67. return cloned;
  68. }
  69. function highlight( uuid ) {
  70. const object = __THREE_DEVTOOLS__.utils.findObjectInScenes( uuid );
  71. if ( ! object ) {
  72. // Object not in scene (e.g., renderer) - hide highlight
  73. if ( highlightObject ) highlightObject.visible = false;
  74. return;
  75. }
  76. // Skip helpers, existing highlights, and objects without geometry
  77. if ( object.type.includes( 'Helper' ) || object.name === '__THREE_DEVTOOLS_HIGHLIGHT__' || ! object.geometry ) {
  78. if ( highlightObject ) highlightObject.visible = false;
  79. return;
  80. }
  81. // Remove old highlight if it exists
  82. if ( highlightObject && highlightObject.parent ) {
  83. highlightObject.parent.remove( highlightObject );
  84. }
  85. // Clone the object to preserve all properties (skeleton, bindMatrix, etc)
  86. highlightObject = object.clone();
  87. highlightObject.name = '__THREE_DEVTOOLS_HIGHLIGHT__';
  88. // Apply yellow wireframe material
  89. if ( highlightObject.material ) {
  90. if ( Array.isArray( highlightObject.material ) ) {
  91. highlightObject.material = highlightObject.material.map( cloneMaterial );
  92. } else {
  93. highlightObject.material = cloneMaterial( highlightObject.material );
  94. }
  95. }
  96. // Disable shadows
  97. highlightObject.castShadow = false;
  98. highlightObject.receiveShadow = false;
  99. // Render on top of everything
  100. highlightObject.renderOrder = Infinity;
  101. // Disable auto update before adding to scene
  102. highlightObject.matrixAutoUpdate = false;
  103. highlightObject.matrixWorldAutoUpdate = false;
  104. // Find the scene and add at root
  105. let scene = object;
  106. while ( scene.parent ) scene = scene.parent;
  107. scene.add( highlightObject );
  108. // Reuse the matrixWorld from original object (after adding to scene)
  109. highlightObject.matrixWorld = object.matrixWorld;
  110. // Make sure it's visible
  111. highlightObject.visible = true;
  112. }
  113. function unhighlight() {
  114. if ( highlightObject ) {
  115. highlightObject.visible = false;
  116. }
  117. }
  118. // Listen for highlight events from bridge.js
  119. __THREE_DEVTOOLS__.addEventListener( 'highlight-object', ( event ) => {
  120. highlight( event.detail.uuid );
  121. } );
  122. __THREE_DEVTOOLS__.addEventListener( 'unhighlight-object', () => {
  123. unhighlight();
  124. } );
  125. } )();
粤ICP备19079148号