Skip to content

Commit

Permalink
separate height and width tile
Browse files Browse the repository at this point in the history
  • Loading branch information
cesswairimu committed Jan 26, 2024
1 parent 056f07c commit 4884c4d
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 23 deletions.
213 changes: 213 additions & 0 deletions dist/png-to-glb-utility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
// function init() {
// toggleSection("checkboxRoughness", "roughness-section");
// toggleSection("normalTexture", "normal-section");

// // display uploads
// const inputFiles = ['fileInput', 'fileInput1', 'fileInput2'];
// const outputFiles = ['fileNameOutput', 'fileNameOutput1', 'fileNameOutput2'];
// inputFiles.forEach((elem) => {
// const input = document.getElementById(elem);
// const output = document.getElementById(outputFiles[inputFiles.indexOf(elem)]);
// input.addEventListener("change", (event) => {
// const fileName = event.target.files[0].name;
// output.value = fileName;
// const file = event.target.files[0];
// const reader = new FileReader();

// reader.onload = (e) => {
// if (elem === "fileInput") {
// document.getElementById('imagePreview').src = e.target.result;
// $("#submitBtn").removeClass("disabled");
// }
// };
// reader.readAsDataURL(file);
// });
// });

// const form = document.getElementById("inputForm");
// form.addEventListener("submit", async (e) => {
// e.preventDefault();
// e.stopPropagation();
// submitForm(form)

// form.reset();
// resetForm();
// });
// }

//submit form
// async function submitForm(form) {
// const formData = new FormData(form);
// const displayImg = formData.get('fileInput1');
// const textureImg = formData.get('fileInput2');
// const normalImg = formData.get('fileInput3');
// const tileWidth = formData.get('repeatWidth');
// const tileHeight = formData.get('repeatHeight');

// console.log("Tile image by width, height", tileWidth, tileHeight);

// await Promise.all([
// readFileAsDataURL(displayImg),
// readFileAsDataURL(textureImg),
// readFileAsDataURL(normalImg)
// ]).then(([baseImage, textureImage, normalImage]) => {
// document.querySelector("#display-image").src = baseImage;
// document.querySelector("#texture-image").src = textureImage;
// document.querySelector("#normal-image").src = normalImage;

// createGLFTAsset(baseImage, textureImage, normalImage, [tileWidth, tileHeight]);
// });
// }

let repeatTexture = false;
async function createGLFTAsset(baseImage, textureImage, normalImage, repeatImage = [1, 1], type = "glb") {
repeatTexture = repeatImage.some(el => el > 1);
console.log(repeatTexture);

// create GLTF asset, scene and node
const asset = new GLTFUtils.GLTFAsset({ "number": 0, "index": 0 });
const scene = new GLTFUtils.Scene("");
asset.addScene(scene);
const node = new GLTFUtils.Node("PngGlb");
scene.addNode(node);

var vertices = [0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0]

let vertex_hash = [];
for (let i = 0; i < vertices.length; i += 5) {
const vertex = new GLTFUtils.Vertex();
vertex.x = vertices[i];
vertex.y = vertices[i + 1];
vertex.z = vertices[i + 2];
vertex.u = vertices[i + 3] * repeatImage[0]; // texture co-ord
vertex.v = vertices[i + 4] * repeatImage[1]; // texture co-ord
vertex_hash.push(vertex);
}

// create mesh and add faces
var triangles = [0, 1, 2, 2, 3, 0];
const mesh = new GLTFUtils.Mesh();
for (let i = 0; i < triangles.length; i += 3) {
v1 = vertex_hash[triangles[i]];
v2 = vertex_hash[triangles[i + 1]];
v3 = vertex_hash[triangles[i + 2]];
mesh.addFace(v1, v2, v3, { r: 1, g: 1, b: 1 }, 0);
}

// create material
const material = await createMaterial(baseImage, textureImage, normalImage);
mesh.material = [material];
node.mesh = mesh;
console.log(asset);

// export asset as GLB or GLTF
if (type == 'glb') {
exportGlb(asset);
} else if (type == 'gltf') {
exportGltf(asset);
}
}

