Skip to content

Commit

Permalink
Fix support for animated uniforms
Browse files Browse the repository at this point in the history
Summary: Three.js caches the value of shader.uniform after first render so changes require making changes to the contents of the original object. This is non-obvious of Three.js!

Reviewed By: andrewimm

Differential Revision: D5802268

fbshipit-source-id: 469832b
  • Loading branch information
mikearmstrong001 authored and facebook-github-bot committed Sep 11, 2017
1 parent 4bc159a commit 44615c9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
25 changes: 19 additions & 6 deletions ReactVR/js/Views/BaseMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as THREE from 'three';
import * as Yoga from '../Utils/Yoga.bundle';

import type {GuiSys} from 'ovrui';
import type {Geometry, Texture, Material} from 'three';
import type {Geometry, Texture, Material, ShaderMaterial} from 'three';
import type {ReactNativeContext} from '../ReactNativeContext';

type ResourceSpecifier = void | null | string | {uri: string};
Expand All @@ -33,7 +33,7 @@ export default class RCTBaseMesh extends RCTBaseView {
_texture: null | Texture;
_litMaterial: Material;
_unlitMaterial: Material;
_shaderMaterial: Material;
_shaderMaterial: ShaderMaterial;
_shaderUniformsMap: any;
mesh: any;
_geometry: any;
Expand Down Expand Up @@ -252,10 +252,23 @@ export default class RCTBaseMesh extends RCTBaseView {
};
}
if (parameters.vertexShader || parameters.fragmentShader) {
parameters.uniforms = {
...this._shaderUniformsMap,
...parameters.uniforms,
};
// extract the uniforms
const uniforms = parameters.uniforms || {};
delete parameters.uniforms;

// set value of uniform is cached so we need to make changes to that
const targetUniforms = this._shaderMaterial.uniforms;
for (const uniform in targetUniforms) {
delete targetUniforms[uniform];
}
for (const uniform in this._shaderUniformsMap) {
targetUniforms[uniform] = this._shaderUniformsMap[uniform];
}
for (const uniform in uniforms) {
targetUniforms[uniform] = uniforms[uniform];
}

// assign the reset via the usual path
this._shaderMaterial.setValues(parameters);
this._shader = true;
if (this.mesh) {
Expand Down
1 change: 1 addition & 0 deletions defs/three.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ declare module 'three' {

declare class ShaderMaterial extends Material {
constructor(): ShaderMaterial,
uniforms: any,
}

declare class Object3D {
Expand Down

0 comments on commit 44615c9

Please sign in to comment.