webgl_raycast_texture.html 9.9 KB

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