webgl_raycaster_texture.html 9.3 KB

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