[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Problem using Three.js, is it due to the way I am using Javascript?

Someone

5/4/2016 12:24:00 PM

Using Three.js I have come across the unexpected problem of the code not working when I created the meshes outside of the load() function. I've supplied an example below using a single model with a single mesh. The following works fine:

<!DOCTYPE html>
<html lang="en">
<head>
<title>working</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #000000;
margin: 0px;
overflow: hidden;
}

</style>
</head>

<body>

<script src="js/build/three.min.js"></script>

<script src="js/loaders/ColladaLoader.js"></script>

<script src="js/Detector.js"></script>

<script>

if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

var container;

var camera, scene, renderer, objects;

var scaleAdj = 100;
init();
animate();

function init()
{

container = document.createElement( 'div' );
document.body.appendChild( container );

camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 20000 );
camera.position.set( 0, 500, 0 );

scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0xcce0ff, 10, 10000 );

var loader = new THREE.JSONLoader();

loader.load( 'cube.json', function ( geometry, materials )
{

var faceMaterial = new THREE.MultiMaterial( materials );

for ( var i = 0; i < 250; i ++ )
{

var x = ( ( i % 27 ) - 13.5 ) * (5 * scaleAdj) + THREE.Math.randFloatSpread( 300 * scaleAdj);
var z = ( Math.floor( i / 27 ) - 13.5 ) * (5 * scaleAdj) + THREE.Math.randFloatSpread( 300 * scaleAdj);

mesh = new THREE.Mesh( geometry, faceMaterial );

var s = THREE.Math.randFloat( 0.5, 2 ) * scaleAdj;
mesh.scale.set( s, s, s );

mesh.position.set( x, scaleAdj, z );
mesh.rotation.y = THREE.Math.randFloat( -0.25, 0.25 );

mesh.matrixAutoUpdate = false;
mesh.updateMatrix();

scene.add( mesh );

}

} );

scene.add( new THREE.AmbientLight( 0xffffff ) );

// ground
var textureLoader = new THREE.TextureLoader();
var groundTexture = textureLoader.load( "texture.jpg" );
groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
groundTexture.repeat.set( 40, 40 );
groundTexture.anisotropy = 16;

var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, map: groundTexture } );

var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20000, 20000 ), groundMaterial );
mesh.position.y = 0;
mesh.rotation.x = - Math.PI / 2;
mesh.receiveShadow = true;
scene.add( mesh );

// Renderer

renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );

renderer.setClearColor( scene.fog.color );

container.appendChild( renderer.domElement );

// Events

window.addEventListener( 'resize', onWindowResize, false );

}


function onWindowResize( event ) {

renderer.setSize( window.innerWidth, window.innerHeight );

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

}

function animate() {

requestAnimationFrame( animate );

render();

}

function render() {

renderer.render( scene, camera );

}



</script>
</body>
</html>

But the following doesn't (I've annotated sections where there are differences):


<!DOCTYPE html>
<html lang="en">
<head>
<title>not working</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #000000;
margin: 0px;
overflow: hidden;
}

</style>
</head>

<body>

<script src="js/build/three.min.js"></script>

<script src="js/loaders/ColladaLoader.js"></script>

<script src="js/Detector.js"></script>

<script>

if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

//--------------- difference number 1
var obj_geometry;
var obj_material;
//-----------------------------------

var container;

var camera, scene, renderer, objects;

var scaleAdj = 100;
init();
animate();

function init()
{

container = document.createElement( 'div' );
document.body.appendChild( container );

camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 20000 );
camera.position.set( 0, 500, 0 );

scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0xcce0ff, 10, 10000 );


//-------------------difference number 2--------------------
var loader = new THREE.JSONLoader();

loader.load( 'cube.json', function ( geometry, material )
{
obj_geometry = geometry;
obj_material = material;
});

var faceMaterial = new THREE.MultiMaterial( obj_material);

for ( var i = 0; i < 250; i ++ )
{

var x = ( ( i % 27 ) - 13.5 ) * (5 * scaleAdj) + THREE.Math.randFloatSpread( 300 * scaleAdj);
var z = ( Math.floor( i / 27 ) - 13.5 ) * (5 * scaleAdj) + THREE.Math.randFloatSpread( 300 * scaleAdj);

mesh = new THREE.Mesh( obj_geometry, faceMaterial);

var s = THREE.Math.randFloat( 0.5, 2 ) * scaleAdj;
mesh.scale.set( s, s, s );

mesh.position.set( x, 0, z );
mesh.rotation.y = THREE.Math.randFloat( -0.25, 0.25 );

mesh.matrixAutoUpdate = false;
mesh.updateMatrix();

scene.add( mesh );

}

