-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* explicit error messaging if invalid image is provided * smoothlerp * param description update * wrapR param * texture 3d helper * path fix
- Loading branch information
1 parent
f9d10ae
commit 930eae1
Showing
2 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Texture } from "../core/Texture.js"; | ||
|
||
export class Texture3D extends Texture { | ||
constructor(gl, args) { | ||
|
||
super(gl, { | ||
...args, | ||
target: gl.TEXTURE_3D, | ||
width: args.width ? args.width : 2, | ||
height: args.height ? args.height : 2, | ||
}); | ||
|
||
const image = new Image(); | ||
image.crossOrigin = "*"; | ||
image.src = args.src; | ||
|
||
image.onload = () => { | ||
|
||
let canvas = document.createElement('canvas'); | ||
canvas.width = image.width; | ||
canvas.height = image.height; | ||
|
||
let ctx = canvas.getContext('2d'); | ||
ctx.scale(1, -1); | ||
ctx.translate(0, -image.height); | ||
ctx.drawImage(image, 0, 0); | ||
const imageData = ctx.getImageData(0, 0, image.width, image.height).data; | ||
|
||
canvas = null; | ||
ctx = null; | ||
let elementCount; | ||
|
||
switch (this.format) { | ||
case gl.RED: elementCount = 1 | ||
break | ||
case gl.RG: elementCount = 2 | ||
break | ||
case gl.RGB: elementCount = 3 | ||
break | ||
default: elementCount = 4 | ||
break | ||
} | ||
|
||
const dataCount = this.width * this.height * this.length * elementCount; | ||
const data = this.type === gl.UNSIGNED_BYTE ? new Uint8Array(dataCount) : new Float32Array(dataCount); | ||
|
||
let dataIterator = 0; | ||
|
||
for (let z = 0; z < this.length; z++) { | ||
for (let y = 0; y < this.height; y++) { | ||
for (let x = 0; x < this.width; x++) { | ||
|
||
let zOffsetX = (z % args.tileCountX) * this.width; | ||
let zOffsetY = Math.floor(z / args.tileCountX) * (this.width * this.height * args.tileCountX); | ||
let index = (x + zOffsetX) + ((y * image.width) + zOffsetY); | ||
|
||
const r = imageData[index * 4]; | ||
const g = imageData[index * 4 + 1]; | ||
const b = imageData[index * 4 + 2]; | ||
const a = imageData[index * 4 + 3]; | ||
|
||
let texel = [r, g, b, a]; | ||
|
||
for (let i = 0; i < elementCount; i++) { | ||
if (this.type === this.gl.UNSIGNED_BYTE) { | ||
data[dataIterator++] = texel[i]; | ||
} else { | ||
data[dataIterator++] = texel[i] / 255; | ||
} | ||
|
||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
this.image = data; | ||
this.needsUpdate = true; | ||
|
||
} | ||
} | ||
} |