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