webgpu_materials_texture_html.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - materials - html 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. <meta property="og:title" content="three.js webgpu - materials - html texture">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_materials_texture_html.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_materials_texture_html.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background-color: #ffffff;
  15. }
  16. #draw_element {
  17. width: 600px;
  18. background-color: #aaaaaa;
  19. color: #000000;
  20. font-family: sans-serif;
  21. font-size: 30px;
  22. line-height: 1.5;
  23. text-align: center;
  24. padding: 30px;
  25. /* border: 10px solid #cccccc; */
  26. }
  27. #draw_element img {
  28. animation: swing 1s ease-in-out infinite alternate;
  29. }
  30. #draw_element input[type="text"] {
  31. font-size: 24px;
  32. padding: 8px 12px;
  33. border: 2px solid #888;
  34. border-radius: 6px;
  35. width: 80%;
  36. margin-top: 10px;
  37. }
  38. #draw_element button {
  39. font-size: 24px;
  40. padding: 8px 20px;
  41. margin-top: 10px;
  42. border: none;
  43. border-radius: 6px;
  44. background-color: #4CAF50;
  45. color: white;
  46. cursor: pointer;
  47. }
  48. #draw_element button:hover {
  49. background-color: #2196F3;
  50. }
  51. @keyframes swing {
  52. from { transform: rotate(-15deg); }
  53. to { transform: rotate(15deg); }
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <div id="info">
  59. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgpu - HTMLTexture
  60. </div>
  61. <script type="importmap">
  62. {
  63. "imports": {
  64. "three": "../build/three.webgpu.js",
  65. "three/addons/": "./jsm/",
  66. "three/tsl": "../build/three.tsl.js",
  67. "three-html-render/polyfill": "https://cdn.jsdelivr.net/npm/three-html-render/dist/polyfill.mjs"
  68. }
  69. }
  70. </script>
  71. <script type="module">
  72. import * as THREE from 'three';
  73. import { installHtmlInCanvasPolyfill } from 'three-html-render/polyfill';
  74. import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
  75. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  76. import { InteractionManager } from 'three/addons/interaction/InteractionManager.js';
  77. if ( ! ( 'requestPaint' in HTMLCanvasElement.prototype ) ) {
  78. installHtmlInCanvasPolyfill();
  79. info.innerHTML += '<br><a href="https://github.com/WICG/html-in-canvas" target="_blank">HTML-in-Canvas API</a> not available. Using <a href="https://github.com/repalash/three-html-render" target="_blank">polyfill</a>.';
  80. }
  81. let camera, scene, renderer, mesh, interactions;
  82. init();
  83. async function init() {
  84. renderer = new THREE.WebGPURenderer( { antialias: true } );
  85. await renderer.init();
  86. renderer.toneMapping = THREE.NeutralToneMapping;
  87. renderer.setPixelRatio( window.devicePixelRatio );
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. renderer.setAnimationLoop( animate );
  90. document.body.appendChild( renderer.domElement );
  91. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
  92. camera.position.z = 500;
  93. scene = new THREE.Scene();
  94. scene.background = new THREE.Color( 0xaaaaaa );
  95. scene.environment = new THREE.PMREMGenerator( renderer ).fromScene( new RoomEnvironment(), 0.02 ).texture;
  96. // HTML element
  97. const element = document.createElement( 'div' );
  98. element.id = 'draw_element';
  99. element.innerHTML = `
  100. Hello world!<br>I'm multi-line, <b>formatted</b>,
  101. rotated text with emoji (&#128512;), RTL text
  102. <span dir=rtl>من فارسی صحبت میکنم</span>,
  103. vertical text,
  104. <p style="writing-mode: vertical-rl;">
  105. 这是垂直文本
  106. </p>
  107. an inline image (<img width="150" src="textures/758px-Canestra_di_frutta_(Caravaggio).jpg">), and
  108. <svg width="50" height="50">
  109. <circle cx="25" cy="25" r="20" fill="green" />
  110. <text x="25" y="30" font-size="15" text-anchor="middle" fill="#fff">
  111. SVG
  112. </text>
  113. </svg>!
  114. <br>
  115. <input type="text" placeholder="Type here...">
  116. <button>Click me</button>
  117. `;
  118. const geometry = new RoundedBoxGeometry( 200, 200, 200, 10, 10 );
  119. const material = new THREE.MeshStandardMaterial( { roughness: 0, metalness: 0.5 } );
  120. material.map = new THREE.HTMLTexture( element );
  121. mesh = new THREE.Mesh( geometry, material );
  122. scene.add( mesh );
  123. // Interaction
  124. interactions = new InteractionManager();
  125. interactions.connect( renderer, camera );
  126. interactions.add( mesh );
  127. // Button click handler
  128. element.querySelector( 'button' ).addEventListener( 'click', function () {
  129. this.textContent = 'Clicked!';
  130. } );
  131. window.addEventListener( 'resize', onWindowResize );
  132. }
  133. function onWindowResize() {
  134. camera.aspect = window.innerWidth / window.innerHeight;
  135. camera.updateProjectionMatrix();
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  137. }
  138. function animate( time ) {
  139. mesh.rotation.x = Math.sin( time * 0.0005 ) * 0.5;
  140. mesh.rotation.y = Math.cos( time * 0.0008 ) * 0.5;
  141. interactions.update();
  142. renderer.render( scene, camera );
  143. }
  144. </script>
  145. </body>
  146. </html>
粤ICP备19079148号