- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
给定一个带有图像二进制数据和扩展名(jpg、png 等)的 Javascript ArrayBuffer我想创建一个 ThreeJS 纹理(不执行任何 HTTP 请求或文件加载,因为我已经有了二进制信息)。
例如假设我有:
var binaryData = getBinaryData(); // this returns an ArrayBuffer with the image, for this example we assume is a PNG image.
var imageFormat = "png";
(我们还有其他纹理属性,如环绕模式、mipmap 等)
如何加载纹理?
我们将不胜感激。
最佳答案
如果将二进制数据数组转换为 base64 字符串。然后,您可以将它附加到 Image
对象,然后将 Image
对象与 Three.js Texture
一起使用。
// Convert the array of data into a base64 string
var stringData = String.fromCharCode.apply(null, new Uint16Array(jpgData));
var encodedData = window.btoa(stringData);
var dataURI = "data:image/jpeg;base64," + encodedData;
// Connect the image to the Texture
var texture = new THREE.Texture();
var image = new Image();
image.onload = function () {
texture.image = image;
texture.needsUpdate = true;
};
image.src = dataURI;
这是完整的代码(猫的图像是故意低分辨率的):
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var jpgData = new Uint8Array([255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,255,254,0,62,67,82,69,65,84,79,82,58,32,103,100,45,106,112,101,103,32,118,49,46,48,32,40,117,115,105,110,103,32,73,74,71,32,74,80,69,71,32,118,54,50,41,44,32,100,101,102,97,117,108,116,32,113,117,97,108,105,116,121,10,255,219,0,67,0,8,6,6,7,6,5,8,7,7,7,9,9,8,10,12,20,13,12,11,11,12,25,18,19,15,20,29,26,31,30,29,26,28,28,32,36,46,39,32,34,44,35,28,28,40,55,41,44,48,49,52,52,52,31,39,57,61,56,50,60,46,51,52,50,255,219,0,67,1,9,9,9,12,11,12,24,13,13,24,50,33,28,33,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,255,192,0,17,8,0,24,0,24,3,1,34,0,2,17,1,3,17,1,255,196,0,31,0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,255,196,0,181,16,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,125,1,2,3,0,4,17,5,18,33,49,65,6,19,81,97,7,34,113,20,50,129,145,161,8,35,66,177,193,21,82,209,240,36,51,98,114,130,9,10,22,23,24,25,26,37,38,39,40,41,42,52,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,131,132,133,134,135,136,137,138,146,147,148,149,150,151,152,153,154,162,163,164,165,166,167,168,169,170,178,179,180,181,182,183,184,185,186,194,195,196,197,198,199,200,201,202,210,211,212,213,214,215,216,217,218,225,226,227,228,229,230,231,232,233,234,241,242,243,244,245,246,247,248,249,250,255,196,0,31,1,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,255,196,0,181,17,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,119,0,1,2,3,17,4,5,33,49,6,18,65,81,7,97,113,19,34,50,129,8,20,66,145,161,177,193,9,35,51,82,240,21,98,114,209,10,22,36,52,225,37,241,23,24,25,26,38,39,40,41,42,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,130,131,132,133,134,135,136,137,138,146,147,148,149,150,151,152,153,154,162,163,164,165,166,167,168,169,170,178,179,180,181,182,183,184,185,186,194,195,196,197,198,199,200,201,202,210,211,212,213,214,215,216,217,218,226,227,228,229,230,231,232,233,234,242,243,244,245,246,247,248,249,250,255,218,0,12,3,1,0,2,17,3,17,0,63,0,225,109,238,89,177,194,175,3,160,56,198,63,90,232,23,193,58,181,198,129,246,255,0,48,91,73,52,196,37,147,225,119,192,49,243,111,45,158,73,29,187,142,213,204,197,117,246,11,104,101,154,88,124,180,145,72,81,247,136,207,183,30,181,239,190,36,188,211,99,68,185,242,148,164,113,121,145,180,127,119,102,51,159,166,43,139,15,77,89,201,149,177,226,44,230,81,37,188,214,234,251,91,107,28,228,238,29,206,79,255,0,90,138,154,242,120,230,19,72,176,198,190,103,207,251,179,157,217,25,200,205,21,196,147,111,64,122,144,216,248,87,251,98,55,143,85,185,158,210,220,169,116,40,60,204,55,24,227,191,83,233,94,129,117,168,19,225,187,125,29,165,23,119,73,103,37,164,183,112,40,10,160,0,17,182,158,229,120,227,184,162,138,214,150,42,106,45,89,21,73,123,73,53,35,137,26,4,176,69,228,137,51,110,16,236,221,130,216,3,140,154,40,162,176,246,175,153,232,67,220,255,217]);
// Convert the array of data into a base64 string
var stringData = String.fromCharCode.apply(null, new Uint16Array(jpgData));
var encodedData = window.btoa(stringData);
var dataURI = "data:image/jpeg;base64," + encodedData;
// Connect the image to the Texture
var texture = new THREE.Texture();
var image = new Image();
image.onload = function () {
texture.image = image;
texture.needsUpdate = true;
};
image.src = dataURI;
var geometry = new THREE.PlaneBufferGeometry(1, 1);
var material = new THREE.MeshBasicMaterial({map: texture});
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );
camera.position.z = 1;
var render = function () {
requestAnimationFrame( render );
renderer.render(scene, camera);
};
render();
<html>
<head>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js"></script>
</body>
</html>
关于javascript - 三个 JS 从原始 javascript ArrayBuffer 加载纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28482776/
我想要一个ArrayBuffer的二维数组 像这样: var myRowOfStrings = new ArrayBuffer[String] val myArrayOfRows = new Arra
假设我有一个 ArrayBuffer,它是一个 mp4 文件。我想创建一个新的 ArrayBuffer,其中包含一些额外的字节用作 header 信息,然后将 mp4 缓冲区存储在新的 的其余部分Ar
我正在尝试使用 .slice() 从 ArrayBuffer 创建 Blob,因为我需要复制 ArrayBuffer > 因为我知道它将来会被我无法控制/想要更改的代码更改(其 byteLength
我创建了一个用于下载pdf的应用程序。 pdf 基于 html 表格。该应用程序在所有浏览器中都工作正常,但是当我在 IE9 中运行时,我收到函数错误:“ArrayBuffer”未定义 Referen
我目前正在为 WebAuthN 编写概念验证 Web 应用程序(FIDO U2F, FIDO2) 整合。 返回字段cred.rawId的数据类型为ArrayBuffer。在 Chromes 的开发者控
这个问题过去已经得到了答案,但我肯定地说它仍然没有得到答案。 一般来说,几乎都有关于 ArrayBuffers 的文档,更不用说特定应用程序了。我已经研究了好几天了,没有结果。 本质上,我需要尝试将从
我有一个 ArrayBuffer,它是通过使用 Frida 读取内存返回的。我正在将 ArrayBuffer 转换为字符串,然后使用 TextDecoder 和 TextEncoder 转换回 Arr
我的目标是将 TIFF 图像加载到 HTML Canvas 上。前端接收 TIFF 图像的 ArrayBuffer,我能够利用 UTIF 解码 ArrayBuffer 并将其呈现在 HTML Canv
当我从 angular 6 项目上传图像时,我正在尝试按照教程将图像转换为 Base64。当我按下提交按钮时,我可以获得作为值的输出:“base64 代码”,而且我还可以通过使用“将你的 Base64
我正在尝试从 Arweave 中获取加密的原始缓冲区数据 (AES-256) ,传递给解密函数并使用它来显示图像。我正在尝试在前端(在我的 React 应用程序中)获取和解密 ArrayBuffer。
是否有任何特殊的案例类来表示空 ArrayBuffer可用于类似于 Nil 的模式匹配对于列表? 还有为什么这有效: scala> collection.mutable.ArrayBuffer.emp
Scala 在过滤不可变序列方面非常优雅: var l = List(1,2,3,4,5,6) l = l.filter(_%2==1) 但是我如何使用像 ArrayBuffer 这样的可变集合来做到
我有这个功能: def getTime() : ArrayBuffer[Timestamp] = { val offset = Timestamp.valueOf("2015-01-01 00
我有一个 Javascript ArrayBuffer,我想将其转换为十六进制字符串。 有人知道我可以调用的函数或已经存在的预先编写的函数吗? 我只能找到数组缓冲区到字符串函数,但我想要数组缓冲区的十
我正在尝试使用 FileReader API 将本地文件读入 ArrayBuffer,如下所示 let reader = new FileReader(); reader.onload = funct
假设我创建了初始大小等于 10 的 ArrayBuffer val buf = new ArrayBuffer[Int](10) 如果我确实调用了方法 buf.size - 缓冲区的大小是否等于 0?
我正在尝试使用 AES-CTR 算法在浏览器上解密数据。 WebCrypto API需要将计数器作为 BufferSource 传递.如何将计数器(数字)转换为预期的输入(字节数组)? 我使用的是全零
我有通过 TCP 连接 (WebSocket) 发送的二进制信息。这以编码格式跟踪到控制台,如下所示: 53 54 41 52 54 45 44 3a 31 34 32 38 36 30 32 30
因此,我有一个 ArrayBuffer[Signal],其中每个 Signal 都有一个时间戳(数组按此时间戳排序)。我想进行二进制搜索并返回某个范围内的信号的 Seq[Signal]。现在是用线性搜
我想在我的程序中使用 ArrayBuffer 来保存数字列表。我想将它用作队列。每次删除列表中的第一项并使用它。然后我想知道每次我删除第一个项目时队列中的所有其他数字都会移动一个位置。我的意思是下次我
我是一名优秀的程序员,十分优秀!