From 8da6ae45c5cdbe8ead81f298e244ca16b9e0a21c Mon Sep 17 00:00:00 2001 From: JannisX11 Date: Fri, 26 Nov 2021 14:31:55 +0100 Subject: [PATCH] Collada material template --- js/io/formats/collada.js | 190 ++++++++++++++++++++++++++++++++++----- 1 file changed, 170 insertions(+), 20 deletions(-) diff --git a/js/io/formats/collada.js b/js/io/formats/collada.js index 1b24fc3..eed14b4 100644 --- a/js/io/formats/collada.js +++ b/js/io/formats/collada.js @@ -32,6 +32,9 @@ var codec = new Codec('collada', { */ let geometries = []; let root = []; + let effects = []; + let images = []; + let materials = []; let model = { type: 'COLLADA', @@ -51,6 +54,18 @@ var codec = new Codec('collada', { {name: 'up_axis', content: 'Y_UP'} ] }, + { + type: 'library_effects', + content: effects + }, + { + type: 'library_images', + content: images + }, + { + type: 'library_materials', + content: materials + }, { type: 'library_geometries', content: geometries @@ -58,7 +73,7 @@ var codec = new Codec('collada', { { type: 'library_visual_scenes', content: [{ - type: visual_scene, + type: 'visual_scene', attributes: { id: 'Scene', name: 'Scene', @@ -78,25 +93,71 @@ var codec = new Codec('collada', { ] } - Cube.all.forEach(cube => { - let node = { + Texture.all.forEach((texture, i) => { + effects.push({ + type: 'effect', + attributes: {id: `Material_${i}-effect`}, + content: { + type: 'profile_COMMON', + content: [ + { + type: 'newparam', + attributes: {sid: `Image_0-surface`}, + content: { + type: 'surface', + attributes: {type: '2D'}, + content: {type: 'init_from', content: `Image_0`} + } + }, + { + type: 'newparam', + attributes: {sid: `Image_0-sampler`}, + content: { + type: 'sampler2D', + attributes: {type: '2D'}, + content: {type: 'source', content: `Image_0-surface`} + } + }, + { + type: 'technique', + attributes: {sid: 'common'}, + content: { + type: 'lambert', + content: [ + {type: 'emission', content: {type: 'color', attributes: {sid: 'emission'}, content: '0 0 0 1'}}, + {type: 'diffuse', content: {type: 'texture', attributes: {texture: `Image_0-sampler`, texcoord: 'UVMap'}}}, + {type: 'index_of_refraction', content: {type: 'float', attributes: {sid: 'ior'}, content: '1.45'}} + ] + } + } + ] + } + }) + images.push({ + type: 'image', attributes: { - id: cube.uuid, - name: cube.name, - type: 'NODE' + id: `Image_${i}`, + name: `Image_${i}`, }, - content: [ - {type: 'scale', attributes: {sid: 'scale'}, content: '1 1 1'}, - {type: 'rotate', attributes: {sid: 'rotationX'}, content: '1 0 0 0'}, - {type: 'rotate', attributes: {sid: 'rotationY'}, content: '0 1 0 0'}, - {type: 'rotate', attributes: {sid: 'rotationZ'}, content: '0 0 1 -7'}, - {type: 'translate', attributes: {sid: 'location'}, content: cube.origin.join(' ')}, - {type: 'instance_geometry', attributes: {url: `#${cube.uuid}-mesh`, name: cube.name}}, - ] - } - root.push(node); + content: { + type: 'init_from', + content: `Image_${i}.png` + } + }) + materials.push({ + type: 'material', + attributes: { + id: `Image_${i}-material`, + name: `Image_${i}`, + }, + content: {name: 'instance_effect', attributes: {url: `#Material_${i}-effect`}} + }) + }) + + Cube.all.forEach(cube => { let geometry = { + type: 'geometry', attributes: { id: `${cube.uuid}-mesh`, name: Cube.name @@ -112,6 +173,18 @@ var codec = new Codec('collada', { type: 'float_array', attributes: {id: `${cube.uuid}-mesh-positions-array`, count: 24}, content: cube.mesh.geometry.attributes.position.array.join(' ') + }, + { + type: 'technique_common', + content: { + type: 'accessor', + attributes: {source: `#${cube.uuid}-mesh-positions-array`, count: 8, stride: 3}, + content: [ + {type: 'param', attributes: {name: 'X', type: 'float'}}, + {type: 'param', attributes: {name: 'Y', type: 'float'}}, + {type: 'param', attributes: {name: 'Z', type: 'float'}}, + ] + } } ] }, @@ -121,8 +194,42 @@ var codec = new Codec('collada', { content: [ { type: 'float_array', - attributes: {id: `${cube.uuid}-mesh-normals-array`, count: 24}, + attributes: {id: `${cube.uuid}-mesh-normals-array`, count: 18}, content: cube.mesh.geometry.attributes.normal.array.join(' ') + }, + { + type: 'technique_common', + content: { + type: 'accessor', + attributes: {source: `#${cube.uuid}-mesh-normals-array`, count: 6, stride: 3}, + content: [ + {type: 'param', attributes: {name: 'X', type: 'float'}}, + {type: 'param', attributes: {name: 'Y', type: 'float'}}, + {type: 'param', attributes: {name: 'Z', type: 'float'}}, + ] + } + } + ] + }, + { + type: 'source', + attributes: {id: `${cube.uuid}-mesh-map-0`}, + content: [ + { + type: 'float_array', + attributes: {id: `${cube.uuid}-mesh-map-0-array`, count: 48}, + content: cube.mesh.geometry.attributes.uv.array.join(' ') + }, + { + type: 'technique_common', + content: { + type: 'accessor', + attributes: {source: `#${cube.uuid}-mesh-map-0-array`, count: 24, stride: 2}, + content: [ + {type: 'param', attributes: {name: 'S', type: 'float'}}, + {type: 'param', attributes: {name: 'T', type: 'float'}}, + ] + } } ] }, @@ -132,9 +239,23 @@ var codec = new Codec('collada', { content: [ { type: 'input', - attributes: {semantic: 'POSITION', id: `${cube.uuid}--mesh-positions`} + attributes: {semantic: 'POSITION', id: `${cube.uuid}-mesh-positions`} } ] + }, + { + type: 'polylist', + attributes: { + count: 6, + material: '' + }, + content: [ + {type: 'input', semantic: 'VERTEX', source: `${cube.uuid}-mesh-positions`, offset: 0}, + {type: 'input', semantic: 'NORMAL', source: `${cube.uuid}-mesh-normals`, offset: 1}, + {type: 'input', semantic: 'TEXCOORD', source: `${cube.uuid}-mesh-map-0`, offset: 2, set: 0}, + {type: 'vcount', content: '4 4 4 4 4 4'}, + {type: 'p', content: '0 0 0 1 0 1 3 0 2 2 0 3 2 1 4 3 1 5 7 1 6 6 1 7 6 2 8 7 2 9 5 2 10 4 2 11 4 3 12 5 3 13 1 3 14 0 3 15 2 4 16 6 4 17 4 4 18 0 4 19 7 5 20 3 5 21 1 5 22 5 5 23'} + ] } ] }] @@ -142,6 +263,34 @@ var codec = new Codec('collada', { geometries.push(geometry); }) + function processNode(node) { + let tag = { + attributes: { + id: node.uuid, + name: node.name, + type: 'NODE' + }, + content: [ + {type: 'scale', attributes: {sid: 'scale'}, content: '1 1 1'}, + {type: 'rotate', attributes: {sid: 'rotationX'}, content: '1 0 0 0'}, + {type: 'rotate', attributes: {sid: 'rotationY'}, content: '0 1 0 0'}, + {type: 'rotate', attributes: {sid: 'rotationZ'}, content: '0 0 1 -7'}, + {type: 'translate', attributes: {sid: 'location'}, content: node.origin.join(' ')}, + {type: 'instance_geometry', attributes: {url: `#${node.uuid}-mesh`, name: node.name}}, + ] + } + if (node instanceof Group) { + node.children.forEach(node => { + tag.content.push(processNode(node)); + }) + } + return tag; + } + + Outliner.root.forEach(node => { + root.push(processNode(node)) + }) + scope.dispatchEvent('compile', {model, options}); @@ -202,10 +351,11 @@ function compileXML(object) { output += spaces() + head; if (typeof object.content == 'string') { output += '>' + object.content + `\n`; - } else if (object.content instanceof Array) { + } else if (typeof object.content == 'object') { depth++; output += `>\n`; - object.content.forEach(node => { + let list = object.content instanceof Array ? object.content : [object.content]; + list.forEach(node => { handleObject(node); }) depth--;