webgl_raycaster_texture.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../build/three.module.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  37. const WRAPPING = {
  38. 'RepeatWrapping': THREE.RepeatWrapping,
  39. 'ClampToEdgeWrapping': THREE.ClampToEdgeWrapping,
  40. 'MirroredRepeatWrapping': THREE.MirroredRepeatWrapping
  41. };
  42. const params = {
  43. wrapS: THREE.RepeatWrapping,
  44. wrapT: THREE.RepeatWrapping,
  45. offsetX: 0,
  46. offsetY: 0,
  47. repeatX: 1,
  48. repeatY: 1,
  49. rotation: 0,
  50. };
  51. function CanvasTexture( parentTexture ) {
  52. this._canvas = document.createElement( 'canvas' );
  53. this._canvas.width = this._canvas.height = 1024;
  54. this._context2D = this._canvas.getContext( '2d' );
  55. if ( parentTexture ) {
  56. this._parentTexture.push( parentTexture );
  57. parentTexture.image = this._canvas;
  58. }
  59. const that = this;
  60. this._background = document.createElement( 'img' );
  61. this._background.addEventListener( 'load', function () {
  62. that._canvas.width = that._background.naturalWidth;
  63. that._canvas.height = that._background.naturalHeight;
  64. that._crossRadius = Math.ceil( Math.min( that._canvas.width, that._canvas.height / 30 ) );
  65. that._crossMax = Math.ceil( 0.70710678 * that._crossRadius );
  66. that._crossMin = Math.ceil( that._crossMax / 10 );
  67. that._crossThickness = Math.ceil( that._crossMax / 10 );
  68. that._draw();
  69. } );
  70. this._background.crossOrigin = '';
  71. this._background.src = 'textures/uv_grid_opengl.jpg';
  72. this._draw();
  73. }
  74. CanvasTexture.prototype = {
  75. constructor: CanvasTexture,
  76. _canvas: null,
  77. _context2D: null,
  78. _xCross: 0,
  79. _yCross: 0,
  80. _crossRadius: 57,
  81. _crossMax: 40,
  82. _crossMin: 4,
  83. _crossThickness: 4,
  84. _parentTexture: [],
  85. addParent: function ( parentTexture ) {
  86. if ( this._parentTexture.indexOf( parentTexture ) === - 1 ) {
  87. this._parentTexture.push( parentTexture );
  88. parentTexture.image = this._canvas;
  89. }
  90. },
  91. setCrossPosition: function ( x, y ) {
  92. this._xCross = x * this._canvas.width;
  93. this._yCross = y * this._canvas.height;
  94. this._draw();
  95. },
  96. _draw: function () {
  97. if ( ! this._context2D ) return;
  98. this._context2D.clearRect( 0, 0, this._canvas.width, this._canvas.height );
  99. // Background.
  100. this._context2D.drawImage( this._background, 0, 0 );
  101. // Yellow cross.
  102. this._context2D.lineWidth = this._crossThickness * 3;
  103. this._context2D.strokeStyle = '#FFFF00';
  104. this._context2D.beginPath();
  105. this._context2D.moveTo( this._xCross - this._crossMax - 2, this._yCross - this._crossMax - 2 );
  106. this._context2D.lineTo( this._xCross - this._crossMin, this._yCross - this._crossMin );
  107. this._context2D.moveTo( this._xCross + this._crossMin, this._yCross + this._crossMin );
  108. this._context2D.lineTo( this._xCross + this._crossMax + 2, this._yCross + this._crossMax + 2 );
  109. this._context2D.moveTo( this._xCross - this._crossMax - 2, this._yCross + this._crossMax + 2 );
  110. this._context2D.lineTo( this._xCross - this._crossMin, this._yCross + this._crossMin );
  111. this._context2D.moveTo( this._xCross + this._crossMin, this._yCross - this._crossMin );
  112. this._context2D.lineTo( this._xCross + this._crossMax + 2, this._yCross - this._crossMax - 2 );
  113. this._context2D.stroke();
  114. for ( let i = 0; i < this._parentTexture.length; i ++ ) {
  115. this._parentTexture[ i ].needsUpdate = true;
  116. }
  117. }
  118. };
  119. const width = window.innerWidth;
  120. const height = window.innerHeight;
  121. let canvas;
  122. let planeTexture, cubeTexture, circleTexture;
  123. let container;
  124. let camera, scene, renderer;
  125. const raycaster = new THREE.Raycaster();
  126. const mouse = new THREE.Vector2();
  127. const onClickPosition = new THREE.Vector2();
  128. init();
  129. render();
  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. container.appendChild( renderer.domElement );
  143. // A cube, in the middle.
  144. cubeTexture = new THREE.Texture( undefined, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping );
  145. cubeTexture.colorSpace = THREE.SRGBColorSpace;
  146. canvas = new CanvasTexture( cubeTexture );
  147. const cubeMaterial = new THREE.MeshBasicMaterial( { map: cubeTexture } );
  148. const cubeGeometry = new THREE.BoxGeometry( 20, 20, 20 );
  149. let uvs = cubeGeometry.attributes.uv.array;
  150. // Set a specific texture mapping.
  151. for ( let i = 0; i < uvs.length; i ++ ) {
  152. uvs[ i ] *= 2;
  153. }
  154. const cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
  155. cube.position.x = 4;
  156. cube.position.y = - 5;
  157. cube.position.z = 0;
  158. scene.add( cube );
  159. // A plane on the left
  160. planeTexture = new THREE.Texture( undefined, THREE.UVMapping, THREE.MirroredRepeatWrapping, THREE.MirroredRepeatWrapping );
  161. planeTexture.colorSpace = THREE.SRGBColorSpace;
  162. canvas.addParent( planeTexture );
  163. const planeMaterial = new THREE.MeshBasicMaterial( { map: planeTexture } );
  164. const planeGeometry = new THREE.PlaneGeometry( 25, 25, 1, 1 );
  165. uvs = planeGeometry.attributes.uv.array;
  166. // Set a specific texture mapping.
  167. for ( let i = 0; i < uvs.length; i ++ ) {
  168. uvs[ i ] *= 2;
  169. }
  170. const plane = new THREE.Mesh( planeGeometry, planeMaterial );
  171. plane.position.x = - 16;
  172. plane.position.y = - 5;
  173. plane.position.z = 0;
  174. scene.add( plane );
  175. // A circle on the right.
  176. circleTexture = new THREE.Texture( undefined, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping );
  177. circleTexture.colorSpace = THREE.SRGBColorSpace;
  178. canvas.addParent( circleTexture );
  179. const circleMaterial = new THREE.MeshBasicMaterial( { map: circleTexture } );
  180. const circleGeometry = new THREE.CircleGeometry( 25, 40, 0, Math.PI * 2 );
  181. uvs = circleGeometry.attributes.uv.array;
  182. // Set a specific texture mapping.
  183. for ( let i = 0; i < uvs.length; i ++ ) {
  184. uvs[ i ] = ( uvs[ i ] - 0.25 ) * 2;
  185. }
  186. const circle = new THREE.Mesh( circleGeometry, circleMaterial );
  187. circle.position.x = 24;
  188. circle.position.y = - 5;
  189. circle.position.z = 0;
  190. scene.add( circle );
  191. window.addEventListener( 'resize', onWindowResize );
  192. container.addEventListener( 'mousemove', onMouseMove );
  193. //
  194. const gui = new GUI();
  195. gui.title( 'Circle Texture Settings' );
  196. gui.add( params, 'wrapS', WRAPPING ).onChange( setwrapS );
  197. gui.add( params, 'wrapT', WRAPPING ).onChange( setwrapT );
  198. gui.add( params, 'offsetX', 0, 5 );
  199. gui.add( params, 'offsetY', 0, 5 );
  200. gui.add( params, 'repeatX', 0, 5 );
  201. gui.add( params, 'repeatY', 0, 5 );
  202. gui.add( params, 'rotation', 0, 2 * Math.PI );
  203. gui.open();
  204. }
  205. function onWindowResize() {
  206. camera.aspect = window.innerWidth / window.innerHeight;
  207. camera.updateProjectionMatrix();
  208. renderer.setSize( window.innerWidth, window.innerHeight );
  209. }
  210. function onMouseMove( evt ) {
  211. evt.preventDefault();
  212. const array = getMousePosition( container, evt.clientX, evt.clientY );
  213. onClickPosition.fromArray( array );
  214. const intersects = getIntersects( onClickPosition, scene.children );
  215. if ( intersects.length > 0 && intersects[ 0 ].uv ) {
  216. const uv = intersects[ 0 ].uv;
  217. intersects[ 0 ].object.material.map.transformUv( uv );
  218. canvas.setCrossPosition( uv.x, uv.y );
  219. }
  220. }
  221. function getMousePosition( dom, x, y ) {
  222. const rect = dom.getBoundingClientRect();
  223. return [ ( x - rect.left ) / rect.width, ( y - rect.top ) / rect.height ];
  224. }
  225. function getIntersects( point, objects ) {
  226. mouse.set( ( point.x * 2 ) - 1, - ( point.y * 2 ) + 1 );
  227. raycaster.setFromCamera( mouse, camera );
  228. return raycaster.intersectObjects( objects, false );
  229. }
  230. function render() {
  231. requestAnimationFrame( render );
  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>