//--------------------------------------

scene.add( new THREE.AmbientLight( 0xffffff ) );

// ground
var textureLoader = new THREE.TextureLoader();
var groundTexture = textureLoader.load( "texture.jpg" );
groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
groundTexture.repeat.set( 40, 40 );
groundTexture.anisotropy = 16;

var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, map: groundTexture } );

var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20000, 20000 ), groundMaterial );
mesh.position.y = 0;
mesh.rotation.x = - Math.PI / 2;
mesh.receiveShadow = true;
scene.add( mesh );

// Renderer

renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );

renderer.setClearColor( scene.fog.color );

container.appendChild( renderer.domElement );

// Events

window.addEventListener( 'resize', onWindowResize, false );

}


function onWindowResize( event ) {

renderer.setSize( window.innerWidth, window.innerHeight );

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

}

function animate() {

requestAnimationFrame( animate );

render();

}

function render() {

renderer.render( scene, camera );

}



</script>
</body>
</html>

For those interested, here are the contents of the cube.json file:

{
"uvs":[[0,0,1,0,1,1,0,1]],
"faces":[43,0,1,2,3,0,0,1,2,3,0,1,2,3,43,4,7,6,5,0,0,1,2,3,4,5,6,7,43,0,4,5,1,0,0,1,2,3,0,4,7,1,43,1,5,6,2,0,0,1,2,3,1,7,6,2,43,2,6,7,3,0,0,1,2,3,2,6,5,3,43,4,0,3,7,0,0,1,2,3,4,0,3,5],
"normals":[0.577349,-0.577349,-0.577349,0.577349,-0.577349,0.577349,-0.577349,-0.577349,0.577349,-0.577349,-0.577349,-0.577349,0.577349,0.577349,-0.577349,-0.577349,0.577349,-0.577349,-0.577349,0.577349,0.577349,0.577349,0.577349,0.577349],
"metadata":{
"generator":"io_three",
"type":"Geometry",
"normals":8,
"vertices":8,
"uvs":1,
"version":3,
"materials":1,
"faces":6
},
"vertices":[1,-1,-1,1,-1,1,-1,-1,1,-1,-1,-1,1,1,-1,0.999999,1,1,-1,1,1,-1,1,-1],
"materials":[{
"DbgName":"Material",
"colorSpecular":[0.5,0.5,0.5],
"DbgIndex":0,
"mapDiffuseWrap":["RepeatWrapping","RepeatWrapping"],
"mapDiffuse":"texture.jpg",
"shading":"phong",
"depthTest":true,
"opacity":1,
"transparent":false,
"colorDiffuse":[0.64,0.64,0.64],
"mapDiffuseAnisotropy":1,
"blending":"NormalBlending",
"depthWrite":true,
"visible":true,
"specularCoef":50,
"mapDiffuseRepeat":[1,1],
"colorEmissive":[0,0,0],
"wireframe":false,
"DbgColor":15658734
}],
"name":"CubeGeometry"
}

Does it seem to those on the forum, that the problem is me misunderstanding how to use javascript, or does the javascript seem fine, but it is a problem using Three.js?

24 Answers

Peter Webb

3/13/2010 6:57:00 AM

0


"Torpedo" <guest@unknown.com> wrote in message
news:hnf6qn$2e48$1@adenine.netfront.net...
> ...importance of mathematics and its teaching methods are well
> acknowledged
> everywhere else.
>
> In Australia, is it a relevant skill for the available job market? If
> Australia had a skill based culture, there wouldn't be so much of
> discrimination compared to the other western countries.
>

What a silly, racist thing to say.

Where are you from that allows you to sit in judgement of Australia ?
Normally I find that people who don't like Australia are not from Western
countries; they are often motivated by resentment or envy, or religious
intolerance of our lifestyles. Most Americans, Kiwis, Brits, European people
seem to actually quite like us and our country. Throw another prawn on the
barbie and all that.




Torpedo

3/13/2010 7:04:00 AM

0

...aren't you acknowledging apartheid?

