webgl_raycast_texture.html 10 KB

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