webgl_raycast_texture.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. <!-- Import maps polyfill -->
  61. <!-- Remove this when import maps will be widely supported -->
  62. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  63. <script type="importmap">
  64. {
  65. "imports": {
  66. "three": "../build/three.module.js"
  67. }
  68. }
  69. </script>
  70. <script type="module">
  71. import * as THREE from 'three';
  72. function CanvasTexture( parentTexture ) {
  73. this._canvas = document.createElement( 'canvas' );
  74. this._canvas.width = this._canvas.height = 1024;
  75. this._context2D = this._canvas.getContext( '2d' );
  76. if ( parentTexture ) {
  77. this._parentTexture.push( parentTexture );
  78. parentTexture.image = this._canvas;
  79. }
  80. const that = this;
  81. this._background = document.createElement( 'img' );
  82. this._background.addEventListener( 'load', function () {
  83. that._canvas.width = that._background.naturalWidth;
  84. that._canvas.height = that._background.naturalHeight;
  85. that._crossRadius = Math.ceil( Math.min( that._canvas.width, that._canvas.height / 30 ) );
  86. that._crossMax = Math.ceil( 0.70710678 * that._crossRadius );
  87. that._crossMin = Math.ceil( that._crossMax / 10 );
  88. that._crossThickness = Math.ceil( that._crossMax / 10 );
  89. that._draw();
  90. } );
  91. this._background.crossOrigin = '';
  92. this._background.src = 'textures/uv_grid_opengl.jpg';
  93. this._draw();
  94. }
  95. CanvasTexture.prototype = {
  96. constructor: CanvasTexture,
  97. _canvas: null,
  98. _context2D: null,
  99. _xCross: 0,
  100. _yCross: 0,
  101. _crossRadius: 57,
  102. _crossMax: 40,
  103. _crossMin: 4,
  104. _crossThickness: 4,
  105. _parentTexture: [],
  106. addParent: function ( parentTexture ) {
  107. if ( this._parentTexture.indexOf( parentTexture ) === - 1 ) {
  108. this._parentTexture.push( parentTexture );
  109. parentTexture.image = this._canvas;
  110. }
  111. },
  112. setCrossPosition: function ( x, y ) {
  113. this._xCross = x * this._canvas.width;
  114. this._yCross = y * this._canvas.height;
  115. this._draw();
  116. },
  117. _draw: function () {
  118. if ( ! this._context2D ) return;
  119. this._context2D.clearRect( 0, 0, this._canvas.width, this._canvas.height );
  120. // Background.
  121. this._context2D.drawImage( this._background, 0, 0 );
  122. // Yellow cross.
  123. this._context2D.lineWidth = this._crossThickness * 3;
  124. this._context2D.strokeStyle = '#FFFF00';
  125. this._context2D.beginPath();
  126. this._context2D.moveTo( this._xCross - this._crossMax - 2, this._yCross - this._crossMax - 2 );
  127. this._context2D.lineTo( this._xCross - this._crossMin, this._yCross - this._crossMin );
  128. this._context2D.moveTo( this._xCross + this._crossMin, this._yCross + this._crossMin );
  129. this._context2D.lineTo( this._xCross + this._crossMax + 2, this._yCross + this._crossMax + 2 );
  130. this._context2D.moveTo( this._xCross - this._crossMax - 2, this._yCross + this._crossMax + 2 );
  131. this._context2D.lineTo( this._xCross - this._crossMin, this._yCross + this._crossMin );
  132. this._context2D.moveTo( this._xCross + this._crossMin, this._yCross - this._crossMin );
  133. this._context2D.lineTo( this._xCross + this._crossMax + 2, this._yCross - this._crossMax - 2 );
  134. this._context2D.stroke();
  135. for ( let i = 0; i < this._parentTexture.length; i ++ ) {
  136. this._parentTexture[ i ].needsUpdate = true;
  137. }
  138. }
  139. };
  140. const width = window.innerWidth;
  141. const height = window.innerHeight;
  142. let canvas;
  143. let planeTexture, cubeTexture, circleTexture;
  144. let container;
  145. let camera, scene, renderer;
  146. const raycaster = new THREE.Raycaster();
  147. const mouse = new THREE.Vector2();
  148. const onClickPosition = new THREE.Vector2();
  149. init();
  150. render();
  151. function init() {
  152. container = document.getElementById( 'container' );
  153. scene = new THREE.Scene();
  154. scene.background = new THREE.Color( 0xeeeeee );
  155. camera = new THREE.PerspectiveCamera( 45, width / height, 1, 1000 );
  156. camera.position.x = - 30;
  157. camera.position.y = 40;
  158. camera.position.z = 50;
  159. camera.lookAt( scene.position );
  160. renderer = new THREE.WebGLRenderer();
  161. renderer.setPixelRatio( window.devicePixelRatio );
  162. renderer.setSize( width, height );
  163. container.appendChild( renderer.domElement );
  164. // A cube, in the middle.
  165. cubeTexture = new THREE.Texture( undefined, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping );
  166. canvas = new CanvasTexture( cubeTexture );
  167. const cubeMaterial = new THREE.MeshBasicMaterial( { map: cubeTexture } );
  168. const cubeGeometry = new THREE.BoxGeometry( 20, 20, 20 );
  169. let uvs = cubeGeometry.attributes.uv.array;
  170. // Set a specific texture mapping.
  171. for ( let i = 0; i < uvs.length; i ++ ) {
  172. uvs[ i ] *= 2;
  173. }
  174. const cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
  175. cube.position.x = 4;
  176. cube.position.y = - 5;
  177. cube.position.z = 0;
  178. scene.add( cube );
  179. // A plane on the left
  180. planeTexture = new THREE.Texture( undefined, THREE.UVMapping, THREE.MirroredRepeatWrapping, THREE.MirroredRepeatWrapping );
  181. canvas.addParent( planeTexture );
  182. const planeMaterial = new THREE.MeshBasicMaterial( { map: planeTexture } );
  183. const planeGeometry = new THREE.PlaneGeometry( 25, 25, 1, 1 );
  184. uvs = planeGeometry.attributes.uv.array;
  185. // Set a specific texture mapping.
  186. for ( let i = 0; i < uvs.length; i ++ ) {
  187. uvs[ i ] *= 2;
  188. }
  189. const plane = new THREE.Mesh( planeGeometry, planeMaterial );
  190. plane.position.x = - 16;
  191. plane.position.y = - 5;
  192. plane.position.z = 0;
  193. scene.add( plane );
  194. // A circle on the right.
  195. circleTexture = new THREE.Texture( undefined, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping );
  196. canvas.addParent( circleTexture );
  197. const circleMaterial = new THREE.MeshBasicMaterial( { map: circleTexture } );
  198. const circleGeometry = new THREE.CircleGeometry( 25, 40, 0, Math.PI * 2 );
  199. uvs = circleGeometry.attributes.uv.array;
  200. // Set a specific texture mapping.
  201. for ( let i = 0; i < uvs.length; i ++ ) {
  202. uvs[ i ] = ( uvs[ i ] - 0.25 ) * 2;
  203. }
  204. const circle = new THREE.Mesh( circleGeometry, circleMaterial );
  205. circle.position.x = 24;
  206. circle.position.y = - 5;
  207. circle.position.z = 0;
  208. scene.add( circle );
  209. window.addEventListener( 'resize', onWindowResize );
  210. container.addEventListener( 'mousemove', onMouseMove );
  211. document.getElementById( 'setwrapS' ).addEventListener( 'change', setwrapS );
  212. document.getElementById( 'setwrapT' ).addEventListener( 'change', setwrapT );
  213. document.getElementById( 'setOffsetU' ).addEventListener( 'change', setOffsetU );
  214. document.getElementById( 'setOffsetV' ).addEventListener( 'change', setOffsetV );
  215. document.getElementById( 'setRepeatU' ).addEventListener( 'change', setRepeatU );
  216. document.getElementById( 'setRepeatV' ).addEventListener( 'change', setRepeatV );
  217. document.getElementById( 'setRotation' ).addEventListener( 'change', setRotation );
  218. }
  219. function onWindowResize() {
  220. camera.aspect = window.innerWidth / window.innerHeight;
  221. camera.updateProjectionMatrix();
  222. renderer.setSize( window.innerWidth, window.innerHeight );
  223. }
  224. function onMouseMove( evt ) {
  225. evt.preventDefault();
  226. const array = getMousePosition( container, evt.clientX, evt.clientY );
  227. onClickPosition.fromArray( array );
  228. const intersects = getIntersects( onClickPosition, scene.children );
  229. if ( intersects.length > 0 && intersects[ 0 ].uv ) {
  230. const uv = intersects[ 0 ].uv;
  231. intersects[ 0 ].object.material.map.transformUv( uv );
  232. canvas.setCrossPosition( uv.x, uv.y );
  233. }
  234. }
  235. function getMousePosition( dom, x, y ) {
  236. const rect = dom.getBoundingClientRect();
  237. return [ ( x - rect.left ) / rect.width, ( y - rect.top ) / rect.height ];
  238. }
  239. function getIntersects( point, objects ) {
  240. mouse.set( ( point.x * 2 ) - 1, - ( point.y * 2 ) + 1 );
  241. raycaster.setFromCamera( mouse, camera );
  242. return raycaster.intersectObjects( objects, false );
  243. }
  244. function render() {
  245. requestAnimationFrame( render );
  246. renderer.render( scene, camera );
  247. }
  248. function setwrapS( event ) {
  249. circleTexture.wrapS = parseFloat( event.target.value );
  250. circleTexture.needsUpdate = true;
  251. }
  252. function setwrapT( event ) {
  253. circleTexture.wrapT = parseFloat( event.target.value );
  254. circleTexture.needsUpdate = true;
  255. }
  256. function setOffsetU( event ) {
  257. circleTexture.offset.x = parseFloat( event.target.value );
  258. }
  259. function setOffsetV( event ) {
  260. circleTexture.offset.y = parseFloat( event.target.value );
  261. }
  262. function setRepeatU( event ) {
  263. circleTexture.repeat.x = parseFloat( event.target.value );
  264. }
  265. function setRepeatV( event ) {
  266. circleTexture.repeat.y = parseFloat( event.target.value );
  267. }
  268. function setRotation( event ) {
  269. circleTexture.rotation = parseFloat( event.target.value );
  270. }
  271. </script>
  272. </body>
  273. </html>