"Peter Webb" <webbfamily@DIESPAMDIEoptusnet.com.au> wrote in message
news:4b9b372c$0$12922$afc38c87@news.optusnet.com.au...
>
> "Torpedo" <guest@unknown.com> wrote in message
> news:hnf6qn$2e48$1@adenine.netfront.net...
> > ...importance of mathematics and its teaching methods are well
> > acknowledged
> > everywhere else.
> >
> > In Australia, is it a relevant skill for the available job market? If
> > Australia had a skill based culture, there wouldn't be so much of
> > discrimination compared to the other western countries.
> >
>
> What a silly, racist thing to say.
>
> Where are you from that allows you to sit in judgement of Australia ?
> Normally I find that people who don't like Australia are not from Western
> countries; they are often motivated by resentment or envy, or religious
> intolerance of our lifestyles. Most Americans, Kiwis, Brits, European
people
> seem to actually quite like us and our country. Throw another prawn on the
> barbie and all that.
>
>
>
>



--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

DMJoshi

3/13/2010 7:18:00 AM

0

On Mar 13, 2:06 am, "Peter Webb"
<webbfam...@DIESPAMDIEoptusnet.com.au> wrote:
> Mathematics syllabuses need massive overhauling.
>
> The current secondary syllabus is really designed to provide the fastest and
> most direct route to Calculus and the sorts of maths used by engineers. So
> its algebra, trig, exponents, etc.
>
> These are all very useful mathematical skills if you are going to be an
> engineer. If you are not, these skills do not translate very well into
> day-to-day problems.
>
> Mathememtics would be better served by expanding the curriculum to include
> more interesting and unusual branches of mathematics, and a focus on
> mathematical thinking (which hopefully will last forever) rather than
> specific equations (which will be forgotten quite quickly unless you use
> them a lot).
>
> Specifically, I would like to see more/some emphasis on the following:
>
> 1. The pigeonhole principle
> 2. Combinatorics and counting
> 3. Set theory
> 4. Topology (Mobius, Klein and Projective Plane)
> 5. Group theory
>
> These are all interesting parts of maths and would at least give students a
> much better idea of what the subject involves. Its not all agebra.

It would be useful for India if Indians knowing Mathematics join you
Mr Webb in discussion.

I know no mathematics at all.

But have always wondered at the wastage of Indian young minds been
incurred by both aspiring to enter Medicine and Engineering after 12
( = UK A-Level) having to study same Mathematics Syllabus. In most of
India they have to study (and score high grades in) same syllabus for
Physics, Chemistry and Biology.

www.dmjoshi.org

DM

3/13/2010 8:31:00 AM

0

On Mar 13, 12:06 pm, "Peter Webb"
<webbfam...@DIESPAMDIEoptusnet.com.au> wrote:
> Mathematics syllabuses need massive overhauling.
>
> The current secondary syllabus is really designed to provide the fastest and
> most direct route to Calculus and the sorts of maths used by engineers. So
> its algebra, trig, exponents, etc.
>
> These are all very useful mathematical skills if you are going to be an
> engineer. If you are not, these skills do not translate very well into
> day-to-day problems.
>
> Mathememtics would be better served by expanding the curriculum to include
> more interesting and unusual branches of mathematics, and a focus on
> mathematical thinking (which hopefully will last forever) rather than
> specific equations (which will be forgotten quite quickly unless you use
> them a lot).
>
> Specifically, I would like to see more/some emphasis on the following:
>
> 1. The pigeonhole principle
> 2. Combinatorics and counting
> 3. Set theory
> 4. Topology (Mobius, Klein and Projective Plane)
> 5. Group theory
>
> These are all interesting parts of maths and would at least give students a
> much better idea of what the subject involves. Its not all agebra.

Don't all those things require algebra ?

DM

Peter Webb

3/13/2010 1:56:00 PM

0


"DM" <d-moss@adfa.edu.au> wrote in message
news:5964d62f-9cdd-4ac7-8e36-bd4a6c959503@s36g2000prh.googlegroups.com...
On Mar 13, 12:06 pm, "Peter Webb"
<webbfam...@DIESPAMDIEoptusnet.com.au> wrote:
> Mathematics syllabuses need massive overhauling.
>
> The current secondary syllabus is really designed to provide the fastest
> and
> most direct route to Calculus and the sorts of maths used by engineers. So
> its algebra, trig, exponents, etc.
>
> These are all very useful mathematical skills if you are going to be an
> engineer. If you are not, these skills do not translate very well into
> day-to-day problems.
>
> Mathememtics would be better served by expanding the curriculum to include
> more interesting and unusual branches of mathematics, and a focus on
> mathematical thinking (which hopefully will last forever) rather than
> specific equations (which will be forgotten quite quickly unless you use
> them a lot).
>
> Specifically, I would like to see more/some emphasis on the following:
>
> 1. The pigeonhole principle
> 2. Combinatorics and counting
> 3. Set theory
> 4. Topology (Mobius, Klein and Projective Plane)
> 5. Group theory
>
> These are all interesting parts of maths and would at least give students
> a
> much better idea of what the subject involves. Its not all agebra.

