webgpu_materials_texture_html.html 5.0 KB

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