0

I have a piece of code I am writing that should animate a 3D scene. I do this having the 3D scene structured neatly in code using classes and functions. This works on itself as a text out-putting thing.

Logically it needs to not spit out just text. The library Threejs should interact with what I build.

I want to do something like this for example:

class Tile extends SuperSpace
  height: 2
  sideLength: 10
class Plain extends Tile
  constructor: ( { @color = 'lightgreen', @height = @height, @heightPlacement = 2 } = {} ) ->
    console.log """
                  New plain:
                    color: '#{@color}'
                    sideLength: #{@sideLength}
                    height: #{@height}
                    heightPlacement: #{@heightPlacement}
                """
  mesh: ->
    geometry = new THREE.BoxGeometry 10/10, 2/10, 10/10
    material = new THREE.MeshBasicMaterial { color: 0x22ff22 }
    cube = new THREE.Mesh geometry, material
    return cube

And call it in the scene simply like this:

scene.add plain.mesh

Which is nothing more and less than a altered existing example from the docs. plain.mesh should be the returned cube

Somehow the object doesn't get trough. I either get undefined or I get my entire function back in the console:

function () {
      var cube, geometry, material;
      geometry = new THREE.BoxGeometry(10 / 10, 2 / 10, 10 / 10);
      material = new THREE.MeshBasicMaterial({
        color: 0x22ff22
      });
      cube …

Hardly satisfying. Any suggestions?

1
  • This is a function reference: plain.mesh. This is a function call: plain.mesh(). The parentheses are required when there aren't any arguments. Commented Jul 27, 2017 at 20:12

1 Answer 1

1

This works:

class Tile extends SuperSpace
  height: 2
  sideLength: 10
class Plain extends Tile
  constructor: ( { @color = 'lightgreen', @height = @height, @heightPlacement = 2 } = {} ) ->
    console.log """
                  New plain:
                    color: '#{@color}'
                    sideLength: #{@sideLength}
                    height: #{@height}
                    heightPlacement: #{@heightPlacement}
                """
  mesh: ->
    geometry = new THREE.BoxGeometry 10/10, 2/10, 10/10
    material = new THREE.MeshBasicMaterial { color: 0x22ff22 }
    cube = new THREE.Mesh geometry, material
    return cube

plain = new Plain()
console.log plain.mesh()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.