webgl_raycast_texture.html 10 KB

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