fromEquirectangularTexture method

WebGLCubeRenderTarget fromEquirectangularTexture(
  1. WebGLRenderer renderer,
  2. Texture texture
)

Implementation

WebGLCubeRenderTarget fromEquirectangularTexture(WebGLRenderer renderer, Texture texture) {
  this.texture.type = texture.type;
  this.texture.encoding = texture.encoding;

  this.texture.generateMipmaps = texture.generateMipmaps;
  this.texture.minFilter = texture.minFilter;
  this.texture.magFilter = texture.magFilter;

  var shader = {
    "uniforms": {
      "tEquirect": {},
    },
    "vertexShader": """

      varying vec3 vWorldDirection;

      vec3 transformDirection( in vec3 dir, in mat4 matrix ) {

        return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );

      }

      void main() {

        vWorldDirection = transformDirection( position, modelMatrix );

        #include <begin_vertex>
        #include <project_vertex>

      }
    """,
    "fragmentShader": """

      uniform sampler2D tEquirect;

      varying vec3 vWorldDirection;

      #include <common>

      void main() {

        vec3 direction = normalize( vWorldDirection );

        vec2 sampleUV = equirectUv( direction );

        gl_FragColor = texture2D( tEquirect, sampleUV );

      }
    """
  };

  var geometry = BoxGeometry(5, 5, 5);

  var material = ShaderMaterial({
    "name": 'CubemapFromEquirect',
    "uniforms": cloneUniforms(shader["uniforms"] as Map<String, dynamic>),
    "vertexShader": shader["vertexShader"],
    "fragmentShader": shader["fragmentShader"],
    "side": BackSide,
    "blending": NoBlending
  });

  material.uniforms["tEquirect"]["value"] = texture;

  var mesh = Mesh(geometry, material);

  var currentMinFilter = texture.minFilter;

  // Avoid blurred poles
  if (texture.minFilter == LinearMipmapLinearFilter) {
    texture.minFilter = LinearFilter;
  }

  var camera = CubeCamera(1, 10, this);
  camera.update(renderer, mesh);

  texture.minFilter = currentMinFilter;

  mesh.geometry!.dispose();
  mesh.material.dispose();

  return this;
}