Don't all those things require algebra ?

__________________________________
Not the same sort of algebra as they learn elsewhere, excepting
combinatorics where its not the focus. To a mathematician "algebra" covers a
lot more than y = 3x^2 + 12

More generally, all of these have applications in "Finite Mathematics",
which is an important and practical part of real wprld mathematics and
hardly taught in schools.





DM

Hunter

3/13/2010 6:34:00 PM

0

Peter Webb wrote:
>
> "Torpedo" <guest@unknown.com> wrote in message
> news:hnf6qn$2e48$1@adenine.netfront.net...
>> ...importance of mathematics and its teaching methods are well
>> acknowledged
>> everywhere else.
>>
>> In Australia, is it a relevant skill for the available job market? If
>> Australia had a skill based culture, there wouldn't be so much of
>> discrimination compared to the other western countries.
>>
>
> What a silly, racist thing to say.
>
> Where are you from that allows you to sit in judgement of Australia ?
> Normally I find that people who don't like Australia are not from
> Western countries; they are often motivated by resentment or envy, or
> religious intolerance of our lifestyles. Most Americans, Kiwis, Brits,
> European people seem to actually quite like us and our country. Throw
> another prawn on the barbie and all that.


He's yet another one of those Indian ex-pats that can't live in India,
yet despises the West while being fully willing to leech from the West
and live in the West. An immoral sack of shit with no self-respect in short.

Luckily they are a bare minimum as far as Indians go, but are a very
sizable percentage of the Indians posting in these newsgroups.


B J Foster

3/13/2010 7:19:00 PM

0

Peter Webb wrote:
> Mathematics syllabuses need massive overhauling.
>
> The current secondary syllabus is really designed to provide the fastest
> and most direct route to Calculus and the sorts of maths used by
> engineers. So its algebra, trig, exponents, etc.
>
> These are all very useful mathematical skills if you are going to be an
> engineer. If you are not, these skills do not translate very well into
> day-to-day problems.
>
> Mathememtics would be better served by expanding the curriculum to
> include more interesting and unusual branches of mathematics, and a
> focus on mathematical thinking (which hopefully will last forever)
> rather than specific equations (which will be forgotten quite quickly
> unless you use them a lot).
>
> Specifically, I would like to see more/some emphasis on the following:
>
> 1. The pigeonhole principle
> 2. Combinatorics and counting
> 3. Set theory
> 4. Topology (Mobius, Klein and Projective Plane)
> 5. Group theory
>
> These are all interesting parts of maths and would at least give
> students a much better idea of what the subject involves. Its not all
> agebra.
>
>

Agreed 200%

These other branches of maths are also useful in Engineering & maybe not
enough emphasis is given *in* Engineering

B J Foster

3/13/2010 7:20:00 PM

0

Torpedo wrote:
> ...importance of mathematics and its teaching methods are well acknowledged
> everywhere else.

You're an idiot.

Precisely the same bias exists in education systems everywhere.

>
> In Australia, is it a relevant skill for the available job market? If
> Australia had a skill based culture, there wouldn't be so much of
> discrimination compared to the other western countries.
>
> "Peter Webb" <webbfamily@DIESPAMDIEoptusnet.com.au> wrote in message
> news:4b9af310$0$1783$afc38c87@news.optusnet.com.au...
>> Mathematics syllabuses need massive overhauling.
>>
>> The current secondary syllabus is really designed to provide the fastest
> and
>> most direct route to Calculus and the sorts of maths used by engineers. So
>> its algebra, trig, exponents, etc.
>>
>> These are all very useful mathematical skills if you are going to be an
>> engineer. If you are not, these skills do not translate very well into
>> day-to-day problems.
>>
>> Mathememtics would be better served by expanding the curriculum to include
>> more interesting and unusual branches of mathematics, and a focus on
>> mathematical thinking (which hopefully will last forever) rather than
>> specific equations (which will be forgotten quite quickly unless you use
>> them a lot).
>>
>> Specifically, I would like to see more/some emphasis on the following:
>>
>> 1. The pigeonhole principle
>> 2. Combinatorics and counting
>> 3. Set theory
>> 4. Topology (Mobius, Klein and Projective Plane)
>> 5. Group theory
>>
>> These are all interesting parts of maths and would at least give students
> a
>> much better idea of what the subject involves. Its not all agebra.
>>
>>
>
>
>
> --- news://freenews.netfront.net/ - complaints: news@netfront.net ---