async function createMaterial(baseImage, textureImage, normalImage) {
const material = new GLTFUtils.Material();
// repeatTexture = true;

// Base texture handling
let baseTexture = "";
try {
baseTexture = new GLTFUtils.Texture(baseImage);
} catch (err) {
console.log(err);
baseTexture = new GLTFUtils.Texture(await fetchJpgImage("display-image"));
}
let wrapMode = repeatTexture ? GLTFUtils.WrappingMode.REPEAT : GLTFUtils.WrappingMode.CLAMP_TO_EDGE;
baseTexture.wrapS = wrapMode;
baseTexture.wrapT = GLTFUtils.WrappingMode.REPEAT;
material.pbrMetallicRoughness.baseColorTexture = baseTexture;

// Roughness texture handling
let roughnessTexture = "";
if (textureImage != "data:application/octet-stream;base64,") {
try {
roughnessTexture = new GLTFUtils.Texture(textureImage);
} catch (err) {
console.log(err);
roughnessTexture = new GLTFUtils.Texture(await fetchJpgImage("texture-image"));
}
roughnessTexture.wrapS = wrapMode;
roughnessTexture.wrapT = GLTFUtils.WrappingMode.REPEAT;
material.pbrMetallicRoughness.metallicRoughnessTexture = roughnessTexture;
}

// Normal texture handling
let normalTexture = "";
if (normalImage != "data:application/octet-stream;base64,") {
try {
normalTexture = new GLTFUtils.Texture(normalImage);
} catch (err) {
console.log(err);
normalTexture = new GLTFUtils.Texture(await fetchJpgImage("normal-image"));
}
normalTexture.wrapS = wrapMode;
normalTexture.wrapT = GLTFUtils.WrappingMode.REPEAT;
material.normalTexture = normalTexture;
}
// other material properties
material.pbrMetallicRoughness.roughnessFactor = 0.5;
material.pbrMetallicRoughness.metallicFactor = 0.5;
material.doubleSided = true;
return material;
}

async function exportGltf(asset) {
let files = await GLTFUtils.exportGLTF(asset, {
bufferOutputType: GLTFUtils.BufferOutputType.DataURI,
imageOutputType: GLTFUtils.BufferOutputType.DataURI
});
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + files['model.gltf']);
element.setAttribute('download', "file.gltf");
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}

