Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to get CubeMaps working? #247

Open
illus0r opened this issue Sep 24, 2020 · 1 comment
Open

Is there a way to get CubeMaps working? #247

illus0r opened this issue Sep 24, 2020 · 1 comment

Comments

@illus0r
Copy link

illus0r commented Sep 24, 2020

Hey there! Thank you for the great editor, which is top on my list.

Shadertoy and some other editors support CubeMaps, which are useful for rendering reflections of environment and other cool effects:

color = texture(iChannel0, reflection_vector).xyz;

where iChannel0 is a cubemap texture.
Example can be found at https://www.shadertoy.com/view/WljBRm

Is it possible to get cubemaps working with Veda? If not, can you suggest a snippet to use regular textures as cubemaps?

@illus0r
Copy link
Author

illus0r commented Sep 27, 2020

Solved this by modifying a snippet from https://en.wikipedia.org/wiki/Cube_mapping#Memory_addressing.

vec4 textureCubeZ(sampler2D tex, vec3 p) {
  float absX = abs(p.x);
  float absY = abs(p.y);
  float absZ = abs(p.z);

  int isXPositive = p.x > 0. ? 1 : 0;
  int isYPositive = p.y > 0. ? 1 : 0;
  int isZPositive = p.z > 0. ? 1 : 0;

  float maxAxis, uc, vc;
  vec2 crop;

  // POSITIVE X
  if (isXPositive!=0 && absX >= absY && absX >= absZ) {
    maxAxis = absX;
    uc = -p.z;
    vc = p.y;
    crop=vec2(2,1);
  }
  // NEGATIVE X
  if (isXPositive==0 && absX >= absY && absX >= absZ) {
    maxAxis = absX;
    uc = p.z;
    vc = p.y;
    crop=vec2(0,1);
  }
  // NEGATIVE Y
  if (isYPositive!=0 && absY >= absX && absY >= absZ) {
    maxAxis = absY;
    uc = p.x;
    vc = -p.z;
    crop=vec2(1,2);
  }
  // POSITIVE Y
  if (isYPositive==0 && absY >= absX && absY >= absZ) {
    maxAxis = absY;
    uc = p.x;
    vc = p.z;
    crop=vec2(1,0);
  }
  // POSITIVE Z
  if (isZPositive!=0 && absZ >= absX && absZ >= absY) {
    maxAxis = absZ;
    uc = p.x;
    vc = p.y;
    crop=vec2(1,1);
  }
  // NEGATIVE Z
  if (isZPositive==0 && absZ >= absX && absZ >= absY) {
    maxAxis = absZ;
    uc = -p.x;
    vc = p.y;
    crop=vec2(3,1);
  }

  // Convert range from -1 to 1 to 0 to 1
  vec2 uv = 0.5 * (vec2(uc,vc) / maxAxis + 1.0);

  uv+=crop;
  uv/=vec2(4,3);

  return texture2D(tex, uv);
}

The image should look like this:

telegram-cloud-photo-size-2-5442726331977674693-y

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant