TiltLoader.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. ( function () {
  2. class TiltLoader extends THREE.Loader {
  3. load( url, onLoad, onProgress, onError ) {
  4. const scope = this;
  5. const loader = new THREE.FileLoader( this.manager );
  6. loader.setPath( this.path );
  7. loader.setResponseType( 'arraybuffer' );
  8. loader.setWithCredentials( this.withCredentials );
  9. loader.load( url, function ( buffer ) {
  10. try {
  11. onLoad( scope.parse( buffer ) );
  12. } catch ( e ) {
  13. if ( onError ) {
  14. onError( e );
  15. } else {
  16. console.error( e );
  17. }
  18. scope.manager.itemError( url );
  19. }
  20. }, onProgress, onError );
  21. }
  22. parse( buffer ) {
  23. const group = new THREE.Group(); // https://docs.google.com/document/d/11ZsHozYn9FnWG7y3s3WAyKIACfbfwb4PbaS8cZ_xjvo/edit#
  24. const zip = fflate.unzipSync( new Uint8Array( buffer.slice( 16 ) ) );
  25. /*
  26. const thumbnail = zip[ 'thumbnail.png' ].buffer;
  27. const img = document.createElement( 'img' );
  28. img.src = URL.createObjectURL( new Blob( [ thumbnail ] ) );
  29. document.body.appendChild( img );
  30. */
  31. const metadata = JSON.parse( fflate.strFromU8( zip[ 'metadata.json' ] ) );
  32. /*
  33. const blob = new Blob( [ zip[ 'data.sketch' ].buffer ], { type: 'application/octet-stream' } );
  34. window.open( URL.createObjectURL( blob ) );
  35. */
  36. const data = new DataView( zip[ 'data.sketch' ].buffer );
  37. const num_strokes = data.getInt32( 16, true );
  38. const brushes = {};
  39. let offset = 20;
  40. for ( let i = 0; i < num_strokes; i ++ ) {
  41. const brush_index = data.getInt32( offset, true );
  42. const brush_color = [ data.getFloat32( offset + 4, true ), data.getFloat32( offset + 8, true ), data.getFloat32( offset + 12, true ), data.getFloat32( offset + 16, true ) ];
  43. const brush_size = data.getFloat32( offset + 20, true );
  44. const stroke_mask = data.getUint32( offset + 24, true );
  45. const controlpoint_mask = data.getUint32( offset + 28, true );
  46. let offset_stroke_mask = 0;
  47. let offset_controlpoint_mask = 0;
  48. for ( let j = 0; j < 4; j ++ ) {
  49. // TOFIX: I don't understand these masks yet
  50. const byte = 1 << j;
  51. if ( ( stroke_mask & byte ) > 0 ) offset_stroke_mask += 4;
  52. if ( ( controlpoint_mask & byte ) > 0 ) offset_controlpoint_mask += 4;
  53. } // console.log( { brush_index, brush_color, brush_size, stroke_mask, controlpoint_mask } );
  54. // console.log( offset_stroke_mask, offset_controlpoint_mask );
  55. offset = offset + 28 + offset_stroke_mask + 4; // TOFIX: This is wrong
  56. const num_control_points = data.getInt32( offset, true ); // console.log( { num_control_points } );
  57. const positions = new Float32Array( num_control_points * 3 );
  58. const quaternions = new Float32Array( num_control_points * 4 );
  59. offset = offset + 4;
  60. for ( let j = 0, k = 0; j < positions.length; j += 3, k += 4 ) {
  61. positions[ j + 0 ] = data.getFloat32( offset + 0, true );
  62. positions[ j + 1 ] = data.getFloat32( offset + 4, true );
  63. positions[ j + 2 ] = data.getFloat32( offset + 8, true );
  64. quaternions[ k + 0 ] = data.getFloat32( offset + 12, true );
  65. quaternions[ k + 1 ] = data.getFloat32( offset + 16, true );
  66. quaternions[ k + 2 ] = data.getFloat32( offset + 20, true );
  67. quaternions[ k + 3 ] = data.getFloat32( offset + 24, true );
  68. offset = offset + 28 + offset_controlpoint_mask; // TOFIX: This is wrong
  69. }
  70. if ( brush_index in brushes === false ) {
  71. brushes[ brush_index ] = [];
  72. }
  73. brushes[ brush_index ].push( [ positions, quaternions, brush_size, brush_color ] );
  74. }
  75. for ( const brush_index in brushes ) {
  76. const geometry = new StrokeGeometry( brushes[ brush_index ] );
  77. const material = getMaterial( metadata.BrushIndex[ brush_index ] );
  78. group.add( new THREE.Mesh( geometry, material ) );
  79. }
  80. return group;
  81. }
  82. }
  83. class StrokeGeometry extends THREE.BufferGeometry {
  84. constructor( strokes ) {
  85. super();
  86. const vertices = [];
  87. const colors = [];
  88. const uvs = [];
  89. const position = new THREE.Vector3();
  90. const prevPosition = new THREE.Vector3();
  91. const quaternion = new THREE.Quaternion();
  92. const prevQuaternion = new THREE.Quaternion();
  93. const vector1 = new THREE.Vector3();
  94. const vector2 = new THREE.Vector3();
  95. const vector3 = new THREE.Vector3();
  96. const vector4 = new THREE.Vector3(); // size = size / 2;
  97. for ( const k in strokes ) {
  98. const stroke = strokes[ k ];
  99. const positions = stroke[ 0 ];
  100. const quaternions = stroke[ 1 ];
  101. const size = stroke[ 2 ];
  102. const color = stroke[ 3 ];
  103. prevPosition.fromArray( positions, 0 );
  104. prevQuaternion.fromArray( quaternions, 0 );
  105. for ( let i = 3, j = 4, l = positions.length; i < l; i += 3, j += 4 ) {
  106. position.fromArray( positions, i );
  107. quaternion.fromArray( quaternions, j );
  108. vector1.set( - size, 0, 0 );
  109. vector1.applyQuaternion( quaternion );
  110. vector1.add( position );
  111. vector2.set( size, 0, 0 );
  112. vector2.applyQuaternion( quaternion );
  113. vector2.add( position );
  114. vector3.set( size, 0, 0 );
  115. vector3.applyQuaternion( prevQuaternion );
  116. vector3.add( prevPosition );
  117. vector4.set( - size, 0, 0 );
  118. vector4.applyQuaternion( prevQuaternion );
  119. vector4.add( prevPosition );
  120. vertices.push( vector1.x, vector1.y, - vector1.z );
  121. vertices.push( vector2.x, vector2.y, - vector2.z );
  122. vertices.push( vector4.x, vector4.y, - vector4.z );
  123. vertices.push( vector2.x, vector2.y, - vector2.z );
  124. vertices.push( vector3.x, vector3.y, - vector3.z );
  125. vertices.push( vector4.x, vector4.y, - vector4.z );
  126. prevPosition.copy( position );
  127. prevQuaternion.copy( quaternion );
  128. colors.push( ...color );
  129. colors.push( ...color );
  130. colors.push( ...color );
  131. colors.push( ...color );
  132. colors.push( ...color );
  133. colors.push( ...color );
  134. const p1 = i / l;
  135. const p2 = ( i - 3 ) / l;
  136. uvs.push( p1, 0 );
  137. uvs.push( p1, 1 );
  138. uvs.push( p2, 0 );
  139. uvs.push( p1, 1 );
  140. uvs.push( p2, 1 );
  141. uvs.push( p2, 0 );
  142. }
  143. }
  144. this.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
  145. this.setAttribute( 'color', new THREE.BufferAttribute( new Float32Array( colors ), 4 ) );
  146. this.setAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( uvs ), 2 ) );
  147. }
  148. }
  149. const BRUSH_LIST_ARRAY = {
  150. '89d104cd-d012-426b-b5b3-bbaee63ac43c': 'Bubbles',
  151. '700f3aa8-9a7c-2384-8b8a-ea028905dd8c': 'CelVinyl',
  152. '0f0ff7b2-a677-45eb-a7d6-0cd7206f4816': 'ChromaticWave',
  153. '1161af82-50cf-47db-9706-0c3576d43c43': 'CoarseBristles',
  154. '79168f10-6961-464a-8be1-57ed364c5600': 'CoarseBristlesSingleSided',
  155. '1caa6d7d-f015-3f54-3a4b-8b5354d39f81': 'Comet',
  156. 'c8313697-2563-47fc-832e-290f4c04b901': 'DiamondHull',
  157. '4391aaaa-df73-4396-9e33-31e4e4930b27': 'Disco',
  158. 'd1d991f2-e7a0-4cf1-b328-f57e915e6260': 'DotMarker',
  159. '6a1cf9f9-032c-45ec-9b1d-a6680bee30f7': 'Dots',
  160. '0d3889f3-3ede-470c-8af4-f44813306126': 'DoubleTaperedFlat',
  161. '0d3889f3-3ede-470c-8af4-de4813306126': 'DoubleTaperedMarker',
  162. 'd0262945-853c-4481-9cbd-88586bed93cb': 'DuctTape',
  163. '3ca16e2f-bdcd-4da2-8631-dcef342f40f1': 'DuctTapeSingleSided',
  164. 'f6e85de3-6dcc-4e7f-87fd-cee8c3d25d51': 'Electricity',
  165. '02ffb866-7fb2-4d15-b761-1012cefb1360': 'Embers',
  166. 'cb92b597-94ca-4255-b017-0e3f42f12f9e': 'Fire',
  167. '2d35bcf0-e4d8-452c-97b1-3311be063130': 'Flat',
  168. '55303bc4-c749-4a72-98d9-d23e68e76e18': 'FlatDeprecated',
  169. '280c0a7a-aad8-416c-a7d2-df63d129ca70': 'FlatSingleSided',
  170. 'cf019139-d41c-4eb0-a1d0-5cf54b0a42f3': 'Highlighter',
  171. '6a1cf9f9-032c-45ec-9b6e-a6680bee32e9': 'HyperGrid',
  172. 'dce872c2-7b49-4684-b59b-c45387949c5c': 'Hypercolor',
  173. 'e8ef32b1-baa8-460a-9c2c-9cf8506794f5': 'HypercolorSingleSided',
  174. '2f212815-f4d3-c1a4-681a-feeaf9c6dc37': 'Icing',
  175. 'f5c336cf-5108-4b40-ade9-c687504385ab': 'Ink',
  176. 'c0012095-3ffd-4040-8ee1-fc180d346eaa': 'InkSingleSided',
  177. '4a76a27a-44d8-4bfe-9a8c-713749a499b0': 'Leaves',
  178. 'ea19de07-d0c0-4484-9198-18489a3c1487': 'LeavesSingleSided',
  179. '2241cd32-8ba2-48a5-9ee7-2caef7e9ed62': 'Light',
  180. '4391aaaa-df81-4396-9e33-31e4e4930b27': 'LightWire',
  181. 'd381e0f5-3def-4a0d-8853-31e9200bcbda': 'Lofted',
  182. '429ed64a-4e97-4466-84d3-145a861ef684': 'Marker',
  183. '79348357-432d-4746-8e29-0e25c112e3aa': 'MatteHull',
  184. 'b2ffef01-eaaa-4ab5-aa64-95a2c4f5dbc6': 'NeonPulse',
  185. 'f72ec0e7-a844-4e38-82e3-140c44772699': 'OilPaint',
  186. 'c515dad7-4393-4681-81ad-162ef052241b': 'OilPaintSingleSided',
  187. 'f1114e2e-eb8d-4fde-915a-6e653b54e9f5': 'Paper',
  188. '759f1ebd-20cd-4720-8d41-234e0da63716': 'PaperSingleSided',
  189. 'e0abbc80-0f80-e854-4970-8924a0863dcc': 'Petal',
  190. 'c33714d1-b2f9-412e-bd50-1884c9d46336': 'Plasma',
  191. 'ad1ad437-76e2-450d-a23a-e17f8310b960': 'Rainbow',
  192. 'faaa4d44-fcfb-4177-96be-753ac0421ba3': 'ShinyHull',
  193. '70d79cca-b159-4f35-990c-f02193947fe8': 'Smoke',
  194. 'd902ed8b-d0d1-476c-a8de-878a79e3a34c': 'Snow',
  195. 'accb32f5-4509-454f-93f8-1df3fd31df1b': 'SoftHighlighter',
  196. 'cf7f0059-7aeb-53a4-2b67-c83d863a9ffa': 'Spikes',
  197. '8dc4a70c-d558-4efd-a5ed-d4e860f40dc3': 'Splatter',
  198. '7a1c8107-50c5-4b70-9a39-421576d6617e': 'SplatterSingleSided',
  199. '0eb4db27-3f82-408d-b5a1-19ebd7d5b711': 'Stars',
  200. '44bb800a-fbc3-4592-8426-94ecb05ddec3': 'Streamers',
  201. '0077f88c-d93a-42f3-b59b-b31c50cdb414': 'Taffy',
  202. 'b468c1fb-f254-41ed-8ec9-57030bc5660c': 'TaperedFlat',
  203. 'c8ccb53d-ae13-45ef-8afb-b730d81394eb': 'TaperedFlatSingleSided',
  204. 'd90c6ad8-af0f-4b54-b422-e0f92abe1b3c': 'TaperedMarker',
  205. '1a26b8c0-8a07-4f8a-9fac-d2ef36e0cad0': 'TaperedMarker_Flat',
  206. '75b32cf0-fdd6-4d89-a64b-e2a00b247b0f': 'ThickPaint',
  207. 'fdf0326a-c0d1-4fed-b101-9db0ff6d071f': 'ThickPaintSingleSided',
  208. '4391385a-df73-4396-9e33-31e4e4930b27': 'Toon',
  209. 'a8fea537-da7c-4d4b-817f-24f074725d6d': 'UnlitHull',
  210. 'd229d335-c334-495a-a801-660ac8a87360': 'VelvetInk',
  211. '10201aa3-ebc2-42d8-84b7-2e63f6eeb8ab': 'Waveform',
  212. 'b67c0e81-ce6d-40a8-aeb0-ef036b081aa3': 'WetPaint',
  213. 'dea67637-cd1a-27e4-c9b1-52f4bbcb84e5': 'WetPaintSingleSided',
  214. '5347acf0-a8e2-47b6-8346-30c70719d763': 'WigglyGraphite',
  215. 'e814fef1-97fd-7194-4a2f-50c2bb918be2': 'WigglyGraphiteSingleSided',
  216. '4391385a-cf83-4396-9e33-31e4e4930b27': 'Wire'
  217. };
  218. const common = {
  219. 'colors': {
  220. 'BloomColor': `
  221. vec3 BloomColor(vec3 color, float gain) {
  222. // Guarantee that there's at least a little bit of all 3 channels.
  223. // This makes fully-saturated strokes (which only have 2 non-zero
  224. // color channels) eventually clip to white rather than to a secondary.
  225. float cmin = length(color.rgb) * .05;
  226. color.rgb = max(color.rgb, vec3(cmin, cmin, cmin));
  227. // If we try to remove this pow() from .a, it brightens up
  228. // pressure-sensitive strokes; looks better as-is.
  229. color = pow(color, vec3(2.2));
  230. color.rgb *= 2. * exp(gain * 10.);
  231. return color;
  232. }
  233. `,
  234. 'LinearToSrgb': `
  235. vec3 LinearToSrgb(vec3 color) {
  236. // Approximation http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
  237. vec3 linearColor = color.rgb;
  238. vec3 S1 = sqrt(linearColor);
  239. vec3 S2 = sqrt(S1);
  240. vec3 S3 = sqrt(S2);
  241. color.rgb = 0.662002687 * S1 + 0.684122060 * S2 - 0.323583601 * S3 - 0.0225411470 * linearColor;
  242. return color;
  243. }
  244. `,
  245. 'hsv': `
  246. // uniform sampler2D lookupTex;
  247. vec4 lookup(vec4 textureColor) {
  248. return textureColor;
  249. }
  250. vec3 lookup(vec3 textureColor) {
  251. return textureColor;
  252. }
  253. vec3 hsv2rgb( vec3 hsv ) {
  254. vec3 rgb = clamp( abs(mod(hsv.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
  255. return hsv.z * mix( vec3(1.0), rgb, hsv.y);
  256. }
  257. vec3 rgb2hsv( vec3 rgb ) {
  258. vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
  259. vec4 p = mix(vec4(rgb.bg, K.wz), vec4(rgb.gb, K.xy), step(rgb.b, rgb.g));
  260. vec4 q = mix(vec4(p.xyw, rgb.r), vec4(rgb.r, p.yzx), step(p.x, rgb.r));
  261. float d = q.x - min(q.w, q.y);
  262. float e = 1.0e-10;
  263. return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
  264. }
  265. `,
  266. 'SrgbToLinear': `
  267. vec3 SrgbToLinear(vec3 color) {
  268. // Approximation http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
  269. vec3 sRGB = color.rgb;
  270. color.rgb = sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);
  271. return color;
  272. }
  273. `
  274. }
  275. };
  276. let shaders = null;
  277. function getShaders() {
  278. if ( shaders === null ) {
  279. const loader = new THREE.TextureLoader().setPath( './textures/tiltbrush/' );
  280. shaders = {
  281. 'Light': {
  282. uniforms: {
  283. mainTex: {
  284. value: loader.load( 'Light.webp' )
  285. },
  286. alphaTest: {
  287. value: 0.067
  288. },
  289. emission_gain: {
  290. value: 0.45
  291. },
  292. alpha: {
  293. value: 1
  294. }
  295. },
  296. vertexShader: `
  297. precision highp float;
  298. precision highp int;
  299. attribute vec2 uv;
  300. attribute vec4 color;
  301. attribute vec3 position;
  302. uniform mat4 modelMatrix;
  303. uniform mat4 modelViewMatrix;
  304. uniform mat4 projectionMatrix;
  305. uniform mat4 viewMatrix;
  306. uniform mat3 normalMatrix;
  307. uniform vec3 cameraPosition;
  308. varying vec2 vUv;
  309. varying vec3 vColor;
  310. ${common.colors.LinearToSrgb}
  311. ${common.colors.hsv}
  312. void main() {
  313. vUv = uv;
  314. vColor = lookup(color.rgb);
  315. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  316. gl_Position = projectionMatrix * mvPosition;
  317. }
  318. `,
  319. fragmentShader: `
  320. precision highp float;
  321. precision highp int;
  322. uniform float emission_gain;
  323. uniform sampler2D mainTex;
  324. uniform float alphaTest;
  325. varying vec2 vUv;
  326. varying vec3 vColor;
  327. ${common.colors.BloomColor}
  328. ${common.colors.SrgbToLinear}
  329. void main(){
  330. vec4 col = texture2D(mainTex, vUv);
  331. vec3 color = vColor;
  332. color = BloomColor(color, emission_gain);
  333. color = color * col.rgb;
  334. color = color * col.a;
  335. color = SrgbToLinear(color);
  336. gl_FragColor = vec4(color, 1.0);
  337. }
  338. `,
  339. side: 2,
  340. transparent: true,
  341. depthFunc: 2,
  342. depthWrite: true,
  343. depthTest: false,
  344. blending: 5,
  345. blendDst: 201,
  346. blendDstAlpha: 201,
  347. blendEquation: 100,
  348. blendEquationAlpha: 100,
  349. blendSrc: 201,
  350. blendSrcAlpha: 201
  351. }
  352. };
  353. }
  354. return shaders;
  355. }
  356. function getMaterial( GUID ) {
  357. const name = BRUSH_LIST_ARRAY[ GUID ];
  358. switch ( name ) {
  359. case 'Light':
  360. return new THREE.RawShaderMaterial( getShaders().Light );
  361. default:
  362. return new THREE.MeshBasicMaterial( {
  363. vertexColors: true,
  364. side: THREE.DoubleSide
  365. } );
  366. }
  367. }
  368. THREE.TiltLoader = TiltLoader;
  369. } )();