async function exportGlb(asset) {
let files = await GLTFUtils.exportGLB(asset);
const arrayBuffer = files;
const blob = new Blob([arrayBuffer], { type: 'application/octet-stream' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'file.glb';
a.click();
}

async function fetchJpgImage(name) {
return document.getElementById(name);
}

// function resetForm() {
// document.getElementById('imagePreview').src = "";
// $("#submitBtn").addClass("disabled");
// toggleSection("checkboxRoughness", "roughness-section");
// toggleSection("normalTexture", "normal-section");
// }

// function toggleSection(checkboxId, sectionId) {
// const checkbox = document.getElementById(checkboxId);
// const section = document.getElementById(sectionId);

// checkbox.addEventListener("change", () => {
// section.style.display = checkbox.checked ? 'block' : 'none';
// });
// }

// function readFileAsDataURL(file) {
// return new Promise((resolve, reject) => {
// const reader = new FileReader();
// reader.onload = () => resolve(reader.result);
// reader.onerror = reject;
// reader.readAsDataURL(file);
// });
// }
Binary file removed examples/favicon.png
Binary file not shown.
Empty file added examples/gltf-only.js
Empty file.
26 changes: 17 additions & 9 deletions index.html → public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<script src="https://cdn.jsdelivr.net/npm/bootstrap.min.js"></script>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap/dist/css/bootstrap.min.css">
<link rel="shortcut icon" type="image/png" href="examples/favicon.png" />
<link rel="stylesheet" href="dist/main.css">
<script src="dist/main.js"></script>
<link rel="shortcut icon" type="image/png" href="#" />
<link rel="stylesheet" href="./main.css">
<script src="./main.js"></script>
</head>

<body>
Expand All @@ -35,15 +35,23 @@ <h3 class="fs-2 text-black-50">Create a textured surface</h3>

<div class="d-inline-flex w-auto pb-2">
<div class="box rounded box-bg" style="height: 150px; width: 150px;">
<!-- <div class="mt-3 flex-grow-1"> -->
<img id="imagePreview" class="img-thumbnail" alt="Image Preview">
<!-- </div> -->
</div>
<div class="btn-group btn-group-md h-25 pt-5 ps-2">
<div class="btn btn-outline-secondary disabled">
Tile/repeat image by:

<div class="d-flex flex-column mb-3">
<div class="btn-group btn-group-md h-30 pt-3 ps-2">
<div class="btn btn-outline-secondary disabled">
Tile image width by:
</div>
<input type="number" min="1" max="50" value="1" class="btn btn-outline-secondary" name="repeatWidth">
</div> <br>

<div class="btn-group btn-group-md h-30 pt-2 ps-2">
<div class="btn btn-outline-secondary disabled">
Tile image height by:
</div>
<input type="number" min="1" max="50" value="1" class="btn btn-outline-secondary" name="repeatHeight">
</div>
<input type="number" min="1" max="50" value="1" class="btn btn-outline-secondary" name="repeatImage">
</div>
</div>

Expand Down
File renamed without changes.
27 changes: 13 additions & 14 deletions dist/main.js → public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ async function submitForm(form) {
const displayImg = formData.get('fileInput1');
const textureImg = formData.get('fileInput2');
const normalImg = formData.get('fileInput3');
const repeatImage = formData.get('repeatImage');
console.log("Tile image by", repeatImage);
const tileWidth = formData.get('repeatWidth');
const tileHeight = formData.get('repeatHeight');

console.log("Tile image by width, height", tileWidth, tileHeight);

await Promise.all([
readFileAsDataURL(displayImg),
Expand All @@ -53,13 +55,14 @@ async function submitForm(form) {
document.querySelector("#texture-image").src = textureImage;
document.querySelector("#normal-image").src = normalImage;

createGLFTAsset(baseImage, textureImage, normalImage, repeatImage);
createGLFTAsset(baseImage, textureImage, normalImage, [tileWidth, tileHeight]);
});
}

let repeatTexture = false;
async function createGLFTAsset(baseImage, textureImage, normalImage, repeatImage = 1, type = "glb") {
repeatTexture = repeatImage > 1;
async function createGLFTAsset(baseImage, textureImage, normalImage, repeatImage, type = "glb") {
repeatTexture = repeatImage.some(el => el > 1);
console.log(repeatTexture);

// create GLTF asset, scene and node
const asset = new GLTFUtils.GLTFAsset({ "number": 0, "index": 0 });
Expand All @@ -68,21 +71,16 @@ async function createGLFTAsset(baseImage, textureImage, normalImage, repeatImage
const node = new GLTFUtils.Node("PngGlb");
scene.addNode(node);

// define vertices
var vertices = [0, 0, 0, 0, 1, 2, 0, 0, 1, 1, 2, 2, 0, 1, 0, 0, 2, 0, 0, 0]
var vertices = [0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0]

let vertex_hash = [];
for (let i = 0; i < vertices.length; i += 5) {
const vertex = new GLTFUtils.Vertex();
vertex.x = vertices[i];
vertex.y = vertices[i + 1];
vertex.z = vertices[i + 2];
if (repeatTexture) {
vertex.u = vertices[i + 3] * repeatImage; // texture co-ord
vertex.v = vertices[i + 4] * repeatImage; // texture co-ord
} else {
vertex.u = vertices[i + 3];
vertex.v = vertices[i + 4];
}
vertex.u = vertices[i + 3] * repeatImage[0]; // texture co-ord
vertex.v = vertices[i + 4] * repeatImage[1]; // texture co-ord
vertex_hash.push(vertex);
}

Expand Down Expand Up @@ -112,6 +110,7 @@ async function createGLFTAsset(baseImage, textureImage, normalImage, repeatImage

async function createMaterial(baseImage, textureImage, normalImage) {
const material = new GLTFUtils.Material();
// repeatTexture = true;

// Base texture handling
let baseTexture = "";
Expand Down

0 comments on commit 4884c4d

Please sign in to comment.