webgl_raycaster_texture.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js raycaster - texture</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <meta property="og:title" content="three.js raycaster - texture">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_raycaster_texture.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_raycaster_texture.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background-color: #fff;
  15. color: #444;
  16. }
  17. a {
  18. color: #08f;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="container"></div>
  24. <div id="info">
  25. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> raycaster - texture
  26. </div>
  27. <script type="importmap">
  28. {
  29. "imports": {
  30. "three": "../build/three.module.js",
  31. "three/addons/": "./jsm/"
  32. }
  33. }
  34. </script>
  35. <script type="module">
  36. import * as THREE from 'three';
  37. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  38. const WRAPPING = {
  39. 'RepeatWrapping': THREE.RepeatWrapping,
  40. 'ClampToEdgeWrapping': THREE.ClampToEdgeWrapping,
  41. 'MirroredRepeatWrapping': THREE.MirroredRepeatWrapping
  42. };
  43. const params = {
  44. wrapS: THREE.RepeatWrapping,
  45. wrapT: THREE.RepeatWrapping,
  46. offsetX: 0,
  47. offsetY: 0,
  48. repeatX: 1,
  49. repeatY: 1,
  50. rotation: 0,
  51. };
  52. function CanvasTexture( parentTexture ) {
  53. this._canvas = document.createElement( 'canvas' );
  54. this._canvas.width = this._canvas.height = 1024;
  55. this._context2D = this._canvas.getContext( '2d' );
  56. if ( parentTexture ) {
  57. this._parentTexture.push( parentTexture );
  58. parentTexture.image = this._canvas;
  59. }
  60. const that = this;
  61. this._background = document.createElement( 'img' );
  62. this._background.addEventListener( 'load', function () {
  63. that._canvas.width = that._background.naturalWidth;
  64. that._canvas.height = that._background.naturalHeight;
  65. that._crossRadius = Math.ceil( Math.min( that._canvas.width, that._canvas.height / 30 ) );
  66. that._crossMax = Math.ceil( 0.70710678 * that._crossRadius );
  67. that._crossMin = Math.ceil( that._crossMax / 10 );
  68. that._crossThickness = Math.ceil( that._crossMax / 10 );
  69. that._draw();
  70. } );
  71. this._background.crossOrigin = '';
  72. this._background.src = 'textures/uv_grid_opengl.jpg';
  73. this._draw();
  74. }
  75. CanvasTexture.prototype = {
  76. constructor: CanvasTexture,
  77. _canvas: null,
  78. _context2D: null,
  79. _xCross: 0,
  80. _yCross: 0,
  81. _crossRadius: 57,
  82. _crossMax: 40,
  83. _crossMin: 4,
  84. _crossThickness: 4,
  85. _parentTexture: [],
  86. addParent: function ( parentTexture ) {
  87. if ( this._parentTexture.indexOf( parentTexture ) === - 1 ) {
  88. this._parentTexture.push( parentTexture );
  89. parentTexture.image = this._canvas;
  90. }
  91. },
  92. setCrossPosition: function ( x, y ) {
  93. this._xCross = x * this._canvas.width;
  94. this._yCross = y * this._canvas.height;
  95. this._draw();
  96. },
  97. _draw: function () {
  98. if ( ! this._context2D ) return;
  99. this._context2D.clearRect( 0, 0, this._canvas.width, this._canvas.height );
  100. // Background.
  101. this._context2D.drawImage( this._background, 0, 0 );
  102. // Yellow cross.
  103. this._context2D.lineWidth = this._crossThickness * 3;
  104. this._context2D.strokeStyle = '#FFFF00';
  105. this._context2D.beginPath();
  106. this._context2D.moveTo( this._xCross - this._crossMax - 2, this._yCross - this._crossMax - 2 );
  107. this._context2D.lineTo( this._xCross - this._crossMin, this._yCross - this._crossMin );
  108. this._context2D.moveTo( this._xCross + this._crossMin, this._yCross + this._crossMin );
  109. this._context2D.lineTo( this._xCross + this._crossMax + 2, this._yCross + this._crossMax + 2 );
  110. this._context2D.moveTo( this._xCross - this._crossMax - 2, this._yCross + this._crossMax + 2 );
  111. this._context2D.lineTo( this._xCross - this._crossMin, this._yCross + this._crossMin );
  112. this._context2D.moveTo( this._xCross + this._crossMin, this._yCross - this._crossMin );
  113. this._context2D.lineTo( this._xCross + this._crossMax + 2, this._yCross - this._crossMax - 2 );
  114. this._context2D.stroke();
  115. for ( let i = 0; i < this._parentTexture.length; i ++ ) {
  116. this._parentTexture[ i ].needsUpdate = true;
  117. }
  118. }
  119. };
  120. const width = window.innerWidth;
  121. const height = window.innerHeight;
  122. let canvas;
  123. let planeTexture, cubeTexture, circleTexture;
  124. let container;
  125. let camera, scene, renderer;
  126. const raycaster = new THREE.Raycaster();
  127. const mouse = new THREE.Vector2();
  128. const onClickPosition = new THREE.Vector2();
  129. init();
  130. function init() {
  131. container = document.getElementById( 'container' );
  132. scene = new THREE.Scene();
  133. scene.background = new THREE.Color( 0xeeeeee );
  134. camera = new THREE.PerspectiveCamera( 45, width / height, 1, 1000 );
  135. camera.position.x = - 30;
  136. camera.position.y = 40;
  137. camera.position.z = 50;
  138. camera.lookAt( scene.position );
  139. renderer = new THREE.WebGLRenderer();
  140. renderer.setPixelRatio( window.devicePixelRatio );
  141. renderer.setSize( width, height );
  142. renderer.setAnimationLoop( animate );
  143. container.appendChild( renderer.domElement );
  144. // A cube, in the middle.
  145. cubeTexture = new THREE.Texture( undefined, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping );
  146. cubeTexture.colorSpace = THREE.SRGBColorSpace;
  147. canvas = new CanvasTexture( cubeTexture );
  148. const cubeMaterial = new THREE.MeshBasicMaterial( { map: cubeTexture } );
  149. const cubeGeometry = new THREE.BoxGeometry( 20, 20, 20 );
  150. let uvs = cubeGeometry.attributes.uv.array;
  151. // Set a specific texture mapping.
  152. for ( let i = 0; i < uvs.length; i ++ ) {
  153. uvs[ i ] *= 2;
  154. }
  155. const cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
  156. cube.position.x = 4;
  157. cube.position.y = - 5;
  158. cube.position.z = 0;
  159. scene.add( cube );
  160. // A plane on the left
  161. planeTexture = new THREE.Texture( undefined, THREE.UVMapping, THREE.MirroredRepeatWrapping, THREE.MirroredRepeatWrapping );
  162. planeTexture.colorSpace = THREE.SRGBColorSpace;
  163. canvas.addParent( planeTexture );
  164. const planeMaterial = new THREE.MeshBasicMaterial( { map: planeTexture } );
  165. const planeGeometry = new THREE.PlaneGeometry( 25, 25, 1, 1 );
  166. uvs = planeGeometry.attributes.uv.array;
  167. // Set a specific texture mapping.
  168. for ( let i = 0; i < uvs.length; i ++ ) {
  169. uvs[ i ] *= 2;
  170. }
  171. const plane = new THREE.Mesh( planeGeometry, planeMaterial );
  172. plane.position.x = - 16;
  173. plane.position.y = - 5;
  174. plane.position.z = 0;
  175. scene.add( plane );
  176. // A circle on the right.
  177. circleTexture = new THREE.Texture( undefined, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping );
  178. circleTexture.colorSpace = THREE.SRGBColorSpace;
  179. canvas.addParent( circleTexture );
  180. const circleMaterial = new THREE.MeshBasicMaterial( { map: circleTexture } );
  181. const circleGeometry = new THREE.CircleGeometry( 25, 40, 0, Math.PI * 2 );
  182. uvs = circleGeometry.attributes.uv.array;
  183. // Set a specific texture mapping.
  184. for ( let i = 0; i < uvs.length; i ++ ) {
  185. uvs[ i ] = ( uvs[ i ] - 0.25 ) * 2;
  186. }
  187. const circle = new THREE.Mesh( circleGeometry, circleMaterial );
  188. circle.position.x = 24;
  189. circle.position.y = - 5;
  190. circle.position.z = 0;
  191. scene.add( circle );
  192. window.addEventListener( 'resize', onWindowResize );
  193. container.addEventListener( 'mousemove', onMouseMove );
  194. //
  195. const gui = new GUI();
  196. gui.title( 'Circle Texture Settings' );
  197. gui.add( params, 'wrapS', WRAPPING ).onChange( setwrapS );
  198. gui.add( params, 'wrapT', WRAPPING ).onChange( setwrapT );
  199. gui.add( params, 'offsetX', 0, 5 );
  200. gui.add( params, 'offsetY', 0, 5 );
  201. gui.add( params, 'repeatX', 0, 5 );
  202. gui.add( params, 'repeatY', 0, 5 );
  203. gui.add( params, 'rotation', 0, 2 * Math.PI );
  204. gui.open();
  205. }
  206. function onWindowResize() {
  207. camera.aspect = window.innerWidth / window.innerHeight;
  208. camera.updateProjectionMatrix();
  209. renderer.setSize( window.innerWidth, window.innerHeight );
  210. }
  211. function onMouseMove( evt ) {
  212. evt.preventDefault();
  213. const array = getMousePosition( container, evt.clientX, evt.clientY );
  214. onClickPosition.fromArray( array );
  215. const intersects = getIntersects( onClickPosition, scene.children );
  216. if ( intersects.length > 0 && intersects[ 0 ].uv ) {
  217. const uv = intersects[ 0 ].uv;
  218. intersects[ 0 ].object.material.map.transformUv( uv );
  219. canvas.setCrossPosition( uv.x, uv.y );
  220. }
  221. }
  222. function getMousePosition( dom, x, y ) {
  223. const rect = dom.getBoundingClientRect();
  224. return [ ( x - rect.left ) / rect.width, ( y - rect.top ) / rect.height ];
  225. }
  226. function getIntersects( point, objects ) {
  227. mouse.set( ( point.x * 2 ) - 1, - ( point.y * 2 ) + 1 );
  228. raycaster.setFromCamera( mouse, camera );
  229. return raycaster.intersectObjects( objects, false );
  230. }
  231. function animate() {
  232. // update texture parameters
  233. circleTexture.offset.x = params.offsetX;
  234. circleTexture.offset.y = params.offsetY;
  235. circleTexture.repeat.x = params.repeatX;
  236. circleTexture.repeat.y = params.repeatY;
  237. circleTexture.rotation = params.rotation;
  238. //
  239. renderer.render( scene, camera );
  240. }
  241. function setwrapS( value ) {
  242. circleTexture.wrapS = value;
  243. circleTexture.needsUpdate = true;
  244. }
  245. function setwrapT( value ) {
  246. circleTexture.wrapT = value;
  247. circleTexture.needsUpdate = true;
  248. }
  249. </script>
  250. </body>
  251. </html>
粤ICP备19079148号