webgl_raycast_texture.html 10 KB

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