B J Foster

3/13/2010 7:34:00 PM

0

Peter Webb wrote:
>
> "DM" <d-moss@adfa.edu.au> wrote in message
> news:5964d62f-9cdd-4ac7-8e36-bd4a6c959503@s36g2000prh.googlegroups.com...
> On Mar 13, 12:06 pm, "Peter Webb"
> <webbfam...@DIESPAMDIEoptusnet.com.au> wrote:
>> Mathematics syllabuses need massive overhauling.
>>
>> The current secondary syllabus is really designed to provide the
>> fastest and
>> most direct route to Calculus and the sorts of maths used by
>> engineers. So
>> its algebra, trig, exponents, etc.
>>
>> These are all very useful mathematical skills if you are going to be an
>> engineer. If you are not, these skills do not translate very well into
>> day-to-day problems.
>>
>> Mathememtics would be better served by expanding the curriculum to
>> include
>> more interesting and unusual branches of mathematics, and a focus on
>> mathematical thinking (which hopefully will last forever) rather than
>> specific equations (which will be forgotten quite quickly unless you use
>> them a lot).
>>
>> Specifically, I would like to see more/some emphasis on the following:
>>
>> 1. The pigeonhole principle
>> 2. Combinatorics and counting
>> 3. Set theory
>> 4. Topology (Mobius, Klein and Projective Plane)
>> 5. Group theory
>>
>> These are all interesting parts of maths and would at least give
>> students a
>> much better idea of what the subject involves. Its not all agebra.
>
> Don't all those things require algebra ?
>
> __________________________________
> Not the same sort of algebra as they learn elsewhere, excepting
> combinatorics where its not the focus. To a mathematician "algebra"
> covers a lot more than y = 3x^2 + 12
>
> More generally, all of these have applications in "Finite Mathematics",
> which is an important and practical part of real wprld mathematics and
> hardly taught in schools.
>

It would be a boon for the country if intending Labor politicians
studied arithmetic.

Nothing 'heavy' mind you, just enough to do a business case on a
broadband network, for example.

And last week we had Faulkner highlighting*** defence expenses like
breast implants and "assisted reproductive services" costing a few
hundred $k which *are* everyday medical expenses and not apparently
paying any attention to the non-performing, late and *expensive* Joint
Strike Fighter programme costing billions, but more to the point,
billions *more* than it should.
(*** Peoples' details plastered all over the national newspapers)
http://www.brisbanetimes.com.au/national/breast-jobs-tummy-tucks-in-the-deal-20100309...

And maybe counting, e.g. "count the zeros", with extra lessons available
for Labor politicians who ever want to become Defence minister.

Addinall

3/13/2010 8:04:00 PM

0

On Mar 13, 6:31 pm, DM <d-m...@adfa.edu.au> wrote:
> On Mar 13, 12:06 pm, "Peter Webb"
>
>
>
>
>
> <webbfam...@DIESPAMDIEoptusnet.com.au> wrote:
> > Mathematics syllabuses need massive overhauling.
>
> > The current secondary syllabus is really designed to provide the fastest and
> > most direct route to Calculus and the sorts of maths used by engineers. So
> > its algebra, trig, exponents, etc.
>
> > These are all very useful mathematical skills if you are going to be an
> > engineer. If you are not, these skills do not translate very well into
> > day-to-day problems.
>
> > Mathememtics would be better served by expanding the curriculum to include
> > more interesting and unusual branches of mathematics, and a focus on
> > mathematical thinking (which hopefully will last forever) rather than
> > specific equations (which will be forgotten quite quickly unless you use
> > them a lot).
>
> > Specifically, I would like to see more/some emphasis on the following:
>
> > 1. The pigeonhole principle
> > 2. Combinatorics and counting
> > 3. Set theory
> > 4. Topology (Mobius, Klein and Projective Plane)
> > 5. Group theory
>
> > These are all interesting parts of maths and would at least give students a
> > much better idea of what the subject involves. Its not all agebra.
>
> Don't all those things require algebra ?
>
> DM- Hide quoted text -
>
> - Show quoted text -

No.

Mark.