-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityWeBGLTextures.txt
More file actions
224 lines (168 loc) · 6.05 KB
/
UnityWeBGLTextures.txt
File metadata and controls
224 lines (168 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
====
http://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
====
https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html
====
https://github.com/atteneder/UnityJavascriptInteraction
var dataPtr = Module._malloc(24);
Module.setValue(dataPtr,vector3.x,'float');
Module.setValue(dataPtr+4,vector3.y,'float');
Module.setValue(dataPtr+8,vector3.z,'float');
Module.setValue(dataPtr+12,vector3_2.x,'float');
Module.setValue(dataPtr+16,vector3_2.y,'float');
Module.setValue(dataPtr+20,vector3_2.z,'float');
c_vx(int1,int2,dataPtr,dataPtr+12);
Module._free(dataPtr);
====
https://github.com/Planeshifter/emscripten-examples/tree/master/01_PassingArrays
function _arrayToHeap(typedArray){
var numBytes = typedArray.length * typedArray.BYTES_PER_ELEMENT;
var ptr = Module._malloc(numBytes);
var heapBytes = new Uint8Array(Module.HEAPU8.buffer, ptr, numBytes);
heapBytes.set(new Uint8Array(typedArray.buffer));
return heapBytes;
}
====
https://kapadia.github.io/emscripten/2013/09/13/emscripten-pointers-and-pointers.html
====
https://forum.unity.com/threads/c-jslib-2-way-communication.323629/
using System;
using System.Runtime.InteropServices;
using AOT;
using UnityEngine;
public class JsCallCsTest : MonoBehaviour
{
[DllImport("__Internal")]
public static extern void ProvideCallback(Action action);
public void Start()
{
ProvideCallback(Callback);
}
[MonoPInvokeCallback(typeof(Action))]
public static void Callback()
{
Debug.Log("Callback called");
}
}
var LibraryJsCallCsTest = {
$JsCallCsTest: {},
ProvideCallback: function(obj)
{
console.log("ProvideCallback");
console.log(obj);
JsCallCsTest.callback = obj;
Runtime.dynCall('v', obj, 0);
},
};
autoAddDeps(LibraryJsCallCsTest, '$JsCallCsTest');
mergeInto(LibraryManager.library, LibraryJsCallCsTest);
====
https://forum.unity.com/threads/super-fast-javascript-interaction-on-webgl.382734/
====
https://stackoverflow.com/questions/35183253/unity3d-upload-a-image-from-pc-memory-to-webgl-app
Unity3D - Upload a image from PC memory to WebGL app
var dataUrl = g.ctx.canvas.toDataURL();
send data url to unity
static string s_dataUrlPrefix = "data:image/png;base64,";
public void ReceiveImage(string dataUrl)
{
if (dataUrl.StartsWith(s_dataUrlPrefix))
{
byte[] pngData = System.Convert.FromBase64String(dataUrl.Substring(s_dataUrlPrefix.Length));
// Create a new Texture (or use some old one?)
Texture2D tex = new Texture2D(1, 1); // does the size matter?
if (tex.LoadImage(pngData))
{
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = tex;
}
else
{
Debug.LogError("could not decode image");
}
}
else
{
Debug.LogError("Error getting image:" + dataUrl);
}
}
====
https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
BindWebGLTexture(texture.GetNativeTextureID());
BindWebGLTexture: function (texture) {
GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[texture]);
},
====
/Applications/Unity/PlaybackEngines/WebGLSupport/BuildTools/lib/
/Applications/Unity/PlaybackEngines/WebGLSupport/BuildTools/prejs/
====
Module.ccall("SetFullscreen", null, ["number"], [fullscreen]);
dynCall('vi', callback, [userData]);
JS_Eval_OpenURL: function(ptr)
{
var str = Pointer_stringify(ptr);
location.href = str;
},
JS_Eval_SetTimeout: function(func, arg, millis)
{
Module['noExitRuntime'] = true;
function wrapper() {
getFuncWrapper(func, 'vi')(arg);
}
return Browser.safeSetTimeout(wrapper, millis);
},
function SendMessage(gameObject, func, param)
{
if (param === undefined)
Module.ccall("SendMessage", null, ["string", "string"], [gameObject, func]);
else if (typeof param === "string")
Module.ccall("SendMessageString", null, ["string", "string", "string"], [gameObject, func, param]);
else if (typeof param === "number")
Module.ccall("SendMessageFloat", null, ["string", "string", "number"], [gameObject, func, param]);
else
throw "" + param + " is does not have a type which is supported by SendMessage.";
}
Module["SendMessage"] = SendMessage; // to avoid emscripten stripping
autoAddDeps(LibraryFileSystemWebGL, '$fs');
mergeInto(LibraryManager.library, LibraryFileSystemWebGL);
JS_SystemInfo_GetGPUInfo : function(buffer, bufferSize)
{
var gpuinfo = UnityLoader.SystemInfo.gpu;
if (buffer)
stringToUTF8(gpuinfo, buffer, bufferSize);
return lengthBytesUTF8(gpuinfo);
}
/Applications/Unity/PlaybackEngines/WebGLSupport/BuildTools/lib/Video.js
JS_Video_UpdateToTexture: function(video, tex)
{
if (videoInstances[video].lastUpdateTextureTime === videoInstances[video].currentTime)
return false;
GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[tex]);
GLctx.pixelStorei(GLctx.UNPACK_FLIP_Y_WEBGL, true);
GLctx.texSubImage2D(GLctx.TEXTURE_2D, 0, 0, 0, GLctx.RGBA, GLctx.UNSIGNED_BYTE, videoInstances[video]);
GLctx.pixelStorei(GLctx.UNPACK_FLIP_Y_WEBGL, false);
videoInstances[video].lastUpdateTextureTime = videoInstances[video].currentTime;
return true;
},
/Applications/Unity/PlaybackEngines/WebGLSupport/BuildTools/lib/WebCam.js
JS_WebCamVideo_GrabFrame: function (deviceId, buffer, destWidth, destHeight)
{
if (!MediaDevices[deviceId].video)
{
console.error("WebCam not initialized.");
return;
}
var context = webcam.canvas.getContext('2d');
if (context)
{
canvas.width = destWidth;
canvas.height = destHeight;
context.drawImage(MediaDevices[deviceId].video, 0, 0, MediaDevices[deviceId].video.videoWidth, MediaDevices[deviceId].video.videoHeight, 0, 0, destWidth, destHeight);
var imageData = context.getImageData(0, 0, destWidth, destHeight);
writeArrayToMemory(imageData.data, buffer);
}
else
{
console.log("2d Context is null");
}
}