Skip to content
Snippets Groups Projects
Commit 926c40b7 authored by Brian Peiris's avatar Brian Peiris
Browse files

Fix shader injection for cloned media

parent 6819453b
No related branches found
No related tags found
No related merge requests found
...@@ -253,9 +253,6 @@ async function loadGLTF(src, contentType, preferredTechnique, onProgress) { ...@@ -253,9 +253,6 @@ async function loadGLTF(src, contentType, preferredTechnique, onProgress) {
); );
const envMap = await CachedEnvMapTexture; const envMap = await CachedEnvMapTexture;
const beginVertexRegex = /\bbegin_vertex\b/;
const materialsSeen = new Set();
const shaderUniforms = [];
gltf.scene.traverse(object => { gltf.scene.traverse(object => {
if (object.material && object.material.type === "MeshStandardMaterial") { if (object.material && object.material.type === "MeshStandardMaterial") {
...@@ -263,31 +260,6 @@ async function loadGLTF(src, contentType, preferredTechnique, onProgress) { ...@@ -263,31 +260,6 @@ async function loadGLTF(src, contentType, preferredTechnique, onProgress) {
object.material = MobileStandardMaterial.fromStandardMaterial(object.material); object.material = MobileStandardMaterial.fromStandardMaterial(object.material);
} else { } else {
object.material.envMap = envMap; object.material.envMap = envMap;
object.material.onBeforeCompile = shader => {
if (!beginVertexRegex.test(shader.vertexShader)) return;
const lines = shader.vertexShader.split("\n");
const beginVertexIndex = lines.findIndex(line => beginVertexRegex.test(line));
lines.splice(
beginVertexIndex + 1,
0,
`
if (hubsShouldAttract) {
vec4 wt = modelMatrix * vec4(transformed, 1);
vec4 p4 = vec4(hubsAttractorPosition, 1);
transformed += (normalize(wt - p4) / 5.0 / pow(distance(wt, p4), -0.2)).xyz;
}
`
);
lines.unshift("uniform vec3 hubsAttractorPosition;");
lines.unshift("uniform bool hubsShouldAttract;");
shader.vertexShader = lines.join("\n");
shader.uniforms.hubsAttractorPosition = { value: [0, 0, 0] };
shader.uniforms.hubsShouldAttract = { value: false };
if (!materialsSeen.has(object.material.uuid)) {
shaderUniforms.push(shader.uniforms);
materialsSeen.add(object.material.uuid);
}
};
object.material.needsUpdate = true; object.material.needsUpdate = true;
} }
} }
...@@ -298,7 +270,7 @@ async function loadGLTF(src, contentType, preferredTechnique, onProgress) { ...@@ -298,7 +270,7 @@ async function loadGLTF(src, contentType, preferredTechnique, onProgress) {
Object.keys(fileMap).forEach(URL.revokeObjectURL); Object.keys(fileMap).forEach(URL.revokeObjectURL);
} }
return { gltf, shaderUniforms }; return gltf;
} }
/** /**
...@@ -334,16 +306,52 @@ AFRAME.registerComponent("gltf-model-plus", { ...@@ -334,16 +306,52 @@ AFRAME.registerComponent("gltf-model-plus", {
}, },
async loadModel(src, contentType, technique, useCache) { async loadModel(src, contentType, technique, useCache) {
let gltf;
if (useCache) { if (useCache) {
if (!GLTFCache[src]) { if (!GLTFCache[src]) {
GLTFCache[src] = await loadGLTF(src, contentType, technique); GLTFCache[src] = await loadGLTF(src, contentType, technique);
} }
gltf = cloneGltf(GLTFCache[src]);
const cached = GLTFCache[src];
return { gltf: cloneGltf(cached.gltf), shaderUniforms: cached.shaderUniforms };
} else { } else {
return await loadGLTF(src, contentType, technique); gltf = await loadGLTF(src, contentType, technique);
} }
const beginVertexRegex = /\bbegin_vertex\b/;
const materialsSeen = new Set();
const shaderUniforms = [];
gltf.scene.traverse(object => {
if (object.material && object.material.type === "MeshStandardMaterial") {
object.material.onBeforeCompile = shader => {
if (!beginVertexRegex.test(shader.vertexShader)) return;
const lines = shader.vertexShader.split("\n");
const beginVertexIndex = lines.findIndex(line => beginVertexRegex.test(line));
lines.splice(
beginVertexIndex + 1,
0,
`
if (hubsShouldAttract) {
vec4 wt = modelMatrix * vec4(transformed, 1);
vec4 p4 = vec4(hubsAttractorPosition, 1);
transformed += (normalize(wt - p4) / 5.0 / pow(distance(wt, p4), -0.2)).xyz;
}
`
);
lines.unshift("uniform vec3 hubsAttractorPosition;");
lines.unshift("uniform bool hubsShouldAttract;");
shader.vertexShader = lines.join("\n");
shader.uniforms.hubsAttractorPosition = { value: [0, 0, 0] };
shader.uniforms.hubsShouldAttract = { value: false };
if (!materialsSeen.has(object.material.uuid)) {
shaderUniforms.push(shader.uniforms);
materialsSeen.add(object.material.uuid);
}
};
object.material.needsUpdate = true;
}
});
return { gltf, shaderUniforms };
}, },
async applySrc(src, contentType) { async applySrc(src, contentType) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment