highlight.js 4.2 KB

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